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();
}

