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