DFRobot Humidity sensor with Sparkfun Thing Plus ESP32 - Sensor read const. 0.0

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!

What board are you using? Please post in the thread, not only the title.

If you are in the Gobi desert.

sparkfun thing plus esp32 c, edited.

Its a soil humidity sensor not air.
Do you perhaps have some useful ideas that may help me?

Try reloading the first sketch again to confirm that hardware didn't change.

Hi, I tried several times and there are no hardware changes or problems.
Thanks.

Meaning, you swapped code without changing the hardware and you get approximately the same values every time? Just to be perfectly clear...

Correct!

Look carefully at pin assignments when using the ESP32. Some analog pins conflict with wifi.

I am using pin A0 which works great using the regular IDE, which is also called pin 26 on the sparkfun thing plus esp32. I tried both options because I saw others had problems declaring AO A1 and so on.

You declare it as a pin number (26) in both sketches. So I don't see how that could make a difference..

I ment I tried using A0 and 26, on both sketches, because I saw it made a difference on another thread. Both work using the regular IDE but not using the cloud.
Thanks for trying to help.

But the sketches that you posted are the exact sketches that produced the output that you posted, correct?

Correct.

Your post #11 did not really address my reply #10

Have you tried using a different analog pin?

True.
I have now tried using pins A1 and A2 instead of A0. I made sure they work properly using the IDE first.
Edit: It is now working properly on pin A2 thank you so much!