Arduino Uno Wifi Rev 2 - WifiNina - restart after network connection loss

After a day of searching I have not found code for restarting the wifi connection on the Uno Wifi Rev 2 with WifiNina when it is lost. I am sure those who breathe this stuff can greatly improve it but here it is for those just wanting to have a quick fix:

#include <WiFiNINA.h>
#include <Time.h>
#include <TimeLib.h>

char ssid[] = "xxx"; //your values
char pass[] = "xxx"; //your values
int status = WL_IDLE_STATUS;

//WiFi.status()
//WL_CONNECTED: assigned when connected to a WiFi network;
//WL_AP_CONNECTED : assigned when a device is connected in Access Point mode;
//WL_AP_LISTENING : assigned when the listening for connections in Access Point mode;
//WL_NO_SHIELD: assigned when no WiFi shield is present;
//WL_NO_MODULE: assigned when the communication with an integrated WiFi module fails;
//WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED);
//WL_NO_SSID_AVAIL: assigned when no SSID are available;
//WL_SCAN_COMPLETED: assigned when the scan networks is completed;
//WL_CONNECT_FAILED: assigned when the connection fails for all the attempts;
//WL_CONNECTION_LOST: assigned when the connection is lost;
//WL_DISCONNECTED: assigned when disconnected from a network;

//WiFi.begin() returns:
// WL_CONNECTED when connected to a network
// WL_IDLE_STATUS when not connected to a network, but powered on

void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

while ( status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(5000);
}

String timd = getTimeandFormat();
Serial.println(timd);
}

void loop () {

if (WiFi.status() != WL_CONNECTED) {
Serial.println(WiFi.status());
Serial.println("Conn lost");
WiFi.end();
WiFi.disconnect();
status = WiFi.begin(ssid, pass);
Serial.println("Tried restart");
delay(5000);
}
else {
Serial.println("OK");
Serial.println(WiFi.status());
}
delay(5000);
}

String getTimeandFormat() {
//Time elements structure for current date/time from internet
tmElements_t tm;
//Get time from internet and format for use in insert statement
long epochtime = WiFi.getTime();
epochtime = epochtime + 36000;
breakTime(epochtime, tm);
String yr = String(tmYearToCalendar(tm.Year));
String mo = PadToTwo(tm.Month);
String da = PadToTwo(tm.Day);
String ho = PadToTwo(tm.Hour);
String mi = PadToTwo(tm.Minute);
String se = PadToTwo(tm.Second);
String dat = yr + "-" + mo + "-" + da + " " + ho + ":" + mi + ":" + se;
return(dat);
}

String PadToTwo(int IntToPad){
String result;
if(IntToPad < 10){
result = "0" + String(IntToPad);
}
else
{
result = String(IntToPad);
}
return result;
}

Hi,

I had the same problem. The examples ignore disconnections...

Try this (you must put the WiFi.begin in a while statement, because the connection is very slow - sometimes 1 or 2 minutes...):

