arduino / ArduinoCore-mbed

330 stars 195 forks source link

[Portenta_H7] WiFi.begin() can't reconnect after WiFi lost, then restored #382

Closed khoih-prog closed 2 years ago

khoih-prog commented 2 years ago

Describe the bug

For other board / core, whenever WiFi is lost, the WiFi.begin() function will reconnect to the restored WiFi AP, without the need of WiFi.end()

For Portenta_H7, using mbed_portenta core v2.6.1, we must call WiFi.end() before WiFi.begin() after WiFi lost, to be able to reconnect to the restored WiFi AP

MRE

Using the following sketch

#ifdef CORE_CM7    // Start M7 Core programming  

#include <WiFi.h>

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "YOUR_SSID";            // your network SSID (name)
char pass[] = "YOUR_PASSWORD";        // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

void printWifiStatus()
{
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void WiFiConnect()
{
  status = WL_IDLE_STATUS;

  // Must have to reconnect after WiFi lost
  WiFi.end();

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED)
  {  
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 3 seconds for connection:
    delay(3000);
  }
}

void setup()
{
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial);

  delay(500);

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_SHIELD)
  {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  WiFiConnect();
}

void loop()
{
  //if (WiFi.status() == WL_CONNECTED)
  if ( (WiFi.status() == WL_CONNECTED) && (WiFi.RSSI() != 0) )      // temporary workaround
  {
    Serial.println("Connected to wifi");
    printWifiStatus();
  }
  else
  {
    Serial.println("Not connected to wifi. Reconnecting");

    WiFiConnect();
  }

  delay(5000);
}

#endif

Terminal output after Power OFF then ON the WiFi AP

Without WiFi.end() ===> Can't Reconnect

Attempting to connect to SSID: YOUR_SSID
Connected to wifi
SSID: YOUR_SSID
IP Address: 192.168.2.104
Signal strength (RSSI):-27 dBm
Connected to wifi
SSID: YOUR_SSID
...
IP Address: 192.168.2.104
Signal strength (RSSI):-38 dBm
Connected to wifi
SSID: YOUR_SSID
IP Address: 192.168.2.104
Signal strength (RSSI):-38 dBm
Not connected to wifi. Reconnecting       <====  Power OFF / ON WiFi Router: Can't reconnect
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID

With WiFi.end() ===> Reconnect OK


Attempting to connect to SSID: YOUR_SSID
Connected to wifi
SSID: YOUR_SSID
IP Address: 192.168.2.104
Signal strength (RSSI):-26 dBm
...
Connected to wifi
SSID: YOUR_SSID
IP Address: 192.168.2.104
Signal strength (RSSI):-34 dBm
Not connected to wifi. Reconnecting       <====  Power OFF / ON WiFi Router: Will reconnect
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Attempting to connect to SSID: YOUR_SSID
Connected to wifi                         <====  WiFi Router reconnected with WiFi.end()
SSID: YOUR_SSID
IP Address: 192.168.2.104
Signal strength (RSSI):-25 dBm
Connected to wifi
SSID: YOUR_SSID
IP Address: 192.168.2.104
Signal strength (RSSI):-25 dBm
Connected to wifi
SSID: YOUR_SSID
IP Address: 192.168.2.104
Signal strength (RSSI):-26 dBm
khoih-prog commented 2 years ago

Tested OK with new core v3.3.0.

Thanks and Regards,