From 6a7959cef5f38c312b73b4e1b3e3778d204c4072 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 27 Jun 2019 18:02:21 +0200 Subject: [PATCH] fix dhcp problems on reconnect --- platformio.ini | 2 ++ src/main.cpp | 20 ++++---------------- src/main.h | 2 +- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/platformio.ini b/platformio.ini index 851b98c..72fd7cf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,6 +16,7 @@ board = nodemcu framework = arduino board_build.f_cpu = 80000000L board_build.f_flash = 40000000L +board_build.flash_mode = dio monitor_speed = 115200 build_flags = @@ -25,6 +26,7 @@ build_flags = ; -DDEBUG_ESP_SSL ; -DDEBUG_ESP_TLS_MEM ; -DDEBUG_ESP_HTTP_CLIENT +; -DDEBUG_ESP_WIFI ; -D PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH lib_deps = diff --git a/src/main.cpp b/src/main.cpp index ceddfd1..7d49555 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,7 +37,7 @@ const char* mqttusername PROGMEM = "esp-weatherstation"; const char* mqttpassword PROGMEM = PWD_MQTT; constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden -constexpr unsigned int dhcp_interval = 60*60*1000; +constexpr unsigned int dhcp_interval = 3*60*1000; uint32_t loop_count = 0; @@ -301,6 +301,7 @@ void ICACHE_FLASH_ATTR sendValues() { } else { DEBUG_MSG("dhcp\n"); + WiFi.disconnect(); int tries = 0; constexpr unsigned int retry_delay = 500; constexpr unsigned int max_retry_delay = 12000; @@ -362,23 +363,12 @@ void ICACHE_FLASH_ATTR sendValues() { net.setInsecure(); mqttclient.begin(mqttserver, 8883, net); int tries = 0; - bool mqtt_error = false; constexpr unsigned int retry_delay = 500; - constexpr unsigned int max_retry_delay = 5000; - while (!mqttclient.connect(mqttusername, mqttusername, mqttpassword)) { - tries++; + for (int tries=0; tries<5 && !mqttclient.connect(mqttusername, mqttusername, mqttpassword); tries++ ) { DEBUG_MSG("."); - if (tries*retry_delay >= max_retry_delay) { - DEBUG_MSG(" [ERROR]\n"); - //DEBUG_MSG("Rebooting..\n"); - //ESP.restart(); - mqtt_error = true; - } delay(retry_delay); } - if (!mqtt_error) { - mqttclient.loop(); getValuesJSON(buffer, MQTT_MAX_PACKET_SIZE, 2); if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) { @@ -389,8 +379,6 @@ void ICACHE_FLASH_ATTR sendValues() { mqttclient.loop(); mqttclient.disconnect(); - } - if (sd.rssi == 0) { // re-read rssi if zero sd.rssi = WiFi.RSSI(); } @@ -404,7 +392,7 @@ void ICACHE_FLASH_ATTR sendValues() { loop_count++; net.stop(); - WiFi.disconnect(); + WiFi.disconnect(true); WiFi.mode(WIFI_OFF); WiFi.forceSleepBegin(); delay(1); // yield(); diff --git a/src/main.h b/src/main.h index 7156216..454bc3b 100644 --- a/src/main.h +++ b/src/main.h @@ -7,6 +7,6 @@ #define DEBUG_MSG(...) #endif -#define FW_VERSION 34 +#define FW_VERSION 35 #endif /* _MAIN_H */