-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomGuard.ino
437 lines (379 loc) · 10.8 KB
/
RoomGuard.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <SPI.h>
#include <WiFiNINA.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include "secrets.h"
#include "config.h"
#include "icons.h"
#ifdef SD_ENABLED
#include <SPI.h>
#include <SD.h>
File oSDFile;
#endif
#ifdef SNMP_ENABLED
#include <AgentuinoWiFi.h>
#include "snmp_config.h"
int16_t iSNMPTemp = 0;
int16_t iSNMPHumid = 0;
#endif
// Variables for WiFi Connection.
int iWiFiStatus = WL_IDLE_STATUS;
WiFiServer oServer(80);
IPAddress WIFI_IP;
char cClientData;
int iSSIDLen = 0;
int iPSKLen = 0;
int iWiFiSignal = -100;
String sSSID = "";
String sPSK = "";
// OLED Display.
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DHT Sensor Variables.
DHT_Unified dht(DHTPIN, DHTTYPE);
bool bHasTemp = false;
bool bHasHumid = false;
sensors_event_t pEvent;
// Update Timing Variables for Display and Sensor.
float fRealTemp = 0.0;
float fRealHumid = 0.0;
long lLastUpdate = 0;
long lUpdateInterval = 2000;
long lNow = 0;
long lLastSensorUpdate = 0;
long lSensorInterval = 5000;
long lLastWiFiCheck = 0;
long lWiFiInterval = 5000;
// Memory Status
int iFreeMemory = 32000;
// Setup all components and connect to WiFi.
void setup() {
Serial.begin(9600);
// Wait for the serial-port to initialize.
if(!Serial) {
delay(3000);
}
// Start Display.
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show Boot-Screen.
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println(F("RoomGuard starting..."));
display.println();
// Load Settings from SD-Card if SD_ENABLED is defined.
#ifdef SD_ENABLED
if (!SD.begin(SD_CS)) {
display.println(F("SD-Card failed!"));
display.display();
while(true) {
delay(1);
}
}
display.println(F("SD-Card OK"));
display.display();
// Read settings from rg.cfg
oSDFile = SD.open(F("rg.cfg"), FILE_READ);
sSSID = oSDFile.readStringUntil('\n');
sPSK = oSDFile.readStringUntil('\n');
String sTempCorr = oSDFile.readStringUntil('\n');
String sHumiCorr = oSDFile.readStringUntil('\n');
String GSM_ALERT_PHONE = oSDFile.readStringUntil('\n');
String sAlertTemp = oSDFile.readStringUntil('\n');
String sAlertHumi = oSDFile.readStringUntil('\n');
oSDFile.close();
// Process/Convert strings from config-file.
iSSIDLen = sSSID.length();
char aSSID[iSSIDLen];
sSSID.toCharArray(aSSID, iSSIDLen);
iPSKLen = sPSK.length();
char aPSK[iPSKLen];
sPSK.toCharArray(aPSK, iPSKLen);
TEMP_CORRECT = sTempCorr.toFloat();
HUMID_CORRECT = sHumiCorr.toFloat();
ALERT_TEMP = sAlertTemp.toFloat();
ALERT_HUMID = sAlertHumi.toFloat();
display.print(F("SSID: \""));
display.print(aSSID);
display.println(F("\""));
#ifdef DEBUG
Serial.print(F("Phone: "));
Serial.println(GSM_ALERT_PHONE);
Serial.print(F("Temp Corr:"));
Serial.println(TEMP_CORRECT);
Serial.print(F("Humi Corr:"));
Serial.println(HUMID_CORRECT);
Serial.print(F("Alert Temp:"));
Serial.println(ALERT_TEMP);
Serial.print(F("Alert Humid:"));
Serial.println(ALERT_HUMID);
#endif
#ifdef SNMP_ENABLED
#ifdef SNMP_SDCONFIG
// Load SNMP values from SD-Card.
readSNMPDatafromSD();
#endif
#endif
#else
// If SD-Card is not enabled, use settings from config.h and secrets.h
char aSSID[] = SECRET_SSID;
char aPSK[] = SECRET_PASS;
sSSID = SECRET_SSID;
sPSK = SECRET_PASS;
iSSIDLen = sSSID.length();
iPSKLen = sPSK.length();
display.print(F("SSID: "));
display.println(SECRET_SSID);
#endif
display.display();
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
display.println(F("WiFi Module Error"));
display.display();
// don't continue
while (true);
}
#ifdef DEBUG
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
display.println(F("FW Upgrade available!"));
display.display();
}
#endif
display.print(F("Connecting."));
display.display();
while (iWiFiStatus != WL_CONNECTED) {
display.print(".");
display.display();
iWiFiStatus = WiFi.begin(aSSID, aPSK);
delay(5000);
}
display.println(F("OK"));
display.display();
WIFI_IP = WiFi.localIP();
oServer.begin();
#ifdef SNMP_ENABLED
// Initialize SNMP
display.print(F("SNMP: "));
display.display();
iSNMPStatus = Agentuino.begin();
if ( iSNMPStatus == SNMP_API_STAT_SUCCESS ) {
Agentuino.onPduReceive(pduReceived);
delay(10);
display.println(F("OK"));
}
else {
display.print(F("ERR "));
display.println(iSNMPStatus);
#ifdef DEBUG
Serial.print(F("SNMP ERR: "));
Serial.println(iSNMPStatus);
#endif
}
display.display();
#endif
blackoutScreen();
printWiFiStatus();
dht.begin();
}
// Default Process.
void loop() {
lNow = millis();
// Check WiFi status after timeout.
if (lNow >= lLastWiFiCheck + lWiFiInterval) {
checkWiFi();
lLastWiFiCheck = lNow;
}
// Update sensor data & memory stats after refresh-timeout.
if (lNow >= lLastSensorUpdate + lSensorInterval || lLastSensorUpdate == 0) {
refreshSensorValues();
iFreeMemory = freeMemory();
#ifdef DEBUG
Serial.print(F("Free RAM: "));
Serial.println(iFreeMemory);
#endif
lLastSensorUpdate = lNow;
}
#ifdef SNMP_ENABLED
if ( millis() - prevMillis > 1000 ) {
// increment previous milliseconds
prevMillis += 1000;
//
// increment up-time counter
locUpTime += 100;
}
#endif
// Update display after refresh-timeout.
if (lNow >= lLastUpdate + lUpdateInterval || lLastUpdate == 0) {
display.fillRect(0,0,128,64,SSD1306_BLACK);
printWiFiStatus();
printSensorValues();
display.display();
lLastUpdate = lNow;
}
// Check for HTTP-Clients connecting to Device on Port 80.
WiFiClient oClient = oServer.available();
if (oClient) {
#ifdef DEBUG
Serial.println("new client");
#endif
String sCurrentLine = "";
bool bCurrentLineIsBlank = true;
// Show RoomGuard Status-XML.
while (oClient.connected()) {
if (oClient.available()) {
cClientData = oClient.read();
if (cClientData == '\n' && bCurrentLineIsBlank) {
// Send HTTP response
oClient.println(F("HTTP/1.1 200 OK"));
oClient.println(F("Content-Type: text/xml"));
oClient.println(F("Connection: close")); // the connection will be closed after completion of the response
oClient.println(F("Refresh: 5")); // refresh the page automatically every 5 sec
oClient.println();
oClient.println(F("<?xml version=\"1.0\"?>"));
oClient.println(F("<RoomGuard>"));
// output the values from DHT sensor
oClient.print(F("<Temperature>"));
oClient.print(fRealTemp);
oClient.println(F("</Temperature>"));
oClient.print(F("<Humidity>"));
oClient.print(fRealHumid);
oClient.print(F("</Humidity>"));
oClient.println("</RoomGuard>");
break;
} else if (cClientData == '\n') {
// you're starting a new line
sCurrentLine = "";
bCurrentLineIsBlank = true;
}
}
}
// Delay, so client can properly fetch the data.
delay(1);
// close the connection
oClient.stop();
#ifdef DEBUG
Serial.println("client disconnected");
#endif
}
#ifdef SNMP_ENABLED
// Check for SNMP requests.
Agentuino.listen();
#endif
}
// Check WiFi Status and Reconnect if necessary.
void checkWiFi() {
iWiFiStatus = WiFi.status();
if (iWiFiStatus != WL_CONNECTED) {
display.fillRect(0,0,128,7,SSD1306_BLACK);
display.setCursor(0, 0);
display.setTextSize(1);
display.print(F("Reconnecting..."));
display.display();
char aSSID[iSSIDLen];
sSSID.toCharArray(aSSID, iSSIDLen);
char aPSK[iPSKLen];
sPSK.toCharArray(aPSK, iPSKLen);
iWiFiStatus = WiFi.begin(aSSID, aPSK);
}
}
// Wipe Screen
void blackoutScreen() {
display.fillRect(0,0,128,64,SSD1306_BLACK);
display.display();
}
// Print WiFi Status on Display.
void printWiFiStatus() {
display.setTextSize(1);
display.setCursor(0,0);
if(iWiFiStatus == WL_CONNECTED) {
display.print(F("IP: "));
display.println(WIFI_IP);
}
else
{
display.print(F("DISCONNECTED ("));
display.print(iWiFiStatus);
display.print(F(")"));
}
display.setCursor(0,8);
iWiFiSignal = WiFi.RSSI();
// Print Signal-Status as Signal-Bar if WIFI_SIGNAL_BAR is defined.
#ifdef WIFI_SIGNAL_BAR
if (iWiFiSignal <= -80 ||iWiFiSignal == 0) {
// No signal, so nothing to show except an error...
display.drawBitmap(111, 29, icWarning, 16, 16, 1);
}
else if(iWiFiSignal <= -70) {
display.drawBitmap(111, 27, icWIFI1, 16, 16, 1);
}
else if(iWiFiSignal <= -60) {
display.drawBitmap(111, 27, icWIFI2, 16, 16, 1);
}
else if(iWiFiSignal <= -50) {
display.drawBitmap(111, 27, icWIFI3, 16, 16, 1);
}
else {
display.drawBitmap(111, 27, icWIFI4, 16, 16, 1);
}
#else
// Print Signal-Status as Text if WIFI_SIGNAL_BAR is commented out.
display.print(F("Signal: "));
display.print(iWiFiSignal);
display.println(F(" dBm"));
#endif
}
// Refresh Sensor-Values from DHT sensor.
void refreshSensorValues() {
#ifdef DEBUG
Serial.println(F("Updating Sensor values"));
#endif
dht.temperature().getEvent(&pEvent);
if(isnan(pEvent.temperature)) {
fRealTemp = 0;
bHasTemp = false;
}
else {
fRealTemp = float(pEvent.temperature) + TEMP_CORRECT;
bHasTemp = true;
}
dht.humidity().getEvent(&pEvent);
if(isnan(pEvent.relative_humidity)) {
fRealHumid = 0;
bHasHumid = false;
}
else {
fRealHumid = float(pEvent.relative_humidity) + HUMID_CORRECT;
bHasHumid = true;
}
#ifdef SNMP_ENABLED
// Convert float to integer for SNMP; DISPLAY-HINT is configured in ROOMGUARD-MIB.txt
iSNMPTemp = fRealTemp * 10;
iSNMPHumid = fRealHumid * 10;
#endif
}
// Print Sensor Values on Display.
void printSensorValues() {
display.drawBitmap(0, 30, icTemperature, 16, 16, 1);
display.drawBitmap(0, 46, icHumidity, 16, 16, 1);
display.setTextSize(2);
display.setCursor(18, 30);
if(bHasTemp) {
display.print(fRealTemp,1);
}
else {
display.print(F("EE"));
}
display.setCursor(18, 46);
if(bHasHumid) {
display.print(fRealHumid,1);
}
else {
display.print(F("EE"));
}
}