0

I am writing a program for a XIAO ESP32S3 using ArduinoIDE.

The function of the program is to have the ESP open an Access Point (using AutoConnect) when it first starts up. Once the user inputs their network and password and some other information the Wi-Fi credentials are stored and the other information is saved to EEPROM. Once this is done the ESP connects to the Wi-Fi and gathers information from various sensors. This data is then sent to the MQTT site Adafruit.io.

When the data has been sent the ESP turns the Wi-Fi off and goes into light sleep mode.

Upon waking from sleep the Wi-Fi is turned on and the ESP reconnects to the Wi-Fi and starts over again with gathering data.

This has been working fairly well but sometimes it doesn't reconnect. I am trying the following code (condensed to just the Wi-Fi connection parts).

I create wifimanager.

WiFiManager wifiManager;

In

    void setup() {
//establish run time for Autoconnect to try to connect to wifi.

wifiManager.setConnectTimeout(120);

//and how long to keep Access Point open for input.

wifiManager.setConfigPortalTimeout(120);

//Is this ok being in setup or should it be in the global variables section?

wifiManager.autoConnect("AutoConnect");

//On the first launch I have the wifiManager use Autoconnect to open an Access
Point to gather the network name and password and other credentials.  If correct the
network name and password are saved and the ESP tries to connect to the network.

//On subsequent launches Autoconnect reads the saved information and connects to
the network.  If the network is unavailable or network name and/or password are     changed
the ESP will try to connect for (wifiManager.setConnectTimeout(120)) the opens an
Access Point for new info (wifiManager.setConfigPortalTimeout(120)).

if (WiFi.status() == WL_CONNECTED) { 
//If connected other credentials are saved to EEPROM.
}

if (WiFi.status() == WL_DISCONNECTED) {

//If for some reason (wifi down or weak signal) the ESP can't connect I restart the
ESP. If the network is down it may be back up and the ESP can connect. 

   ESP.restart();
} 

}

In

    void loop() 
    {
//after the ESP wakes from lite sleep and the WIFI is turned ON wifiManager uses
Autoconnect to connect to the wifi using the stored data.

 wifiManager.autoConnect("AutoConnect");

if (WiFi.status() == WL_CONNECTED) {  Serial.println("Connected2");

//If the wifi connects then I gather data from sensors, use the stored credentials
on EEPROM to access Adafruit.io and upload and receive data.
//if the wifi is down or inaccessible the ESP will try for 2 minutes to connect the
open the Access Point for 2 minutes.

}

 if (WiFi.status() == WL_DISCONNECTED) {

//However if the ESP does not connect I restart it to try to get back on the network.


   ESP.restart();

} 

}

here is the code in one chunk.

    #include <WiFiManager.h>
 WiFiManager wifiManager;

void setup() {

   wifiManager.setConnectTimeout(120);

    wifiManager.setConfigPortalTimeout(120);

    wifiManager.autoConnect(&quot;AutoConnect&quot;);

    if (WiFi.status() == WL_CONNECTED) { 
    Serial.println(&quot;Connected&quot;);
    }

    if (WiFi.status() == WL_DISCONNECTED) {

     ESP.restart();
    } 

}

     void loop() 
    {



     wifiManager.autoConnect(&quot;AutoConnect&quot;);

    if (WiFi.status() == WL_CONNECTED) {  

    Serial.println(&quot;Connected&quot;);
    }

     if (WiFi.status() == WL_DISCONNECTED) {

    ESP.restart();

    } 

}

To make this work you do not have to have the additional parameters saved to EEPROM.

My basic question is this the best way to insure my XIAO ESP32S3 with reliably connect to the Network and send the information to Adafruit.io even after a network interruption such as a power outage or service provider outage?

I hope I have been clear and concise with my question and the information provided. If you need more let me know.

hcheung
  • 1,933
  • 8
  • 15

0 Answers0