-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetTempHumSensORly.ino
368 lines (307 loc) · 9.89 KB
/
NetTempHumSensORly.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// +++++++++++++++++++++++++
// NetTempHumSensORly (oO)
// +++++++++++++++++++++++++
// Copyright (C) 2017 Martin Feil aka. SGDW
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/
// Features ------------------------------------------------------------------
#define SERIAL_MODE
#define NETWORK_MODE
// #define VERBOSE
// Macros --------------------------------------------------------------------
#ifdef SERIAL_MODE
#define USE_SERIAL
#ifdef VERBOSE
#define VERBOSE_AND_SERIAL
#endif
#endif
#ifdef VERBOSE_AND_SERIAL
#define LOG(s) Serial.println(s)
#define LOGA(s) Serial.print(s)
#else
#define LOG(s)
#define LOGA(s)
#endif
#ifdef NETWORK_MODE
#define USE_NETWORK
#endif
// Application ---------------------------------------------------------------
#include <dht.h> // see http://playground.arduino.cc//Main/DHTLib
#ifdef USE_NETWORK
#include <UIPEthernet.h> // see https://github.com/ntruchsess/arduino_uip
#endif
// Sensor
#define DHT22_PIN 3
#define SENSOR_COOLDOWN 4000
// Network
#define CONNECTION_DELAY 200
// Board
#define LED_PIN 13
#define BAUD_RATE 9600
dht DHT;
#ifdef USE_NETWORK
byte mac[] = { 0x42, 0x53, 0x42, 0x02, 0x00, 0x01 };
IPAddress ip(192, 168, 178, 201);
EthernetServer server(80);
#endif
struct
{
uint32_t total;
uint32_t ok;
uint32_t crc_error;
uint32_t time_out;
uint32_t connect;
uint32_t ack_l;
uint32_t ack_h;
uint32_t unknown;
} stat = { 0, 0, 0, 0, 0, 0, 0, 0};
#define ULONG_MAX pow(2, 32) -1
unsigned long time;
unsigned long last_sensor_read;
// Setup ---------------------------------------------------------------------
void setup() {
setup_led();
setup_serial();
setup_network();
setup_sensor();
LOG(F("Setup finished [OK]"));
}
void setup_led() {
pinMode(LED_PIN, OUTPUT);
}
void setup_serial() {
#ifdef USE_SERIAL
Serial.begin(BAUD_RATE);
while (!Serial) ;
LOG(F("Serial [OK]"));
#endif
}
void setup_network() {
#ifdef USE_NETWORK
Ethernet.begin(mac, ip);
server.begin();
LOG(F("Network [OK]"));
LOGA(F("IP Address: "));
LOG(Ethernet.localIP());
#endif
}
void setup_sensor() {
LOGA(F("Sensor DHTlib "));
LOGA(F(DHT_LIB_VERSION));
LOGA(F(" [OK]"));
time = millis();
last_sensor_read = 0;
}
// Loop ----------------------------------------------------------------------
void loop() {
time = millis();
LOGA(F("Time: ")); LOG(time);
LOGA(F("Last read: ")); LOG(last_sensor_read);
bool do_read_sensor =
((time < last_sensor_read) && ((ULONG_MAX - last_sensor_read + time) > SENSOR_COOLDOWN)) ||
((time >= last_sensor_read) && ((time - last_sensor_read) > SENSOR_COOLDOWN));
if(do_read_sensor) {
read_sensor();
blink(50);
last_sensor_read = millis();
}
serve_web_request();
server_serial_request();
blink(50);
}
// Networking ----------------------------------------------------------------
void serve_web_request() {
#ifdef USE_NETWORK
LOG(F("Web: Waiting for connection"));
EthernetClient client = server.available();
if (client)
{
LOG(F("Web: New Connection"));
boolean currentLineIsBlank = true; // an http request ends with a blank line
int headerPos = 0;
boolean isGetRequest = false;
char pathFirstChar = '/';
boolean endOfHeader = false;
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (headerPos == 0 && c == 'G') {
isGetRequest = true;
} else if (isGetRequest && headerPos == 5) {
pathFirstChar = c;
}
headerPos++;
endOfHeader = (c == '\n' && currentLineIsBlank);
#ifdef VERBOSE_AND_SERIAL
if (headerPos < 10) {
Serial.print("# ");
Serial.print(headerPos);
Serial.print(": '");
Serial.print(c);
Serial.println("'");
}
#endif
if (endOfHeader) // http hackery ... first empty line
{
if (pathFirstChar == 'c') {
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/plain; charset=UTF-8"));
client.println(F(""));
client.print(F("T,"));
client.print(DHT.temperature, 1);
client.print(F(",H,"));
client.println(DHT.humidity, 1);
} else if (pathFirstChar == 'j') {
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: application/json; charset=UTF-8"));
client.println(F(""));
client.print(F("{temperature: "));
client.print(DHT.temperature, 1);
client.print(F(",humidity: "));
client.print(DHT.humidity, 1);
client.print(F("}"));
} else {
client.println("<html><meta http-equiv=\"refresh\" content=\"60\"><title>Sensor Data</title><body></body>");
client.print("<div><span>Temperatur:</span> <span id=\"temperature\">");
client.print(DHT.temperature, 1);
client.print("</span><span>°C</span></div>");
client.print("<div><span>Luftfeuchtigkeit:</span> <span id=\"humidity\">");
client.print(DHT.humidity, 1);
client.print("<span>%</span></div>");
client.println("</body></html>");
}
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r')
{
currentLineIsBlank = false;
}
}
}
delay(10); // give the web browser time to receive the data
client.stop(); // close the connection:
LOG(F("Web: Disconnected\n"));
} else {
LOG(F("Web: No request"));
delay(CONNECTION_DELAY);
}
#endif
}
// Sensor --------------------------------------------------------------------
int auto_write_sensor = 0;
void read_sensor() {
uint32_t dift = micros();
int chk = DHT.read22(DHT22_PIN);
dift = micros() - dift;
stat.total++;
switch (chk) {
case DHTLIB_OK:
stat.ok++;
#ifdef VERBOSE
LOG(F("DHT22: OK,\t"));
#endif
break;
case DHTLIB_ERROR_CHECKSUM:
stat.crc_error++;
LOG(F("DHT22: Checksum error,\t"));
break;
case DHTLIB_ERROR_TIMEOUT:
stat.time_out++;
LOG(F("DHT22: Time out error,\t"));
break;
case DHTLIB_ERROR_CONNECT:
stat.connect++;
LOG(F("DHT22: DHT22 Connect error,\t"));
break;
case DHTLIB_ERROR_ACK_L:
stat.ack_l++;
LOG(F("DHT22: Ack Low error,\t"));
break;
case DHTLIB_ERROR_ACK_H:
stat.ack_h++;
LOG(F("DHT22: Ack High error,\t"));
break;
default:
stat.unknown++;
LOG(F("DHT22: Unknown error,\t"));
break;
}
// DISPLAY DATA
#ifdef VERBOSE_AND_SERIAL
Serial.print(F("# "));
Serial.print(DHT.humidity, 1);
Serial.print(F("%,\t"));
Serial.print(DHT.temperature, 1);
Serial.print(F("°C,\t"));
Serial.print(dift);
Serial.println();
if (stat.total % 20 == 0)
{
Serial.print(F("# "));
Serial.println(F("\nTOT\tOK\tCRC\tTO\tUNK"));
Serial.print(stat.total);
Serial.print(F("\t"));
Serial.print(stat.ok);
Serial.print(F("\t"));
Serial.print(stat.crc_error);
Serial.print(F("\t"));
Serial.print(stat.time_out);
Serial.print(F("\t"));
Serial.print(stat.connect);
Serial.print(F("\t"));
Serial.print(stat.ack_l);
Serial.print(F("\t"));
Serial.print(stat.ack_h);
Serial.print(F("\t"));
Serial.print(stat.unknown);
Serial.println(F("\n"));
}
#endif
}
void server_serial_request() {
#ifdef SERIAL_MODE
int serial_data = 0;
if (Serial.available() > 0) {
serial_data = Serial.read();
if (serial_data == 'r') {
write_sensor_to_serial();
} else if (serial_data == 'a') {
auto_write_sensor = 1;
} else if (serial_data == 's') {
auto_write_sensor = 0;
}
} else if (auto_write_sensor) {
write_sensor_to_serial();
}
#endif
}
void write_sensor_to_serial() {
#ifdef USE_SERIAL
Serial.print(F("T,"));
Serial.print(DHT.temperature, 1);
Serial.print(F(",H,"));
Serial.print(DHT.humidity, 1);
Serial.println();
#endif
}
// Helpers -------------------------------------------------------------------
void blink(int delayMs) {
digitalWrite(LED_PIN, HIGH); delay(delayMs);
digitalWrite(LED_PIN, LOW); delay(delayMs);
}