void loop ()
{
int status;
status=WiFi.status();
if (status==WL_DISCONNECTED || status==WL_CONNECTION_LOST)
{
while ( status != WL_CONNECTED)
{
status = WiFi.begin(ssid, pass);
delay(10000);
}
}

no need to WiFi.end and WiFi.disconnect.
:wink:

This is better:

#include <SPI.h>
#include <WiFiNINA.h>
char ssid[] = "xxx";         // my network SSID (name)
char pass[] = "yyy";       // my network key
int status = WL_IDLE_STATUS;                     // the Wifi radio's status
unsigned long time1;
unsigned long time2=0;
char dbg=1;
void setup()
{
 //Initialize serial and wait for port to open:
 if(dbg) Serial.begin(9600);
 pinMode(9, OUTPUT);   // set the LED pin mode
 digitalWrite(9, LOW); //LED OFF to show disconnected
 if(dbg) while(!Serial); // wait for serial port to connect. Needed for Leonardo only
 while(!ScanSSIDs()) WiFiConnect();
}

void loop()
{
 time1 = millis();
 if((time1-time2)>3000) //every 3s
 {
   time2=time1;    
   TestWiFiConnection(); //test connection, and reconnect if necessary
   long rssi=WiFi.RSSI();
   if(dbg) Serial.print("RSSI:");
   if(dbg) Serial.println(rssi);
 }
}

void TestWiFiConnection()
//test if always connected
{
 int StatusWiFi=WiFi.status();
 if(StatusWiFi==WL_CONNECTION_LOST || StatusWiFi==WL_DISCONNECTED || StatusWiFi==WL_SCAN_COMPLETED) //if no connection
 {
  digitalWrite(9, LOW); //LED OFF to show disconnected
  if(ScanSSIDs()) WiFiConnect(); //if my SSID is present, connect
 }
} 

void WiFiConnect()
//connect to my SSID
{
 status= WL_IDLE_STATUS;
 while(status!=WL_CONNECTED)
 {
   status = WiFi.begin(ssid,pass);
   if(status==WL_CONNECTED) digitalWrite(9, HIGH); //LED ON to show connected
   else delay(500);
  }
}

char ScanSSIDs()
//scan SSIDs, and if my SSID is present return 1
{
 char score=0;
 int numSsid = WiFi.scanNetworks();
 if(numSsid==-1) return(0); //error
 for(int thisNet=0;thisNet<numSsid;thisNet++) if(strcmp(WiFi.SSID(thisNet),ssid)==0) score=1; //if one is = to my SSID
 return(score);
}

And if you want a board with fast WiFi connection (about 1 sec to connect, for less than $10, 1MB Flash, 80KB RAM, IDE Arduino compatible), try NodeMCU V3. :slight_smile:

Hey Guys, so i have a Arduino Uno Wifi Rev 2 which i have conncetd to my WiFi with the example WiFi Connection Sketch given in the IDE, my question, does the connection always happen when i Disonnect and Connect the Arduino, and what happanes when i Upload new Code that uses the Functionality of the WiFi, will the Arduino still have the WiFi Connection?

This works very well for me - thanks:

lillics:
This is better:

#include <SPI.h>

#include <WiFiNINA.h>
char ssid[] = "xxx";         // my network SSID (name)
char pass[] = "yyy";       // my network key
int status = WL_IDLE_STATUS;                     // the Wifi radio's status
unsigned long time1;
unsigned long time2=0;
char dbg=1;
void setup()
{
//Initialize serial and wait for port to open:
if(dbg) Serial.begin(9600);
pinMode(9, OUTPUT);   // set the LED pin mode
digitalWrite(9, LOW); //LED OFF to show disconnected
if(dbg) while(!Serial); // wait for serial port to connect. Needed for Leonardo only
while(!ScanSSIDs()) WiFiConnect();
}

void loop()
{
time1 = millis();
if((time1-time2)>3000) //every 3s
{
  time2=time1;    
  TestWiFiConnection(); //test connection, and reconnect if necessary
  long rssi=WiFi.RSSI();
  if(dbg) Serial.print("RSSI:");
  if(dbg) Serial.println(rssi);
}
}

void TestWiFiConnection()
//test if always connected
{
int StatusWiFi=WiFi.status();
if(StatusWiFi==WL_CONNECTION_LOST || StatusWiFi==WL_DISCONNECTED || StatusWiFi==WL_SCAN_COMPLETED) //if no connection
{
 digitalWrite(9, LOW); //LED OFF to show disconnected
 if(ScanSSIDs()) WiFiConnect(); //if my SSID is present, connect
}
}

void WiFiConnect()
//connect to my SSID
{
status= WL_IDLE_STATUS;
while(status!=WL_CONNECTED)
{
  status = WiFi.begin(ssid,pass);
  if(status==WL_CONNECTED) digitalWrite(9, HIGH); //LED ON to show connected
  else delay(500);
 }
}

char ScanSSIDs()
//scan SSIDs, and if my SSID is present return 1
{
char score=0;
int numSsid = WiFi.scanNetworks();
if(numSsid==-1) return(0); //error
for(int thisNet=0;thisNet<numSsid;thisNet++) if(strcmp(WiFi.SSID(thisNet),ssid)==0) score=1; //if one is = to my SSID
return(score);
}