First Commit
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
Copyright 2016 German Martin (gmag11@gmail.com). All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are
|
||||
permitted provided that the following conditions are met :
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
of conditions and the following disclaimer in the documentation and / or other materials
|
||||
provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those of the
|
||||
authors and should not be interpreted as representing official policies, either expressed
|
||||
or implied, of German Martin
|
||||
*/
|
||||
/*
|
||||
Name: NtpClientAvr.ino
|
||||
Created: 20/08/2016
|
||||
Author: gmag11@gmail.com
|
||||
Editor: http://www.visualmicro.com
|
||||
*/
|
||||
|
||||
#include <TimeLib.h>
|
||||
#include <NtpClientLib.h>
|
||||
#include <SPI.h>
|
||||
#include <EthernetUdp.h>
|
||||
#include <Ethernet.h>
|
||||
#include <Dns.h>
|
||||
#include <Dhcp.h>
|
||||
|
||||
#define SHOW_TIME_PERIOD 5000
|
||||
|
||||
// Enter a MAC address for your controller below.
|
||||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
||||
byte mac[] = {
|
||||
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
|
||||
};
|
||||
|
||||
EthernetClient client;
|
||||
|
||||
void setup () {
|
||||
Serial.begin (115200);
|
||||
if (Ethernet.begin (mac) == 0) {
|
||||
Serial.println ("Failed to configure Ethernet using DHCP");
|
||||
// no point in carrying on, so do nothing forevermore:
|
||||
for (;;)
|
||||
;
|
||||
}
|
||||
NTP.onNTPSyncEvent ([](NTPSyncEvent_t error) {
|
||||
if (error) {
|
||||
Serial.print ("Time Sync error: ");
|
||||
if (error == noResponse)
|
||||
Serial.println ("NTP server not reachable");
|
||||
else if (error == invalidAddress)
|
||||
Serial.println ("Invalid NTP server address");
|
||||
} else {
|
||||
Serial.print ("Got NTP time: ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getLastNTPSync ()));
|
||||
}
|
||||
});
|
||||
NTP.setInterval (63);
|
||||
NTP.setNTPTimeout (1000);
|
||||
NTP.begin ("pool.ntp.org", 1, true);
|
||||
}
|
||||
|
||||
void loop () {
|
||||
static int i = 0;
|
||||
static int last = 0;
|
||||
|
||||
if ((millis () - last) > SHOW_TIME_PERIOD) {
|
||||
//Serial.println(millis() - last);
|
||||
last = millis ();
|
||||
Serial.print (i); Serial.print (" ");
|
||||
Serial.print (NTP.getTimeDateString ()); Serial.print (". ");
|
||||
Serial.print (NTP.isSummerTime () ? "Summer Time. " : "Winter Time. ");
|
||||
Serial.print ("Uptime: ");
|
||||
Serial.print (NTP.getUptimeString ()); Serial.print (" since ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getFirstSync ()).c_str ());
|
||||
|
||||
i++;
|
||||
}
|
||||
delay (0);
|
||||
Ethernet.maintain (); // Check DHCP for renewal
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright 2018 German Martin (gmag11@gmail.com). All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are
|
||||
permitted provided that the following conditions are met :
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
of conditions and the following disclaimer in the documentation and / or other materials
|
||||
provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those of the
|
||||
authors and should not be interpreted as representing official policies, either expressed
|
||||
or implied, of German Martin
|
||||
*/
|
||||
|
||||
/*
|
||||
Name: NtpClient.ino
|
||||
Created: 04/01/2018
|
||||
Author: gmag11@gmail.com
|
||||
Editor: http://www.visualmicro.com
|
||||
*/
|
||||
|
||||
#include <TimeLib.h> //TimeLib library is needed https://github.com/PaulStoffregen/Time
|
||||
#include <NtpClientLib.h> //Include NtpClient library header
|
||||
#include <ESP8266WiFi.h> // ESP8266
|
||||
// #include <WiFi.h> // ESP32
|
||||
|
||||
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
|
||||
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
|
||||
|
||||
void setup () {
|
||||
Serial.begin (115200);
|
||||
Serial.println ();
|
||||
|
||||
//Start WiFi communication
|
||||
WiFi.mode (WIFI_STA);
|
||||
WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);
|
||||
|
||||
// NTP begin with default parameters:
|
||||
// NTP server: pool.ntp.org
|
||||
// TimeZone: UTC
|
||||
// Daylight saving: off
|
||||
NTP.begin (); // Only statement needed to start NTP sync.
|
||||
|
||||
}
|
||||
|
||||
void loop () {
|
||||
//To keep time updated you need to call now() from time to time inside loop
|
||||
//in this case getTimeDateString() implies a call to now()
|
||||
Serial.println (NTP.getTimeDateString ());
|
||||
|
||||
delay (1000);
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
Copyright 2016 German Martin (gmag11@gmail.com). All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are
|
||||
permitted provided that the following conditions are met :
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
of conditions and the following disclaimer in the documentation and / or other materials
|
||||
provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those of the
|
||||
authors and should not be interpreted as representing official policies, either expressed
|
||||
or implied, of German Martin
|
||||
*/
|
||||
|
||||
/*
|
||||
Name: NtpClient.ino
|
||||
Created: 20/08/2016
|
||||
Author: gmag11@gmail.com
|
||||
Editor: http://www.visualmicro.com
|
||||
*/
|
||||
|
||||
#include <TimeLib.h>
|
||||
#include "WifiConfig.h"
|
||||
#include <NtpClientLib.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#ifndef WIFI_CONFIG_H
|
||||
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
|
||||
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
|
||||
#endif // !WIFI_CONFIG_H
|
||||
|
||||
#define ONBOARDLED 5 // Built in LED on ESP-12/ESP-07
|
||||
#define SHOW_TIME_PERIOD 5000
|
||||
#define NTP_TIMEOUT 1500
|
||||
|
||||
int8_t timeZone = 1;
|
||||
int8_t minutesTimeZone = 0;
|
||||
const PROGMEM char *ntpServer = "pool.ntp.org";
|
||||
bool wifiFirstConnected = false;
|
||||
|
||||
void onEvent (system_event_id_t event, system_event_info_t info) {
|
||||
Serial.printf ("[WiFi-event] event: %d\n", event);
|
||||
|
||||
switch (event) {
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
Serial.printf ("Connected to %s. Asking for IP address.\r\n", info.connected.ssid);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
Serial.printf ("Got IP: %s\r\n", IPAddress (info.got_ip.ip_info.ip.addr).toString ().c_str ());
|
||||
Serial.printf ("Connected: %s\r\n", WiFi.status () == WL_CONNECTED ? "yes" : "no");
|
||||
digitalWrite (ONBOARDLED, LOW); // Turn on LED
|
||||
wifiFirstConnected = true;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
Serial.printf ("Disconnected from SSID: %s\n", info.disconnected.ssid);
|
||||
Serial.printf ("Reason: %d\n", info.disconnected.reason);
|
||||
digitalWrite (ONBOARDLED, HIGH); // Turn off LED
|
||||
//NTP.stop(); // NTP sync can be disabled to avoid sync errors
|
||||
WiFi.reconnect ();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void processSyncEvent (NTPSyncEvent_t ntpEvent) {
|
||||
if (ntpEvent < 0) {
|
||||
Serial.printf ("Time Sync error %d:", ntpEvent);
|
||||
if (ntpEvent == noResponse)
|
||||
Serial.println ("NTP server not reachable");
|
||||
else if (ntpEvent == invalidAddress)
|
||||
Serial.println ("Invalid NTP server address");
|
||||
} else if (!ntpEvent) {
|
||||
Serial.print ("Got NTP time: ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getLastNTPSync ()));
|
||||
} else {
|
||||
Serial.println ("NTP request Sent");
|
||||
}
|
||||
}
|
||||
|
||||
boolean syncEventTriggered = false; // True if a time even has been triggered
|
||||
NTPSyncEvent_t ntpEvent; // Last triggered event
|
||||
|
||||
void setup () {
|
||||
Serial.begin (115200);
|
||||
Serial.println ();
|
||||
WiFi.mode (WIFI_STA);
|
||||
WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);
|
||||
|
||||
pinMode (ONBOARDLED, OUTPUT); // Onboard LED
|
||||
digitalWrite (ONBOARDLED, HIGH); // Switch off LED
|
||||
|
||||
NTP.onNTPSyncEvent ([](NTPSyncEvent_t event) {
|
||||
ntpEvent = event;
|
||||
syncEventTriggered = true;
|
||||
});
|
||||
|
||||
// Deprecated
|
||||
/*WiFi.onEvent([](WiFiEvent_t e) {
|
||||
Serial.printf("Event wifi -----> %d\n", e);
|
||||
});*/
|
||||
|
||||
WiFi.onEvent (onEvent);
|
||||
|
||||
}
|
||||
|
||||
void loop () {
|
||||
static int i = 0;
|
||||
static int last = 0;
|
||||
|
||||
if (wifiFirstConnected) {
|
||||
wifiFirstConnected = false;
|
||||
NTP.setInterval (63);
|
||||
NTP.setNTPTimeout (NTP_TIMEOUT);
|
||||
NTP.begin (ntpServer, timeZone, true, minutesTimeZone);
|
||||
}
|
||||
|
||||
if (syncEventTriggered) {
|
||||
processSyncEvent (ntpEvent);
|
||||
syncEventTriggered = false;
|
||||
}
|
||||
|
||||
if ((millis () - last) > SHOW_TIME_PERIOD) {
|
||||
//Serial.println(millis() - last);
|
||||
last = millis ();
|
||||
Serial.print (i); Serial.print (" ");
|
||||
Serial.print (NTP.getTimeDateString ()); Serial.print (" ");
|
||||
Serial.print (NTP.isSummerTime () ? "Summer Time. " : "Winter Time. ");
|
||||
Serial.print ("WiFi is ");
|
||||
Serial.print (WiFi.isConnected () ? "connected" : "not connected"); Serial.print (". ");
|
||||
Serial.print ("Uptime: ");
|
||||
Serial.print (NTP.getUptimeString ()); Serial.print (" since ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getFirstSync ()).c_str ());
|
||||
Serial.printf ("Free heap: %u\n", ESP.getFreeHeap ());
|
||||
i++;
|
||||
}
|
||||
delay (0);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#ifndef WIFI_CONFIG_H
|
||||
#define WIFI_CONFIG_H
|
||||
|
||||
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
|
||||
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
|
||||
|
||||
#endif //WIFI_CONFIG_H
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
Copyright 2016 German Martin (gmag11@gmail.com). All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are
|
||||
permitted provided that the following conditions are met :
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
of conditions and the following disclaimer in the documentation and / or other materials
|
||||
provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those of the
|
||||
authors and should not be interpreted as representing official policies, either expressed
|
||||
or implied, of German Martin
|
||||
*/
|
||||
|
||||
/*
|
||||
Name: NtpClient.ino
|
||||
Created: 20/08/2016
|
||||
Author: gmag11@gmail.com
|
||||
Editor: http://www.visualmicro.com
|
||||
*/
|
||||
|
||||
#include <TimeLib.h>
|
||||
#include "WifiConfig.h"
|
||||
#include <NtpClientLib.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#ifndef WIFI_CONFIG_H
|
||||
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
|
||||
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
|
||||
#endif // !WIFI_CONFIG_H
|
||||
|
||||
#define ONBOARDLED 2 // Built in LED on ESP-12/ESP-07
|
||||
#define SHOW_TIME_PERIOD 5000
|
||||
#define NTP_TIMEOUT 1500
|
||||
|
||||
int8_t timeZone = 1;
|
||||
int8_t minutesTimeZone = 0;
|
||||
const PROGMEM char *ntpServer = "pool.ntp.org";
|
||||
bool wifiFirstConnected = false;
|
||||
|
||||
void onSTAConnected (WiFiEventStationModeConnected ipInfo) {
|
||||
Serial.printf ("Connected to %s\r\n", ipInfo.ssid.c_str ());
|
||||
}
|
||||
|
||||
|
||||
// Start NTP only after IP network is connected
|
||||
void onSTAGotIP (WiFiEventStationModeGotIP ipInfo) {
|
||||
Serial.printf ("Got IP: %s\r\n", ipInfo.ip.toString ().c_str ());
|
||||
Serial.printf ("Connected: %s\r\n", WiFi.status () == WL_CONNECTED ? "yes" : "no");
|
||||
digitalWrite (ONBOARDLED, LOW); // Turn on LED
|
||||
wifiFirstConnected = true;
|
||||
}
|
||||
|
||||
// Manage network disconnection
|
||||
void onSTADisconnected (WiFiEventStationModeDisconnected event_info) {
|
||||
Serial.printf ("Disconnected from SSID: %s\n", event_info.ssid.c_str ());
|
||||
Serial.printf ("Reason: %d\n", event_info.reason);
|
||||
digitalWrite (ONBOARDLED, HIGH); // Turn off LED
|
||||
//NTP.stop(); // NTP sync can be disabled to avoid sync errors
|
||||
WiFi.reconnect ();
|
||||
}
|
||||
|
||||
void processSyncEvent (NTPSyncEvent_t ntpEvent) {
|
||||
if (ntpEvent < 0) {
|
||||
Serial.printf ("Time Sync error: %d\n", ntpEvent);
|
||||
if (ntpEvent == noResponse)
|
||||
Serial.println ("NTP server not reachable");
|
||||
else if (ntpEvent == invalidAddress)
|
||||
Serial.println ("Invalid NTP server address");
|
||||
else if (ntpEvent == errorSending)
|
||||
Serial.println ("Error sending request");
|
||||
else if (ntpEvent == responseError)
|
||||
Serial.println ("NTP response error");
|
||||
} else {
|
||||
if (ntpEvent == timeSyncd) {
|
||||
Serial.print ("Got NTP time: ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getLastNTPSync ()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean syncEventTriggered = false; // True if a time even has been triggered
|
||||
NTPSyncEvent_t ntpEvent; // Last triggered event
|
||||
|
||||
void setup () {
|
||||
static WiFiEventHandler e1, e2, e3;
|
||||
|
||||
Serial.begin (115200);
|
||||
Serial.println ();
|
||||
WiFi.mode (WIFI_STA);
|
||||
WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);
|
||||
|
||||
pinMode (ONBOARDLED, OUTPUT); // Onboard LED
|
||||
digitalWrite (ONBOARDLED, HIGH); // Switch off LED
|
||||
|
||||
NTP.onNTPSyncEvent ([](NTPSyncEvent_t event) {
|
||||
ntpEvent = event;
|
||||
syncEventTriggered = true;
|
||||
});
|
||||
|
||||
// Deprecated
|
||||
/*WiFi.onEvent([](WiFiEvent_t e) {
|
||||
Serial.printf("Event wifi -----> %d\n", e);
|
||||
});*/
|
||||
|
||||
e1 = WiFi.onStationModeGotIP (onSTAGotIP);// As soon WiFi is connected, start NTP Client
|
||||
e2 = WiFi.onStationModeDisconnected (onSTADisconnected);
|
||||
e3 = WiFi.onStationModeConnected (onSTAConnected);
|
||||
}
|
||||
|
||||
void loop () {
|
||||
static int i = 0;
|
||||
static int last = 0;
|
||||
|
||||
if (wifiFirstConnected) {
|
||||
wifiFirstConnected = false;
|
||||
NTP.setInterval (63);
|
||||
NTP.setNTPTimeout (NTP_TIMEOUT);
|
||||
NTP.begin (ntpServer, timeZone, true, minutesTimeZone);
|
||||
}
|
||||
|
||||
if (syncEventTriggered) {
|
||||
processSyncEvent (ntpEvent);
|
||||
syncEventTriggered = false;
|
||||
}
|
||||
|
||||
if ((millis () - last) > SHOW_TIME_PERIOD) {
|
||||
//Serial.println(millis() - last);
|
||||
last = millis ();
|
||||
Serial.print (i); Serial.print (" ");
|
||||
Serial.print (NTP.getTimeDateString ()); Serial.print (" ");
|
||||
Serial.print (NTP.isSummerTime () ? "Summer Time. " : "Winter Time. ");
|
||||
Serial.print ("WiFi is ");
|
||||
Serial.print (WiFi.isConnected () ? "connected" : "not connected"); Serial.print (". ");
|
||||
Serial.print ("Uptime: ");
|
||||
Serial.print (NTP.getUptimeString ()); Serial.print (" since ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getFirstSync ()).c_str ());
|
||||
Serial.printf ("Free heap: %u\n", ESP.getFreeHeap ());
|
||||
i++;
|
||||
}
|
||||
delay (0);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#ifndef WIFI_CONFIG_H
|
||||
#define WIFI_CONFIG_H
|
||||
|
||||
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
|
||||
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
|
||||
|
||||
#endif //WIFI_CONFIG_H
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
Copyright 2016 German Martin (gmag11@gmail.com). All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are
|
||||
permitted provided that the following conditions are met :
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
of conditions and the following disclaimer in the documentation and / or other materials
|
||||
provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those of the
|
||||
authors and should not be interpreted as representing official policies, either expressed
|
||||
or implied, of German Martin
|
||||
*/
|
||||
|
||||
/*
|
||||
Name: NtpClientAvr.ino
|
||||
Created: 20/08/2016
|
||||
Author: gmag11@gmail.com
|
||||
Editor: http://www.visualmicro.com
|
||||
*/
|
||||
|
||||
#include <WiFiClient.h>
|
||||
#include <SPI.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <WiFi101.h>
|
||||
#include <TimeLib.h>
|
||||
#include <NtpClientLib.h>
|
||||
#include <Dns.h>
|
||||
#include <Dhcp.h>
|
||||
#include "WifiConfig.h"
|
||||
|
||||
#ifndef WIFI_CONFIG_H
|
||||
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
|
||||
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
|
||||
#endif // !WIFI_CONFIG_H
|
||||
|
||||
#define SHOW_TIME_PERIOD 5000
|
||||
|
||||
|
||||
void setup () {
|
||||
Serial.begin (115200);
|
||||
WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);
|
||||
while (WiFi.status () != WL_CONNECTED) {
|
||||
Serial.print ('.');
|
||||
delay (500);
|
||||
}
|
||||
Serial.println ();
|
||||
NTP.onNTPSyncEvent ([](NTPSyncEvent_t error) {
|
||||
if (error) {
|
||||
Serial.print ("Time Sync error: ");
|
||||
if (error == noResponse)
|
||||
Serial.println ("NTP server not reachable");
|
||||
else if (error == invalidAddress)
|
||||
Serial.println ("Invalid NTP server address");
|
||||
} else {
|
||||
Serial.print ("Got NTP time: ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getLastNTPSync ()));
|
||||
}
|
||||
});
|
||||
NTP.setInterval (63);
|
||||
NTP.setNTPTimeout (1000);
|
||||
NTP.begin ("pool.ntp.org", 1, true);
|
||||
}
|
||||
|
||||
void loop () {
|
||||
static int i = 0;
|
||||
static int last = 0;
|
||||
|
||||
if ((millis () - last) > SHOW_TIME_PERIOD) {
|
||||
//Serial.println(millis() - last);
|
||||
last = millis ();
|
||||
Serial.print (i); Serial.print (" ");
|
||||
Serial.print (NTP.getTimeDateString ()); Serial.print (" ");
|
||||
Serial.print (NTP.isSummerTime () ? "Summer Time. " : "Winter Time. ");
|
||||
Serial.print ("WiFi is ");
|
||||
|
||||
Serial.print ((WiFi.status () == WL_CONNECTED) ? "connected" : "not connected"); Serial.print (". ");
|
||||
Serial.print ("Uptime: ");
|
||||
Serial.print (NTP.getUptimeString ()); Serial.print (" since ");
|
||||
Serial.println (NTP.getTimeDateString (NTP.getFirstSync ()).c_str ());
|
||||
|
||||
i++;
|
||||
}
|
||||
delay (0);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#ifndef WIFI_CONFIG_H
|
||||
#define WIFI_CONFIG_H
|
||||
|
||||
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
|
||||
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
|
||||
|
||||
#endif //WIFI_CONFIG_H
|
||||
95
libraries/NtpClientLib/examples/NTPDSTTest/NTPDSTTest.ino
Normal file
95
libraries/NtpClientLib/examples/NTPDSTTest/NTPDSTTest.ino
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
Copyright 2016 German Martin (gmag11@gmail.com). All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are
|
||||
permitted provided that the following conditions are met :
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
of conditions and the following disclaimer in the documentation and / or other materials
|
||||
provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those of the
|
||||
authors and should not be interpreted as representing official policies, either expressed
|
||||
or implied, of German Martin
|
||||
*/
|
||||
|
||||
/*
|
||||
Name: NTPDSTTest.ino
|
||||
Created: 13/03/2018
|
||||
Author: xose.perez@gmail.com
|
||||
Editor: http://www.platformio.org
|
||||
*/
|
||||
|
||||
#include <TimeLib.h>
|
||||
#include <NtpClientLib.h>
|
||||
#include <assert.h>
|
||||
|
||||
void test(bool expected) {
|
||||
|
||||
char buffer[32];
|
||||
snprintf(
|
||||
buffer, sizeof(buffer),
|
||||
"Testing %04d/%02d/%02d %02d:%02d:%02d - ",
|
||||
year(), month(), day(), hour(), minute(), second()
|
||||
);
|
||||
Serial.print(buffer);
|
||||
Serial.println(NTP.isSummerTime() == expected ? "OK" : "FAIL");
|
||||
|
||||
}
|
||||
|
||||
void setup () {
|
||||
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
|
||||
NTP.setDayLight(true);
|
||||
|
||||
// EU Test
|
||||
NTP.setDSTZone(DST_ZONE_EU);
|
||||
NTP.setTimeZone(1, 0);
|
||||
Serial.println();
|
||||
Serial.println("Europe DST");
|
||||
Serial.println("------------------------");
|
||||
setTime(0,0,0,15,02,2018); test(false);
|
||||
setTime(0,0,0,15,04,2018); test(true);
|
||||
setTime(0,0,0,01,03,2018); test(false);
|
||||
setTime(0,0,0,30,03,2018); test(true);
|
||||
setTime(1,0,0,25,03,2018); test(false);
|
||||
setTime(3,0,0,25,03,2018); test(true);
|
||||
setTime(0,0,0,01,10,2018); test(true);
|
||||
setTime(0,0,0,30,10,2018); test(false);
|
||||
setTime(1,0,0,28,10,2018); test(true);
|
||||
setTime(3,0,0,28,10,2018); test(false);
|
||||
|
||||
// USA Test
|
||||
NTP.setDSTZone(DST_ZONE_USA);
|
||||
NTP.setTimeZone(-6, 0);
|
||||
Serial.println();
|
||||
Serial.println("USA DST");
|
||||
Serial.println("------------------------");
|
||||
setTime(0,0,0,15,02,2018); test(false);
|
||||
setTime(0,0,0,15,04,2018); test(true);
|
||||
setTime(0,0,0,01,03,2018); test(false);
|
||||
setTime(0,0,0,30,03,2018); test(true);
|
||||
setTime(1,0,0,11,03,2018); test(false);
|
||||
setTime(3,0,0,11,03,2018); test(true);
|
||||
setTime(0,0,0,01,11,2018); test(true);
|
||||
setTime(0,0,0,30,11,2018); test(false);
|
||||
setTime(1,0,0,04,11,2018); test(true);
|
||||
setTime(3,0,0,04,11,2018); test(false);
|
||||
}
|
||||
|
||||
void loop () {}
|
||||
10
libraries/NtpClientLib/examples/NTPDSTTest/platformio.ini
Normal file
10
libraries/NtpClientLib/examples/NTPDSTTest/platformio.ini
Normal file
@@ -0,0 +1,10 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_dir = ../..
|
||||
|
||||
[env:leonardo]
|
||||
platform = atmelavr
|
||||
board = leonardo
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
https://github.com/xoseperez/Time
|
||||
Reference in New Issue
Block a user