0

Given an Arduino Uno Wifi Rev2 with this small U-blox NINA wifi module.

All work great however after a long time (I can't be more exact but we talk here about weeks if not months), it suddenly disconnects.

This board is unattended, and the only way to recover is to reset the Arduino.

Here is my code:

WiFiServer server(PORT);

void setup() {
  connectToWifi();
  server.begin();
}

void connectToWifi() {
  // 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);
    if (status == WL_CONNECTED) break;

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

void loop() {
  // check WiFi status
  if (WL_CONNECTED != WiFi.status()) connectToWifi();

  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        ... (other wifi-unrelated code)

        // close the connection:
        client.stop();
      }
    }
  } 
}

When it disconnects from wifi, the router's dhcp manager console shows it with "waiting" status.

Note: I'm the only client, and only one character is sent to the Arduino at once, then the socket is closed. It is very unlikely, that a client hangs, and the board is stuck in the while loop.

Is this a known problem, or I have to dig myself more into this?

Thank you!

2
  • do you use the newest versions of firmware and library? – Juraj May 17 '20 at 18:13
  • I've updated that at the end of last year. Until then it is unattended, and this is the second time this situation happened. (after the first time we had to apply a power recycle to the board). So I'm not sure that's the latest, and I can not query it, as it is far away and lost the connection – Daniel May 17 '20 at 18:47
1

As you have not given the details there are some things to check

  • is your Nina-Firmware > 1.2.4
  • If not update to the latest 1.3.0 via the WiFi101-FirmwareUpdater-Plugin 0.10.10 is included in the ArduinoIDE 1.8.12 or greater
  • Disconnect before you reconnect (in case of router drop-outs) and
  • Close any connection before sending/receiving. This will free the socket on the Nina module. Stop the client after doing the stuff needed with client.stop();
  • If there is any server connection code in setup this should be run in a reconnect routine (as you did not include setup) just as a hint
  • Make sure that after a certain period the while (status != WL_CONNECTED) and while (client.connected()) are broken and error testing takes place e.g.
    if (status==WL_DISCONNECTED || status==WL_CONNECTION_LOST){.. do something to change that ..}

  • If all fails implement a routine that checks the signal and restarts the sketch if the router has dropped out (for whatever reason).

Disconnect code:

void connectToWifi() {
    // 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) {
    .... REST of your code ...
    }
}

Hint (as you did not show setup) if you have something like

client.connect(server, 80);

move it to the reconnection routine in the form of

if (client.connect(server, 80)) {
    Serial.println("connecting...");
.... REST of reconnect ...
} 

Restart code if wifi signal is lost

void(* resetFunc) (void) = 0; // Declare reset function at address 0

void loop() {
  if (WiFi.RSSI() == 0) // Checks if Wifi signal lost.
  { resetFunc(); // Calls Reset
  }
// Rest of code goes here.
}

This should altogether ensure a long and stable running client

4
  • Thank you, points 1-2-3 I can check once I return to the board. client.stop(); is actually called, I've updated the Q. server.begin is called only once in setup - Q is updated. I need to call server.begin after every connectToWifi call? – Daniel May 18 '20 at 6:23
  • No not server.begin() thats ok as it is - please give infos to your schematic of the network. Are there clients connected to the UNO (server.begin indicates that) and the UNO acts himself as client to anothrer server. Or is the UNO the only server ? . Its hard to work with bread crumbs (what libs are you using, is String clas called, what is attached to the UNO, ... The more info the more precise the answers you get – Codebreaker007 May 18 '20 at 8:55
  • UNO functions as a server. One client very rarely (once/week) connects to it, sends 1 character as a command, then UNO responds with client.print("response"); (always 1 line only). Very simple logic: UNO reads some GPIO and put it into response. – Daniel May 18 '20 at 9:46
  • I had 1.2.4 version on the NINA, now upgraded to 1.3.0. Also added the status = WiFi.disconnect(); line to the connectToWifi routine. Keeping my fingers crossed. – Daniel Aug 20 '20 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.