For my project I am using an ESP32 to connect to a WLAN network for communicating using MQTT.
The program offers several features. It worked fine, except when I included some program code for buzzering. To make the noise more comfortable I used two timers to let it sound for several intervals. But when the timer is included, the WiFi network does not reconnect anymore. I googled for a day and I have not found neither a similar problem nor an answer for that.
I am using following libraries:
Code: Select all
/* Libraries */
#include <WiFi.h>
#include <MQTTClient.h>
#include <ArduinoOTA.h>
#include "Arduino.h"
#include <SimpleTimer.h>
I used for connecting to WiFi the example code provided. For reconnecting I adapt to following code which is called in the loop().
Code: Select all
void loopWiFi()
{
if(WiFi.status() != WL_CONNECTED)
{
WiFi.disconnect(true);
delay(2000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int numOfTries = 0;
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
numOfTries++;
if(numOfTries>10)
{
#if SERIAL_ENABLE
Serial.println("WiFi not connected after 10 attempts!");
#endif
break;
}
}
}
return;
}
Thanks
AndonProject