Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
khoih-prog opened this issue Jan 8, 2022 · 1 comment

Comments

@khoih-prog
Copy link

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

  • Arduino IDE v1.8.19,
  • mbed_portenta core v2.6.1
  • Portenta_H7_M7 with Murata WiFi
#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
Copy link
Author

Tested OK with new core v3.3.0.

Thanks and Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant