Arduino Forum

Products => Arduino WiFi Rev2 => Topic started by: dachurchill on Apr 06, 2019, 07:31 am

Title: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: dachurchill on Apr 06, 2019, 07:31 am
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.
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: Coding Badly on Apr 06, 2019, 07:50 am

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

Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: dachurchill on Apr 06, 2019, 07:57 am
sorry, new to this forum and initially missed that there was a section dedicated to the WiFi Rev2. It won't happen again :-)
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: Coding Badly on Apr 06, 2019, 04:49 pm

Use Report to moderator (http://forum.arduino.cc/index.php?action=reporttm;topic=608321.0;msg=4126140) if you ever want this thread moved.

Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: kenharpster on Apr 07, 2019, 02:19 am
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();

}
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: dachurchill on Apr 07, 2019, 03:07 am
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
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: kenharpster on Apr 07, 2019, 03:24 am
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");
  }
}
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: dachurchill on Apr 07, 2019, 04:50 am
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
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: kenharpster on Apr 07, 2019, 02:37 pm
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...
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: dachurchill on Apr 07, 2019, 11:13 pm
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 !!
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: kenharpster on Apr 07, 2019, 11:57 pm
You're welcome! Glad to hear that the fix in 1.2.2 resolved your problem!
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: ZainS on Apr 17, 2019, 03:26 am
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
                                   
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: pert on Apr 17, 2019, 03:43 am
Is 1.2.2 released as yet?          
It depends on what you mean by "released".

There are three components to that question:
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: ZainS on Apr 17, 2019, 11:52 pm
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. :)
Title: Re: Arduino Uno WiFi Rev2 hangs if WiFi connection disrupted
Post by: pert on Apr 18, 2019, 03:28 am
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.