Hello friends!
I'm trying to creat my first arduino iot project using a humidity sensor from dfrobot (power, ground and analog going to A0 or pin number 26).
I am using sparkfun thing plus esp32 c.
It is all working fine from the regular IDE getting good readings from the sensor, but once applied using iot cloud the value I get is constant zero 0.0.
My code using the regular IDE:
// Capacitive soil moisture sensor
//
int soil_pin = 26; // AOUT pin on sensor
float log_raw = 0;
float humidity = 0;
void setup() {
Serial.begin(9600); //9600bps is used for debug statements
}
void loop() {
log_raw = float(analogRead(soil_pin));
humidity = map(log_raw,500, 3700, 100 , 0);
Serial.print("Sensor raw: ");
Serial.println(log_raw);
Serial.print("Humidity: ");
Serial.println(humidity);
delay(100); // slight delay between readings
}
The output:
Sensor raw: 3623.00
Humidity: 3.00
Which is fine and well.
The code using IOT cloud:
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/b1856ab1-1ec3-426f-b680-5f2830feb0da
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudRelativeHumidity humidity;
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 "thingProperties.h"
const int soil_pin = 26;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// 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
Serial.print("Raw Data: ");
float log_raw = analogRead(soil_pin);
Serial.println(log_raw); // read sensor
Serial.print("Soil Moisture Sensor: ");
humidity = map(log_raw,500, 3700, 100 , 0);
Serial.println(humidity); // read sensor
delay(500); // slight delay between readings
}
/*
Since Humidity is READ_WRITE variable, onHumidityChange() is
executed every time a new value is received from IoT Cloud.
*/
void onHumidityChange() {
// Add your code here to act upon Humidity change
float log_raw = analogRead(soil_pin);
humidity = map(log_raw,500, 3700, 100 , 0);
}
The output:
Raw Data: 0.0
Soil Moisture Sensor: 115.0
Any help would be greatly appreciated!