Arduino Nano IOT 33 stops sending data after 3 hours

I have managed to get the arduino to send data to my dashboard but after a few hours, it seems to stop. Not really sure where to start with this one. Maybe someone can help by looking at my code.

Purpose: I have 2 HC-SR04 sensors to detect tank levels and display them onto the dashboard:

/********************************
 * Libraries included
 *******************************/

#include <SPI.h>
#include <WiFiNINA.h>
#include <avr/dtostrf.h>

const int trigPin = 2;
const int echoPin = 3;
const int trig2Pin = 4;
const int echo2Pin = 5;
float duration2, distance2;
float duration, distance;
float trueLenght2;
float fullTank2 = 115; //Reading when tank is at full capacity
float emptyTank2 = 15; //Reading when tank is empty
float litres2;
float trueLenght;
float fullTank = 115; //Reading when tank is at full capacity
float emptyTank = 15; //Reading when tank is empty
float litres;
void(* resetFunc) (void) = 0; //Reset Function

/********************************
 * Constants and objects
 *******************************/

#define DEVICE_LABEL "arduino-nano-33"
#define TOKEN "token"

char const * VARIABLE_LABEL_1 = "sensor";
char const * VARIABLE_LABEL_2 = "sensor2";
char const *SERVER="industrial.api.ubidots.com";
//Replace the above line if you are an Educational user char const *SERVER="things.ubidots.com";

const int HTTPPORT= 443;
char const *AGENT="Arduino Nano 33 IoT";
char const *HTTP_VERSION = " HTTP/1.1\r\n";
char const *VERSION ="1.0";
char const *PATH= "/api/v1.6/devices/";

char const * SSID_NAME = ""; // Put here your SSID name
char const * SSID_PASS = ""; // Put here your password

int status = WL_IDLE_STATUS;

WiFiSSLClient client;

/********************************
 * Auxiliar Functions
 *******************************/



void getResponseServer() {
  
  while (client.available()) {
    char c = client.read();
    
  }
}

void waitServer() {
  int timeout = 0;
  while (!client.available() && timeout < 5000) {
    timeout++;
    delay(1);
    if (timeout >= 5000) {
      
      break;
    }
  }
}

void sendData(char* payload) {
  int contentLength = strlen(payload);

  /* Connecting the client */
  if (client.connect(SERVER, HTTPPORT)) {
   
   
    client.print(F("POST "));
    client.print(PATH);    
    client.print(DEVICE_LABEL);
    client.print(F("/"));
    client.print(HTTP_VERSION);
    client.print(F("Host: "));
    client.print(SERVER);
    client.print(F("\r\n"));  
    client.print(F("User-Agent: "));
    client.print(AGENT);
    client.print(F("\r\n"));
    client.print(F("X-Auth-Token: "));
    client.print(TOKEN);
    client.print(F("\r\n"));
    client.print(F("Connection: close\r\n"));
    client.print(F("Content-Type: application/json\r\n"));
    client.print(F("Content-Length: "));
    client.print(contentLength);
    client.print(F("\r\n\r\n"));
    client.print(payload);
    client.print(F("\r\n"));
   

   
    waitServer();
    getResponseServer();
  }

    /* Disconnecting the client */
  client.stop();
}

/********************************
 * Main Functions
 *******************************/

void setup() {
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trig2Pin, OUTPUT);
  pinMode(echo2Pin, INPUT);


  

  
 
  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    
    status = WiFi.begin("Kemp", "Kemp1234");
    // wait 10 seconds for connection:
    delay(10000);
  }
 

}

void loop(){

digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
   trueLenght = fullTank-emptyTank-distance;
  litres = (3.14159265359)*(sq(0.4))*(trueLenght)*10;

  digitalWrite(trig2Pin, LOW);
  delayMicroseconds(2);
  digitalWrite(trig2Pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig2Pin, LOW);
duration2 = pulseIn(echo2Pin, HIGH);
  distance2 = (duration2*.0343)/2;
  trueLenght2 = fullTank2-emptyTank2-distance2;
  litres2 = (3.14159265359)*(sq(0.4))*(trueLenght2)*10;

  
delay(1);

  char payload[200];
  char str_val_1[30];
  char payload2[200];
  char str_val_2[30];
  
  /*4 is the total lenght of number,maximun number accepted is 99.99*/
  float value = litres;
  float value2 = litres2;
  
  dtostrf(value, 5, 2, str_val_1);
  dtostrf(value2, 5, 2, str_val_2);
  sprintf(payload, "%s","");
   sprintf(payload2, "%s","");
  sprintf(payload, "{\"");
   sprintf(payload2, "{\"");
  sprintf(payload, "%s%s\":%s", payload, VARIABLE_LABEL_1, str_val_1);
  sprintf(payload2, "%s%s\":%s", payload2, VARIABLE_LABEL_2, str_val_2);
  sprintf(payload, "%s}", payload);
  sprintf(payload2, "%s}", payload2);
 
  //Send the payload to Ubidots
  sendData(payload);
  sendData(payload2);
  delay(500);

Hi @Jpkemp213,

A few things to keep in mind.

  1. As an STEM user, you need to mind about the maximum amount of Dots per day you’re allowed to send, which is 4,000 as described here. With this in mind and based on your Delay(500), you’re sending 4 Dots per second. This rapidly escalate to the 4,000 limit. To be precise, in just about half hour, you’d run out of Dots, so I even consider it unlikely you have been able to send data for about 3 hours at this rate.
    I recommend decreasing your update frequency significantly to make the best possible out of your 4,000 Dots per day.

  2. In the same line, Ubidots STEM users get a throttling of 1 request per second. You’re making close to 4 per second as payload1 and payload2 are sent in separate requests. This would be another reason to miss data.

  3. You’re missing an important line in the void getResponseServer() function to print the response from the server and make it easier to debug the issue. Here’s how it should look. Don’t forget to initialize the Serial in the setup() function as Serial.begin(9600);
    Please make this changes and see the server response once you stop receiving data.

void getResponseServer() {
  
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}
  1. You should send both sensor and sensor2 variables values in the same payload and hence, the same request, as appose to a single payload and a single request for each one. Here’s how:
dtostrf(value, 5, 2, str_val_1);
dtostrf(value2, 5, 2, str_val_2);
sprintf(payload, "%s","");
sprintf(payload, "{\"");
sprintf(payload, "%s%s\":%s,", payload, VARIABLE_LABEL_1, str_val_1);
sprintf(payload, "%s\"%s\":%s", payload, VARIABLE_LABEL_2, str_val_2);
sprintf(payload, "%s}", payload);

Serial.print(payload);

//Send the payload to Ubidots
sendData(payload);
  1. Last but not least, I recommend referring to our Hardware Docs to better understand point 4 in regards to building a payload for 2 variables.
    In addition, we have a library for the Arduino IoT nano 33 found at https://github.com/ubidots/ubidots-ArduinoMKR

Best,

–David

Hi,

I made all the changes and had it running one payload every 25 seconds. which I calculated would equate to 4000 dots. Yet after a couple of hours its gave me this error message:error

Hi, your issue seems to be related not with Ubidots but with the way that you are managing your board sockets. Arduino has a limit of available sockets to be opened and used at the same time, it seems that you are not closing properly every socket that you are attempting to open for sending data. Try to call the method flush() and close() of the TCP client at the beginning and the end of your routine to be sure that you are closing properly any previous opened socket.

All the best