-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
776 lines (671 loc) · 18.3 KB
/
main.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//-----------------Libraries-----------------
//Libraries for WiFi
#include "WiFi.h"
//Libraries for MQTT Client
#include <PubSubClient.h>
//Libraries for Message Handling
#include <ArduinoJson.h>
#include <string.h>
//Libraries for I2C LCD Display
#include <LiquidCrystal_I2C.h>
//Libraries for SD Card
#include "FS.h"
#include "SD.h"
#include <SPI.h>
//Libraries for Temperature Sensor
#include <dht11.h>
//WiFi ADC2 Fix
#include <soc/sens_reg.h>
#include <bits/stdc++.h>
//----------------- Function Prototypes------
void mqtt_message(char *topic, byte *payload, unsigned int length);
//-----------------Variables-----------------
//WiFi
WiFiClient wifiClient;
String pass = "";
String ssid = "";
//MQTT Variables
String broker_port = "";
String broker_username = "";
String broker_address = "";
String broker_password = "";
String broker_subscription = "";
String broker_calibrate = "hub/calib/";
PubSubClient mqtt_client(wifiClient);
//Allocated Devices Pins
#define DEV_1 17 //1
#define DEV_2 16 //2
#define DEV_3 4 //5
#define DEV_6 0 //6
#define DEV_5 2 //5
#define DEV_7 15 //7
#define DEV_4 14 //4
#define DEV_8 12 //8
//Allocated Sensor Pins
#define CS_1 32
#define VS_1 33
#define TS_1 34
//SD CARD Variables
#define SD_MOSU 23
#define SD_CS 5
#define SD_MISO 19
#define SD_CLK 18
//LCD Display Variables
LiquidCrystal_I2C lcd(0x20, 8, 1);
//Tempature Sensor Variable
dht11 DHT11;
float temp_sense = 0.0;
//Main Loop Counter
uint32_t counter_lcd = 0;
uint8_t disp_counter = 1;
String a = WiFi.localIP().toString();
String ip_hold = "";
uint8_t period_counter = 0;
//Current Sensor Offset
float CS_1_OS = 0.0;
//Sensor Value Accumulators
float avg_curr = 0.0;
float avg_volt = 0.0;
float avg_temp = 0.0;
uint64_t prevPub = 0;
uint64_t prevSample = 0;
//-----------------Setup-----------------
void setup()
{
//LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("STARTUP");
//Serial
Serial.begin(9600);
Serial.println("Initilizing");
LCD_Print("Init...");
analogReadResolution(10);
analogSetAttenuation((adc_attenuation_t)2);
//SD Card
SD.begin(SD_CS);
while (!SD.begin(SD_CS) || (SD.cardType() == CARD_NONE))
{
if (SD.cardType() == CARD_NONE)
{
Serial.println("SD Card Error - Card Not Found");
LCD_Print("SD_Error");
delay(1000);
}
else
{
if (!SD.begin(SD_CS))
{
Serial.println("SD Card Error - Initilization Failed");
LCD_Print("SD_Error");
delay(1000);
}
}
}
LCD_Print("SD_Init");
Serial.println("Initializing SD Card.");
SD.mkdir("/devices");
SD.mkdir("/devices/dev");
SD.mkdir("/credentials");
SD.mkdir("/credentials/wifi");
SD.mkdir("/credentials/mqtt");
//Load Credentials
GetCredentials(SD);
String hold_str = "";
for (uint8_t i = 1; i < 9; i++)
{
//--------------------------Link Device to Device Pin---------------------------------------------
//---------File Name: Device Number
//---------File Contents: Device Pin
hold_str = "/devices/dev/" + String(i) + ".txt";
switch (i)
{
case 1:
writeFile(SD, hold_str.c_str(), String(DEV_1).c_str());
break;
case 2:
writeFile(SD, hold_str.c_str(), String(DEV_2).c_str());
break;
case 3:
writeFile(SD, hold_str.c_str(), String(DEV_3).c_str());
break;
case 4:
writeFile(SD, hold_str.c_str(), String(DEV_4).c_str());
break;
case 5:
writeFile(SD, hold_str.c_str(), String(DEV_5).c_str());
break;
case 6:
writeFile(SD, hold_str.c_str(), String(DEV_6).c_str());
break;
case 7:
writeFile(SD, hold_str.c_str(), String(DEV_7).c_str());
break;
case 8:
writeFile(SD, hold_str.c_str(), String(DEV_8).c_str());
break;
default:
break;
}
}
//Wifi
LCD_Print("WiFi_Ini");
Serial.println("Wifi Initializing");
WiFi.begin(ssid.c_str(), pass.c_str());
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.print("\n");
//MQTT
LCD_Print("MQTT_Ini");
Serial.println("MQTT Initializing");
mqtt_client.setServer(broker_address.c_str(), broker_port.toInt());
mqtt_client.setCallback(mqtt_message);
mqtt_reconnect();
//Relay
pinMode(DEV_1, OUTPUT);
pinMode(DEV_2, OUTPUT);
pinMode(DEV_3, OUTPUT);
pinMode(DEV_4, OUTPUT);
pinMode(DEV_5, OUTPUT);
pinMode(DEV_6, OUTPUT);
pinMode(DEV_7, OUTPUT);
pinMode(DEV_8, OUTPUT);
//Relay Off
Serial.println("Relays Off");
LCD_Print("RelayOff");
RelaySetAll(1);
//Calibrate Current Sensors
CalibrateCurrentSensor();
//Final
LCD_Print("Ready!__");
}
void loop()
{
mqtt_client.loop();
//---------------------------------------------------------------------------
//-------------------------Cycling Stats-------------------------------------
//---------------------------------------------------------------------------
if ((millis() - counter_lcd) > 10000)
{
counter_lcd = millis();
switch (disp_counter % 5)
{
case 1:
LCD_Print(String(avg_temp) + (char)223 + "C");
Serial.println("Internal Temperature = " + String(avg_temp) + (char)223 + "C");
break;
case 2:
a = WiFi.localIP().toString();
ip_hold = "";
period_counter = 0;
for (uint8_t i = 0; i < a.length(); i++)
{
if (String(a[i]) == String("."))
{
period_counter++;
}
if (period_counter >= 3)
{
ip_hold += String(a[i]);
}
}
LCD_Print("IP:*" + ip_hold);
Serial.println("Device IP:\t\t " + ip_hold);
break;
case 3:
LCD_Print(deviceState());
Serial.println("Device States:\t\t " + deviceState());
break;
case 4:
LCD_Print(broker_subscription);
Serial.println("Subscribed to:\t\t " + broker_subscription);
disp_counter = 0;
break;
default:
break;
}
disp_counter++;
}
//---------------------------------------------------------------------------
//-------------------------Data Sampling-------------------------------------
//---------------------------------------------------------------------------
if ((millis() - prevSample) > 10)
{
prevSample = millis();
DataSampling();
}
if ((millis() - prevPub) > 1000)
{
prevPub = millis();
dataPush();
}
}
/*
Converts the states of all devices relay pins, and converts them into a String
return - String of enabled relays.
*/
String deviceState()
{
String states = "";
uint8_t stat = 0;
if (digitalRead(DEV_1) == stat)
{
states += "X";
}
else
{
states += "_";
}
if (digitalRead(DEV_2) == stat)
{
states += "X";
}
else
{
states += "_";
}
if (digitalRead(DEV_3) == stat)
{
states += "X";
}
else
{
states += "_";
}
if (digitalRead(DEV_4) == stat)
{
states += "X";
}
else
{
states += "_";
}
if (digitalRead(DEV_5) == stat)
{
states += "X";
}
else
{
states += "_";
}
if (digitalRead(DEV_6) == stat)
{
states += "X";
}
else
{
states += "_";
}
if (digitalRead(DEV_7) == stat)
{
states += "X";
}
else
{
states += "_";
}
if (digitalRead(DEV_8) == stat)
{
states += "X";
}
else
{
states += "_";
}
return states;
}
/*
Control the Power State of the Relay
device - Hub device number to control.
return - None.
Note:
The device number used is the non-unique number.
Not the device's ID.
*/
void RelayOn(uint8_t device)
{
uint8_t pin = getDevicePin(device);
digitalWrite(pin, 0);
Serial.print("DEV");
Serial.print(device);
Serial.println("-ON_");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DEV");
lcd.print(device);
lcd.print("-ON_");
}
void RelayOff(uint8_t device)
{
uint8_t pin = getDevicePin(device);
digitalWrite(pin, 1);
Serial.print("DEV");
Serial.print(device);
Serial.println("-OFF");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DEV");
lcd.print(device);
lcd.print("-OFF");
}
/*
Control Print on LCD Display
message - Message to Print
return - None.
*/
void LCD_Print(String message)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
}
/*
Get Credentials of WiFi and MQTT
fs - SD Card
return - None.
Note:
Relevant credentials must be placed in the following locations
MQTT Username - /credentials/mqtt/user.txt
MQTT Password - /credentials/mqtt/pass.txt
MQTT Broker Address - /credentials/mqtt/broker.txt
MQTT Subscribed Channel - /credentials/mqtt/subscribe.txt
WiFi SSID - /credentials/wifi/ssid.txt
WiFi Password - /credentials/wifi/pass.txt
*/
void GetCredentials(fs::FS &fs)
{
//Wifi
ssid = ReadData(String("/credentials/wifi/ssid.txt"));
pass = ReadData(String("/credentials/wifi/pass.txt"));
//MQTT
broker_username = ReadData(String("/credentials/mqtt/user.txt"));
broker_password = ReadData(String("/credentials/mqtt/pass.txt"));
broker_address = ReadData(String("/credentials/mqtt/broker.txt"));
broker_subscription = ReadData(String("/credentials/mqtt/subscribe.txt"));
broker_port = ReadData(String("/credentials/mqtt/port.txt"));
broker_calibrate = "hub/calib/" + ReadData("/credentials/mqtt/id.txt");
Serial.println("Credentials Loaded");
}
/*
Setup the WiFi connection
return - None.
*/
void WiFi_Connect()
{
WiFi.begin(ssid.c_str(), pass.c_str());
while (WiFi.status() != WL_CONNECTED)
{
delay(10);
}
}
/*
Transmits MQTT Message
subscription - Topic of published message
JSON_Buffer - JSON Buffer to transmit
return - None.
*/
void mqtt_publish(StaticJsonDocument<300> JSON_Buffer, String subscription)
{
char Payload_Char[300];
size_t n = serializeJson(JSON_Buffer, Payload_Char);
if (mqtt_client.publish(subscription.c_str(), Payload_Char, n) == true)
{
}
else
{
mqtt_reconnect();
Serial.println("Failed to Send Message.");
}
}
/*
MQTT Message Handler Function
topic - Topic of recieved message
payload - message recieved. This is in a JSON format, and will need to be manually managed.
length - length of payload.
return - None.
*/
void mqtt_message(char *topic, byte *payload, unsigned int length)
{
String hub_id = ReadData("/credentials/mqtt/id.txt");
String hub_calibration = "hub/calib/" + hub_id;
String payload_str;
StaticJsonDocument<500> jsonBuffer;
DeserializationError error = deserializeJson(jsonBuffer, payload);
//Array [Device Number][Device ID]
uint8_t hold_val = 0;
StaticJsonDocument<300> Payload_JSON;
char Payload_Char[300];
String broker_calibration = "";
if (error)
{
Serial.println(error.c_str());
return;
}
//=================================================================Process Payload
const char *element_1 = jsonBuffer["0"]; //IP ADDRESS
const char *element_2 = jsonBuffer["1"]; //HUB ID | Defined by Server
const char *element_3 = jsonBuffer["2"]; //Hub Pin | Device Pin
const char *element_4 = jsonBuffer["3"]; //State | State
//=================================================================Change Hub ID
if ((String(topic) == "hub/init") && (WiFi.localIP().toString() == String(element_1)))
{
payload_str = (String) "hub/" + String(element_2);
mqtt_client.subscribe(payload_str.c_str());
mqtt_client.unsubscribe(broker_subscription.c_str());
String hold_calibrate = "hub/calib/" + String(element_2);
mqtt_client.subscribe(hold_calibrate.c_str());
mqtt_client.unsubscribe(broker_calibrate.c_str());
//Serial Print
Serial.print("Subscibed to: ");
Serial.println(payload_str);
Serial.print("Unsubscribed from: ");
Serial.println(broker_subscription);
broker_calibrate = hold_calibrate;
broker_subscription = payload_str;
writeFile(SD, "/credentials/mqtt/subscribe.txt", payload_str.c_str());
writeFile(SD, "/credentials/mqtt/id.txt", String(element_2).c_str());
}
//=================================================================Manage Device
else if ((String(topic) == broker_subscription) && (WiFi.localIP().toString() == String(element_1)))
{
//Check if wantingto calibrate
//Calibration requres State = X | Device = 0
if (String(element_3[0]) == String(0))
{
Serial.println("");
}
//Control Device State
//Device Control requres State = 1 or 0 | Device = [1 - 8]
else if ((String(element_4[0]) == String(1)) && ((String(element_3[0]) > String(0)) && (String(element_3[0]) < String(9))))
{
RelayOn(String(element_3[0]).toInt());
}
else if ((String(element_4[0]) == String(0)) && ((String(element_3[0]) > String(0)) && (String(element_3[0]) < String(9))))
{
RelayOff(String(element_3[0]).toInt());
}
}
}
/*
MQTT Connect Client
return - None.
*/
void mqtt_reconnect()
{
while (!mqtt_client.connected())
{
String ClientID = String(random(0xffff), HEX);
if (mqtt_client.connect(ClientID.c_str(), broker_username.c_str(), broker_password.c_str()))
{
mqtt_client.subscribe("hub/init");
mqtt_client.subscribe(broker_subscription.c_str());
mqtt_client.subscribe(broker_calibrate.c_str());
}
else
{
delay(5000);
Serial.print("Attempting MQTT connection...\n");
}
}
}
/*
Writes information to SD Card
fs - SD Card handle
path - path directory + extension
message - Data to be writent to file
return - None.
*/
void writeFile(fs::FS &fs, const char *path, const char *message)
{
File file = fs.open(path, FILE_WRITE);
if (!file)
{
Serial.println("Failed to Open File");
return;
}
if (file.print(message))
{
Serial.print("Successfully Created - ");
Serial.println(path);
}
else
{
Serial.print("Creation Failed - ");
Serial.println(path);
}
file.close();
}
/*
Reads Information from a defined file
path - path directory + extension
return - Data inside file.
*/
String ReadData(String path)
{
File file = SD.open(path.c_str());
String hold_str = "";
if (!file)
{
Serial.println("Failed to open file for reading - " + path);
}
while (file.available())
{
hold_str += (char)file.read();
}
file.close();
return String(hold_str);
}
/*
Dynamically updates rolling average
mean - Current average
newVal - Newly measured value
return - New updated average.
*/
float rollAverage(float mean, uint16_t newVal)
{
uint8_t N = 100;
mean -= (float)mean / N;
mean += (float)newVal / N;
return mean;
}
/*
Samples data (temperature / voltage / current)
return - Updated rolling averages for all measurements.
*/
void DataSampling()
{
prevSample = millis();
uint16_t Curr = analogRead(CS_1);
uint16_t Volt = analogRead(VS_1);
uint16_t Temp = (float)DHT11.temperature;
avg_curr = rollAverage(avg_curr, Curr);
avg_volt = rollAverage(avg_volt, Volt);
avg_temp = rollAverage(avg_temp, Temp);
}
/*
Pushes data to MQTT
return - Published MQTT Data
*/
void dataPush()
{
StaticJsonDocument<300> Payload_JSON;
char Payload_Char[300];
float Vref = 3.3;
float buffSize = 1023.0;
String subscription = "sensors/" + ReadData("/credentials/mqtt/id.txt");
//Publishing Payload
Payload_JSON["ID"] = (ReadData("/credentials/mqtt/id.txt")).toInt();
Payload_JSON["CURR"] = (float)(CS_1_OS - avg_curr) * (Vref / buffSize) / 0.066;
Payload_JSON["VOLT"] = (float)avg_volt * (Vref / buffSize);
Payload_JSON["TEMP"] = (float)avg_temp;
mqtt_publish(Payload_JSON, subscription);
prevPub = millis();
}
/*
Calibrates Current Sensor, and gets an offset value
*/
void CalibrateCurrentSensor()
{
delay(5000);
float acc = 0.0; //Accumulator
Serial.println("Calibrating Sensors...");
LCD_Print("Calib...");
RelaySetAll(1);
Serial.println("Filling Rolling Average Buffers..");
for (uint16_t i = 0; i < 50000; i++)
{
DataSampling();
}
Serial.println("Finding Current Offset..");
delay(1000);
for (uint16_t i = 0; i < 10000; i++)
{
acc += analogRead(CS_1);
if (i % 100 == 0)
{
Serial.print(".");
}
}
Serial.print("\n");
CS_1_OS = (float)acc / 10000.0;
LCD_Print("Complete");
Serial.println("Calibration Complete.");
Serial.println("Values Found: " + String(CS_1_OS));
}
/*
Test Relays
*/
void RelaySetAll(uint8_t state)
{
if (state == 1)
{
Serial.println("All Outlets Off");
LCD_Print("All_OFF_");
}
else
{
Serial.println("All Outlets On");
LCD_Print("All_ON__");
}
digitalWrite(DEV_1, state);
digitalWrite(DEV_2, state);
digitalWrite(DEV_3, state);
digitalWrite(DEV_4, state);
digitalWrite(DEV_5, state);
digitalWrite(DEV_6, state);
digitalWrite(DEV_7, state);
digitalWrite(DEV_8, state);
}
int8_t getDevicePin(uint8_t device)
{
//Pin Linked to Device Number
String path = "/devices/dev/" + String(device) + ".txt";
String hold_str = ReadData(path);
return (int8_t)hold_str.toInt();
}
//------END