Invalid readings from DHT11 sensor

i am doing an greenhouse project with esp8266 and i compiled the code but only the soil moisture was working and the temp and humidity was staying the same.what might be the problem

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

// Replace with your network credentials
const char* ssid = "Vaibhav";
const char* password = "11111111";

// Initialize components
DHT dht(13, DHT11); 
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 cols, 2 rows
const int soilMoisturePin = A0;


// Create an instance of the 
ESP8266WebServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  dht.begin();
  lcd.init();
  lcd.backlight();

  // Connect to Wi-Fi
  Serial.println();
  Serial.println("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("Connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Routes for the server
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void handleRoot() {
  int h = dht.readHumidity();
  int t = dht.readTemperature();
  int soilMoisture = analogRead(soilMoisturePin);

  // Display data on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Hum: ");
  lcd.print(h);
  lcd.print("%");

  // Create HTML response
  String html = "<html><body>";
  html += "<h1>Greenhouse Control System</h1>";
  html += "<p>Temperature: " + String(t) + " &deg;C</p>";
  html += "<p>Humidity: " + String(h) + " %</p>";
  html += "<p>Soil Moisture: " + String(soilMoisture) + "</p>";
  html += "</body></html>";
  
  server.send(200, "text/html", html);
}

in the web page it says

Greenhouse Control System
Temperature: 2147483647 °C
Humidity: 2147483647 %
Soil Moisture: 990

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

ok thank you

bro people get very distracted when they see so many comments,i think its better if i make another topic

bc no one will comment anything

The reasons for not receiving help are:

  • unspecific title
  • violating forumrules (crossposting)
  • use of chatGPT
  • asking for maximum help by demanding "fix my code" in combination with almost zero own effort
    (at least not witnessing your own effort)

My suggestion is:
add a sentence that tells all users in the first posting to scroll down to post number XY where you did a new start

then
to reduce your code to something much simpler and to test this simpler code if it is working.
Just the DHT-sensor printing to the serial monitor
if this works

adding the LCD and printing DHT-values to the LCD

very important: witnessing your own effort by posting the demo-codes as a code-sections

using a demo -code for Ubidot testing if the Ubidot-connection itself is working.
Do you understand a demo -code that is well known for working.

adding the DHT-LCD-Code to the Ubidot-Demo-code testing if this works

If this is too much work for you: go buying a ready to use product off the shelf.

best regards Stefan

from the DHTtester.ino

float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

these functions return 'float' type variables, not 'int'

2147483647 = 0x7FFFFFFF

that is nearly all bits high. Have you connected everything properly (don't forget the pullup on the data line)

thx you bro you are always very helpful

best regards ice guy

Hello guys i am new to arduino and i was testing me DHT11 sensor with esp8266 but it is giving me "failed to read from dht sensor" .when i change the code it gives me other errors like nan or it gives me some random value which doesnt change

(example)Temperature: 2147483647 °C

Humidity: 2147483647 %

this is the wiring

this is the code

// Import required libraries
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

#define DHTPIN 5     // Digital pin connected to the DHT sensor

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

// current temperature & humidity, updated in loop()
float t = 0.0;
float h = 0.0;

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;    // will store last time DHT was updated

// Updates DHT readings every 10 seconds
const long interval = 10000;  

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
    .dht-labels{
      font-size: 1.5rem;
      vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
</head>
<body>
  <h2>ESP8266 DHT Server</h2>
  <p>
    <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
    <span class="dht-labels">Temperature</span> 
    <span id="temperature">%TEMPERATURE%</span>
    <sup class="units">&deg;C</sup>
  </p>
  <p>
    <i class="fas fa-tint" style="color:#00add6;"></i> 
    <span class="dht-labels">Humidity</span>
    <span id="humidity">%HUMIDITY%</span>
    <sup class="units">%</sup>
  </p>
</body>
<script>
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature", true);
  xhttp.send();
}, 10000 ) ;

setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("humidity").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity", true);
  xhttp.send();
}, 10000 ) ;
</script>
</html>)rawliteral";

// Replaces placeholder with DHT values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return String(t);
  }
  else if(var == "HUMIDITY"){
    return String(h);
  }
  return String();
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);
  dht.begin();
  
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println(".");
  }

  // Print ESP8266 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", String(t).c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", String(h).c_str());
  });

  // Start server
  server.begin();
}
 
void loop(){  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you updated the DHT values
    previousMillis = currentMillis;
    // Read temperature as Celsius (the default)
    float newT = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    //float newT = dht.readTemperature(true);
    // if temperature read failed, don't change t value
    if (isnan(newT)) {
      Serial.println("Failed to read from DHT sensor!");
    }
    else {
      t = newT;
      Serial.println(t);
    }
    // Read Humidity
    float newH = dht.readHumidity();
    // if humidity read failed, don't change h value 
    if (isnan(newH)) {
      Serial.println("Failed to read from DHT sensor!");
    }
    else {
      h = newH;
      Serial.println(h);
    }
  }
}

Please read this li k and use it: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

Please post schematics and not useless pictures. Pen and paper works fine.

Please post the code You use, here, using the function.

What do the serial prints on the processor report the values as?

From a comment on the linked page (maybe the drawing has changed):

"This circuit schematics still has a mistake. 4.7 K should be a pull up resistor, whereas your sketch shows it as a pull down to GND. It does not work like this, I tried it."

Are you using a DHT22 or DHT11 ?

Did you add a pullup resistor to the sensor ?

Double check your sensor pinout designations.

dht11
my sensor is the exact same as in the schematic

Your image shows a DHT11.

However your code tells a different story.

// Uncomment the type of sensor in use:
//#define DHTTYPE    DHT11     // DHT 11
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

If you have a DHT11 you want to uncomment the DHT11. Most DHT11 and DHT22 modules as you posted an image of also have a pullup resistor on the board. Anyway try changing your code for a DHT11 verse the DHT22 you have uncommented in your code. Make it look like this.

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

Ron

What schematic?

The schematic in post #1, The one that looks like a picture. :slight_smile: Oh wait, it is a picture.

Ron

srry i was talking abt the picture

i have uncommonented dht11 but still the same problem

i am trying a lot of different arduino codes but my sensor gives differnet errors like
"nan" or "failed to read from dht sensor" or "0 humidity and 0 temp".i am using arduino uno board

this is my sensor and i think its working bc the red light is on


#include <dht11.h>
#define DHT11PIN 4

dht11 DHT11;

void  setup()
{
  Serial.begin(9600);
 
}

void loop()
{
  Serial.println();

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature  (C): ");
  Serial.println((float)DHT11.temperature, 2);

  delay(2000);

}

this is one of the codes i was having trouble with

this are wiring

can you help me find the problem