mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-15 00:02:49 +03:00
lwip2 fixes and time/ntp management
core: +settimeofday() core: +coredecls.h +sntp-lwip2.c core: fix clock_gettime() with micros64() core: honor DST in configTime() core: internal clock is automatically started examples: +esp8266/NTP-TZ-DST.ino lwip2: sntp client removed lwip2: fix crashing with WiFi.softAPConfig(ip,ip,ip) fix #3852
This commit is contained in:
committed by
Ivan Grokhotkov
parent
2f09ea7117
commit
cbfbc1ad63
128
libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino
Normal file
128
libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino
Normal file
@ -0,0 +1,128 @@
|
||||
/*
|
||||
NTP-TZ-DST
|
||||
NetWork Time Protocol - Time Zone - Daylight Saving Time
|
||||
|
||||
This example shows how to read and set time,
|
||||
and how to use NTP (set NTP0_OR_LOCAL1 to 0 below)
|
||||
or an external RTC (set NTP0_OR_LOCAL1 to 1 below)
|
||||
|
||||
TZ and DST below have to be manually set
|
||||
according to your local settings.
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
#include <time.h> // time() ctime()
|
||||
#include <sys/time.h> // struct timeval
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
|
||||
#define SSID "open"
|
||||
#define SSIDPWD ""
|
||||
#define TZ 1 // (utc+) TZ in hours
|
||||
#define DST_MN 60 // use 60mn for summer time in some countries
|
||||
|
||||
#define NTP0_OR_LOCAL1 1 // 0:use NTP 1:fake external RTC
|
||||
#define RTC_TEST 1510592825 // 1510592825 = Monday 13 November 2017 17:07:05 UTC
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
|
||||
#define TZ_MN ((TZ)*60)
|
||||
#define TZ_SEC ((TZ)*3600)
|
||||
#define DST_SEC ((DST_MN)*60)
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
#if NTP0_OR_LOCAL1
|
||||
// local
|
||||
|
||||
ESP.eraseConfig();
|
||||
time_t rtc = RTC_TEST;
|
||||
timeval tv = { rtc, 0 };
|
||||
timezone tz = { TZ_MN + DST_MN, 0 };
|
||||
settimeofday(&tv, &tz);
|
||||
|
||||
#else // ntp
|
||||
|
||||
configTime(TZ_SEC, DST_SEC, "pool.ntp.org");
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SSID, SSIDPWD);
|
||||
// don't wait, observe time changing when ntp timestamp is received
|
||||
|
||||
#endif // ntp
|
||||
}
|
||||
|
||||
// for testing purpose:
|
||||
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);
|
||||
|
||||
#define PTM(w) \
|
||||
Serial.print(":" #w "="); \
|
||||
Serial.print(tm->tm_##w);
|
||||
|
||||
void printTm (const char* what, const tm* tm) {
|
||||
Serial.print(what);
|
||||
PTM(isdst); PTM(yday); PTM(wday);
|
||||
PTM(year); PTM(mon); PTM(mday);
|
||||
PTM(hour); PTM(min); PTM(sec);
|
||||
}
|
||||
|
||||
timeval tv;
|
||||
timespec tp;
|
||||
time_t now;
|
||||
uint32_t now_ms, now_us;
|
||||
|
||||
void loop() {
|
||||
|
||||
gettimeofday(&tv, nullptr);
|
||||
clock_gettime(0, &tp);
|
||||
now = time(nullptr);
|
||||
now_ms = millis();
|
||||
now_us = micros();
|
||||
|
||||
// localtime / gmtime every second change
|
||||
static time_t lastv = 0;
|
||||
if (lastv != tv.tv_sec) {
|
||||
lastv = tv.tv_sec;
|
||||
Serial.println();
|
||||
printTm("localtime", localtime(&now));
|
||||
Serial.println();
|
||||
printTm("gmtime ", gmtime(&now));
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// time from boot
|
||||
Serial.print("clock:");
|
||||
Serial.print((uint32_t)tp.tv_sec);
|
||||
Serial.print("/");
|
||||
Serial.print((uint32_t)tp.tv_nsec);
|
||||
Serial.print("ns");
|
||||
|
||||
// time from boot
|
||||
Serial.print(" millis:");
|
||||
Serial.print(now_ms);
|
||||
Serial.print(" micros:");
|
||||
Serial.print(now_us);
|
||||
|
||||
// EPOCH+tz+dst
|
||||
Serial.print(" gtod:");
|
||||
Serial.print((uint32_t)tv.tv_sec);
|
||||
Serial.print("/");
|
||||
Serial.print((uint32_t)tv.tv_usec);
|
||||
Serial.print("us");
|
||||
|
||||
// EPOCH+tz+dst
|
||||
Serial.print(" time:");
|
||||
Serial.print((uint32_t)now);
|
||||
|
||||
// human readable
|
||||
Serial.print(" ctime:(UTC+");
|
||||
Serial.print((uint32_t)(TZ * 60 + DST_MN));
|
||||
Serial.print("mn)");
|
||||
Serial.print(ctime(&now));
|
||||
|
||||
// simple drifting loop
|
||||
delay(100);
|
||||
}
|
Reference in New Issue
Block a user