let the library handle the reconnect, manually do it after 5 seconds

This commit is contained in:
Thomas Göttgens 2023-01-09 23:26:47 +01:00
parent 5cec370cf5
commit 1fc5d70221
3 changed files with 17 additions and 13 deletions

View File

@ -5,7 +5,7 @@
#include "configuration.h"
#include "main.h"
#include "mesh/http/WebServer.h"
#include "mesh/wifi/WiFiServerAPI.h"
#include "mesh/api/WiFiServerAPI.h"
#include "mqtt/MQTT.h"
#include "target_specific.h"
#include <ESPmDNS.h>
@ -55,12 +55,14 @@ static int32_t reconnectWiFi()
// Make sure we clear old connection credentials
WiFi.disconnect(false, true);
LOG_INFO("Reconnecting to WiFi access point %s\n",wifiName);
WiFi.mode(WIFI_MODE_STA);
delay(5000);
if (!WiFi.isConnected()) {
WiFi.begin(wifiName, wifiPsw);
}
}
#ifndef DISABLE_NTP
if (WiFi.isConnected() && (((millis() - lastrun_ntp) > 43200000) || (lastrun_ntp == 0))) { // every 12 hours
@ -167,7 +169,7 @@ bool initWifi()
WiFi.mode(WIFI_MODE_STA);
WiFi.setHostname(ourHost);
WiFi.onEvent(WiFiEvent);
WiFi.setAutoReconnect(false);
WiFi.setAutoReconnect(true);
WiFi.setSleep(false);
if (config.network.address_mode == Config_NetworkConfig_AddressMode_STATIC && config.network.ipv4_config.ip != 0) {
WiFi.config(config.network.ipv4_config.ip,
@ -182,7 +184,7 @@ bool initWifi()
WiFi.onEvent(
[](WiFiEvent_t event, WiFiEventInfo_t info) {
LOG_WARN("WiFi lost connection. Reason: %s", info.wifi_sta_disconnected.reason);
LOG_WARN("WiFi lost connection. Reason: %d\n", info.wifi_sta_disconnected.reason);
/*
If we are disconnected from the AP for some reason,

View File

@ -207,15 +207,18 @@ void MQTT::reconnect()
sendSubscriptions();
} else {
LOG_ERROR("Failed to contact MQTT server (%d/10)...\n",reconnectCount);
#if HAS_WIFI && !defined(ARCH_PORTDUINO)
if (reconnectCount > 9) {
LOG_ERROR("Failed to contact MQTT server (%d/5)...\n",reconnectCount + 1);
if (reconnectCount >= 4) {
needReconnect = true;
wifiReconnect->setIntervalFromNow(1000);
}
#endif
wifiReconnect->setIntervalFromNow(0);
reconnectCount = 0;
} else {
reconnectCount++;
}
#endif
}
}
}
@ -285,7 +288,6 @@ int32_t MQTT::runOnce()
String topic = cryptTopic + env->channel_id + "/" + owner.id;
LOG_INFO("publish %s, %u bytes from queue\n", topic.c_str(), numBytes);
pubSub.publish(topic.c_str(), bytes, numBytes, false);
if (moduleConfig.mqtt.json_enabled) {
@ -299,7 +301,7 @@ int32_t MQTT::runOnce()
}
mqttPool.release(env);
}
return 20;
return 200;
} else {
return 30000;
}

View File

@ -13,7 +13,7 @@
#include <EthernetClient.h>
#endif
#define MAX_MQTT_QUEUE 32
#define MAX_MQTT_QUEUE 16
/**
* Our wrapper/singleton for sending/receiving MQTT "udp" packets. This object isolates the MQTT protocol implementation from