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!)
0 comments:
Post a Comment
Thanks