Difficulty creating code for esp32 and connecting to the cloud dashboard

Hello, I'm new here, but I would like some help if possible, I'm trying to create a code that sends data from sensors to monitor the water level in the Arduino cloud, but every time I try to compile it shows an error

#include <WiFi.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[] = "WIFI";
const char PASS[] = "PASS_WIFI";

void setup() {
  // Inicialize a conexão Wi-Fi
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }

  // Inicialize o Arduino IoT Cloud
  ArduinoCloud.setThingId("THING_ID"); // Substitua pelo ID do seu Thing no Arduino Cloud
  ConnectionHandler.attachConnectionHandler();

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
}

void loop() {
  ArduinoCloud.update();
  readSensorData();
  sendToArduinoCloud();
  delay(5000); // Enviar dados a cada 5 segundos
}

void readSensorData() {
  int sensor1Value = analogRead(A0);
  int sensor2Value = analogRead(A1);
  int sensor3Value = analogRead(A2);

  int consolidatedSensorData = 0;

  if (sensor1Value < 500) {
    consolidatedSensorData = 25;
  } else if (sensor1Value < 500 && sensor2Value < 500) {
    consolidatedSensorData = 50;
  } else if (sensor1Value < 500 && sensor2Value < 500 && sensor3Value < 500) {
    consolidatedSensorData = 75;
  } else {
    consolidatedSensorData = 0;
  }

  ArduinoCloud.update(THINGS[0], consolidatedSensorData);
}

the code consists of reading 3 float-type sensors and depending on whether their condition was reached, a single value is returned showing whether it is at 25%, 50% or 75% of the value.
Can anyone help me? I intend to use just one ESP32

  1. Missing THINGS declaration: In your readSensorData() function, you're trying to update THINGS[0] in the Arduino Cloud with the consolidated sensor data. However, THINGS is not declared anywhere in your code. You need to declare it and initialize it with the properties of your Thing from the Arduino Cloud.

  2. Missing sendToArduinoCloud() function: In your loop() function, you're calling a function named sendToArduinoCloud(), but this function is not defined anywhere in your code.

  3. Missing ConnectionHandler initialization: You're using ConnectionHandler.attachConnectionHandler(); in your setup() function, but you haven't initialized ConnectionHandler anywhere.

Here's a revised version of your code with these issues addressed:

#include <WiFi.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[] = "WIFI";
const char PASS[] = "PASS_WIFI";
const char THING_ID[] = "THING_ID"; // Replace with your Thing ID

WiFiConnectionHandler conHandler(SSID, PASS);
CloudFloat consolidatedSensorData; // This will hold your consolidated sensor data

void setup() {
  // Initialize Wi-Fi connection
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }

  // Initialize Arduino IoT Cloud
  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addThingProperty(consolidatedSensorData, READ, ON_CHANGE); // Add consolidatedSensorData as a property of your Thing
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
}

void loop() {
  ArduinoCloud.update();
  readSensorData();
  delay(5000); // Send data every 5 seconds
}

void readSensorData() {
  int sensor1Value = analogRead(A0);
  int sensor2Value = analogRead(A1);
  int sensor3Value = analogRead(A2);

  if (sensor1Value < 500) {
    consolidatedSensorData = 25;
  } else if (sensor1Value < 500 && sensor2Value < 500) {
    consolidatedSensorData = 50;
  } else if (sensor1Value < 500 && sensor2Value < 500 && sensor3Value < 500) {
    consolidatedSensorData = 75;
  } else {
    consolidatedSensorData = 0;
  }
}

Hello, thanks for your support, I made some changes to the code, and I would like some help again...
To be the same as the THING created, I changed the "consolidatedSensorData" to "nivelagua", I changed the board pinout and the condition, I followed the recommendations but I still get errors.
The variable in the cloud was declared as bool "nivelagua"

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/bc737a5a-ad8a-4311-ae41-05f0d50a89b2 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool nivelagua;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/
#include <WiFi.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

#include "thingProperties.h"

#define sensor1Value 18
#define sensor2Value 19
#define sensor3Value 21


const char SSID[] = "AGROPECUARIA RIO PARAISO";
const char PASS[] = "@@1010@@2020@@";
const char THING_ID[] = "9b97343f-6f53-47cc-a80e-c311c0a09b7c"; // Replace with your Thing ID

