wifi reconnection workaround on esp32
#173 , currently wifi autoreconnection on ESP32 doesn't work reliably. To bypass this issue I added a reconnection after a failure_number
This commit is contained in:
1 parent
b25eacdcbb
commit
ea987ca945
1 file changed
+5
-16
+5
-16
@@ -152,7 +152,6 @@ unsigned long timer_sys_measures = 0;
|
|||||||
//flag for saving data
|
//flag for saving data
|
||||||
bool shouldSaveConfig = true;
|
bool shouldSaveConfig = true;
|
||||||
//do we have been connected once to mqtt
|
//do we have been connected once to mqtt
|
||||||
boolean connected_once = false;
|
|
||||||
|
|
||||||
//callback notifying us of the need to save config
|
//callback notifying us of the need to save config
|
||||||
void saveConfigCallback () {
|
void saveConfigCallback () {
|
||||||
@@ -160,15 +159,6 @@ void saveConfigCallback () {
|
|||||||
shouldSaveConfig = true;
|
shouldSaveConfig = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ESP32)
|
|
||||||
void WiFiEvent(WiFiEvent_t event){
|
|
||||||
if(event == SYSTEM_EVENT_STA_DISCONNECTED){
|
|
||||||
trc("Event: SYSTEM_EVENT_STA_DISCONNECTED, reconnecting");
|
|
||||||
WiFi.reconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
boolean reconnect() {
|
boolean reconnect() {
|
||||||
|
|
||||||
int failure_number = 0;
|
int failure_number = 0;
|
||||||
@@ -177,7 +167,7 @@ boolean reconnect() {
|
|||||||
trc(F("MQTT connection...")); //F function enable to decrease sram usage
|
trc(F("MQTT connection...")); //F function enable to decrease sram usage
|
||||||
if (client.connect(Gateway_Name, mqtt_user, mqtt_pass, will_Topic, will_QoS, will_Retain, will_Message)) {
|
if (client.connect(Gateway_Name, mqtt_user, mqtt_pass, will_Topic, will_QoS, will_Retain, will_Message)) {
|
||||||
trc(F("Connected to broker"));
|
trc(F("Connected to broker"));
|
||||||
connected_once = true;
|
failure_number = 0;
|
||||||
// Once connected, publish an announcement...
|
// Once connected, publish an announcement...
|
||||||
client.publish(will_Topic,Gateway_AnnouncementMsg,will_Retain);
|
client.publish(will_Topic,Gateway_AnnouncementMsg,will_Retain);
|
||||||
// publish version
|
// publish version
|
||||||
@@ -193,7 +183,7 @@ boolean reconnect() {
|
|||||||
trc(F("Subscription OK to the subjects"));
|
trc(F("Subscription OK to the subjects"));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!connected_once) failure_number ++; // if we never connected to broker we count the failure
|
failure_number ++; // we count the failure
|
||||||
trc(F("failed, rc="));
|
trc(F("failed, rc="));
|
||||||
trc(String(client.state()));
|
trc(String(client.state()));
|
||||||
trc(F("try again in 5s"));
|
trc(F("try again in 5s"));
|
||||||
@@ -366,6 +356,7 @@ void setup()
|
|||||||
#if defined(ESP32) || defined(ESPWifiManualSetup)
|
#if defined(ESP32) || defined(ESPWifiManualSetup)
|
||||||
void setup_wifi() {
|
void setup_wifi() {
|
||||||
delay(10);
|
delay(10);
|
||||||
|
int failureAttempt = 0; //DIRTY FIX ESP32 waiting for https://github.com/espressif/arduino-esp32/issues/653
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
// We start by connecting to a WiFi network
|
// We start by connecting to a WiFi network
|
||||||
trc(F("Connecting to "));
|
trc(F("Connecting to "));
|
||||||
@@ -380,6 +371,8 @@ void setup_wifi() {
|
|||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500);
|
||||||
trc(F("."));
|
trc(F("."));
|
||||||
|
failureAttempt++; //DIRTY FIX ESP32
|
||||||
|
if (failureAttempt > 30) setup_wifi(); //DIRTY FIX ESP32
|
||||||
}
|
}
|
||||||
|
|
||||||
trc(F("WiFi ok with manual config credentials"));
|
trc(F("WiFi ok with manual config credentials"));
|
||||||
@@ -549,10 +542,6 @@ void loop()
|
|||||||
//MQTT client connexion management
|
//MQTT client connexion management
|
||||||
if (!client.connected()) { // not connected
|
if (!client.connected()) { // not connected
|
||||||
|
|
||||||
#if defined(ESP32) // if wifi disconnected on ESP32 we reconnect
|
|
||||||
if(WiFi.getAutoReconnect()) WiFi.onEvent(WiFiEvent);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//RED ON
|
//RED ON
|
||||||
digitalWrite(led_error, LOW);
|
digitalWrite(led_error, LOW);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user