DHT22 problem with code

Hello

At first everything worked fine, but then I made some changes to the code but I don't remember which ones and the humidity and temperature sensor stopped working, I tried everything. Can someone tell me where the error is?


  float voltage;
  CloudLight led;
  CloudTemperatureSensor temperature;
  int bat_percentage;
  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"

#include "DHT.h"
#define DHTTYPE DHT22     // DHT 22
#define DHTPIN D4 //DHT22 Pin D4(GPIO 2)
#define led D2
DHT dht(DHTPIN, DHTTYPE);
int analogInPin  = A0;    // Analog input pin
int sensorValue; 
float calibration = 0.40; // Check Battery voltage using multimeter & add/subtract the value
void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  dht.begin();

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
pinMode(led, OUTPUT);
  // 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();
 sensorValue = analogRead(analogInPin);
  voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration); //multiply by two as voltage divider network is 100K & 100K Resistor
 
  bat_percentage = mapfloat(voltage, 2.8, 4.2, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage
 
  if (bat_percentage >= 100)
  {
    bat_percentage = 100;
  }
  if (bat_percentage <= 0)
  {
    bat_percentage = 1;
  }
 
  Serial.print("Analog Value = ");
  Serial.print(sensorValue);
  Serial.print("t Output Voltage = ");
  Serial.print(voltage);
  Serial.print("t Battery Percentage = ");
  Serial.println(bat_percentage);
  delay(1000);


  float hm= dht.readHumidity();
  Serial.print("Humidity ");
  Serial.println(hm);
  float temp=dht.readTemperature();
  Serial.print("Temperature ");
  Serial.println(temp);
  humidity=hm;
  temperature=temp;
   

  
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

/*
  Since Temperature is READ_WRITE variable, onTemperatureChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperatureChange()  {
  // Add your code here to act upon Temperature change
}

/*
  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
}

/*

  


/*
  Since Led is READ_WRITE variable, onLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedChange()  {
  // Add your code here to act upon Led change
  if(led)
  {
    digitalWrite(led,HIGH);
    Serial.println("ON");
  }
  else
  {
    digitalWrite(led,LOW);
    Serial.println("OFF");
  }
}


/*
  Since Temp is READ_WRITE variable, onTempChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTempChange()  {
  // Add your code here to act upon Temp change
}

Please help

The voltage looks rather small indicating that nothing is connected to pin A0 hence you are seeing only the value of the variable calibration.

I guess that the humidity and temperature show NaN (not a number) for the same reason that the DHT sensor is not connected to anything.

Show the output you are seeing on the serial monitor generated by such statements as Serial.print("Analog Value = "); etc. in your code.

Yes, but when I connect the cables to the batteries, everything is fine

But it worked before the changes

But then I wanted to add a few more things to the code and I messed something up

I'll do it when I get back from work

#include "thingProperties.h" probably needs to be first line, before defining variable types...

Also missing "comment mark" before "Variables which are marked..."

/*

I just copied it without to make shorter post

Start with the original... post it here... (code and wiring). Describe the features you want added.

That's the point, I don't have the original saved when it worked. I thought I'd just fix this code somehow

Maybe searching on some of the lines in your code will help. Post the full code you have.

If you are not a "structured" programmer... maybe the lines that you added will seem "un-structured" and maybe you can identify those lines.

You have Serial.print for T/RH . Do they show values on serial monitor?

1 Like

You are not using Arduino Uno or Nano. Which board are you using?

Esp8266 match with that.

1 Like

^It was the full code.

Esp8266 Nodemcu v CH340.
But it's not about the board. It worked before

I will try after work :slight_smile: in few hours

There are lots of details that aren't the problem bu might help us to figure out the problem. Knowing what board you are using will help to understand how the code should work.

1 Like

Im sorry, now I understand.
Esp8266 Nodemcu v3 CH340

That code does not compile. You need to post the code that compiles. See post #4.

Note to save your code as you write it, as versions, even non working code, so you can always go back to a previous version if you stuff up.

You do have a pull up resistor on the DHT22?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

2 Likes

Good advice for future :stuck_out_tongue_winking_eye:

I have this one

I removed everything and loaded the simplest code I found on the internet.

Maybe I damaged the sensor when I desoldered the pins (I wanted to connect directly with cables to save space) :thinking: