Browse Source

restart after connection error

main
Hendrik Langer 5 years ago
parent
commit
bd8a4ce397
  1. 11
      src/main.cpp

11
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();
}

Loading…
Cancel
Save