Go Down

Topic: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted (Read 672 times) previous topic - next topic

dachurchill

I'm currently trying to implement a project using the Arduino Uno WiFi Rev2. I'm testing using one of the examples - WiFiWebClientRepeating and all works fine when I have a valid WiFi connection but when I try to test various error conditions the Uno hangs and can only be recovered via hard reset.
I've tracked it down to the command "client.connect(server, 80). Documentation states that this returns a flag indicating success or failure, however when the WiFi network is not available the application gets stuck on this command and I do not see anything returned.
My use-case is unattended without the opportunity to manually reset the device and because of the hang a programatic reset will not work. Given that in any typical implementation one should plan for network outages I cannot see that this behavior is correct.
Is this a problem with the library or am I doing something wrong?
Thanks for any guidance.

Coding Badly


@dachurchill, please do not cross-post.  Other thread removed.


dachurchill

sorry, new to this forum and initially missed that there was a section dedicated to the WiFi Rev2. It won't happen again :-)

Coding Badly


kenharpster

#4
Apr 07, 2019, 02:19 am Last Edit: Apr 07, 2019, 02:40 am by kenharpster
I tested the example and after it was running I blacklisted the device on my router, thereby disconnecting it. Once I removed the blacklist entry the Arduino did not re-establish a connection to the router. This is exactly what should happen based on the example code. Please notice that the connection to the router is currently done in the setup method, this method only runs on boot (or reset). When the network connection is broken you will continually receive the "connection failed" message on the serial monitor because the program cannot reestablish it based on how it is written. You would want to call the code that creates the connection ( I would pull the code out of the setup method, put it in it's own method (see below) and call the new method from setup (in place of the existing code) as well as when you receive the "connection failed" error on line 108 of the example.

void createConnection() {

// This new line is required to disconnect the previous connection otherwise
// it will try to use the old connection and never reconnect if the connection fails
status=WiFi.disconnect();                                 

// 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 10 seconds for connection:
   delay(10000);
 }
 // you're connected now, so print out the status:
 printWifiStatus();

}

dachurchill

Thanks for testing this.
I understand what you are saying however what I am reporting is that the UNO WiFi Rev2 hangs and requires a hard reset at the command "client.connect(server,80)" This command is supposed to return a flag but in my case does not return anything, the command never returns control to the application.
I am using latest firmware (1.2.1) and library (1.4.0) as far as I can tell.
I tested this by running the example and then disconnecting power to my router. As soon as I do this the Uno just hangs and is completely unresponsive. There are no further updates to the serial monitor.
This is the issue I am trying to address.
Thanks

kenharpster

This is the original code that I am running from the example and I cannot replicate what you are saying is happening. If the client.connect(server, 80) fails, it drops through to the else and sends a message to the serial monitor. Which is where I suggest calling the new createConnection method.

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the Nina module
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET / HTTP/1.1");
    client.println("Host: example.org");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

dachurchill

Thanks again.
In your test you state that you blacklisted the device. The only remaining difference is that I physically shut down the Router for a short period.... is there anyway you can test this scenario please?
Thanks

kenharpster

#8
Apr 07, 2019, 02:37 pm Last Edit: Apr 07, 2019, 06:09 pm by kenharpster
I cannot shutdown the router but I realized in the middle of the night a difference that has documentation to back it up. I recently installed firmware version 1.2.2 to test another problem from the hourly build of IDE 1.8.9 at https://www.arduino.cc/en/Main/Software#hourly and it is still installed on my Arduino. The change log shows fixes for hangs when the WiFi is disconnected. You could try updating to 1.2.2 and see if that gets rid of the hang. The program will still need to run the router connection code outside of setup. As far as I can tell during my testing, once connected, the status remains a 3 (WL_CONNECTED) even when the router is disconnected so it cannot be relied upon at this time to control whether the connection to the router failed (after successfully connected). As a work around, I just attempt to reconnect to the router if the client.connect fails...

dachurchill

Thanks for the excellent support !!!
I installed the latest firmware (1.2.2) and re-tested. It certainly looks like the hang condition has been fixed. In all my tests with this version, so far the WiFi Rev2 has fully recovered each time.
still much more testing needed but I am now past the one which was killing me, I'll post again when I am complete with my testing and/or I uncover anything else.
Again, I really appreciate the rapid assistance and advice.
Thanks !!

kenharpster

You're welcome! Glad to hear that the fix in 1.2.2 resolved your problem!

ZainS

Is 1.2.2 released as yet?

FWIW I have run into what appears to be this exact problem using 1.2.1 Firmware. And with can with just some print commands isolate it to

client.connect(server, 80)

If I remote disable the wifi radio at the right moment the code will stop right there and never return.

I attempted to do things like check WiFi.status() before executing client.connect but all indications are that status stays WL_Connected for a few seconds after the Wifi radio goes silent, thus allowing client.connect() to try and fail and not return either a true nor false.  

So in the following code fragment example, I can see "Attempting to connect to server..." in the Serial Monitor but not "connecting..." which pretty clearly says client.connect() is going away and never coming back...


--------------
void makehttpRequest() {
 // If Wifi died before this call bail out

   if (WiFi.status() != WL_CONNECTED) {
   return;
   }
 
 
   Serial.println("Attempting to connect to server...");
 // if there's a successful connection:
 if (client.connect(server, 80)) {                                          
   Serial.println("connecting...");                                          
   // send the HTTP PUT request:                                            
   client.println("GET /data/2.5/forecast?q=" + nameOfCity + "&APPID=" + apiKey + "&mode=json&units=imperial&cnt=5 HTTP/1.1");
   client.println("Host: api.openweathermap.org");
   client.println("User-Agent: ArduinoWiFi/1.1");
   client.println("Connection: close");
   client.println();

   Serial.println("Request Sent...");
   ... etc etc
                                   

pert

Is 1.2.2 released as yet?          
It depends on what you mean by "released".

There are three components to that question:
  • Has there been a release of the NINA firmware? I would say "yes" (actually there is even a 1.2.3 now), though the developers haven't created Git tags in that repository since the 1.1.0 release for some reason.
  • Has there been a release of the WiFi101/WiFi NINA Firmware Updater plugin for the Arduino IDE which contains NINA firmware 1.2.2? Yes. The 0.10.7 release of the plugin contains NINA firmware 1.2.2. The 0.10.8 release of the plugin contains NINA firmware 1.2.3.
  • Has there been a release of the Arduino IDE which contains WiFi101/WiFi NINA Firmware Updater plugin 0.10.7 or newer? No. There has not been an Arduino IDE release since the time that WiFi NINA 1.2.2 came out. It is part of the Arduino IDE hourly build though.

ZainS

Thanks. Being the non-software newb I am, my definition of "released" is probably #3. Or anything were I can just ask the IDE to update itself and update firmware with handy menus. :)

pert

The Arduino IDE doesn't have the capability of updating itself with a new NINA firmware version, but if you use the Hourly Build, the 1.2.2 version is certainly there in the handy menus.

Go Up