fix compile issue with getLocalTime,restructure NTP handling

This commit is contained in:
coelner 2022-08-19 21:29:43 +02:00
parent db6cd39b01
commit 8a50acdff4
3 changed files with 59 additions and 60 deletions

View File

@ -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;

View File

@ -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(

View File

@ -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,24 +32,7 @@ String printLocalTime() {
}
void syncNTP() {
if (iotWebConf.getState() == iotwebconf::OnLine) {
//init and get the time
#ifdef ESP8266
//sntp_servermode_dhcp(0);
configTime(MYTZ, ntpServerParam.value());
#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);
#endif
Serial.println(printLocalTime());
if (NTPreachable) {
void timeRefresh( bool from_sntp ) {
if (clockwiseRing) {
currentSec = timeinfo.tm_sec;
currentMin = timeinfo.tm_min;
@ -68,10 +51,28 @@ void syncNTP() {
#ifdef LDR_PIN
brightnessFading.enable();
#endif
}
clockTick.enable(); //enable to check for ntp every 60 seconds
brightnessAdjustment.enable();
}
}
void setNTP() {
//init and get the time
#ifdef ESP8266
//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);
#endif
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) {