-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome_thing.ino
607 lines (514 loc) · 15 KB
/
home_thing.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <vector>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define SPEAKER_PIN D5
#define WIFI_SSID "Dialog 4G 544"
#define WIFI_PASS "5d1ddCFD"
#define OFF_BTN D6
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char *ssid = WIFI_SSID;
const char *password = WIFI_PASS;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
// Week Days
String weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Month names
String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int Speaker = SPEAKER_PIN; // GPIO13
WiFiServer server(80);
bool isRingingAlarm = false;
void renderViews()
{
if (!isRingingAlarm)
{
display.stopscroll();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
time_t epochTime = timeClient.getEpochTime();
display.print(weekDays[timeClient.getDay()]);
display.print(", ");
struct tm *ptm = gmtime((time_t *)&epochTime);
display.print(months[ptm->tm_mon]);
display.print(" ");
display.print(timeClient.getDay());
display.println();
display.println();
// Display time
display.setTextSize(4);
display.print(timeClient.getHours() % 12 == 0 ? 12 : timeClient.getHours() % 12);
display.print(":");
display.print(timeClient.getMinutes());
display.display();
}
else
{
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.setTextSize(3);
display.print("ALARM");
display.display();
}
}
struct Alarm
{
unsigned int hour;
unsigned int minute;
unsigned int weekend;
};
std::vector<Alarm> alarms;
void sendAlarmsToServer(const String &json)
{
WiFiClient client;
HTTPClient http;
if (http.begin(client, "http://home-s1.tronic247.com/"))
{
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
client->setInsecure();
http.addHeader("Content-Type", "application/json");
// add valid headers such as user agent
int httpCode = http.POST(json);
if (httpCode == HTTP_CODE_OK)
{
Serial.println("Alarms saved successfully");
}
else
{
Serial.print("Failed to save alarms. Error code: ");
Serial.println(httpCode);
}
http.end();
}
else
{
Serial.println("Failed to connect to server");
}
}
String serializeAlarms()
{
StaticJsonDocument<512> doc;
JsonArray alarmsJson = doc.createNestedArray("alarms");
for (const Alarm &alarm : alarms)
{
JsonObject alarmJson = alarmsJson.createNestedObject();
alarmJson["hour"] = alarm.hour;
alarmJson["minute"] = alarm.minute;
alarmJson["weekend"] = alarm.weekend;
}
String json;
serializeJson(doc, json);
return json;
}
void saveAlarms()
{
String json = serializeAlarms();
sendAlarmsToServer(json);
}
void loadAlarms()
{
WiFiClient client;
HTTPClient http;
if (http.begin(client, "http://home-s1.tronic247.com/"))
{
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
{
String json = http.getString();
StaticJsonDocument<512> doc;
deserializeJson(doc, json);
JsonArray alarmsJson = doc["alarms"];
Serial.println("Alarms loaded successfully");
Serial.println(json);
for (JsonObject alarmJson : alarmsJson)
{
Alarm alarm;
alarm.hour = alarmJson["hour"];
alarm.minute = alarmJson["minute"];
alarm.weekend = alarmJson["weekend"];
alarms.push_back(alarm);
}
}
else
{
Serial.print("Failed to load alarms. Error code: ");
Serial.println(httpCode);
}
http.end();
}
else
{
Serial.println("Failed to connect to server");
}
}
void addAlarm(unsigned int hour, unsigned int minute, unsigned int weekend = 0)
{
Alarm alarm;
alarm.hour = hour;
alarm.minute = minute;
alarm.weekend = weekend;
alarms.push_back(alarm);
saveAlarms();
}
void deleteAlarm(unsigned int hour, unsigned int minute)
{
unsigned int alarmId = 0;
for (const Alarm &alarm : alarms)
{
if (alarm.hour == hour && alarm.minute == minute)
{
alarms.erase(alarms.begin() + alarmId);
break;
}
alarmId++;
}
saveAlarms();
}
unsigned int lastPlayedAlarmId = 0;
void checkAlarms()
{
if (!isRingingAlarm)
{
for (const Alarm &alarm : alarms)
{
int alarmTime = alarm.hour * 60 + alarm.minute;
int currentDay = timeClient.getDay();
bool isWeekend = (currentDay == 0 || currentDay == 6);
if (alarm.hour == timeClient.getHours() && alarm.minute == timeClient.getMinutes() && alarmTime != lastPlayedAlarmId &&
((alarm.weekend == 1 && isWeekend)))
{
isRingingAlarm = true;
lastPlayedAlarmId = alarmTime;
break; // Exit the loop after the first alarm is found
}
}
}
}
const unsigned long toneDuration = 800; // Duration of each tone in milliseconds
const unsigned long pauseDuration = 100; // Duration of pause between tones in milliseconds
unsigned long previousTime = 0;
int toneCounter = 0;
void alarm_music()
{
unsigned long currentTime = millis();
if (isRingingAlarm)
{
if (currentTime - previousTime >= toneDuration)
{
previousTime = currentTime;
if (toneCounter % 2 == 0)
{
tone(SPEAKER_PIN, 600); // Play tone
}
else
{
noTone(SPEAKER_PIN); // Silence
}
toneCounter++;
}
}
}
void setup()
{
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display();
delay(1000);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextColor(WHITE);
display.setTextSize(1);
// display.setRotation(2);
display.println("Loading...");
display.display();
delay(2000);
/**
* Connect to the internet
*/
WiFi.begin(ssid, password);
/*Keep trying to connect to the internet until it is connected*/
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
/**
* Connect to the NTP server
*/
timeClient.begin();
timeClient.setTimeOffset(19800);
timeClient.forceUpdate();
/**
* Server start
*/
server.begin();
display.clearDisplay();
display.setTextColor(WHITE);
display.display();
/**
* Pinmodes
*/
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(OFF_BTN, INPUT);
/**
* Alarm stuff
*/
loadAlarms();
}
void loop()
{
/*
Todo: figure out a more efficient way to do this
*/
alarm_music();
checkAlarms();
renderViews();
if (digitalRead(OFF_BTN) == HIGH)
{
isRingingAlarm = false;
noTone(SPEAKER_PIN);
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}
while (!client.available())
{
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
client.flush();
if (request.indexOf("/ALARM") != -1)
{
noTone(SPEAKER_PIN); // Silence
isRingingAlarm = true;
}
/**
* If request has /ADDALARM/HH/MM/W
*/
if (request.indexOf("/ADDALARM/") != -1)
{
int hourIndex = request.indexOf("/ADDALARM/") + 10;
int hour = request.substring(hourIndex, hourIndex + 2).toInt();
int minuteIndex = hourIndex + 3;
int minute = request.substring(minuteIndex, minuteIndex + 2).toInt();
int weekendIndex = minuteIndex + 3;
int weekend = request.substring(weekendIndex, weekendIndex + 1).toInt();
addAlarm(hour, minute, weekend);
}
/**
* If request has /DELETEALARM/HH/MM
*/
if (request.indexOf("/DELETEALARM/") != -1)
{
int hourIndex = request.indexOf("/DELETEALARM/") + 13;
int hour = request.substring(hourIndex, hourIndex + 2).toInt();
int minuteIndex = hourIndex + 3;
int minute = request.substring(minuteIndex, minuteIndex + 2).toInt();
deleteAlarm(hour, minute);
}
/**
* If request is ALARMS
*/
if (request.indexOf("/JSON") != -1)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("{\"running_for\": " + String(millis() / 1000) + ", \"alarms\": ");
client.println(serializeAlarms());
client.println("}");
}
else if (request.indexOf("/STOP") != -1)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("1");
ESP.restart();
}
else
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println(R"html(
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<div class="m-auto max-w-2xl shadow-xl border rounded-lg px-6 py-4 mt-8">
<button class="bg-blue-500 border border-transparent text-white py-2 px-4 rounded transition-all hover:bg-blue-700 active:bg-blue-800" id="trigger">Manually Trigger the alarm</button>
<button class="border border-red-500 text-red-600 py-2 px-4 rounded transition-all hover:bg-red-100 active:bg-red-200" id="stop">Restart</button>
<button class="border border-transparent bg-gray-500 text-white py-2 px-4 rounded transition-all hover:bg-gray-600 active:bg-gray-700" id="loadLogs">Load Logs</button>
<br /><br />
<h1 class="text-2xl font-semibold mb-2">Add Alarm</h1>
<div class="flex items-center space-x-2">
<div>
<label for="HH" class="sr-only">HH</label>
<input type="number" class="border focus:ring border-gray-300 rounded-md outline-none px-4 py-2" id="HH" />
</div>
<div>
<label for="MM" class="sr-only">MM</label>
<input type="number" class="border focus:ring border-gray-300 rounded-md outline-none px-4 py-2" id="MM" />
</div>
<div>
<label for="MM" class="sr-only">Weekends?</label>
<input type="checkbox" class="border focus:ring border-gray-300 rounded-md outline-none px-4 py-2" id="ONWEEKENDS" />
</div>
<div>
<button type="submit" class="bg-blue-500 border border-transparent text-white py-2 px-4 rounded transition-all hover:bg-blue-700 active:bg-blue-800" onclick="addAlarm()">
Add Alarm
</button>
</div>
</div>
<h2 class="text-2xl font-semibold my-2">Current Alarms</h2>
<div id="alarms"></div>
<h2 class="text-2xl font-semibold my-2">Log</h2>
<pre id="LOG"></pre>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<script>
function addLog(...a){
$("#LOG").append(a.join(" ") + "\n");
}
addLog("Started");
$("#trigger").click(function () {
let t = $(this);
$(this).attr("disabled", true);
$.ajax({
url: "/ALARM",
//when the request is done
complete: function () {
setTimeout(function () {
t.attr("disabled", false);
}, 800);
addLog("Triggered");
},
});
});
$("#stop").click(function () {
let t = $(this);
$(this).attr("disabled", true);
$.ajax({
url: "/STOP",
});
addLog("Stopped");
let i = 1;
const interval = setInterval(function () {
t.text(`Reconnecting (${i++}s)`);
$.ajax({
url: "/",
complete: function (data) {
clearInterval(interval);
t.attr("disabled", false);
t.text("Stop");
},
});
}, 1000);
});
function addAlarm() {
let HH = +$("#HH").val();
let MM = +$("#MM").val();
//add 0 if HH or MM is less than 10
if (HH < 10) {
HH = "0" + HH;
}
if (MM < 10) {
MM = "0" + MM;
}
$.ajax({
url: `/ADDALARM/${HH}/${MM}/${$("#ONWEEKENDS").is(":checked") ? 1 : 0}`,
complete: function () {
window.location.reload();
},
});
}
$("#loadLogs").click(function(){
$.ajax({
url: "/JSON",
complete: function(data){
addLog("Loaded Logs");
const blob = new Blob([data.responseText], {type: "text/json"});
window.open(URL.createObjectURL(blob), "Logs", "width=600,height=400,scrollbars=yes");
}
});
});
const alarms = [
)html");
for (const Alarm &alarm : alarms)
{
client.print("[");
client.print(alarm.hour);
client.print(",");
client.print(alarm.minute);
client.print(",");
client.print(alarm.weekend);
client.print("],");
}
client.println(R"html(
];
alarms.forEach((alarm) => {
$("#alarms").append(
`<div class="flex items-center space-x-2 mb-3">
<div>
<label for="staticEmail2" class="sr-only">HH</label>
<input type="number" readonly class="border border-gray-300 rounded-md px-4 py-2" id="staticEmail2" value="${alarm[0]}" />
</div>
<div>
<label for="inputPassword2" class="sr-only">MM</label>
<input type="number" readonly class="border border-gray-300 rounded-md px-4 py-2" id="inputPassword2" value="${alarm[1]}" />
</div>
<div>
${alarm[2] == 1 ? '<span class="text-green-500">Also on weekends</span>' : ""}
<button type="submit" class="bg-red-500 border border-transparent text-white py-2 px-4 rounded transition-all hover:bg-red-700 active:bg-red-800" onclick="deleteAlarm(${alarm[0]}, ${alarm[1]})">
Delete
</button>
</div>
</div>`
);
});
function deleteAlarm(HH, MM) {
//add 0 if HH or MM is less than 10
if (HH < 10) {
HH = "0" + HH;
}
if (MM < 10) {
MM = "0" + MM;
}
$.ajax({
url: `/DELETEALARM/${HH}/${MM}`,
complete: function () {
window.location.reload();
},
});
}
</script>
</html>
)html");
}
}