@@ -67,7 +67,7 @@ bool skipNightScreen = false;
67
67
void updateData (OLEDDisplay *display, bool firstStart);
68
68
bool loadIoTCenter ( bool firstStart, const String& iot_url, const char *deviceID, InfluxDBSettings *influxdbSettings, unsigned int & iotRefreshMin, float & latitude, float & longitude);
69
69
int detectLocationFromIP (RegionalSettings *pRegionalSettings);
70
- bool updateClock ( bool firstStart, int utc_offset, const String &ntp);
70
+ bool updateClock (int utc_offset, const String &ntp);
71
71
bool updateAstronomy (bool firstStart, const float lat, const float lon);
72
72
bool updateCurrentWeather (RegionalSettings *pRegionalSettings, const String& APIKey);
73
73
bool updateForecast ( RegionalSettings *pRegionalSettings, const String& APIKey);
@@ -200,7 +200,7 @@ void updateData(OLEDDisplay *display, bool firstStart) {
200
200
if (influxdbHelper.getWriteSuccess () == 0 ) { // without any successfull write?
201
201
Serial.println (F (" Failed all writes to InfluxDB, restarting!" ));
202
202
// TODO: store status to identify reason of restart
203
-
203
+ safeRestart ();
204
204
}
205
205
Serial.print (F (" InfluxDB successful writes: " ));
206
206
Serial.println ( influxdbHelper.getWriteSuccess ());
@@ -243,13 +243,7 @@ void updateData(OLEDDisplay *display, bool firstStart) {
243
243
}
244
244
245
245
drawUpdateProgress (display, 10 , getStr (s_Updating_time));
246
- ServicesTracker.updateServiceState (SyncServices::ServiceClock, ServiceState::SyncStarted);
247
- ServicesTracker.save ();
248
- if (updateClock ( firstStart, station.getRegionalSettings ()->utcOffset , station.getAdvancedSettings ()->ntpServers )) {
249
- ServicesTracker.updateServiceState (SyncServices::ServiceClock, ServiceState::SyncOk);
250
- } else {
251
- ServicesTracker.updateServiceState (SyncServices::ServiceClock, ServiceState::SyncFailed);
252
- }
246
+ updateTime ();
253
247
254
248
255
249
drawUpdateProgress (display, 20 , getStr (s_Checking_update));
@@ -456,20 +450,33 @@ void loop() {
456
450
delay (500 );
457
451
--i;
458
452
}
453
+
454
+ if (time (nullptr ) < 1000000000ul ) {
455
+ Serial.println (F (" Time is not synced, trying update" ));
456
+ updateTime ();
457
+ }
459
458
ServicesTracker.updateServiceState (SyncServices::ServiceDBWriteData, ServiceState::SyncStarted);
460
459
ServicesTracker.save ();
461
460
// always save in celsius
462
461
unsigned long start = millis ();
463
- if (influxdbHelper.write ( Sensor::tempF2C ( pSensor->getTempF ()), pSensor->getHum (), station.getRegionalSettings ()->latitude , station.getRegionalSettings ()->longitude )) {
462
+ bool writeStatus = influxdbHelper.write ( Sensor::tempF2C ( pSensor->getTempF ()), pSensor->getHum (), station.getRegionalSettings ()->latitude , station.getRegionalSettings ()->longitude );
463
+ if (writeStatus) {
464
464
ServicesTracker.updateServiceState (SyncServices::ServiceDBWriteData, ServiceState::SyncOk);
465
465
} else {
466
466
ServicesTracker.updateServiceState (SyncServices::ServiceDBWriteData, ServiceState::SyncFailed);
467
467
}
468
468
ServicesTracker.save ();
469
469
Serial.print (F (" InfluxDB write " ));
470
- Serial.println (String (millis () - start) + String (F (" ms" )));
470
+ uint32_t writeTime = millis () - start;
471
+ Serial.println (String (writeTime) + String (F (" ms" )));
471
472
digitalWrite ( PIN_LED, HIGH);
472
473
WS_DEBUG_RAM (" After write" );
474
+ if (!writeStatus && influxdbHelper.statusCode () == HTTPC_ERROR_READ_TIMEOUT) {
475
+ Serial.println (F (" Restarting influxdb client" ));
476
+ influxdbHelper.release ();
477
+ influxdbHelper.begin (station.getInfluxDBSettings ());
478
+ WS_DEBUG_RAM (" After influxdb client restart" );
479
+ }
473
480
}
474
481
}
475
482
if (!station.isServerStarted ()) {
@@ -538,6 +545,16 @@ bool isInfluxDBError() {
538
545
return influxdbHelper.isError ();
539
546
}
540
547
548
+ void updateTime () {
549
+ ServicesTracker.updateServiceState (SyncServices::ServiceClock, ServiceState::SyncStarted);
550
+ ServicesTracker.save ();
551
+ if (updateClock ( station.getRegionalSettings ()->utcOffset , station.getAdvancedSettings ()->ntpServers )) {
552
+ ServicesTracker.updateServiceState (SyncServices::ServiceClock, ServiceState::SyncOk);
553
+ } else {
554
+ ServicesTracker.updateServiceState (SyncServices::ServiceClock, ServiceState::SyncFailed);
555
+ }
556
+ }
557
+
541
558
void wifiConnectionEventHandler (WifiConnectionEvent event, const char *ssid) {
542
559
// WiFi interrupt events, don't do anything complex, just update state variables
543
560
switch (event) {
0 commit comments