How to reconnect the MKR to WIFi

Yes, this I know. I have different names: mkr01, mkr02, esp03 and esp04.

Hello guys,

there is a "problem" in the documentation.

if you try to reconnect with this code :

while ( status != WL_CONNECTED) {
   Serial.print("Attempting to connect to WEP network, SSID: ");
   Serial.println(ssid);
   status = WiFi.begin(ssid, keyIndex, key);

   // wait 10 seconds for connection:
   delay(10000);
 }

it will never reconnect.

  • While status is different of WL_CONNECTED, it try to connect. The status connection is stocked in "status" variable.
  • When connected, this status will be WL_CONNECTED forever, because you will never enter in this loop again to change the variable value

The solution may be to add this line after the loop to update the wifi status.

while ( WifiStatus != WL_CONNECTED ) {

   Serial.print("Connexion au SSID : ");
   Serial.println(ssid);

   WiFi.begin(ssid, pass);
   delay(10000);
 }
 WifiStatus = WiFi.status();

If you don't want to check the WiFi.status(); every time arduino run the loop, you can check every X second.

Globaly =

int WifiStatus = WL_IDLE_STATUS;
int lastNetworkCheck = millis(); // Now
int networkCheckInterval = 1 * 60 * 1000; // 1 minute

And in your connect function :

 while ( WifiStatus != WL_CONNECTED ) {

   Serial.print("Connexion au SSID : ");
   Serial.println(ssid);

   WifiStatus = WiFi.begin(ssid, pass);
   delay(10000);
   lastNetworkCheck = millis(); // Reset the last check time at Now
 }
 if (lastNetworkCheck + networkCheckInterval < millis()) {
   WifiStatus = WiFi.status();
   lastNetworkCheck = millis();

   if (WifiStatus == WL_CONNECTED) {
     Serial.println("Checking WiFi Network... The connection status is OK !");
   }
   else {
     Serial.println("Checking WiFi Network... The connection status is BAD... :(");
   }
 }

We will check every 1 minutes the connection status and then, print the result to the Serial.
If the status changed, the code enter in the while loop and try to reconnect.

This connect funciton is trigger in setup() and loop() function.

09:23:08.501 -> Valeur : 0.00
09:23:08.501 -> Intensity : 0 > 0
09:23:18.615 -> Checking WiFi Network... The connection status is OK ! <<<<<<<<<<<<<<
09:23:24.128 -> 1191.71 Lux
09:23:24.128 -> Prise de luminosité suite au PIR
09:23:24.128 -> Valeur : 0.00
09:23:24.128 -> Intensity : 0 > 0
09:23:41.073 -> 1165.22 Lux
09:23:41.073 -> Prise de luminosité suite au PIR
09:23:41.073 -> Valeur : 0.00
09:23:41.073 -> Intensity : 0 > 0
09:23:53.011 -> GAZ BURN
09:23:53.389 -> GAZ BURN
09:23:53.763 -> GAZ BURN
09:23:54.138 -> GAZ BURN
09:23:54.513 -> GAZ BURN
09:23:54.854 -> GAZ BURN
09:23:55.232 -> GAZ BURN
09:23:55.609 -> GAZ BURN
09:23:55.983 -> GAZ BURN
09:23:56.358 -> 1218.81 Lux
09:23:56.358 -> Prise de luminosité suite au PIR
09:23:56.358 -> Valeur : 0.00
09:23:56.358 -> Intensity : 0 > 0
09:23:56.358 -> GAZ BURN
09:23:56.736 -> GAZ BURN
09:23:57.110 -> GAZ BURN
09:23:57.486 -> GAZ BURN
09:23:57.864 -> GAZ BURN
09:23:58.205 -> GAZ BURN
09:23:58.583 -> GAZ BURN
09:23:58.959 -> GAZ BURN
09:23:59.339 -> GAZ BURN
09:23:59.714 -> GAZ BURN
09:24:00.088 -> GAZ BURN
09:24:00.461 -> GAZ BURN
09:24:00.836 -> GAZ BURN
09:24:01.214 -> GAZ BURN
09:24:01.555 -> GAZ BURN
09:24:01.934 -> GAZ BURN
09:24:02.312 -> GAZ BURN
09:24:02.688 -> GAZ BURN
09:24:03.065 -> Read module...
09:24:03.065 ->  ##################  Free RAM = 
09:24:03.065 -> 24663
09:24:03.065 -> 1191.71 Lux
09:24:03.443 -> Temperature = 22.59 *C
09:24:03.443 -> Pressure = 985.45 hPa
09:24:03.443 -> Humidity = 42.43 %
09:24:03.443 -> Gas = 309077.00 KOhms
09:24:03.443 -> Gas Baseline :250000.00
09:24:03.443 -> Temperature = 22.59 *C
09:24:03.443 -> Humidity = 42.43 %
09:24:03.443 -> Pressure = 985.45 hPa
09:24:03.443 -> Gas = 309077.00 KOhms
09:24:03.443 -> Gas Baseline :250000.00
09:24:03.443 -> IAQ = 98.99
09:24:03.443 -> Reply : date:201904192026;pir:1804874lum:1191.71tmp:22.59hm:42.43pr:985.45gas:309077.00iaq:98.99
09:24:03.443 ->  ##################  Free RAM = 
09:24:03.443 -> 24639
09:24:03.443 ->  ##################  Free RAM = 
09:24:03.443 -> 24663
09:24:11.352 -> 1218.81 Lux
09:24:11.352 -> Prise de luminosité suite au PIR
09:24:11.352 -> Valeur : 0.00
09:24:11.352 -> Intensity : 0 > 0

