-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHT11 sensor
151 lines (132 loc) · 4.63 KB
/
DHT11 sensor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "WiFi.h" //adding the wifi libary
#include "ESPAsyncWebServer.h" //Async basically allows multiple requests on the webserver
#include <Adafruit_Sensor.h> // To read all the values from the DHT sensor (conversions are offered by adafruit sensors)
#include <DHT.h> //DHT library that has all the functions of the Sensor
const char* ssid = "_____"; //add your wifi name here
const char* password = "_____"; //add your wifi password here
#define DHTPIN 4 // GPIO pin that is connected to the DHT 4
#define DHTTYPE DHT11 // DHT 11 (you could change it to the type of DHT you have. But make sure the library includes the DHT type)
DHT dht(DHTPIN, DHTTYPE);
AsyncWebServer server(80); // Create AsyncWebServer object on port 80 (80 is default)
String readDHTTemperature() {
float t = dht.readTemperature(); //reading temp in degree centigrades 'C
// Check for a reading else try again.
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return "--";
}
// if reading is found then display the value
else {
Serial.println(t);
return String(t);
}
}
String readDHTHumidity() {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
if (isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return "**";
}
else {
Serial.println(h);
return String(h);
}
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<script src="https://kit.fontawesome.com/e10a7f45ba.js" crossorigin="anonymous"></script>
<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; color:#2d81c8; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.dht-labels{font-size: 1.5rem; vertical-align:middle;padding-bottom: 15px;}
.temp_number{color:#2d81c8;}
.hum_number{color:#2d81c8;}
</style>
</head>
<body>
<h2>DHT Server - Simplified</h2>
<p>
<i class="fa-solid fa-temperature-quarter"></i>
<span class="dht-labels">Temperature</span>
<span class="temp_number" id="temperature">%TEMPERATURE%</span>
<sup class="units">°C</sup>
</p>
<p>
<i class="fa-solid fa-percent"></i>
<span class="dht-labels">Humidity</span>
<span class="hum_number" 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 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(){
}