Semi adjustable NTP polling interval.

Fixes #3285
This commit is contained in:
Blaz Kristan 2023-08-14 22:56:58 +02:00
parent a2bda5a4db
commit d9e576c35c
7 changed files with 777 additions and 754 deletions

View File

@ -481,6 +481,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
JsonObject if_ntp = interfaces[F("ntp")];
CJSON(ntpEnabled, if_ntp["en"]);
getStringFromJson(ntpServerName, if_ntp[F("host")], 33); // "1.wled.pool.ntp.org"
CJSON(ntpSyncInterval, if_ntp[F("int")]);
CJSON(currentTimezone, if_ntp[F("tz")]);
CJSON(utcOffsetSecs, if_ntp[F("offset")]);
CJSON(useAMPM, if_ntp[F("ampm")]);

View File

@ -171,6 +171,13 @@
Get time from NTP server: <input type="checkbox" name="NT"><br>
<input type="text" name="NS" maxlength="32"><br>
Use 24h format: <input type="checkbox" name="CF"><br>
NTP refresh: <select name="NP">
<option value="86400">once</option>
<option value="43200" selected>twice</option>
<option value="21600">4 times</option>
<option value="14400">6 times</option>
<option value="10800">8 times</option>
</select> per day<br>
Time zone:
<select name="TZ">
<option value="0" selected>GMT(UTC)</option>

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,6 @@
* Acquires time from NTP server
*/
//#define WLED_DEBUG_NTP
#define NTP_SYNC_INTERVAL 42000UL //Get fresh NTP time about twice per day
Timezone* tz;
@ -180,7 +179,7 @@ void handleTime() {
void handleNetworkTime()
{
if (ntpEnabled && ntpConnected && millis() - ntpLastSyncTime > (1000*NTP_SYNC_INTERVAL) && WLED_CONNECTED)
if (ntpEnabled && ntpConnected && millis() - ntpLastSyncTime > (1000UL*ntpSyncInterval) && WLED_CONNECTED)
{
if (millis() - ntpPacketSentTime > 10000)
{

View File

@ -390,6 +390,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
ntpEnabled = request->hasArg(F("NT"));
strlcpy(ntpServerName, request->arg(F("NS")).c_str(), 33);
useAMPM = !request->hasArg(F("CF"));
ntpSyncInterval = min(86400U, max(10800U, (unsigned int)request->arg(F("NP")).toInt()));
currentTimezone = request->arg(F("TZ")).toInt();
utcOffsetSecs = request->arg(F("UO")).toInt();

View File

@ -639,13 +639,14 @@ WLED_GLOBAL DNSServer dnsServer;
// network time
WLED_GLOBAL bool ntpConnected _INIT(false);
WLED_GLOBAL time_t localTime _INIT(0);
WLED_GLOBAL uint32_t ntpSyncInterval _INIT(43200U);
WLED_GLOBAL unsigned long ntpLastSyncTime _INIT(999000000L);
WLED_GLOBAL unsigned long ntpPacketSentTime _INIT(999000000L);
WLED_GLOBAL IPAddress ntpServerIP;
WLED_GLOBAL uint16_t ntpLocalPort _INIT(2390);
WLED_GLOBAL uint16_t rolloverMillis _INIT(0);
WLED_GLOBAL float longitude _INIT(0.0);
WLED_GLOBAL float latitude _INIT(0.0);
WLED_GLOBAL float longitude _INIT(0.0f);
WLED_GLOBAL float latitude _INIT(0.0f);
WLED_GLOBAL time_t sunrise _INIT(0);
WLED_GLOBAL time_t sunset _INIT(0);
WLED_GLOBAL Toki toki _INIT(Toki());

View File

@ -574,6 +574,15 @@ void getSettingsJS(byte subPage, char* dest)
sappend('c',SET_F("NT"),ntpEnabled);
sappends('s',SET_F("NS"),ntpServerName);
sappend('c',SET_F("CF"),!useAMPM);
int syncIntervalIndex;
switch (ntpSyncInterval) {
case 86400: syncIntervalIndex = 0; break;
case 21600: syncIntervalIndex = 2; break;
case 14400: syncIntervalIndex = 3; break;
case 10800: syncIntervalIndex = 4; break;
default : syncIntervalIndex = 1; break;
}
sappend('i',SET_F("NP"),syncIntervalIndex);
sappend('i',SET_F("TZ"),currentTimezone);
sappend('v',SET_F("UO"),utcOffsetSecs);
char tm[32];