<<<<<< Here i cut the Wifi

09:24:18.604 -> Checking WiFi Network... The connection status is BAD... :( <<<<<<<<<<<<
09:24:18.604 -> Connexion au SSID : Synology <<<<<<<<<<<<
09:24:31.129 -> Connexion au SSID : Synology <<<<<<<<<<<<
09:24:43.641 -> Connexion au SSID : Synology <<<<<<<<<<<<

<<<<<< Here i switch on the wifi

09:24:56.188 -> Connexion au SSID : Synology <<<<<<<<<<<<
09:25:16.193 -> 1246.53 Lux
09:25:16.193 -> Prise de luminosité suite au PIR
09:25:16.193 -> Valeur : 0.00
09:25:16.193 -> Intensity : 0 > 0
09:25:31.207 -> 1218.81 Lux
09:25:31.207 -> Prise de luminosité suite au PIR
09:25:31.207 -> Valeur : 0.00
09:25:31.207 -> Intensity : 0 > 0

Hello,
I have been experiencing the MRK1000 losing its connection. Varies between every 2 to 4 days.
I have read many posts about this issue and upgrading the firmware.
I came across this post and clambert68 has said makes the most sense to me. After reviewing the wifi101 example code I have been using it only connects during setup. There is nothing in the main loop to reconnect?

I intend to try what clambert68 has suggested. I am very curious now to understand how this code could ever have expected to have stayed connected without something in the main loop to reconnect?
Is it assumed that the wifi will never drop out?

Im experienced same problems with re-connecting the MKR1000.
The while loop (WifiStatus != WL_CONNECTED), seems not always to work, as WL_CONNECTED stays active (3) even when the wifi connection is gone.
I have 2 suggestions:

  1. : Add a Rssi (signal strength) in your check. Rssi is normaly -30 to -80, if disconnected it returns 0 or -100 out of experience...
  2. : Add a timer to your while loop to prevent for-ever-while behavior (general recommendations to avoid while loops)
       byte t=0;
       while ( status != WL_CONNECTED) || (WiFi.RSSI() <= -90) || (WiFi.RSSI() ==0) ) {  
       // attempt to connect to WiFi network:
       status = WiFi.begin(ssid, pass);     // Connect to network.
       delay(4000);                                // wait 4 seconds for connection:
       if( t++ < 5) break;                        // try-counter 5 times max
  }

