-
Notifications
You must be signed in to change notification settings - Fork 6
/
AlarmSystem.ino
1749 lines (1516 loc) · 70.8 KB
/
AlarmSystem.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************************************************************************************
* File name : AlarmSystem.ino
* Compiler :
* Autor : Vigasan
* Created : 13/02/2023
* Modified :
* Last modified :
*
*
* Description :
*
* Other info : Alarm System Application for ESP32
**************************************************************************************************************************************************/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------Include Files----------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
#include <WiFi.h>
#include <PubSubClient.h> // Remember to edit this file and change MQTT_MAX_PACKET_SIZE from default 256 to 600
#include <ArduinoJson.h>
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------Local definitions------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
#define PERIOD_MILLSEC_1000 1000
#define PERIOD_MILLSEC_500 500
#define PERIOD_MILLSEC_250 250
#define DELTA_VOLTAGE 0.05
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// COMMUNICATION PROTOCOL DEFINES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define LODWORD(l) (*( (unsigned long*)(&l)))
#define HIDWORD(l) (*( ( (unsigned long*) (&l) ) + 1 ))
#define MAKEDWORD(hi,lo) ((unsigned long)(((unsigned long)(lo)) | (((unsigned long)(hi))<<16)))
//Word manipulation
#define LOWORD(l) (*( (word*)(&l)))
#define HIWORD(l) (*( ( (word*) (&l) ) + 1 ))
#define MAKEWORD(hi,lo) ((word)(((word)(lo)) | (((word)(hi))<<8)))
//Byte manipulation
#define LOBYTE(w) (*( (byte*) (&w)))
#define HIBYTE(w) (*( ( (byte*) (&w) ) + 1 ))
#define UPBYTE(w) (*( ( (byte*) (&w) ) + 2 ))
#define MSBYTE(w) (*( ( (byte*) (&w) ) + 3 ))
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PROTOCOL FRAME DEFINES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define GPL_ST_IDLE 0
#define GPL_ST_CMD 1
#define GPL_ST_NUM_BYTE 2
#define GPL_ST_DATA 3
#define GPL_ST_CHECKSUM 4
#define GPL_START_FRAME 0x8A
#define GPL_ESCAPE_CHAR 0x8B
#define GPL_XOR_CHAR 0x20
#define GPL_CMD_TIMEOUT 1000 // Frame Timeout
#define GPL_BYTE_SOF 0
#define GPL_BYTE_CMD 1
#define GPL_BYTE_LENGTH 2
#define GPL_BYTE_FIRST_DATA 3
#define GPL_NUM_EXTRA_BYTES 4
#define LEN_IN_Serial2_BUFFER 100
#define LEN_OUT_Serial2_BUFFER 30
#define LEN_OUT_FRAME_BUFFER 30
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CMD ID FOR PROTOCOL DATA EXCHANGE
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------ Global CMDs ---------------------------------------------------
#define CMD_CONFIRM 0
#define CMD_IS_BOOTLOADER 1
#define CMD_PROGRAM_VERSION 2
#define CMD_GOTO_BOOTLOADER 3
#define CMD_DEVICE_ID 4
#define CMD_DELAYED_OPERATION 5
#define CMD_HW_VERSION 6
#define CMD_QUIT_BOOTLOADER 7
#define CMD_FLASH_DATA 8
#define CMD_EEPROM_DATA 9
//------------------------------------------------------ Application Specific CMD s--------------------------------------
#define CMD_GET_AIN1 10
#define CMD_GET_AIN2 11
#define CMD_GET_AIN3 12
#define CMD_GET_AIN4 13
#define CMD_GET_AIN5 14
#define CMD_GET_AIN6 15
#define CMD_GET_AIN7 16
#define CMD_GET_AIN8 17
#define CMD_GET_DIN1 18
#define CMD_GET_DIN2 19
#define CMD_GET_DIN3 20
#define CMD_GET_DIN4 21
#define CMD_SET_THR_AIN1 22
#define CMD_GET_THR_AIN1 23
#define CMD_SET_THR_AIN2 24
#define CMD_GET_THR_AIN2 25
#define CMD_SET_THR_AIN3 26
#define CMD_GET_THR_AIN3 27
#define CMD_SET_THR_AIN4 28
#define CMD_GET_THR_AIN4 29
#define CMD_SET_THR_AIN5 30
#define CMD_GET_THR_AIN5 31
#define CMD_SET_THR_AIN6 32
#define CMD_GET_THR_AIN6 33
#define CMD_SET_THR_AIN7 34
#define CMD_GET_THR_AIN7 35
#define CMD_SET_THR_AIN8 36
#define CMD_GET_THR_AIN8 37
#define CMD_GET_INPUTS 38
#define CMD_SET_AUX_POWER 39
#define CMD_GET_AUX_POWER 40
#define CMD_SET_TEST_BATTERY 41
#define CMD_GET_TEST_BATTERY 42
#define CMD_SET_RELAY2 43
#define CMD_GET_RELAY2 44
#define CMD_SET_RELAY3 45
#define CMD_GET_RELAY3 46
#define CMD_SET_RELAY4 47
#define CMD_GET_RELAY4 48
#define CMD_SET_RELAY5 49
#define CMD_GET_RELAY5 50
#define CMD_SET_SYSTEM_STATUS 51
#define CMD_GET_SYSTEM_STATUS 52
#define CMD_SET_GUI_STATUS 53
#define CMD_GET_V_EXT 54
#define CMD_GET_V_BATT 55
#define CMD_GET_V_BOARD 56
#define CMD_SET_BUS_POWER 57
#define CMD_GET_BUS_POWER 58
#define CMD_SET_DATE_TIME 59
#define CMD_GET_DATE_TIME 60
#define CMD_INIT_SYSTEM 61
#define CMD_EXT_POWER_STATUS 62
#define CMD_OUTPUTS 63
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------MQTT DISCOVERY PARAMETERS----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
const char* g_ssid = "Wifi Name"; // Wifi SSID Name
const char* g_password = "Wifi Passoword"; // Wifi Password
const char* g_mqtt_server = "192.168.1.25"; // IP Address of MQTT Broker (Maybe same of Home Assistant)
const char* g_mqttUser = "mosquitto"; // MQTT Broker User
const char* g_mqttPsw = "psw"; // MQTT Broker Password
int g_mqttPort = 1883; // MQTT Broker Port
const char* g_mqtt_DeviceName = "IOTMB_Alarm"; // Your Device Name
String g_TopicState_ain1 = "IOTMB_Alarm/ain1/state"; // State Topic for entity input ain1
String g_TopicState_ain2 = "IOTMB_Alarm/ain2/state"; // State Topic for entity input ain2
String g_TopicState_ain3 = "IOTMB_Alarm/ain3/state"; // State Topic for entity input ain3
String g_TopicState_ain4 = "IOTMB_Alarm/ain4/state"; // State Topic for entity input ain4
String g_TopicState_ain5 = "IOTMB_Alarm/ain5/state"; // State Topic for entity input ain5
String g_TopicState_ain6 = "IOTMB_Alarm/ain6/state"; // State Topic for entity input ain6
String g_TopicState_ain7 = "IOTMB_Alarm/ain7/state"; // State Topic for entity input ain7
String g_TopicState_ain8 = "IOTMB_Alarm/ain8/state"; // State Topic for entity input ain8
String g_TopicState_din1 = "IOTMB_Alarm/din1/state"; // State Topic for entity input din1
String g_TopicState_din2 = "IOTMB_Alarm/din2/state"; // State Topic for entity input din2
String g_TopicState_din3 = "IOTMB_Alarm/din3/state"; // State Topic for entity input din3
String g_TopicState_din4 = "IOTMB_Alarm/din4/state"; // State Topic for entity input din4
String g_TopicRelay1 = "IOTMB_Alarm/relay1"; // Topic for entity relay1
String g_TopicRelay2 = "IOTMB_Alarm/relay2"; // Topic for entity relay2
String g_TopicRelay3 = "IOTMB_Alarm/relay3"; // Topic for entity relay3
String g_TopicRelay4 = "IOTMB_Alarm/relay4"; // Topic for entity relay4
String g_TopicRelayBell = "IOTMB_Alarm/relayBell"; // Topic for entity relayBell (Alarm Siren)
String g_TopicSwitchBus = "IOTMB_Alarm/switchBus"; // Topic for entity switch Power Bus
String g_TopicSwitchTestBattery = "IOTMB_Alarm/switchTestBatt"; // Topic for entity switch Test Battery
String g_TopicPowerState = "IOTMB_Alarm/power/state"; // State Topic for entity Power (Check for blackout)
String g_TopicVoltBattery = "IOTMB_Alarm/battery/state"; // State Topic for entity Battery
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------Generic global variables-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
WiFiClient g_WiFiClient;
PubSubClient g_mqttPubSub(g_WiFiClient);
int g_mqttCounterConn = 0;
String g_UniqueId;
bool g_InitSystem = true;
byte g_InputBuffer[LEN_IN_Serial2_BUFFER];
byte g_OutputBuffer[LEN_OUT_Serial2_BUFFER];
byte g_FrameBuffer[LEN_OUT_FRAME_BUFFER];
byte dataReceived = 0;
byte gplStatus = GPL_ST_IDLE;
byte xored = 0x00;
byte checkSum;
int numByte = 0, i = 0;
double g_VoltagePowerSupply = 0.0;
double g_VoltageBoard = 0.0;
double g_VoltageBattery = 0.0;
word g_Inputs = 0;
byte g_Outputs = 0;
double g_oldVoltagePowerSupply = 1.0;
double g_oldVoltageBoard = 1.0;
double g_oldVoltageBattery = 1.0;
word g_oldInputs = 1;
byte g_oldOutputs = 1;
int g_canPublish = 0;
double delta = 0.0;
unsigned long g_Time = 0;
word g_aIn1 = 0;
word g_old_aIn1 = 0xFFFF;
word g_aIn2 = 0;
word g_old_aIn2 = 0xFFFF;
word g_aIn3 = 0;
word g_old_aIn3 = 0xFFFF;
word g_aIn4 = 0;
word g_old_aIn4 = 0xFFFF;
word g_aIn5 = 0;
word g_old_aIn5 = 0xFFFF;
word g_aIn6 = 0;
word g_old_aIn6 = 0xFFFF;
word g_aIn7 = 0;
word g_old_aIn7 = 0xFFFF;
word g_aIn8 = 0;
word g_old_aIn8 = 0xFFFF;
word g_dIn1 = 0;
word g_old_dIn1 = 0xFFFF;
word g_dIn2 = 0;
word g_old_dIn2 = 0xFFFF;
word g_dIn3 = 0;
word g_old_dIn3 = 0xFFFF;
word g_dIn4 = 0;
word g_old_dIn4 = 0xFFFF;
byte g_Relay1 = 0;
byte g_oldRelay1 = 0xFF;
byte g_Relay2 = 0;
byte g_oldRelay2 = 0xFF;
byte g_Relay3 = 0;
byte g_oldRelay3 = 0xFF;
byte g_Relay4 = 0;
byte g_oldRelay4 = 0xFF;
byte g_RelayBell = 0;
byte g_oldRelayBell = 0xFF;
byte g_SwitchBus = 0;
byte g_oldSwitchBus = 0xFF;
byte g_SwitchTestBattery = 0;
byte g_oldSwitchTestBattery = 0xFF;
String g_PowerStatus = "OFF";
String g_oldPowerStatus = "ON";
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------ SETUP ----------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
void setup()
{
Serial.begin(115200);
Serial2.begin(9600);
delay(100); //Take some time to open up the Serial2 Monitor
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Configurazione I/O
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Serial.println("");
Serial.println("");
Serial.println("-------- IOTMB ALARM ----------");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Wifi Init
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
setup_wifi();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MQTT Init
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
g_mqttPubSub.setServer(g_mqtt_server, g_mqttPort);
g_mqttPubSub.setCallback(MqttReceiverCallback);
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------ LOOP -----------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
void loop()
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MQTT Connection
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(WiFi.status() == WL_CONNECTED)
{
if(!g_mqttPubSub.connected())
MqttReconnect();
else
g_mqttPubSub.loop();
} else
{
Serial.println("WiFi NOT connected!!!");
setup_wifi();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MQTT Discovery Init
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(g_InitSystem)
{
delay(1000);
g_InitSystem = false;
MqttHomeAssistantDiscovery();
delay(1000);
Serial.println("INIT BOARD...");
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_INIT_SYSTEM, 0);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Serial2 - Communication with ATMega2560
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (Serial2.available() > 0)
{
dataReceived = (byte)Serial2.read();
if(dataReceived == GPL_ESCAPE_CHAR)
{
xored = GPL_XOR_CHAR;
} else
{
dataReceived ^= xored;
xored = 0x00;
switch(gplStatus)
{
case GPL_ST_IDLE:
{
if(dataReceived == GPL_START_FRAME)
{
i = 0;
g_InputBuffer[i++] = dataReceived;
checkSum = dataReceived;
gplStatus = GPL_ST_CMD;
}
} break;
case GPL_ST_CMD:
{
g_InputBuffer[i++] = dataReceived;
checkSum += dataReceived;
gplStatus = GPL_ST_NUM_BYTE;
} break;
case GPL_ST_NUM_BYTE:
{
numByte = dataReceived;
if(numByte > 0)
{
g_InputBuffer[i++] = dataReceived;
checkSum += dataReceived;
gplStatus = GPL_ST_DATA;
} else
{
gplStatus = GPL_ST_IDLE;
}
} break;
case GPL_ST_DATA:
{
g_InputBuffer[i++] = dataReceived;
checkSum += dataReceived;
if(--numByte == 0)
gplStatus = GPL_ST_CHECKSUM;
} break;
case GPL_ST_CHECKSUM:
{
if(dataReceived == checkSum)
{
gplStatus = GPL_ST_IDLE;
g_InputBuffer[i++] = dataReceived;
switch(g_InputBuffer[GPL_BYTE_CMD])
{
case CMD_GET_INPUTS:
{
Serial.print("Received Inputs: ");
g_Inputs = GPL_GetWord(g_InputBuffer);
Serial.println(g_Inputs);
g_aIn1 = ((g_Inputs & 0x0001) > 0) ? 1 : 0;
if(g_aIn1 != g_old_aIn1)
{
g_old_aIn1 = g_aIn1;
MqttPublishStatus_aIn1();
}
g_aIn2 = ((g_Inputs & 0x0002) > 0) ? 1 : 0;
if(g_aIn2 != g_old_aIn2)
{
g_old_aIn2 = g_aIn2;
MqttPublishStatus_aIn2();
}
g_aIn3 = ((g_Inputs & 0x0004) > 0) ? 1 : 0;
if(g_aIn3 != g_old_aIn3)
{
g_old_aIn3 = g_aIn3;
MqttPublishStatus_aIn3();
}
g_aIn4 = ((g_Inputs & 0x0008) > 0) ? 1 : 0;
if(g_aIn4 != g_old_aIn4)
{
g_old_aIn4 = g_aIn4;
MqttPublishStatus_aIn4();
}
g_aIn5 = ((g_Inputs & 0x0010) > 0) ? 1 : 0;
if(g_aIn5 != g_old_aIn5)
{
g_old_aIn5 = g_aIn5;
MqttPublishStatus_aIn5();
}
g_aIn6 = ((g_Inputs & 0x0020) > 0) ? 1 : 0;
if(g_aIn6 != g_old_aIn6)
{
g_old_aIn6 = g_aIn6;
MqttPublishStatus_aIn6();
}
g_aIn7 = ((g_Inputs & 0x0040) > 0) ? 1 : 0;
if(g_aIn7 != g_old_aIn7)
{
g_old_aIn7 = g_aIn7;
MqttPublishStatus_aIn7();
}
g_aIn8 = ((g_Inputs & 0x0080) > 0) ? 1 : 0;
if(g_aIn8 != g_old_aIn8)
{
g_old_aIn8 = g_aIn8;
MqttPublishStatus_aIn8();
}
g_dIn1 = ((g_Inputs & 0x0100) > 0) ? 1 : 0;
if(g_dIn1 != g_old_dIn1)
{
g_old_dIn1 = g_dIn1;
MqttPublishStatus_dIn1();
}
g_dIn2 = ((g_Inputs & 0x0200) > 0) ? 1 : 0;
if(g_dIn2 != g_old_dIn2)
{
g_old_dIn2 = g_dIn2;
MqttPublishStatus_dIn2();
}
g_dIn3 = ((g_Inputs & 0x0400) > 0) ? 1 : 0;
if(g_dIn3 != g_old_dIn3)
{
g_old_dIn3 = g_dIn3;
MqttPublishStatus_dIn3();
}
g_dIn4 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_dIn4 != g_old_dIn4)
{
g_old_dIn4 = g_dIn4;
MqttPublishStatus_dIn4();
}
} break;
case CMD_OUTPUTS:
{
Serial.print("Received Outputs: ");
g_Outputs = GPL_GetByte(g_InputBuffer);
Serial.println(g_Outputs);
g_Relay1 = ((g_Outputs & 0x01) > 0) ? 1 : 0;
if(g_Relay1 != g_oldRelay1)
{
g_oldRelay1 = g_Relay1;
MqttPublishStatus_Relay1();
}
g_Relay2 = ((g_Outputs & 0x02) > 0) ? 1 : 0;
if(g_Relay2 != g_oldRelay2)
{
g_oldRelay2 = g_Relay2;
MqttPublishStatus_Relay2();
}
g_Relay3 = ((g_Outputs & 0x04) > 0) ? 1 : 0;
if(g_Relay3 != g_oldRelay3)
{
g_oldRelay3 = g_Relay3;
MqttPublishStatus_Relay3();
}
g_Relay4 = ((g_Outputs & 0x08) > 0) ? 1 : 0;
if(g_Relay4 != g_oldRelay4)
{
g_oldRelay4 = g_Relay4;
MqttPublishStatus_Relay4();
}
g_RelayBell = ((g_Outputs & 0x10) > 0) ? 1 : 0;
if(g_RelayBell != g_oldRelayBell)
{
g_oldRelayBell = g_RelayBell;
MqttPublishStatus_RelayBell();
}
g_SwitchBus = ((g_Outputs & 0x40) > 0) ? 1 : 0;
if(g_SwitchBus != g_oldSwitchBus)
{
g_oldSwitchBus = g_SwitchBus;
MqttPublishStatus_SwitchBus();
}
g_SwitchTestBattery = ((g_Outputs & 0x20) > 0) ? 1 : 0;
if(g_SwitchTestBattery != g_oldSwitchTestBattery)
{
g_oldSwitchTestBattery = g_SwitchTestBattery;
MqttPublishStatus_SwitchTestBattery();
}
} break;
case CMD_EXT_POWER_STATUS:
{
Serial.print("Received Power Status: ");
if(GPL_GetByte(g_InputBuffer) == 1)
g_PowerStatus = "ON";
else
g_PowerStatus = "OFF";
Serial.println(g_PowerStatus);
MqttPublishStatus_Power();
} break;
case CMD_GET_V_BATT:
{
Serial.print("Received V Batt: ");
g_VoltageBattery = (double)(15 * GPL_GetWord(g_InputBuffer)) / 1024;
Serial.println(g_VoltageBattery);
MqttPublishStatus_Battery();
} break;
}
}
} break;
}
}
}
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------ Public Functions -----------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
void setup_wifi()
{
int counter = 0;
byte mac[6];
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(g_ssid);
WiFi.begin(g_ssid, g_password);
WiFi.macAddress(mac);
g_UniqueId = String(mac[0],HEX) +String(mac[1],HEX) +String(mac[2],HEX) +String(mac[3],HEX) + String(mac[4],HEX) + String(mac[5],HEX);
Serial.print("Unique ID: ");
Serial.println(g_UniqueId);
while(WiFi.status() != WL_CONNECTED && counter++ < 5)
{
delay(500);
Serial.print(".");
}
if(WiFi.status() == WL_CONNECTED)
{
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else
{
Serial.println("WiFi NOT connected!!!");
}
}
void MqttReconnect()
{
// Loop until we're MqttReconnected
while (!g_mqttPubSub.connected() && (g_mqttCounterConn++ < 4))
{
Serial.print("Attempting MQTT connection...");
if (g_mqttPubSub.connect(g_mqtt_DeviceName, g_mqttUser, g_mqttPsw))
{
Serial.println("connected");
// ESP32 Subscribe following topics
// Home assistant status
g_mqttPubSub.subscribe("homeassistant/status");
g_mqttPubSub.subscribe((g_TopicRelay1 + "/set").c_str());
g_mqttPubSub.subscribe((g_TopicRelay2 + "/set").c_str());
g_mqttPubSub.subscribe((g_TopicRelay3 + "/set").c_str());
g_mqttPubSub.subscribe((g_TopicRelay4 + "/set").c_str());
g_mqttPubSub.subscribe((g_TopicRelayBell + "/set").c_str());
g_mqttPubSub.subscribe((g_TopicSwitchBus + "/set").c_str());
g_mqttPubSub.subscribe((g_TopicSwitchTestBattery + "/set").c_str());
delay(500);
} else
{
Serial.print("failed, rc=");
Serial.print(g_mqttPubSub.state());
Serial.println(" try again in 3 seconds");
delay(3000);
}
}
g_mqttCounterConn = 0;
}
void MqttReceiverCallback(char* topic, byte* inFrame, unsigned int length)
{
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
byte state = 0;
String payload;
String topicMsg;
for (int i = 0; i < length; i++)
{
Serial.print((char)inFrame[i]);
payload += (char)inFrame[i];
}
Serial.println();
if(payload == "ON")
state = 1;
else if(payload == "OFF")
state = 0;
if(String(topic) == String("homeassistant/status"))
{
if(payload == "online")
{
MqttHomeAssistantDiscovery();
delay(1000);
Serial.println("INIT BOARD...");
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_INIT_SYSTEM, 0);
}
}
if(String(topic) == String(g_TopicRelay1 + "/set"))
{
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY2, state);
}
if(String(topic) == String(g_TopicRelay2 + "/set"))
{
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY3, state);
}
if(String(topic) == String(g_TopicRelay3 + "/set"))
{
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY4, state);
}
if(String(topic) == String(g_TopicRelay4 + "/set"))
{
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY5, state);
}
if(String(topic) == String(g_TopicRelayBell + "/set"))
{
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_AUX_POWER, state);
}
if(String(topic) == String(g_TopicSwitchBus + "/set"))
{
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_BUS_POWER, state);
}
if(String(topic) == String(g_TopicSwitchTestBattery + "/set"))
{
GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_TEST_BATTERY, state);
}
}
void MqttHomeAssistantDiscovery()
{
String discoveryTopic;
String payload;
String strPayload;
if(g_mqttPubSub.connected())
{
Serial.println("SEND HOME ASSISTANT DISCOVERY!!!");
StaticJsonDocument<600> payload;
JsonObject device;
JsonArray identifiers;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 1
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain1/config"; // Discovery Topic for entity ain1
payload["name"] = "IOTMB_Alarm.ain1";
payload["uniq_id"] = g_UniqueId + "_ain1";
payload["stat_t"] = g_TopicState_ain1;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 2
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain2/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.ain2";
payload["uniq_id"] = g_UniqueId + "_ain2";
payload["stat_t"] = g_TopicState_ain2;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 3
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain3/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.ain3";
payload["uniq_id"] = g_UniqueId + "_ain3";
payload["stat_t"] = g_TopicState_ain3;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 4
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain4/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.ain4";
payload["uniq_id"] = g_UniqueId + "_ain4";
payload["stat_t"] = g_TopicState_ain4;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 5
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain5/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.ain5";
payload["uniq_id"] = g_UniqueId + "_ain5";
payload["stat_t"] = g_TopicState_ain5;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 6
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain6/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.ain6";
payload["uniq_id"] = g_UniqueId + "_ain6";
payload["stat_t"] = g_TopicState_ain6;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 7
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain7/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.ain7";
payload["uniq_id"] = g_UniqueId + "_ain7";
payload["stat_t"] = g_TopicState_ain7;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analog Input 8
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/ain8/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.ain8";
payload["uniq_id"] = g_UniqueId + "_ain8";
payload["stat_t"] = g_TopicState_ain8;
payload["dev_cla"] = "door";
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Digital Input 1
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/din1/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.din1";
payload["uniq_id"] = g_UniqueId + "_din1";
payload["stat_t"] = g_TopicState_din1;
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Digital Input 2
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/din2/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.din2";
payload["uniq_id"] = g_UniqueId + "_din2";
payload["stat_t"] = g_TopicState_din2;
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");
serializeJson(payload, strPayload);
g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str());
delay(100);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Digital Input 3
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
discoveryTopic = "homeassistant/binary_sensor/din3/config";
payload.clear();
device.clear();
identifiers.clear();
strPayload.clear();
payload["name"] = "IOTMB_Alarm.din3";
payload["uniq_id"] = g_UniqueId + "_din3";
payload["stat_t"] = g_TopicState_din3;
device = payload.createNestedObject("device");
device["name"] = g_mqtt_DeviceName;
device["model"] = "IOTMB_RevA";
device["manufacturer"] = "VLC";
identifiers = device.createNestedArray("identifiers");
identifiers.add(g_UniqueId);
serializeJsonPretty(payload, Serial);
Serial.println(" ");