Connection to server fails after a while

Hi,

I'm using an Arduino Nano 33 IOT together with the WiFiNINA library to transmit sensor data to an influx database. All works well, I gather data every 5 seconds and transmit this every 10 seconds. The datastream is steady for a while until the connection with the server fails:
(client.connect(server, 443)) returns false.

If I wait long enough the device recovers and starts to transmit data again:

When I watch the serial monitor, the following line appear just before the sending the data failed:

09:32:21.156 -> [C:\Users\Documents\Arduino\libraries\WiFiNINA\src\utility\spi_drv.cpp::247]-I-1,0
09:32:31.158 -> Send request failed

Never seen that kind of message on the serial monitor, is that something from the new IDE?

I've tested on may different Wifi networks, this appears to make no difference. The time it takes for the connection to fail does appear to have a relation with the transmit interval, if I only send data every 60 seconds, it will take longer for the connection to fail. After each attempt to send data, I call the client.flush() function to clear the buffer.

Anyone familiar with this problem? Any clues on how to debug this?

Thanks a bunch!

edit: this is my code:

#include <SPI.h>
#include <WiFiNINA.h>
#include <NTPClient.h>
#include "settings.h"

int status = WL_IDLE_STATUS;
String body;


WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
WiFiSSLClient client;

void setup() {
  // initialize serial communications at 115200 bps:
  Serial.begin(115200);
  Wire.begin();

  createWirelessConnection();
  timeClient.begin();

  delay(1000);
}

void loop() {

  updateWifiStatus();   //check if wifi is still connected
  timeClient.update();  //update the timeclient

  //--- Get sensordata ---
  if ((millis() - updateTimer) > updateInterval) {
    //Create body String containing sensordata
  }

  //--- Transmit data ---
  if ((millis() - transmit_timer) > transmit_interval) {
    //Update the Wifi Status to check if wifi connection is still available:
    updateWifiStatus();
    sendRequest();
  }
}

void updateWifiStatus() {
  if (String(WiFi.localIP()) == "0" || String(WiFi.RSSI()) == "0") {
    Serial.println("Wifi connection lost, try to establish new connection");
    status = WL_CONNECTION_LOST;
    WiFi.end();
    createWirelessConnection();
  }
  else {
    status = WL_CONNECTED;
  }
}

void createWirelessConnection() {
  // 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 for connection and blink while doing so:
    for (int i = 0; i < 20; i++) {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(50);
      digitalWrite(LED_BUILTIN, LOW);
      delay(50);
    }

  }
  Serial.println();
  Serial.println("Connected to wifi");
}

void sendRequest() {

  if (client.connect(server, 443)) {

    Serial.println("-------START REQUEST-------");
    client.print("POST /api/v2/write");
    client.print("?org=");
    client.print(organisation);
    client.print("&bucket=");
    client.print(bucket);
    client.print("&precision=s");
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Accept: application/json");
    client.println("Content-Type: text/plain");
    client.print("Authorization: Token ");
    client.println(token);
    client.println("Content-Length: " + String(body.length()));
    client.println("Connection: keep-alive");
    client.println();
    client.println(body);
    client.println();
    Serial.println("-------END REQUEST-------");
    Serial.println();
	
    ConnectionCounter++;

  } else {
    Serial.println("Send request failed");
	client.stop();
  }
  body = "";
  client.flush();
  transmit_timer = millis();
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Reads like an overflow issue of some sort. The faster the data send the quicker the issue and the slow the data send the slower the issue.

Thanks for your reply and sorry for not sharing my code, I've edited my initial post.

I agree that the issue appears to be an overflow of some sort. However, I've not yet been able to find the source. I'm currently doing a test in which I count the number of times the Nano has been establishing a connection with the server to see if it fails at a consistent number of uploads.

This is the result for the first try:

The counter reaches 283 when the connection becomes flaky. After 293 is stops all together.

What do the router logs show why the connection is dropped?

The router logs show no sign of a dropped connection. I've also verified this by performing a ping to google by the arduino. When (client.connect(server, 443)) returns false, the ping continues to come back ok. So it appears as if the wifi connection is still good whilst the connection with the server fails.

This did make me wonder, what if I simply disconnect from wifi and reinitiate the connection? So I've changed my code a litte:

void sendRequest() {

  if (client.connect(server, 443)) {

    Serial.println("-------START REQUEST-------");
   // do many client.print
    Serial.println("-------END REQUEST-------");
    Serial.println();
	
    ConnectionCounter++;

  } else {
    Serial.println("Send request failed");
    WiFi.disconnect(); //disconnect from wifi if the connection when server fails
    delay(1000);
    createWirelessConnection(); //reinitiate wifi
  }
  body = "";
  client.flush();
  transmit_timer = millis();
}

And that did the trick! Might not be the prettiest solution but definitely workable for me!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.