-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11.ino
304 lines (259 loc) · 7.8 KB
/
dht11.ino
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <TM1637Display.h>
// Replace with your network credentials
const char* ssid = "SSID"; // replace with your ssid
const char* password = "PASSWORD"; // replace with your key
const String hostname = "Home.ESP.DHT11.1"; // hostname for zabbix
WiFiServer AgentServer(10050);
#define DHTPIN 5 // Digital pin connected to the DHT sensor - for me it was D1
// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
#define DISPLAY_CLK 6 // it was display, but it doesn't work for me :(
#define DISPLAY_DIO 7
DHT dht(DHTPIN, DHTTYPE);
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>%HOSTNAME%</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">°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);
}
else if(var == "HOSTNAME"){
return hostname;
}
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);
}
// 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 web-server
server.begin();
// Start agent-server
AgentServer.begin();
AgentServer.setNoDelay(true);
}
void zabbixAgentAnswer(WiFiClient *client, String data) {
unsigned int tmp;
// protocol + flags
client->write((unsigned char *) "ZBXD\x01", 5);
// packet size
tmp = data.length();
client->write((unsigned char *) &tmp, 4);
// reserved
tmp = 0;
client->write((unsigned char *) &tmp, 4);
// payload
client->print(data);
}
void zabbixAgent() {
char tmp[4];
WiFiClient client = AgentServer.available();
if (!client)
return;
Serial.println("New client! Waiting for data...");
int timeout = 1000, i = 0;
while (client.connected() && !client.available() && i < timeout) {
delay(1);
i++;
}
if (!client.connected() || !client.available()) {
Serial.println("Client timeout...");
client.stop();
return;
}
// Protocol name
if (client.read((unsigned char *) tmp, 4) != 4 || strncmp(tmp, "ZBXD", 4) != 0) {
Serial.println("Invalid zabbix protocol name...");
client.stop();
return;
}
// Flags
int flags = client.read();
if (flags < 0) {
Serial.println("Unexpected EOF when flags read...");
client.stop();
return;
}
// Packet size
unsigned int packet_size;
if (client.read((unsigned char *) &packet_size, 4) != 4) {
Serial.println("Unexpected EOF when packet_size read...");
client.stop();
return;
}
// Reserved
if (client.read((unsigned char *) tmp, 4) != 4) {
Serial.println("Unexpected EOF when reserved read...");
client.stop();
return;
}
Serial.print("flags=");
Serial.print(flags);
Serial.print("\n");
Serial.print("packet_size=");
Serial.print(packet_size);
Serial.print("\n");
char command[64];
if (packet_size >= sizeof(command)) {
Serial.println("Packet size overflow...");
client.stop();
return;
}
if (client.read((unsigned char *) command, packet_size) != packet_size) {
Serial.println("Unexpected EOF when command read...");
client.stop();
return;
}
command[packet_size] = '\0';
Serial.print("command=");
Serial.println(command);
if (strcmp(command, "agent.ping") == 0) {
Serial.println("->ping");
zabbixAgentAnswer(&client, "1");
} else if (strcmp(command, "agent.version") == 0) {
Serial.println("->version");
zabbixAgentAnswer(&client, "Esp.Agent/1.0");
} else if (strcmp(command, "agent.uptime") == 0) {
Serial.println("->uptime");
zabbixAgentAnswer(&client, String(millis() / 1000).c_str());
} else if (strcmp(command, "agent.hostname") == 0) {
Serial.println("->hostname");
zabbixAgentAnswer(&client, hostname);
} else if (strcmp(command, "read.temperature") == 0) {
Serial.println("->temperature");
zabbixAgentAnswer(&client, String(t).c_str());
} else if (strcmp(command, "read.humidity") == 0) {
Serial.println("->humidity");
zabbixAgentAnswer(&client, String(h).c_str());
} else {
Serial.println("->unknown command");
zabbixAgentAnswer(&client, "ZBX_NOTSUPPORTED");
}
Serial.println("flush and close...");
client.flush();
client.stop();
}
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);
}
}
zabbixAgent();
}