Skip to content

Commit 66923c6

Browse files
committed
chore: check time always, restart influxdb client on timeout
1 parent 6353b7a commit 66923c6

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

arduino/WeatherStation/InfluxDBHelper.h

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class InfluxDBHelper {
3333
String errorMsg() {
3434
return _client?_client->getLastErrorMessage():"";
3535
}
36+
int8_t statusCode() {
37+
return _client?_client->getLastStatusCode():0;
38+
}
3639
void begin( InfluxDBSettings *settings);
3740
void update( bool firstStart, const String &deviceID, const String &wifi, const String &version, const String &location, bool metric);
3841
void registerResetInfo(const String &resetReason);

arduino/WeatherStation/ScreenClock.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "WeatherStationFonts.h"
55
#include "WeatherStationImages.h"
66

7-
bool updateClock( bool firstStart, int utc_offset, const String &ntp) {
7+
bool updateClock( int utc_offset, const String &ntp) {
88
//Convert ntp comma separated list to array
99
char ntpbuff[50];
1010
char *ntparr[3];
@@ -15,7 +15,7 @@ bool updateClock( bool firstStart, int utc_offset, const String &ntp) {
1515

1616
//Set TZ and NTP
1717
configTime( utc_offset, 0, ntparr[0], ntparr[1], ntparr[2]);
18-
if (firstStart) {
18+
if (time(nullptr) < 1000000000ul) {
1919
// Wait till time is synced
2020
Serial.print(F("Syncing time"));
2121
int i = 0;

arduino/WeatherStation/WeatherStation.ino

+28-11
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool skipNightScreen = false;
6767
void updateData(OLEDDisplay *display, bool firstStart);
6868
bool loadIoTCenter( bool firstStart, const String& iot_url, const char *deviceID, InfluxDBSettings *influxdbSettings, unsigned int& iotRefreshMin, float& latitude, float& longitude);
6969
int detectLocationFromIP(RegionalSettings *pRegionalSettings);
70-
bool updateClock( bool firstStart, int utc_offset, const String &ntp);
70+
bool updateClock(int utc_offset, const String &ntp);
7171
bool updateAstronomy(bool firstStart, const float lat, const float lon);
7272
bool updateCurrentWeather(RegionalSettings *pRegionalSettings, const String& APIKey);
7373
bool updateForecast( RegionalSettings *pRegionalSettings, const String& APIKey);
@@ -200,7 +200,7 @@ void updateData(OLEDDisplay *display, bool firstStart) {
200200
if (influxdbHelper.getWriteSuccess() == 0) { //without any successfull write?
201201
Serial.println(F("Failed all writes to InfluxDB, restarting!"));
202202
//TODO: store status to identify reason of restart
203-
203+
safeRestart();
204204
}
205205
Serial.print(F("InfluxDB successful writes: "));
206206
Serial.println( influxdbHelper.getWriteSuccess());
@@ -243,13 +243,7 @@ void updateData(OLEDDisplay *display, bool firstStart) {
243243
}
244244

245245
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();
253247

254248

255249
drawUpdateProgress(display, 20, getStr(s_Checking_update));
@@ -456,20 +450,33 @@ void loop() {
456450
delay(500);
457451
--i;
458452
}
453+
454+
if(time(nullptr) < 1000000000ul) {
455+
Serial.println(F("Time is not synced, trying update"));
456+
updateTime();
457+
}
459458
ServicesTracker.updateServiceState(SyncServices::ServiceDBWriteData, ServiceState::SyncStarted);
460459
ServicesTracker.save();
461460
//always save in celsius
462461
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) {
464464
ServicesTracker.updateServiceState(SyncServices::ServiceDBWriteData, ServiceState::SyncOk);
465465
} else {
466466
ServicesTracker.updateServiceState(SyncServices::ServiceDBWriteData, ServiceState::SyncFailed);
467467
}
468468
ServicesTracker.save();
469469
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")));
471472
digitalWrite( PIN_LED, HIGH);
472473
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+
}
473480
}
474481
}
475482
if(!station.isServerStarted()) {
@@ -538,6 +545,16 @@ bool isInfluxDBError() {
538545
return influxdbHelper.isError();
539546
}
540547

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+
541558
void wifiConnectionEventHandler(WifiConnectionEvent event, const char *ssid) {
542559
// WiFi interrupt events, don't do anything complex, just update state variables
543560
switch(event) {

0 commit comments

Comments
 (0)