Help needed with ESP32 and DHT22 sensor

Hello everyone, this is my first post in this forum and I need help

I want to display temp and humidity in my IoT dashboard but i keep getting this error, I admit that I'm not a programmer but this issue is a headache, and I'll attach the entire code and a screenshot of the error for your review.

error message: expected primary-expression before ')' token
in this line: DHT22 dht22_sensor (DHT22_SENSOR_PIN, DHT22_SENSOR_TYPE);

Thanks in advance

Yusuf.

// DHT22 - Version: 1.0.6


/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/4aa1dbdb-13be-4258-8cf6-e57e44c8cc33 

  Arduino IoT Cloud Variables description

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

  CloudSwitch motor_Switch;
  CloudTemperatureSensor temp;
  CloudRelativeHumidity humi;

  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"
#include <DHT22.h>
#define MOTOR_PIN 27
#define DHT22_SENSOR_PIN 25
#define DHT22_SENSOR_TYPE DHT22
DHT22 dht22_sensor (DHT22_SENSOR_PIN, DHT22_SENSOR_TYPE);

void setup() {
  pinMode(DHT22_SENSOR_PIN, INPUT);
  pinMode(MOTOR_PIN, OUTPUT);
  DHT22_SENSOR.begin();
  // 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 
  humi = DHT22_sensor.readHumidity();
  temp = DHT22_sensor.readTemperature();
   
  if(isnan(humi) || isnan(temp)){
    Serial.println("Failed to read from dht22 Sensor|");
  }else{
    Serial.print("Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | ");
    Serial.print("Temperature: "); Serial.print(temp); Serial.println("∁°");
  }  
    delay(2000); 
  
}


/*
  Since MotorSwitch is READ_WRITE variable, onMotorSwitchChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMotorSwitchChange()  {
  // Add your code here to act upon MotorSwitch change
  if(motor_Switch){
    digitalWrite(MOTOR_PIN, HIGH); 
  }else{
    digitalWrite(MOTOR_PIN, LOW); 
  }
  
}




Where did you get the code from? That line doesn't have any meaning (or it shouldn't be there). Do you have the link with the source of that code?

1 Like

I took it from here:

See if this code works for you (I don't have that sensor).

1 Like

Same error

That DHT22 is most likely a define, and I don't know what it is (it's probably defined in some header file). What happens if you comment out that line?

1 Like

You instantiated "DHT22 dht22_sensor (DHT22_SENSOR_PIN, DHT22_SENSOR_TYPE);"
but started: " DHT22_SENSOR.begin();"

dht22_sensor and DHT22_SENSOR
They are different objects.

1 Like

Update..

I used the example code in Arduino IDE for the sensor and it worked like a pro

Much appreciated for your help raduprv

The only example that I can find is DHTtester which does work for me. Where is the cloud example that uses the DHT library? I've tried an example that I found on YouTube that often doesn't read anything from the sensor or when it does the temperature and humidity readings are wildly incorrect.