Browse Source

fix dhcp problems on reconnect

main
Hendrik Langer 5 years ago
parent
commit
6a7959cef5
  1. 2
      platformio.ini
  2. 20
      src/main.cpp
  3. 2
      src/main.h

2
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 =

20
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();

2
src/main.h

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

Loading…
Cancel
Save