Merge pull request #8 from sandeepmistry/all-architectures

Support for other boards and shields
This commit is contained in:
FWeinb 2016-04-12 14:30:47 +02:00
commit b910b126ba
6 changed files with 48 additions and 31 deletions

View File

@ -21,22 +21,28 @@
#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;
}
NTPClient::NTPClient(const char* poolServerName) {
NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
this->_udp = &udp;
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->_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->_poolServerName = poolServerName;
this->_updateInterval = updateInterval;
@ -47,24 +53,21 @@ void NTPClient::forceUpdate() {
Serial.println("Update from NTP Server");
#endif
IPAddress address;
WiFi.hostByName(this->_poolServerName, address);
this->sendNTPPacket(address);
this->sendNTPPacket();
// Wait till data is there or timeout...
byte timeout = 0;
int cb = 0;
do {
delay ( 10 );
cb = this->_udp.parsePacket();
cb = this->_udp->parsePacket();
if (timeout > 100) return; // timeout after 1000 ms
timeout++;
} while (cb == 0);
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 lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
@ -78,7 +81,7 @@ void NTPClient::forceUpdate() {
void NTPClient::update() {
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|| 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();
}
}
@ -116,7 +119,7 @@ String NTPClient::getFormattedTime() {
return hoursStr + ":" + minuteStr + ":" + secondStr;
}
void NTPClient::sendNTPPacket(IPAddress ip) {
void NTPClient::sendNTPPacket() {
// set all bytes in the buffer to 0
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
@ -133,8 +136,8 @@ void NTPClient::sendNTPPacket(IPAddress ip) {
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
this->_udp.beginPacket(ip, 123); //NTP requests are to port 123
this->_udp.write(this->_packetBuffer, NTP_PACKET_SIZE);
this->_udp.endPacket();
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
this->_udp->endPacket();
}

View File

@ -2,15 +2,14 @@
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Udp.h>
#define SEVENZYYEARS 2208988800UL
#define NTP_PACKET_SIZE 48
class NTPClient {
private:
WiFiUDP _udp;
UDP* _udp;
const char* _poolServerName = "time.nist.gov"; // Default time server
int _port = 1337;
@ -23,14 +22,14 @@ class NTPClient {
byte _packetBuffer[NTP_PACKET_SIZE];
void sendNTPPacket(IPAddress _timeServerIP);
void sendNTPPacket();
public:
NTPClient();
NTPClient(int timeOffset);
NTPClient(const char* poolServerName);
NTPClient(const char* poolServerName, int timeOffset);
NTPClient(const char* poolServerName, int timeOffset, int updateInterval);
NTPClient(UDP& udp);
NTPClient(UDP& udp, int timeOffset);
NTPClient(UDP& udp, const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
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

View File

@ -4,11 +4,17 @@ Connect to a NTP server, here is how:
```cpp
#include <NTPClient.h>
// change next line to use with another board/shield
#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 *password = "<PASSWORD>";
WiFiUDP ntpUDP(ntpUDP);
// By default 'time.nist.gov' is used.
NTPClient timeClient;

View File

@ -1,12 +1,18 @@
#include <NTPClient.h>
// change next line to use with another board/shield
#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 *password = "<PASSWORD>";
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset, (in seconds)
// 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(){
Serial.begin(115200);

View File

@ -1,12 +1,15 @@
#include <NTPClient.h>
// change next line to use with another board/shield
#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 *password = "<PASSWORD>";
NTPClient timeClient;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup(){
Serial.begin(115200);

View File

@ -1,9 +1,9 @@
name=NTPClient
version=2.0.0
version=3.0.0
author=Fabrice Weinberg
maintainer=Fabrice Weinberg <fabrice@weinberg.me>
sentence=An NTPClient to connect to a time server
paragraph=Get time from a NTP server and keep it in sync.
category=Timing
url=https://github.com/FWeinb/NTPClient
architectures=esp8266
architectures=*