The problem I still face is that my WebServer (defined as WiFiServer server(80):wink: is not activated after a reconnect, using server.begin(); does not help. Is there a statement to re-activate your server(80) ?

Fyi: When running this code on a Wifi1010 (NiNa board), the reconnect always works incl server.

I am still having the reconnect issue as well. I was waiting to post, after having made changes to include attempts to reconnect from within the main loop. I am using the technique Clalmbert68 mentioned. This time it ran 8 days before dropping out. But did not re-establish itself from within the main loop.

I also had the concern that Voske65 did - not knowing of the Wifiserver(80) would need to be re-established.

Anyone here with good Ethernet protocol knowledge that can help with what the solution to this issue may be?

@ela55 I'm sorry that my code doesn't resolve your issues.

In my case, the arduino mkr1010 is up for 1 month without any problem.

What about your logs ? Let the arduino connected to you computer and try to look at your log.
Print all information about wifi every 10min for exemple, and print the connection status, and reconnect tries.

The problem is the wifi connection only ? Or there is a webserver or something like this ?

Hello Clambert68,
Thank you for your input. By logs to you mean serial output?
The issue has been that my project is not located near a computer so I have been unable to access serial debugging capability.
I have not had time to address this issue since a reboot will reconnect and so I have been rebooting about once every 8 days or so.

I understand that I will need to get more serious in my debugging of the issue. When I can I will move the project to a location that will allow me to connect to the computer and print out serial debugging information. I will then experiment with interfering with the wifi signal in order to force a reconnect.

I guess I should try updating the firmware also.

I am still fighting this issue. Finally got around to putting in serial debug code and monitoring that way.
I have two MKR1000 units exhibiting this issue. One had been firmware upgraded and the other was not. Upgraded both to 19.5.2. Both still exhibited issue.
I had already added a reconnect attempt in the main loop.
When a dropout occurred the status went to 6 and the unit did indeed reconnect. Status was now 3.
I could look in the router and MKR1000s were both online. Yet I could not connect via the web server on either. Could ping them both.
Was also monitoring signal strength and it never went to 0 or <-90.

So it looks like having the main loop check is a must so that it does at least reconnect but the web sever still did not work. I added the server.begin(); after the main loop check to reconnect and will continue to monitor.
I will see if I have any better luck than you Voske65.

After adding the server.begin() to the main loop recovery routine I purposely rebooted the router to test and the reconnect routine worked fine.

I have been running now for 13 days on two different MKR1000's with no disconnects.

While it would seem things are now fixed I figured I would jinx it by posting. :slight_smile:

I was monitoring via serial for 10 days and never experienced any reconnect attempts. I have debug variables that would tell me if a reconnect had happened and none have occurred. These variables show in both the serial and wifi debug printouts.
Until I see that variable change I am still unsure all is fine. I do now feel that the code will be robust in reconnecting - as long as the wifi status changes away from 3 at some point.
I am also monitoring and reporting an rssi debug variable. RSSI never dipped below -81 on either device thus far.

In addition to retries (in the 10 second loop) I also added a 30 minute reset to the retries counter. This was so that if the router is down for a while, that the retries will get reset every 1/2 hr.

Hopin for the best now.

I did indeed jinx it. A day later one of the 2 stopped working. Debug showed status was still at 3 and rssi was 0.
I was only monitoring rssi and not acting on it as part of the reconnect routine. And so no reconnect of course. Did add that now.

If Voske65 is still checking in... have you had some luck? I now fear the that server.begin() may not work under this unusual condition of rssi = 0, whatever it is. As you had encountered.

Next time it happens it sure would be great to have an additional thing to try or something else to monitor.

Hello everyone,

Although this topic is older and about MKR1000; I encountered the same problem with MKR1010. It keeps disconnecting after an hour and won't reconnect until I press the reset button on board.

Has anyone found any solution for this problem?

Okey. It very Buggy! What has worked for me

  1. start arduino ide. (it has to be the offline version with connect mrk it should download some stuff
  2. make the update of the mrk board
    reboot
  3. 5ghz is not working make sure you have it disabled. I have connected the pc and the arduino to a android tablet, very short range.
  4. turn the baterry on the motorshield then connect over wifi. I had to try it severeal times.
    In my oppinion its very unstable.

Good Luck

@thetha

The MKR1000 never had 5 Ghz. 2.4 is the limit.
Most routers now have all the way up to 5 Ghz with secondary settings for both 2.4 and 5.
Simple connect it to the named 2.4.

Mine is still running on THINGSPEAK after over a year but only with updated firmware.
Twice a year I bring it in and check the firmware and update if needed.
Also give me chance to tweak any settings at the same time.

Bob.

Having compared (2) MKR1000's debug data for more than a month now.
both experienced disconnects without an automatic ( firmware) reconnect
both were updated to 19.5.2 firmware early on

Tried all the application based fixes talked about here with no luck. One ran recently for 31days before it failed to reconnect - the other would only go 5 -8 days between.

I read the 1.8.9 Arduino update notes today and saw that it was modified (back in 3/2019) to allow the user to upgrade the firmware to 19.6.1. Wish I would have checked that earlier.

Upgraded on both units and now hoping that will improve things?

Anyone running 19.6.1 firmware and still having issues?

Got a fix: call neatly WiFi.begin() in the loop().

I had to fix this reconnection issue for our ArduinoProps (MQTT) library which support Arduino MKR WiFi 1010 with the WiFiNINA library.

Googling I came to this thread which was very inspiring to find the solution.

After you call WiFi.begin(), if the connection failed you have to clean up correctly the driver before making a new WiFi.begin() call. Fortunately WiFiNINA offers WiFi.end() which when I looked at source code cleans wifi_drv and spi_drv.

Fix code extracted from BlinkOnWifiProps.ino:

const char* ssid = "";
const char *passphrase = "";

bool wifiBegun(false);

void setup()
{
  // don't call WiFi from setup
}

void loop()
{
  if (!wifiBegun) {
    WiFi.begin(ssid, passphrase);
    delay(250); // acceptable freeze for this props (otherwise use PropsAction for async-like behavior)
    if (WiFi.status() == WL_CONNECTED) {
      wifiBegun = true;
    } else {
      WiFi.end();
    }
  } else if (wifiBegun && WiFi.status() != WL_CONNECTED) {
    WiFi.end();
    wifiBegun = false;
  }

  // etc...
}

I tested with Arduino MKR WiFi 1010 and Arduino Uno WiFi R2 and it works like a charm.

I powered off my Internet box (Orange Livebox) and powered up again, when the box shows its WiFi ready, the Arduinoi WiFi reconnects to MQTT broker in a few seconds.

Full BlinkOnWifiProps.ino example is availbale from our ArduidoProps library at GitHub: GitHub - xcape-io/ArduinoProps: Library to make MQTT props sketches for Escape Room 2.0 with Arduino Yun, Ethernet, WiFi and STM32 Nucleo-144 boards.

Thanks for getting me to the solution btw!

Hi all,
I had some similar issues recently.
My router get stuck sometimes and even when I reboot it, my MKR WiFi1010 doesn't reconnect.
I'm using Blynk and the solution from @marie123 works great here too.
Some parts of my standard code below, that might be useful for you guys too.
Tested with latest NINA FW.
Cheers.

.... 
#include <BlynkSimpleWiFiNINA.h> 
// You should use this instead of #include <WiFiNINA.h>
// search on git for the latest version.

bool wifiBegun(false);

void setup() {
... your setup stuff goes here, don't call WiFi in setup

timer.setInterval(10000L, myTimerEvent);  
}

void myTimerEvent()
{
.... your main loop goes here 
}

void loop() {
  WDT_WiFi(); // Normally you should not include stuff in the loop here when using Blynk but in this case is needed.
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

void WDT_WiFi() {
   if (!wifiBegun) {
    Blynk.begin(auth, ssid, pass);
    delay(10000); // acceptable freeze
    if (WiFi.status() == WL_CONNECTED) {
      wifiBegun = true;
    } else {
      WiFi.end();
    }
  } else if (wifiBegun && WiFi.status() != WL_CONNECTED) {
    WiFi.end();
    wifiBegun = false;
  }

}