From bd8a4ce397d6b8260f19570e283f0ba3b99de0eb Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 4 Jul 2019 11:52:56 +0200 Subject: [PATCH] restart after connection error --- src/main.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2b39052..2ba1d4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,6 +102,7 @@ volatile unsigned long geiger_counts = 0; unsigned long geiger_previousMillis; unsigned long last_dhcp = 0; unsigned long previousMillis = 0; +unsigned long lastSuccess = 0; IPAddress ip, dns, gateway, subnet; char ssid[64]; char password[64]; @@ -226,7 +227,7 @@ void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int for } void ICACHE_FLASH_ATTR sendValues() { - int errors = 0; + unsigned long currentMillis = rtcMillis(); sd.temperature = dht.readTemperature(); sd.humidity = dht.readHumidity(); @@ -357,6 +358,7 @@ void ICACHE_FLASH_ATTR sendValues() { if (httpCode > 0) { if (httpCode == HTTP_CODE_CREATED) { + lastSuccess = currentMillis; #ifdef USERDEBUG httpclient.writeToStream(&Serial); DEBUG_MSG("\n"); @@ -369,7 +371,6 @@ void ICACHE_FLASH_ATTR sendValues() { httpclient.end(); } - if (httpCode != HTTP_CODE_CREATED) errors++; net.setInsecure(); mqttclient.begin(mqttserver, 8883, net); @@ -382,10 +383,10 @@ void ICACHE_FLASH_ATTR sendValues() { mqttclient.loop(); getValuesJSON(buffer, MQTT_MAX_PACKET_SIZE, 2); if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) { + lastSuccess = currentMillis; DEBUG_MSG("mqtt done\n"); } else { DEBUG_MSG("mqtt failed\n"); - errors++; } mqttclient.loop(); mqttclient.disconnect(); @@ -394,8 +395,8 @@ void ICACHE_FLASH_ATTR sendValues() { sd.rssi = WiFi.RSSI(); } - if (errors >= 2) { - DEBUG_MSG("[ERROR] could not send any values\nRebooting..\n"); + if (currentMillis - lastSuccess > 5* postingInterval) { + DEBUG_MSG("[ERROR] could not send any values for 5 times\nRebooting..\n"); ESP.restart(); }