using configTime and AceTime to replace ezTime

This commit is contained in:
Robert van den Breemen 2021-10-16 20:36:00 +02:00
parent a449a01a06
commit 45b51f2068
4 changed files with 78 additions and 54 deletions

View File

@ -10,7 +10,9 @@
*/
#include <Arduino.h>
#include <ezTime.h> // https://github.com/ropg/ezTime
//#include <ezTime.h> // https://github.com/ropg/ezTime
#include <AceTime.h>
#include <TimeLib.h>
#include <TelnetStream.h> // https://github.com/jandrassy/TelnetStream/commit/1294a9ee5cc9b1f7e51005091e351d60c8cddecf
#include <ArduinoJson.h> // https://arduinojson.org/
#include "Wire.h"
@ -41,10 +43,9 @@ void setLed(int8_t, uint8_t);
#define _HOSTNAME "OTGW"
#define SETTINGS_FILE "/settings.ini"
#define NTP_DEFAULT_TIMEZONE "Europe/Amsterdam"
#define NTP_HOST_DEFAULT "time.google.com"
#define NTP_HOST_DEFAULT "pool.ntp.org"
#define NTP_RESYNC_TIME 1800 //seconds = every 30 minutes
#define HOME_ASSISTANT_DISCOVERY_PREFIX "homeassistant" // Home Assistant discovery prefix
#define CMSG_SIZE 512
#define JSON_BUFF_MAX 1024
#define CSTR(x) x.c_str()
@ -64,9 +65,14 @@ char fChar[10];
String lastReset = "";
uint32_t upTimeSeconds = 0;
uint32_t rebootCount = 0;
Timezone myTZ;
String sMessage = "";
//Use acetime
using namespace ace_time;
static BasicZoneProcessor timeProcessor;
static const int CACHE_SIZE = 3;
static BasicZoneManager<CACHE_SIZE> manager(zonedb::kZoneRegistrySize, zonedb::kZoneRegistry);
const char *weekDayName[] { "Unknown", "Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Unknown" };
const char *flashMode[] { "QIO", "QOUT", "DIO", "DOUT", "Unknown" };

View File

@ -131,45 +131,6 @@ void restartWifi(){
if (iTryRestarts >= 15) doRestart("Too many wifi reconnect attempts");
}
//====[ startNTP ]===
void startNTP(){
// Initialisation ezTime
if (!settingNTPenable) return;
setDebug(NONE);
if (settingNTPtimezone.length()==0) settingNTPtimezone = "Europe/Amsterdam"; //set back to default timezone
if (myTZ.setLocation(settingNTPtimezone)){
DebugTf("Timezone set to: %s\r\n", CSTR(settingNTPtimezone));
DebugTf("Olson TZ : %s\r\n", CSTR(myTZ.getOlson()));
DebugTf("Posix TZ : %s\r\n", CSTR(myTZ.getPosix()));
DebugTf("TZ Name : %s\r\n", CSTR(myTZ.getTimezoneName()));
DebugTf("TX Offset: %d\r\n", myTZ.getOffset());
DebugTf("DST : %d\r\n", myTZ.isDST());
} else {
DebugTf("Error setting Timezone: %s\r\n", CSTR(errorString()));
settingNTPtimezone = "Europe/Amsterdam";
}
myTZ.setDefault();
setServer(CSTR(settingNTPhostname));
updateNTP(); //force NTP sync
//active wait for sync for 60 seconds
DECLARE_TIMER_SEC(timeoutNTPsync, 60, CATCH_UP_MISSED_TICKS);
while (timeStatus() == timeNotSet)
{
delay(100);
feedWatchDog(); //feeding the dog, while waiting activly
if DUE(timeoutNTPsync) break; //timeout, then break out of this loop
}
setDebug(NONE); //turn off any other debug information
DebugTln("UTC time : "+ UTC.dateTime());
DebugTln("local time: "+ myTZ.dateTime());
}
//===[ blink status led ]===
void setLed(uint8_t led, uint8_t status){
pinMode(led, OUTPUT);
@ -261,7 +222,7 @@ void doBackgroundTasks()
handleOTGW(); // OTGW handling
httpServer.handleClient();
MDNS.update();
events(); // trigger ezTime update etc
loopNTP();
} //otherwise, just wait until reconnected gracefully
delay(1);
}

