fix compile issue with getLocalTime,restructure NTP handling
This commit is contained in:
parent
db6cd39b01
commit
8a50acdff4
@ -66,9 +66,9 @@ const long gmtOffset_sec = 3600; //ToDo changable from user
|
||||
const int daylightOffset_sec = 3600;
|
||||
struct tm timeinfo;
|
||||
|
||||
volatile int currentSec = 59;
|
||||
volatile int currentMin = 1;
|
||||
volatile int currentHour = 2;
|
||||
volatile int currentSec = 30;
|
||||
volatile int currentMin = 50;
|
||||
volatile int currentHour = 50;
|
||||
volatile bool NTPreachable = false;
|
||||
|
||||
volatile int MAX_BRIGHTNESS = 200;
|
||||
|
@ -109,7 +109,7 @@ void iotWebConf_Setup() {
|
||||
|
||||
iotWebConf.setConfigSavedCallback(&iotWebConfConfigSaved);
|
||||
//iotWebConf.setWifiConnectionTimeoutMs(60000);
|
||||
iotWebConf.setWifiConnectionCallback(&syncNTP); //NTP call after connection established
|
||||
iotWebConf.setWifiConnectionCallback(&setNTP); //setNTP call after connection established
|
||||
//iotWebConf.setFormValidator(&iotWebConfFormValidator);
|
||||
iotWebConf.getApTimeoutParameter()->visible = true;
|
||||
iotWebConf.setupUpdateServer(
|
||||
|
111
realtime.ino
111
realtime.ino
@ -1,20 +1,21 @@
|
||||
|
||||
#ifdef ESP8266
|
||||
bool getLocalTime(struct tm * info, uint32_t ms)
|
||||
{
|
||||
uint32_t start = millis();
|
||||
time_t now;
|
||||
while ((millis() - start) <= ms) {
|
||||
time(&now);
|
||||
localtime_r(&now, info);
|
||||
if (info->tm_year > (2016 - 1900)) {
|
||||
return true;
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
// https://github.com/esp8266/Arduino/commit/a05a71fa9d2e6b143cb34f01b47e22c4b66b80a1
|
||||
//#ifdef ESP8266
|
||||
//bool getLocalTimeMe(struct tm * info, uint32_t ms)
|
||||
//{
|
||||
// uint32_t start = millis();
|
||||
// time_t now;
|
||||
// while ((millis() - start) <= ms) {
|
||||
// time(&now);
|
||||
// localtime_r(&now, info);
|
||||
// if (info->tm_year > (2016 - 1900)) {
|
||||
// return true;
|
||||
// }
|
||||
// delay(10);
|
||||
// }
|
||||
// return false;
|
||||
//}
|
||||
//#endif
|
||||
|
||||
String printLocalTime() {
|
||||
if (!getLocalTime(&timeinfo, 200)) {
|
||||
@ -23,7 +24,6 @@ String printLocalTime() {
|
||||
return "N/A";
|
||||
}
|
||||
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
|
||||
|
||||
char timeStringBuff[50]; //50 chars should be enough
|
||||
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
|
||||
//Serial.println(timeStringBuff);
|
||||
@ -32,46 +32,47 @@ String printLocalTime() {
|
||||
|
||||
}
|
||||
|
||||
void timeRefresh( bool from_sntp ) {
|
||||
if (clockwiseRing) {
|
||||
currentSec = timeinfo.tm_sec;
|
||||
currentMin = timeinfo.tm_min;
|
||||
currentHour = MOD(timeinfo.tm_hour, 12);
|
||||
}
|
||||
else {
|
||||
//inverted logic - remaining time
|
||||
currentSec = 60 - timeinfo.tm_sec;
|
||||
currentMin = 60 - timeinfo.tm_min;
|
||||
currentHour = 12 - MOD(timeinfo.tm_hour, 12);
|
||||
}
|
||||
Serial.println("NTP mapped: " + String(int(currentHour * NUM_LEDS / 12)) + ":" + String(int(currentMin * NUM_LEDS / 60)) + ":" + String(int(currentSec * NUM_LEDS / 60)));
|
||||
bootAnim.disable();
|
||||
iotWebConfConfigSaved();
|
||||
ledRefresh.enable();
|
||||
#ifdef LDR_PIN
|
||||
brightnessFading.enable();
|
||||
#endif
|
||||
clockTick.enable(); //enable to check for ntp every 60 seconds
|
||||
brightnessAdjustment.enable();
|
||||
}
|
||||
|
||||
void syncNTP() {
|
||||
if (iotWebConf.getState() == iotwebconf::OnLine) {
|
||||
//init and get the time
|
||||
void setNTP() {
|
||||
//init and get the time
|
||||
#ifdef ESP8266
|
||||
//sntp_servermode_dhcp(0);
|
||||
configTime(MYTZ, ntpServerParam.value());
|
||||
//sntp_servermode_dhcp(0);
|
||||
configTime(MYTZ, ntpServerParam.value());
|
||||
yield();
|
||||
settimeofday_cb(timeRefresh);
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
configTime(0, 0, ntpServer);
|
||||
// TZ string information: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
|
||||
setenv("TZ", "AEST-10", 1);
|
||||
tzset(); // save the TZ variable
|
||||
//setTimeZone(long offset, int daylight);
|
||||
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
|
||||
configTime(0, 0, ntpServer);
|
||||
// TZ string information: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
|
||||
setenv("TZ", "AEST-10", 1);
|
||||
tzset(); // save the TZ variable
|
||||
//setTimeZone(long offset, int daylight);
|
||||
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
|
||||
#endif
|
||||
Serial.println(printLocalTime());
|
||||
if (NTPreachable) {
|
||||
if (clockwiseRing) {
|
||||
currentSec = timeinfo.tm_sec;
|
||||
currentMin = timeinfo.tm_min;
|
||||
currentHour = MOD(timeinfo.tm_hour, 12);
|
||||
}
|
||||
else {
|
||||
//inverted logic - remaining time
|
||||
currentSec = 60 - timeinfo.tm_sec;
|
||||
currentMin = 60 - timeinfo.tm_min;
|
||||
currentHour = 12 - MOD(timeinfo.tm_hour, 12);
|
||||
}
|
||||
Serial.println("NTP mapped: " + String(int(currentHour * NUM_LEDS / 12)) + ":" + String(int(currentMin * NUM_LEDS / 60)) + ":" + String(int(currentSec * NUM_LEDS / 60)));
|
||||
bootAnim.disable();
|
||||
iotWebConfConfigSaved();
|
||||
ledRefresh.enable();
|
||||
#ifdef LDR_PIN
|
||||
brightnessFading.enable();
|
||||
#endif
|
||||
}
|
||||
clockTick.enable(); //enable to check for ntp every 60 seconds
|
||||
brightnessAdjustment.enable();
|
||||
}
|
||||
Serial.println(printLocalTime());
|
||||
timeRefresh(false);
|
||||
}
|
||||
|
||||
// Scheduler
|
||||
@ -84,14 +85,13 @@ void clockTickCallback() {
|
||||
currentSec = 0;
|
||||
currentMin++;
|
||||
if (!NTPreachable) {
|
||||
syncNTP();
|
||||
setNTP();
|
||||
}
|
||||
if (currentMin >= 60) {
|
||||
currentMin = 0;
|
||||
currentHour++;
|
||||
if (currentHour >= 12) {
|
||||
currentHour = 0;
|
||||
syncNTP();
|
||||
}
|
||||
}
|
||||
if (followingHour) {
|
||||
@ -108,14 +108,13 @@ void clockTickCallback() {
|
||||
currentSec = 59;
|
||||
currentMin--;
|
||||
if (!NTPreachable) {
|
||||
syncNTP();
|
||||
setNTP();
|
||||
}
|
||||
if (currentMin < 0) {
|
||||
currentMin = 59;
|
||||
currentHour--;
|
||||
if (currentHour < 0) {
|
||||
currentHour = 11;
|
||||
syncNTP();
|
||||
}
|
||||
}
|
||||
if (followingHour) {
|
||||
|
Loading…
Reference in New Issue
Block a user