CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

29 June, 2024

ESP-32 With DHT-11 Sensor not returning expected output

 Programing Coderfunda     June 29, 2024     No comments   

I am trying to use the output of a DHT sensor module whose data pin I have connected to Pin 27 of my ESP-32 Dev Module. The original code uses the DHT library but I can't seem to get that to work so I am using the DHT_nonblocking library which seems to work with my sensor. Here is example code from the DHT_nonblocking library that I tried to implement into my own code to create an asynchronous web server that displays the data. The example code worked, but my own code which I added direct lines from the example code is not returning any data from the DHT sensor.


Here are the codes:


EXAMPLE CODE that works with my sensor on an ESP-32:


`
#include
/* Uncomment according to your sensortype. */
#define DHT_SENSOR_TYPE DHT_TYPE_11
//#define DHT_SENSOR_TYPE DHT_TYPE_21
//#define DHT_SENSOR_TYPE DHT_TYPE_22

static const int DHT_SENSOR_PIN = 27;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin( 9600);
}



/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}

return( false );
}

/*
* Main program loop.
*/
void loop( )
{
float temperature;
float humidity;

/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
}



}


`

---



Here are my results by executing this code:
12:34:16.068 -> T = 26.0 deg. C, H = 6.0%
12:34:19.331 -> T = 26.0 deg. C, H = 6.0%
12:34:22.634 -> T = 27.0 deg. C, H = 6.0%
12:34:25.905 -> T = 27.0 deg. C, H = 6.0%
12:34:29.180 -> T = 27.0 deg. C, H = 6.0%
12:34:32.451 -> T = 27.0 deg. C, H = 6.0%
12:34:35.722 -> T = 28.0 deg. C, H = 7.0%
12:34:38.991 -> T = 28.0 deg. C, H = 7.0%
12:34:42.262 -> T = 28.0 deg. C, H = 7.0%
12:34:45.559 -> T = 28.0 deg. C, H = 7.0%
12:34:48.825 -> T = 29.0 deg. C, H = 8.0%
12:34:52.090 -> T = 29.0 deg. C, H = 8.0%
12:34:55.392 -> T = 30.0 deg. C, H = 8.0%
12:34:58.660 -> T = 31.0 deg. C, H = 9.0%
12:35:01.924 -> T = 31.0 deg. C, H = 9.0%
12:35:05.197 -> T = 31.0 deg. C, H = 9.0%
12:35:08.466 -> T = 32.0 deg. C, H = 9.0%

MY CODE THAT DOES NOT GIVE ANY RESULTS FROM THE DHT11 SENSOR:



`
// Import required libraries
#include
#include "ESPAsyncWebServer.h"
#include
// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "PASSSWORD";

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

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

DHT_nonblocking dht(DHTPIN, DHTTYPE);

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );

/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}

return( false );
}
float temperature;
float humidity;
String readDHTTemperature() {
if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C");

return String(temperature);

}}

String readDHTHumidity() {
if( measure_environment( &temperature, &humidity ) == true )
{

Serial.print( "H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );

return String(humidity);

}}

const char index_html[] PROGMEM = R"rawliteral(





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;
}





ESP32 DHT Server






Temperature
%TEMPERATURE%
°C





Humidity
%HUMIDITY%
%



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 ) ;

)rawliteral";

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

void setup(){
// Serial port for debugging purposes
Serial.begin(115200);

// dht.begin();

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}

// Print ESP32 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", readDHTTemperature().c_str());
});
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readDHTHumidity().c_str());
});

// Start server
server.begin();
}

void loop(){

}



`


Here are my results by running this code:
12:27:40.469 -> Connecting to WiFi..
12:27:40.469 -> 10.0.0.236
12:27:47.375 -> T = 0.0 deg. CH = 0.0%
12:28:32.344 -> T = 0.0 deg. CH = 0.0%
12:29:32.312 -> T = 0.0 deg. CH = 0.0%
12:30:32.378 -> T = 0.0 deg. CH = 0.0%



Why is this happening?


The wiring, baud rate of the Serial Monitor is right, and I have tried everything from this website:
https://randomnerdtutorials.com/solved-dht11-dht22-failed-to-read-from-dht-sensor/, but the output keeps saying the temperature and the humidity are 0 or null. How can I change the code which creates a web server to give the right results? Thanks!


(Sorry for any bad grammar or formatting. Let me know if you need any more details. Thanks again!)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Sitaare Zameen Par Full Movie Review
     Here’s a  complete Vue.js tutorial for beginners to master level , structured in a progressive and simple way. It covers all essential topi...
  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • iOS 17 Force Screen Rotation not working on iPAD only
    I have followed all the links on Google and StackOverFlow, unfortunately, I could not find any reliable solution Specifically for iPad devic...
  • C++ in Hindi Introduction
    C ++ का परिचय C ++ एक ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग लैंग्वेज है। C ++ को Bjarne Stroustrup द्वारा विकसित किया गया था। C ++ में आने से पह...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda