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; const int daylightOffset_sec = 3600;
struct tm timeinfo; struct tm timeinfo;
volatile int currentSec = 59; volatile int currentSec = 30;
volatile int currentMin = 1; volatile int currentMin = 50;
volatile int currentHour = 2; volatile int currentHour = 50;
volatile bool NTPreachable = false; volatile bool NTPreachable = false;
volatile int MAX_BRIGHTNESS = 200; volatile int MAX_BRIGHTNESS = 200;

View File

@ -109,7 +109,7 @@ void iotWebConf_Setup() {
iotWebConf.setConfigSavedCallback(&iotWebConfConfigSaved); iotWebConf.setConfigSavedCallback(&iotWebConfConfigSaved);
//iotWebConf.setWifiConnectionTimeoutMs(60000); //iotWebConf.setWifiConnectionTimeoutMs(60000);
iotWebConf.setWifiConnectionCallback(&syncNTP); //NTP call after connection established iotWebConf.setWifiConnectionCallback(&setNTP); //setNTP call after connection established
//iotWebConf.setFormValidator(&iotWebConfFormValidator); //iotWebConf.setFormValidator(&iotWebConfFormValidator);
iotWebConf.getApTimeoutParameter()->visible = true; iotWebConf.getApTimeoutParameter()->visible = true;
iotWebConf.setupUpdateServer( iotWebConf.setupUpdateServer(

View File

@ -1,20 +1,21 @@
#ifdef ESP8266 // https://github.com/esp8266/Arduino/commit/a05a71fa9d2e6b143cb34f01b47e22c4b66b80a1
bool getLocalTime(struct tm * info, uint32_t ms) //#ifdef ESP8266
{ //bool getLocalTimeMe(struct tm * info, uint32_t ms)
uint32_t start = millis(); //{
time_t now; // uint32_t start = millis();
while ((millis() - start) <= ms) { // time_t now;
time(&now); // while ((millis() - start) <= ms) {
localtime_r(&now, info); // time(&now);
if (info->tm_year > (2016 - 1900)) { // localtime_r(&now, info);
return true; // if (info->tm_year > (2016 - 1900)) {
} // return true;
delay(10); // }
} // delay(10);
return false; // }
} // return false;
#endif //}
//#endif
String printLocalTime() { String printLocalTime() {
if (!getLocalTime(&timeinfo, 200)) { if (!getLocalTime(&timeinfo, 200)) {
@ -23,7 +24,6 @@ String printLocalTime() {
return "N/A"; return "N/A";
} }
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); //Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
char timeStringBuff[50]; //50 chars should be enough char timeStringBuff[50]; //50 chars should be enough
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo); strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
//Serial.println(timeStringBuff); //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() { void setNTP() {
if (iotWebConf.getState() == iotwebconf::OnLine) { //init and get the time
//init and get the time
#ifdef ESP8266 #ifdef ESP8266
//sntp_servermode_dhcp(0); //sntp_servermode_dhcp(0);
configTime(MYTZ, ntpServerParam.value()); configTime(MYTZ, ntpServerParam.value());
yield();
settimeofday_cb(timeRefresh);
#endif #endif
#ifdef ESP32 #ifdef ESP32
configTime(0, 0, ntpServer); configTime(0, 0, ntpServer);
// TZ string information: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html // TZ string information: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
setenv("TZ", "AEST-10", 1); setenv("TZ", "AEST-10", 1);
tzset(); // save the TZ variable tzset(); // save the TZ variable
//setTimeZone(long offset, int daylight); //setTimeZone(long offset, int daylight);
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue); //configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
#endif #endif
Serial.println(printLocalTime()); Serial.println(printLocalTime());
if (NTPreachable) { timeRefresh(false);
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();
}
} }
// Scheduler // Scheduler
@ -84,14 +85,13 @@ void clockTickCallback() {
currentSec = 0; currentSec = 0;
currentMin++; currentMin++;
if (!NTPreachable) { if (!NTPreachable) {
syncNTP(); setNTP();
} }
if (currentMin >= 60) { if (currentMin >= 60) {
currentMin = 0; currentMin = 0;
currentHour++; currentHour++;
if (currentHour >= 12) { if (currentHour >= 12) {
currentHour = 0; currentHour = 0;
syncNTP();
} }
} }
if (followingHour) { if (followingHour) {
@ -108,14 +108,13 @@ void clockTickCallback() {
currentSec = 59; currentSec = 59;
currentMin--; currentMin--;
if (!NTPreachable) { if (!NTPreachable) {
syncNTP(); setNTP();
} }
if (currentMin < 0) { if (currentMin < 0) {
currentMin = 59; currentMin = 59;
currentHour--; currentHour--;
if (currentHour < 0) { if (currentHour < 0) {
currentHour = 11; currentHour = 11;
syncNTP();
} }
} }
if (followingHour) { if (followingHour) {