Merge pull request #8 from sandeepmistry/all-architectures
Support for other boards and shields
This commit is contained in:
commit
b910b126ba
@ -21,22 +21,28 @@
|
|||||||
|
|
||||||
#include "NTPClient.h"
|
#include "NTPClient.h"
|
||||||
|
|
||||||
NTPClient::NTPClient() {}
|
NTPClient::NTPClient(UDP& udp) {
|
||||||
|
this->_udp = &udp;
|
||||||
|
}
|
||||||
|
|
||||||
NTPClient::NTPClient(int timeOffset) {
|
NTPClient::NTPClient(UDP& udp, int timeOffset) {
|
||||||
|
this->_udp = &udp;
|
||||||
this->_timeOffset = timeOffset;
|
this->_timeOffset = timeOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
NTPClient::NTPClient(const char* poolServerName) {
|
NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
|
||||||
|
this->_udp = &udp;
|
||||||
this->_poolServerName = poolServerName;
|
this->_poolServerName = poolServerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
NTPClient::NTPClient(const char* poolServerName, int timeOffset) {
|
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) {
|
||||||
|
this->_udp = &udp;
|
||||||
this->_timeOffset = timeOffset;
|
this->_timeOffset = timeOffset;
|
||||||
this->_poolServerName = poolServerName;
|
this->_poolServerName = poolServerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
NTPClient::NTPClient(const char* poolServerName, int timeOffset, int updateInterval) {
|
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) {
|
||||||
|
this->_udp = &udp;
|
||||||
this->_timeOffset = timeOffset;
|
this->_timeOffset = timeOffset;
|
||||||
this->_poolServerName = poolServerName;
|
this->_poolServerName = poolServerName;
|
||||||
this->_updateInterval = updateInterval;
|
this->_updateInterval = updateInterval;
|
||||||
@ -47,24 +53,21 @@ void NTPClient::forceUpdate() {
|
|||||||
Serial.println("Update from NTP Server");
|
Serial.println("Update from NTP Server");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
IPAddress address;
|
this->sendNTPPacket();
|
||||||
WiFi.hostByName(this->_poolServerName, address);
|
|
||||||
|
|
||||||
this->sendNTPPacket(address);
|
|
||||||
|
|
||||||
// Wait till data is there or timeout...
|
// Wait till data is there or timeout...
|
||||||
byte timeout = 0;
|
byte timeout = 0;
|
||||||
int cb = 0;
|
int cb = 0;
|
||||||
do {
|
do {
|
||||||
delay ( 10 );
|
delay ( 10 );
|
||||||
cb = this->_udp.parsePacket();
|
cb = this->_udp->parsePacket();
|
||||||
if (timeout > 100) return; // timeout after 1000 ms
|
if (timeout > 100) return; // timeout after 1000 ms
|
||||||
timeout++;
|
timeout++;
|
||||||
} while (cb == 0);
|
} while (cb == 0);
|
||||||
|
|
||||||
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
|
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
|
||||||
|
|
||||||
this->_udp.read(this->_packetBuffer, NTP_PACKET_SIZE);
|
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||||
|
|
||||||
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
|
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
|
||||||
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
|
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
|
||||||
@ -78,7 +81,7 @@ void NTPClient::forceUpdate() {
|
|||||||
void NTPClient::update() {
|
void NTPClient::update() {
|
||||||
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|
||||||
|| this->_lastUpdate == 0) { // Update if there was no update yet.
|
|| this->_lastUpdate == 0) { // Update if there was no update yet.
|
||||||
if (this->_lastUpdate == 0) this->_udp.begin(this->_port); // Start _udp if there was no update yet.
|
if (this->_lastUpdate == 0) this->_udp->begin(this->_port); // Start _udp if there was no update yet.
|
||||||
this->forceUpdate();
|
this->forceUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +119,7 @@ String NTPClient::getFormattedTime() {
|
|||||||
return hoursStr + ":" + minuteStr + ":" + secondStr;
|
return hoursStr + ":" + minuteStr + ":" + secondStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NTPClient::sendNTPPacket(IPAddress ip) {
|
void NTPClient::sendNTPPacket() {
|
||||||
// set all bytes in the buffer to 0
|
// set all bytes in the buffer to 0
|
||||||
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
|
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
|
||||||
// Initialize values needed to form NTP request
|
// Initialize values needed to form NTP request
|
||||||
@ -133,8 +136,8 @@ void NTPClient::sendNTPPacket(IPAddress ip) {
|
|||||||
|
|
||||||
// all NTP fields have been given values, now
|
// all NTP fields have been given values, now
|
||||||
// you can send a packet requesting a timestamp:
|
// you can send a packet requesting a timestamp:
|
||||||
this->_udp.beginPacket(ip, 123); //NTP requests are to port 123
|
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
|
||||||
this->_udp.write(this->_packetBuffer, NTP_PACKET_SIZE);
|
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||||
this->_udp.endPacket();
|
this->_udp->endPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
NTPClient.h
17
NTPClient.h
@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <Udp.h>
|
||||||
#include <WiFiUdp.h>
|
|
||||||
|
|
||||||
#define SEVENZYYEARS 2208988800UL
|
#define SEVENZYYEARS 2208988800UL
|
||||||
#define NTP_PACKET_SIZE 48
|
#define NTP_PACKET_SIZE 48
|
||||||
|
|
||||||
class NTPClient {
|
class NTPClient {
|
||||||
private:
|
private:
|
||||||
WiFiUDP _udp;
|
UDP* _udp;
|
||||||
|
|
||||||
const char* _poolServerName = "time.nist.gov"; // Default time server
|
const char* _poolServerName = "time.nist.gov"; // Default time server
|
||||||
int _port = 1337;
|
int _port = 1337;
|
||||||
@ -23,14 +22,14 @@ class NTPClient {
|
|||||||
|
|
||||||
byte _packetBuffer[NTP_PACKET_SIZE];
|
byte _packetBuffer[NTP_PACKET_SIZE];
|
||||||
|
|
||||||
void sendNTPPacket(IPAddress _timeServerIP);
|
void sendNTPPacket();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
NTPClient();
|
NTPClient(UDP& udp);
|
||||||
NTPClient(int timeOffset);
|
NTPClient(UDP& udp, int timeOffset);
|
||||||
NTPClient(const char* poolServerName);
|
NTPClient(UDP& udp, const char* poolServerName);
|
||||||
NTPClient(const char* poolServerName, int timeOffset);
|
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
|
||||||
NTPClient(const char* poolServerName, int timeOffset, int updateInterval);
|
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This should be called in the main loop of your application. By default an update from the NTP Server is only
|
* This should be called in the main loop of your application. By default an update from the NTP Server is only
|
||||||
|
@ -4,11 +4,17 @@ Connect to a NTP server, here is how:
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
|
// change next line to use with another board/shield
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
//#include <WiFi.h> // for WiFi shield
|
||||||
|
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
|
||||||
const char *ssid = "<SSID>";
|
const char *ssid = "<SSID>";
|
||||||
const char *password = "<PASSWORD>";
|
const char *password = "<PASSWORD>";
|
||||||
|
|
||||||
|
WiFiUDP ntpUDP(ntpUDP);
|
||||||
|
|
||||||
// By default 'time.nist.gov' is used.
|
// By default 'time.nist.gov' is used.
|
||||||
NTPClient timeClient;
|
NTPClient timeClient;
|
||||||
|
|
||||||
|
@ -1,12 +1,18 @@
|
|||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
|
// change next line to use with another board/shield
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
//#include <WiFi.h> // for WiFi shield
|
||||||
|
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
|
||||||
const char *ssid = "<SSID>";
|
const char *ssid = "<SSID>";
|
||||||
const char *password = "<PASSWORD>";
|
const char *password = "<PASSWORD>";
|
||||||
|
|
||||||
|
WiFiUDP ntpUDP;
|
||||||
|
|
||||||
// You can specify the time server pool and the offset, (in seconds)
|
// You can specify the time server pool and the offset, (in seconds)
|
||||||
// additionaly you can specify the update interval (in milliseconds).
|
// additionaly you can specify the update interval (in milliseconds).
|
||||||
NTPClient timeClient("europe.pool.ntp.org", 3600, 60000);
|
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
|
// change next line to use with another board/shield
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
//#include <WiFi.h> // for WiFi shield
|
||||||
|
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
|
||||||
const char *ssid = "<SSID>";
|
const char *ssid = "<SSID>";
|
||||||
const char *password = "<PASSWORD>";
|
const char *password = "<PASSWORD>";
|
||||||
|
|
||||||
|
WiFiUDP ntpUDP;
|
||||||
|
NTPClient timeClient(ntpUDP);
|
||||||
NTPClient timeClient;
|
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
name=NTPClient
|
name=NTPClient
|
||||||
version=2.0.0
|
version=3.0.0
|
||||||
author=Fabrice Weinberg
|
author=Fabrice Weinberg
|
||||||
maintainer=Fabrice Weinberg <fabrice@weinberg.me>
|
maintainer=Fabrice Weinberg <fabrice@weinberg.me>
|
||||||
sentence=An NTPClient to connect to a time server
|
sentence=An NTPClient to connect to a time server
|
||||||
paragraph=Get time from a NTP server and keep it in sync.
|
paragraph=Get time from a NTP server and keep it in sync.
|
||||||
category=Timing
|
category=Timing
|
||||||
url=https://github.com/FWeinb/NTPClient
|
url=https://github.com/FWeinb/NTPClient
|
||||||
architectures=esp8266
|
architectures=*
|
||||||
|
Loading…
Reference in New Issue
Block a user