-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
329 lines (273 loc) · 8.59 KB
/
main.cpp
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
/*
Power Checker by Karel Cermak | Karlosoft.com
Licensed under MIT
*/
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>;
// OVERALL
int state = 1;
// ETHERNET
byte mac[] = {0xA6, 0x2A, 0xFA, 0xC7, 0x70, 0xC0}; // make it random
IPAddress ip(10, 0, 0, 60); // set the IP address to whatever you want
EthernetServer server(80);
// DHT 22
#define DHTPIN 28
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float hum;
float temp;
// CURRENT INPUT
const int checkMain = A8;
const int checkBackup = A9;
bool mainPower;
bool backupPower;
// LEDS
const byte ledBlue = 29;
const byte ledOrange = 30;
const byte ledRed = 31;
unsigned long previousMillisBlue = 0;
unsigned long previousMillisOrange = 0;
unsigned long previousMillisRed = 0;
boolean ledStateBlue = true;
boolean ledStateOrange = true;
boolean ledStateRed = true;
// TIMES
unsigned long longOn = 2000;
unsigned long longOff = 400;
unsigned long shortOn = 200;
unsigned long shortOff = 75;
// ALARMS
const byte redLight = 32;
const byte buzzer = 33;
boolean buzzerState = true;
unsigned long previousMillisBuzzer = 0;
// STOP BUTTON
const byte stopButton = 34;
byte pauseType = 0; // 0 - no pause, 1 - pause alarm, 2 - pause light and alarm
unsigned long pauseTill = 0;
void(* resetFunc) (void) = 0;
void setup() {
pinMode(checkMain, INPUT);
pinMode(checkBackup, INPUT);
pinMode(ledBlue, OUTPUT);
pinMode(ledOrange, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(redLight, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(stopButton, INPUT_PULLUP);
//dht
dht.begin();
//ethernet
Ethernet.begin(mac, ip);
server.begin();
// blink leds and buzz
digitalWrite(ledBlue, HIGH);
digitalWrite(ledOrange, HIGH);
digitalWrite(ledRed, HIGH);
delay(300);
for (int i = 0; i < 10; i++) {
digitalWrite(buzzer, HIGH);
digitalWrite(ledBlue, HIGH);
digitalWrite(ledOrange, HIGH);
digitalWrite(ledRed, HIGH);
delay(150);
digitalWrite(ledBlue, LOW);
digitalWrite(ledOrange, LOW);
digitalWrite(ledRed, LOW);
digitalWrite(buzzer, LOW);
delay(75);
}
}
void loop() {
dataProcess();
tempProcess();
internetWorks();
serverProcess();
blinkLeds();
checkReset();
checkStopButton();
}
void dataProcess() {
mainPower = digitalRead(checkMain);
backupPower = digitalRead(checkBackup);
if (backupPower == LOW) {
state = 5;
} else if (mainPower == LOW) {
state = 4;
} else if (state != 2 && state != 3) {
state = 1;
}
}
void tempProcess() {
hum = dht.readHumidity();
temp = dht.readTemperature();
if ((isnan(hum) || isnan(temp)) && state == 1) {
state = 2;
} else if (!isnan(hum) && !isnan(temp) && state == 2) {
state = 1;
}
}
void internetWorks() {
if ((Ethernet.hardwareStatus() == EthernetNoHardware || Ethernet.linkStatus() == LinkOFF) && state <= 3) {
state = 3;
} else if (state == 3) {
state = 1;
}
}
void serverProcess() {
EthernetClient client = server.available();
if (client && client.connected() && client.available()) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.println("{");
client.println("\"state\":\"" + stateToString(state) + "\",");
client.println("\"time\":" + String(millis()) + ",");
client.println("\"humidity\":" + String(hum) + ",");
client.println("\"temperature\":" + String(temp) + ",");
client.println("\"mainPower\":" + String(mainPower) + ",");
client.println("\"backupPower\":" + String(backupPower) + "}");
client.println("}");
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
String stateToString(int state) {
switch (state) {
case 1:
return "ok";
case 2:
return "sensor_error";
case 3:
return "internet_error";
case 4:
return "main_power_error";
case 5:
return "backup_power_error";
default:
return "unknown_error";
}
}
void blinkLeds() {
unsigned long calcBlue = millis() - previousMillisBlue;
unsigned long calcOrange = millis() - previousMillisOrange;
unsigned long calcRed = millis() - previousMillisRed;
switch (state) {
case 1:
//blue
if ((calcBlue >= longOn && !ledStateBlue) || (calcBlue >= longOff && ledStateBlue)) {
previousMillisBlue = millis();
ledStateBlue = !ledStateBlue;
digitalWrite(ledBlue, getState(ledStateBlue));
digitalWrite(ledOrange, LOW);
digitalWrite(ledRed, LOW);
digitalWrite(buzzer, LOW);
}
break;
case 2:
//blue
if ((calcBlue >= shortOn && !ledStateBlue) || (calcBlue >= shortOff && ledStateBlue)) {
previousMillisBlue = millis();
ledStateBlue = !ledStateBlue;
digitalWrite(ledBlue, getState(ledStateBlue));
}
//orange
if ((calcOrange >= longOn && !ledStateOrange) || (calcOrange >= longOff && ledStateOrange)) {
previousMillisOrange = millis();
ledStateOrange = !ledStateOrange;
digitalWrite(ledOrange, getState(ledStateOrange));
digitalWrite(ledRed, LOW);
digitalWrite(buzzer, LOW);
}
break;
case 3:
//blue
if ((calcBlue >= shortOn && !ledStateBlue) || (calcBlue >= shortOff && ledStateBlue)) {
previousMillisBlue = millis();
ledStateBlue = !ledStateBlue;
digitalWrite(ledBlue, getState(ledStateBlue));
}
//red
if ((calcRed >= longOn && !ledStateRed) || (calcRed >= longOff && ledStateRed)) {
previousMillisRed = millis();
ledStateRed = !ledStateRed;
digitalWrite(ledRed, getState(ledStateRed));
digitalWrite(ledOrange, LOW);
digitalWrite(buzzer, LOW);
}
break;
case 4:
//orange
if ((calcOrange >= shortOn && !ledStateOrange) || (calcOrange >= shortOff && ledStateOrange)) {
previousMillisOrange = millis();
ledStateOrange = !ledStateOrange;
digitalWrite(ledBlue, LOW);
digitalWrite(ledOrange, getState(ledStateOrange));
digitalWrite(ledRed, LOW);
digitalWrite(buzzer, LOW);
}
break;
case 5:
//red
if ((calcRed >= shortOn && !ledStateRed) || (calcRed >= shortOff && ledStateRed)) {
previousMillisRed = millis();
ledStateRed = !ledStateRed;
digitalWrite(ledBlue, LOW);
digitalWrite(ledOrange, LOW);
digitalWrite(ledRed, getState(ledStateRed));
}
// buzzer
if (((millis() - previousMillisBuzzer >= 500 && buzzerState == true) || (millis() - previousMillisBuzzer >= 200 && buzzerState == false)) && pauseType == 0) {
previousMillisBuzzer = millis();
buzzerState = !buzzerState;
digitalWrite(buzzer, getState(buzzerState));
}
break;
default:
digitalWrite(ledBlue, LOW);
digitalWrite(ledOrange, LOW);
digitalWrite(ledRed, LOW);
break;
}
// RED LIGHT
if (state == 5 && pauseType != 2) {
digitalWrite(redLight, HIGH);
} else {
digitalWrite(redLight, LOW);
}
}
int getState(boolean state) {
if (state) {
return 1;
}
return 0;
}
void checkStopButton() {
if (pauseTill < millis()) {
pauseType = 0;
}
unsigned long buttonStart = millis();
while (digitalRead(stopButton) == LOW) {
if (millis() - buttonStart > 5000 && pauseType != 2) {
digitalWrite(buzzer, HIGH);
}
}
if (millis() - buttonStart > 5000 && pauseType != 2) {
pauseType = 2;
pauseTill = millis() + (12 * 60 * 60 * 1000L);
digitalWrite(buzzer, LOW);
} else if (millis() - buttonStart > 500 && pauseType == 0) {
pauseType = 1;
pauseTill = millis() + (30 * 60 * 1000L);
digitalWrite(buzzer, LOW);
}
}
void checkReset() {
// reset after 45 days of running
if (millis() > 4000000000L) {
resetFunc();
}
}