WiFiConnectionHandler conHandler(SSID, PASS);
CloudBool nivelagua; // This will hold your consolidated sensor data


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);

pinMode(sensor1Value, INPUT);
pinMode(sensor2Value, INPUT);
pinMode(sensor3Value, INPUT);

  // Initialize Wi-Fi connection
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
  
  
  // Inicialize o Arduino IoT Cloud
  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addThingProperty(nivelagua, READ, ON_CHANGE); // Add consolidatedSensorData as a property of your Thing
 // ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  readSensorData();
  delay(5000); // Send data every 5 seconds
  
}

void readSensorData() {


  if ((sensor1Value == 1) && (sensor2Value == 0) && (sensor3Value == 0){
    nivelagua = 25;
  } else if ((sensor1Value == 1) && (sensor2Value == 1) && (sensor3Value == 0) {
    nivelagua = 50;
  } else if ((sensor1Value == 1) && (sensor2Value == 1) && (sensor3Value == 1) {
    nivelagua = 75;
  } else {
    nivelagua = 0;
  }
}

When compiling it gives an error on line 26

It seems like you're missing closing parentheses in your if and else if conditions inside the readSensorData() function. Here's the corrected code:

void readSensorData() {
  if ((sensor1Value == 1) && (sensor2Value == 0) && (sensor3Value == 0)) {
    nivelagua = 25;
  } else if ((sensor1Value == 1) && (sensor2Value == 1) && (sensor3Value == 0)) {
    nivelagua = 50;
  } else if ((sensor1Value == 1) && (sensor2Value == 1) && (sensor3Value == 1)) {
    nivelagua = 75;
  } else {
    nivelagua = 0;
  }
}

Also, it seems like you're comparing pin numbers (sensor1Value, sensor2Value, sensor3Value) with values directly. You should use the digitalRead() function to read the state of a digital pin. Here's how you can do it:

void readSensorData() {
  if ((digitalRead(sensor1Value) == HIGH) && (digitalRead(sensor2Value) == LOW) && (digitalRead(sensor3Value) == LOW)) {
    nivelagua = 25;
  } else if ((digitalRead(sensor1Value) == HIGH) && (digitalRead(sensor2Value) == HIGH) && (digitalRead(sensor3Value) == LOW)) {
    nivelagua = 50;
  } else if ((digitalRead(sensor1Value) == HIGH) && (digitalRead(sensor2Value) == HIGH) && (digitalRead(sensor3Value) == HIGH)) {
    nivelagua = 75;
  } else {
    nivelagua = 0;
  }
}

Okay tanks, when compiling the code the following error is displayed:

Start verifying
In file included from /mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA/src/WiFiStorage.h:23,
from /mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA/src/WiFi.h:38,
from /tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:16:
/mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA/src/utility/wifi_drv.h:293:12: error: 'PinStatus' does not name a type
static PinStatus digitalRead(uint8_t pin);
^~~~~~~~~
/tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:27:17: error: redefinition of 'const char SSID []'
const char SSID[] = "AGROPECUARIA RIO PARAISO";
^
In file included from /tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:20:
/tmp/1779136720/Nivel_Caixa_nov01a/thingProperties.h:8:12: note: 'const char SSID [25]' previously defined here
const char SSID[] = SECRET_SSID; // Network SSID (name)
^~~~
/tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:28:17: error: redefinition of 'const char PASS []'
const char PASS[] = "@@1010@@2020@@";
^
In file included from /tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:20:
/tmp/1779136720/Nivel_Caixa_nov01a/thingProperties.h:9:12: note: 'const char PASS [15]' previously defined here
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
^~~~
/tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:32:6: error: redefinition of 'bool nivelagua'
bool nivelagua; // This will hold your consolidated sensor data
^~~~~~~~~
In file included from /tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:20:
/tmp/1779136720/Nivel_Caixa_nov01a/thingProperties.h:13:6: note: 'bool nivelagua' previously declared here
bool nivelagua;
^~~~~~~~~
/tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino: In function 'void setup()':
/tmp/1779136720/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:54:16: error: 'class ArduinoIoTCloudTCP' has no member named 'addThingProperty'
ArduinoCloud.addThingProperty(nivelagua, READ, ON_CHANGE); // Add consolidatedSensorData as a property of your Thing
^~~~~~~~~~~~~~~~
Multiple libraries were found for "WiFiClientSecure.h"
Used: /home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/libraries/WiFiClientSecure
Not used: /home/builder/opt/libraries/seeed_arduino_rpcwifi_1_0_6
Multiple libraries were found for "WiFi.h"
Used: /mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA
Not used: /home/builder/opt/libraries/seeed_arduino_rpcwifi_1_0_6
Not used: /home/builder/opt/libraries/indhilib_3_0_5
Not used: /home/builder/opt/libraries/wifinina_1_8_14
Not used: /home/builder/opt/libraries/vega_wifinina_1_0_1
Not used: /home/builder/opt/libraries/da16200_wi_fi_library_for_arduino_1_1_0
Not used: /home/builder/opt/libraries/wifi_1_2_7
Not used: /home/builder/opt/libraries/wifiespat_1_4_3
Not used: /home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/libraries/WiFi
Multiple libraries were found for "SPI.h"
Used: /home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/libraries/SPI
Not used: /home/builder/opt/libraries/eventethernet_1_0_0
Error during build: exit status 1

  1. 'PinStatus' does not name a type: This error usually occurs when the compiler can't find a definition for PinStatus. Make sure that the library or file where PinStatus is defined is correctly included.

  2. Redefinition of 'const char SSID []' and 'const char PASS []': It seems like you have defined SSID and PASS in two places: once in your main .ino file and once in thingProperties.h. You should only define them once.

  3. Redefinition of 'bool nivelagua': Similar to the SSID and PASS, you have also defined nivelagua in two places: once in your main .ino file and once in thingProperties.h. You should only define it once.

  4. 'class ArduinoIoTCloudTCP' has no member named 'addThingProperty': This error suggests that the method addThingProperty does not exist in the class ArduinoIoTCloudTCP. You might be using an outdated version of the library or the method name could be incorrect.

  5. Multiple libraries were found for "WiFiClientSecure.h", "WiFi.h", "SPI.h": This warning indicates that there are multiple versions of these libraries installed. While this might not be causing the errors, it's generally a good idea to clean up and keep only the versions you need.

Now I am only presented with this PinStatus error (and the libraries), but as I am creating the code outside the IDE, online, how could I check the library or remove it?

/usr/local/bin/arduino-cli compile --fqbn esp32:esp32:esp32doit-devkit1 --build-cache-path /tmp --output-dir /tmp/2744136787/build --build-path /tmp/arduino-build-C66C01C800F93E1FBDC9E7AEF58901BA --library /mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA /tmp/2744136787/Nivel_Caixa_nov01a

In file included from /mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA/src/WiFiStorage.h:23,

from /mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA/src/WiFi.h:38,

from /tmp/2744136787/Nivel_Caixa_nov01a/Nivel_Caixa_nov01a.ino:16:

/mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA/src/utility/wifi_drv.h:292:12: error: 'PinStatus' does not name a type

static PinStatus digitalRead(uint8_t pin);

^~~~~~~~~

Multiple libraries were found for "WiFi.h"

Used: /mnt/create-efs/webide/dd/6d/dd6d2d177f1d97af15c1b3ca95a2e80e:gash51/libraries_v2/WiFiNINA

Not used: /home/builder/opt/libraries/da16200_wi_fi_library_for_arduino_1_1_0

Not used: /home/builder/opt/libraries/wifiespat_1_4_3

Not used: /home/builder/opt/libraries/wifi_1_2_7

Not used: /home/builder/opt/libraries/vega_wifinina_1_0_1

Not used: /home/builder/opt/libraries/indhilib_3_0_5

Not used: /home/builder/opt/libraries/wifinina_1_8_14

Not used: /home/builder/opt/libraries/seeed_arduino_rpcwifi_1_0_6

Not used: /home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/libraries/WiFi

Multiple libraries were found for "SPI.h"

Used: /home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/libraries/SPI

Not used: /home/builder/opt/libraries/eventethernet_1_0_0

Multiple libraries were found for "WiFiClientSecure.h"

Used: /home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/libraries/WiFiClientSecure

Not used: /home/builder/opt/libraries/seeed_arduino_rpcwifi_1_0_6

Error during build: exit status 1

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