View File

@ -52,6 +52,19 @@
//#include <FS.h> // part of ESP8266 Core https://github.com/esp8266/Arduino
#include <LittleFS.h>
//Use the NTP SDK ESP 8266
#include <time.h>
enum NtpStatus_t {
TIME_NOTSET,
TIME_SYNC,
TIME_WAITFORSYNC,
TIME_NEEDSYNC
};
NtpStatus_t NtpStatus = TIME_NOTSET;
time_t NtpLastSync = 0; //last sync moment in EPOCH seconds
ESP8266WebServer httpServer (80);
ESP8266HTTPUpdateServer httpUpdater(true);
@ -165,6 +178,50 @@ void startLLMNR(const char *hostname)
} // startLLMNR()
//====[ startNTP ]===
void startNTP(){
// Initialisation ezTime
if (!settingNTPenable) return;
if (settingNTPtimezone.length()==0) settingNTPtimezone = NTP_DEFAULT_TIMEZONE; //set back to default timezone
if (settingNTPhostname.length()==0) settingNTPhostname = NTP_HOST_DEFAULT; //set back to default timezone
//void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
configTime(0, 0, CSTR(settingNTPhostname), nullptr, nullptr);
NtpStatus = TIME_WAITFORSYNC;
}
void loopNTP(){
if (!settingNTPenable) return;
switch (NtpStatus){
case TIME_NOTSET:
case TIME_NEEDSYNC:
NtpLastSync = time(nullptr); //remember last sync
startNTP();
NtpStatus = TIME_WAITFORSYNC;
break;
case TIME_WAITFORSYNC:
if ((time(nullptr)>0) || (time(nullptr) >= NtpLastSync)) {
NtpLastSync = time(nullptr); //remember last sync
auto myTz = manager.createForZoneName(CSTR(settingNTPtimezone));
auto myTime = ZonedDateTime::forUnixSeconds(NtpLastSync, myTz);
setTime(myTime.hour(), myTime.minute(), myTime.second(), myTime.day(), myTime.month(), myTime.year());
NtpStatus = TIME_SYNC;
}
break;
case TIME_SYNC:
if ((time(nullptr)-NtpLastSync) > NTP_RESYNC_TIME){
//when xx seconds have passed, resync using NTP
NtpStatus = TIME_NEEDSYNC;
}
break;
}
DECLARE_TIMER_SEC(timerNTPtime, 10, CATCH_UP_MISSED_TICKS);
if DUE(timerNTPtime) DebugTf("Epoch Sec (UTC): %d\n\r", time(nullptr)); //timeout, then break out of this loop
}
String getMacAddress() {
uint8_t baseMac[6];
char baseMacChr[13] = {0};

View File

@ -2,15 +2,15 @@
#define _VERSION_MAJOR 0
#define _VERSION_MINOR 8
#define _VERSION_PATCH 7
#define _VERSION_BUILD 1089
#define _VERSION_GITHASH "9c79ca5"
#define _VERSION_BUILD 1119
#define _VERSION_GITHASH "a449a01"
#define _VERSION_PRERELEASE beta
#define _VERSION_DATE "16-09-2021"
#define _VERSION_TIME "23:50:25"
#define _VERSION_DATE "16-10-2021"
#define _VERSION_TIME "19:03:17"
#define _SEMVER_CORE "0.8.7"
#define _SEMVER_BUILD "0.8.7+1089"
#define _SEMVER_GITHASH "0.8.7+9c79ca5"
#define _SEMVER_FULL "0.8.7-beta+9c79ca5"
#define _SEMVER_NOBUILD "0.8.7-beta (16-09-2021)"
#define _VERSION "0.8.7-beta+9c79ca5 (16-09-2021)"
#define _SEMVER_BUILD "0.8.7+1119"
#define _SEMVER_GITHASH "0.8.7+a449a01"
#define _SEMVER_FULL "0.8.7-beta+a449a01"
#define _SEMVER_NOBUILD "0.8.7-beta (16-10-2021)"
#define _VERSION "0.8.7-beta+a449a01 (16-10-2021)"
//The version information is created automatically, more information here: https://github.com/rvdbreemen/autoinc-semver