Browse Source

reboot on error, fwupgrade first

main
Hendrik Langer 5 years ago
parent
commit
eb4a13a205
  1. 21
      src/main.cpp
  2. 2
      src/main.h

21
src/main.cpp

@ -226,6 +226,8 @@ void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int for
} }
void ICACHE_FLASH_ATTR sendValues() { void ICACHE_FLASH_ATTR sendValues() {
int errors = 0;
sd.temperature = dht.readTemperature(); sd.temperature = dht.readTemperature();
sd.humidity = dht.readHumidity(); sd.humidity = dht.readHumidity();
@ -334,6 +336,14 @@ void ICACHE_FLASH_ATTR sendValues() {
sd.rssi = WiFi.RSSI(); sd.rssi = WiFi.RSSI();
loop_count++;
if (loop_count == 1) {
XD0OTA ota;
ota.update();
} else if (loop_count > 720) { // do an update every 12h
loop_count = 0;
}
int httpCode = 0; int httpCode = 0;
for (int tries=0; tries<3 && httpCode != HTTP_CODE_CREATED; tries++) { for (int tries=0; tries<3 && httpCode != HTTP_CODE_CREATED; tries++) {
@ -359,6 +369,7 @@ void ICACHE_FLASH_ATTR sendValues() {
httpclient.end(); httpclient.end();
} }
if (httpCode != HTTP_CODE_CREATED) errors++;
net.setInsecure(); net.setInsecure();
mqttclient.begin(mqttserver, 8883, net); mqttclient.begin(mqttserver, 8883, net);
@ -375,6 +386,7 @@ void ICACHE_FLASH_ATTR sendValues() {
DEBUG_MSG("mqtt done\n"); DEBUG_MSG("mqtt done\n");
} else { } else {
DEBUG_MSG("mqtt failed\n"); DEBUG_MSG("mqtt failed\n");
errors++;
} }
mqttclient.loop(); mqttclient.loop();
mqttclient.disconnect(); mqttclient.disconnect();
@ -383,13 +395,10 @@ void ICACHE_FLASH_ATTR sendValues() {
sd.rssi = WiFi.RSSI(); sd.rssi = WiFi.RSSI();
} }
if (loop_count == 1) { if (errors >= 2) {
XD0OTA ota; DEBUG_MSG("[ERROR] could not send any values\nRebooting..\n");
ota.update(); ESP.restart();
} else if (loop_count > 720) { // do an update every 12h
loop_count = 0;
} }
loop_count++;
net.stop(); net.stop();
WiFi.disconnect(true); WiFi.disconnect(true);

2
src/main.h

@ -7,6 +7,6 @@
#define DEBUG_MSG(...) #define DEBUG_MSG(...)
#endif #endif
#define FW_VERSION 35 #define FW_VERSION 37
#endif /* _MAIN_H */ #endif /* _MAIN_H */

Loading…
Cancel
Save