forked from vigasan/SecutityAlarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlarmSystem.ino
2188 lines (1909 loc) · 90.1 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 : 13/10/2023
* Last modified :
*
*
* Description : expanding to 30 analog inputs
*
* Other info : Alarm System Application for ESP32
**************************************************************************************************************************************************/
// Map multiplexer control pins.
#define S0 52
#define S1 53
#define S2 50
#define S3 51
#define Z A0 // Common pin
void setup() {
// Define pin modes.
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(Z, INPUT);
// Initialize serial communication for debugging.
Serial.begin(9600);
}
void loop() {
// Read from each of the 30 analog inputs.
for (int i = 0; i < 30; i++) {
// Determine the control pin settings for the given channel.
int controlPinSetting = i;
// Set the multiplexer control pins to the received setting.
digitalWrite(S0, bitRead(controlPinSetting, 0));
digitalWrite(S1, bitRead(controlPinSetting, 1));
digitalWrite(S2, bitRead(controlPinSetting, 2));
digitalWrite(S3, bitRead(controlPinSetting, 3));
// Read the analog value from the multiplexer's common pin.
int sensorValue = analogRead(Z);
// Print the current channel and the read value to the serial monitor.
Serial.print("Channel ");
Serial.print(i);
Serial.print(": ");
Serial.println(sensorValue);
}
// Wait a little before starting the next round of readings.
delay(1000);
}
***** chatgpt
#include <Arduino.h>
// Define the control pins for the first 74HC4067 multiplexer
const int S0_1 = PK0;
const int S1_1 = PK1;
const int S2_1 = PK2;
const int S3_1 = PK3;
const int Z_1 = PA7;
const int E_1 = PK5;
// Define the control pins for the second 74HC4067 multiplexer
const int S0_2 = PC1;
const int S1_2 = PC2;
const int S2_2 = PC3;
const int S3_2 = PC0;
const int Z_2 = PF3;
void setup() {
// Set control pins as OUTPUT
pinMode(S0_1, OUTPUT);
pinMode(S1_1, OUTPUT);
pinMode(S2_1, OUTPUT);
pinMode(S3_1, OUTPUT);
pinMode(Z_1, OUTPUT);
pinMode(E_1, OUTPUT);
pinMode(S0_2, OUTPUT);
pinMode(S1_2, OUTPUT);
pinMode(S2_2, OUTPUT);
pinMode(S3_2, OUTPUT);
pinMode(Z_2, OUTPUT);
// Initialize the multiplexers to read from Y0 initially
selectChannel(0, 1); // Select channel 0 on the first multiplexer
selectChannel(0, 2); // Select channel 0 on the second multiplexer
}
void loop() {
for (int channel = 0; channel < 16; channel++) {
// Select the channel on the first multiplexer
selectChannel(channel, 1);
// Read the analog value from the first multiplexer
int sensorValue_1 = analogRead(A0); // Assuming A0 is the analog input on ATmega2560
// Select the channel on the second multiplexer
selectChannel(channel, 2);
// Read the analog value from the second multiplexer
int sensorValue_2 = analogRead(A0);
// Print the values from both multiplexers
Serial.print("Analog Input from Multiplexer 1, Channel ");
Serial.print(channel);
Serial.print(": ");
Serial.println(sensorValue_1);
Serial.print("Analog Input from Multiplexer 2, Channel ");
Serial.print(channel);
Serial.print(": ");
Serial.println(sensorValue_2);
delay(1000); // Delay for a second (adjust as needed)
}
}
void selectChannel(int channel, int multiplexer) {
// Determine which multiplexer to select
if (multiplexer == 1) {
digitalWrite(S0_1, channel & 0x01);
digitalWrite(S1_1, (channel >> 1) & 0x01);
digitalWrite(S2_1, (channel >> 2) & 0x01);
digitalWrite(S3_1, (channel >> 3) & 0x01);
} else if (multiplexer == 2) {
digitalWrite(S0_2, channel & 0x01);
digitalWrite(S1_2, (channel >> 1) & 0x01);
digitalWrite(S2_2, (channel >> 2) & 0x01);
digitalWrite(S3_2, (channel >> 3) & 0x01);
}
}
*****
// **************************************************************************************************************************************************
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------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_AIN9 18
#define CMD_GET_AIN10 19
#define CMD_GET_AIN11 20
#define CMD_GET_AIN12 21
#define CMD_GET_AIN13 22
#define CMD_GET_AIN14 23
#define CMD_GET_AIN15 24
#define CMD_GET_AIN16 25
#define CMD_GET_AIN17 26
#define CMD_GET_AIN18 27
#define CMD_GET_AIN19 28
#define CMD_GET_AIN20 29
#define CMD_GET_AIN21 30
#define CMD_GET_AIN22 31
#define CMD_GET_AIN23 32
#define CMD_GET_AIN24 33
#define CMD_GET_AIN25 34
#define CMD_GET_AIN26 35
#define CMD_GET_AIN27 36
#define CMD_GET_AIN28 37
#define CMD_GET_AIN29 38
#define CMD_GET_AIN30 39
#define CMD_SET_THR_AIN1 40
#define CMD_GET_THR_AIN1 41
#define CMD_SET_THR_AIN2 42
#define CMD_GET_THR_AIN2 43
#define CMD_SET_THR_AIN3 44
#define CMD_GET_THR_AIN3 45
#define CMD_SET_THR_AIN4 46
#define CMD_GET_THR_AIN4 47
#define CMD_SET_THR_AIN5 48
#define CMD_GET_THR_AIN5 49
#define CMD_SET_THR_AIN6 50
#define CMD_GET_THR_AIN6 51
#define CMD_SET_THR_AIN7 52
#define CMD_GET_THR_AIN7 53
#define CMD_SET_THR_AIN8 54
#define CMD_GET_THR_AIN8 55
#define CMD_SET_THR_AIN9 56
#define CMD_GET_THR_AIN9 57
#define CMD_SET_THR_AIN10 58
#define CMD_GET_THR_AIN10 59
#define CMD_SET_THR_AIN11 60
#define CMD_GET_THR_AIN11 61
#define CMD_SET_THR_AIN12 62
#define CMD_GET_THR_AIN12 63
#define CMD_SET_THR_AIN13 64
#define CMD_GET_THR_AIN13 65
#define CMD_SET_THR_AIN14 66
#define CMD_GET_THR_AIN14 67
#define CMD_SET_THR_AIN15 68
#define CMD_GET_THR_AIN15 69
#define CMD_SET_THR_AIN16 70
#define CMD_GET_THR_AIN16 71
#define CMD_SET_THR_AIN17 72
#define CMD_GET_THR_AIN17 73
#define CMD_SET_THR_AIN18 74
#define CMD_GET_THR_AIN18 75
#define CMD_SET_THR_AIN19 76
#define CMD_GET_THR_AIN19 77
#define CMD_SET_THR_AIN20 78
#define CMD_GET_THR_AIN20 79
#define CMD_SET_THR_AIN21 80
#define CMD_SET_THR_AIN22 81
#define CMD_GET_THR_AIN22 82
#define CMD_SET_THR_AIN23 83
#define CMD_GET_THR_AIN23 84
#define CMD_SET_THR_AIN24 85
#define CMD_GET_THR_AIN24 86
#define CMD_SET_THR_AIN25 87
#define CMD_GET_THR_AIN25 88
#define CMD_SET_THR_AIN26 89
#define CMD_GET_THR_AIN26 90
#define CMD_SET_THR_AIN27 91
#define CMD_GET_THR_AIN27 92
#define CMD_SET_THR_AIN28 93
#define CMD_GET_THR_AIN28 94
#define CMD_SET_THR_AIN29 95
#define CMD_GET_THR_AIN29 96
#define CMD_SET_THR_AIN30 97
#define CMD_GET_THR_AIN30 98
#define CMD_GET_INPUTS 99
#define CMD_SET_AUX_POWER 100
#define CMD_GET_AUX_POWER 101
#define CMD_SET_TEST_BATTERY 102
#define CMD_GET_TEST_BATTERY 103
#define CMD_SET_RELAY2 104
#define CMD_GET_RELAY2 105
#define CMD_SET_RELAY3 106
#define CMD_GET_RELAY3 107
#define CMD_SET_RELAY4 108
#define CMD_GET_RELAY4 109
#define CMD_SET_RELAY5 110
#define CMD_GET_RELAY5 111
#define CMD_SET_SYSTEM_STATUS 112
#define CMD_GET_SYSTEM_STATUS 113
#define CMD_SET_GUI_STATUS 114
#define CMD_GET_V_EXT 115
#define CMD_GET_V_BATT 116
#define CMD_GET_V_BOARD 117
#define CMD_SET_BUS_POWER 118
#define CMD_GET_BUS_POWER 119
#define CMD_SET_DATE_TIME 120
#define CMD_GET_DATE_TIME 121
#define CMD_INIT_SYSTEM 122
#define CMD_EXT_POWER_STATUS 123
#define CMD_OUTPUTS 124
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------MQTT DISCOVERY PARAMETERS----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
const char* g_ssid = "Wifi Name"; // Wifi SSID Name
const char* g_password = "Wifi Password"; // Wifi Password
const char* g_mqtt_server = "192.168.1.125"; // 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 = "Security"; // Your Device Name
String g_TopicState_ain1 = "Security/ain1/state"; // State Topic for entity input ain1
String g_TopicState_ain2 = "Security/ain2/state"; // State Topic for entity input ain2
String g_TopicState_ain3 = "Security/ain3/state"; // State Topic for entity input ain3
String g_TopicState_ain4 = "Security/ain4/state"; // State Topic for entity input ain4
String g_TopicState_ain5 = "Security/ain5/state"; // State Topic for entity input ain5
String g_TopicState_ain6 = "Security/ain6/state"; // State Topic for entity input ain6
String g_TopicState_ain7 = "Security/ain7/state"; // State Topic for entity input ain7
String g_TopicState_ain8 = "Security/ain8/state"; // State Topic for entity input ain8
String g_TopicState_ain1 = "Security/ain9/state"; // State Topic for entity input ain1
String g_TopicState_ain2 = "Security/ain10/state"; // State Topic for entity input ain2
String g_TopicState_ain3 = "Security/ain11/state"; // State Topic for entity input ain3
String g_TopicState_ain4 = "Security/ain12/state"; // State Topic for entity input ain4
String g_TopicState_ain5 = "Security/ain13/state"; // State Topic for entity input ain5
String g_TopicState_ain6 = "Security/ain14/state"; // State Topic for entity input ain6
String g_TopicState_ain7 = "Security/ain15/state"; // State Topic for entity input ain7
String g_TopicState_ain8 = "Security/ain16/state"; // State Topic for entity input ain8
String g_TopicState_ain1 = "Security/ain17/state"; // State Topic for entity input ain1
String g_TopicState_ain2 = "Security/ain18/state"; // State Topic for entity input ain2
String g_TopicState_ain3 = "Security/ain19/state"; // State Topic for entity input ain3
String g_TopicState_ain4 = "Security/ain20/state"; // State Topic for entity input ain4
String g_TopicState_ain5 = "Security/ain21/state"; // State Topic for entity input ain5
String g_TopicState_ain6 = "Security/ain22/state"; // State Topic for entity input ain6
String g_TopicState_ain7 = "Security/ain23/state"; // State Topic for entity input ain7
String g_TopicState_ain8 = "Security/ain24/state"; // State Topic for entity input ain8
String g_TopicState_ain1 = "Security/ain25/state"; // State Topic for entity input ain1
String g_TopicState_ain2 = "Security/ain26/state"; // State Topic for entity input ain2
String g_TopicState_ain3 = "Security/ain27/state"; // State Topic for entity input ain3
String g_TopicState_ain4 = "Security/ain28/state"; // State Topic for entity input ain4
String g_TopicState_ain5 = "Security/ain29/state"; // State Topic for entity input ain5
String g_TopicState_ain6 = "Security/ain30/state"; // State Topic for entity input ain6
// String g_TopicState_din1 = "Security/din1/state"; // State Topic for entity input din1
// String g_TopicState_din2 = "Security/din2/state"; // State Topic for entity input din2
// String g_TopicState_din3 = "Security/din3/state"; // State Topic for entity input din3
// String g_TopicState_din4 = "Security/din4/state"; // State Topic for entity input din4
String g_TopicRelay1 = "Security/relay1"; // Topic for entity relay1
String g_TopicRelay2 = "Security/relay2"; // Topic for entity relay2
String g_TopicRelay3 = "Security/relay3"; // Topic for entity relay3
String g_TopicRelay4 = "Security/relay4"; // Topic for entity relay4
String g_TopicRelayBell = "Security/relayBell"; // Topic for entity relayBell (Alarm Siren)
String g_TopicSwitchBus = "Security/switchBus"; // Topic for entity switch Power Bus
String g_TopicSwitchTestBattery = "Security/switchTestBatt"; // Topic for entity switch Test Battery
String g_TopicPowerState = "Security/power/state"; // State Topic for entity Power (Check for blackout)
String g_TopicVoltBattery = "Security/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_aIn9 = 0;
word g_old_aIn9 = 0xFFFF;
word g_aIn10 = 0;
word g_old_aIn10 = 0xFFFF;
word g_aIn11 = 0;
word g_old_aIn11 = 0xFFFF;
word g_aIn12 = 0;
word g_old_aIn12 = 0xFFFF;
word g_aIn13 = 0;
word g_old_aIn13 = 0xFFFF;
word g_aIn14 = 0;
word g_old_aIn14 = 0xFFFF;
word g_aIn15 = 0;
word g_old_aIn15 = 0xFFFF;
word g_aIn16 = 0;
word g_old_aIn16 = 0xFFFF;
word g_aIn17 = 0;
word g_old_aIn17 = 0xFFFF;
word g_aIn18 = 0;
word g_old_aIn18 = 0xFFFF;
word g_aIn19 = 0;
word g_old_aIn19 = 0xFFFF;
word g_aIn20 = 0;
word g_old_aIn20 = 0xFFFF;
word g_aIn21 = 0;
word g_old_aIn21 = 0xFFFF;
word g_aIn22 = 0;
word g_old_aIn22 = 0xFFFF;
word g_aIn23 = 0;
word g_old_aIn23 = 0xFFFF;
word g_aIn24 = 0;
word g_old_aIn24 = 0xFFFF;
word g_aIn25 = 0;
word g_old_aIn25 = 0xFFFF;
word g_aIn26 = 0;
word g_old_aIn26 = 0xFFFF;
word g_aIn27 = 0;
word g_old_aIn27 = 0xFFFF;
word g_aIn28 = 0;
word g_old_aIn28 = 0xFFFF;
word g_aIn29 = 0;
word g_old_aIn29 = 0xFFFF;
word g_aIn30 = 0;
word g_old_aIn30 = 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("-------- SECURITY ----------");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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();
// }
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_aIn9 = ((g_Inputs & 0x0100) > 0) ? 1 : 0;
if(g_aIn9 != g_old_aIn9)
{
g_old_aIn9 = g_aIn9;
MqttPublishStatus_aIn9();
}
g_aIn10 = ((g_Inputs & 0x0200) > 0) ? 1 : 0;
if(g_aIn10 != g_old_aIn10)
{
g_old_aIn10 = g_aIn10;
MqttPublishStatus_aIn10();
}
g_aIn11 = ((g_Inputs & 0x0400) > 0) ? 1 : 0;
if(g_aIn11 != g_old_aIn11)
{
g_old_aIn11 = g_aIn11;
MqttPublishStatus_aIn11();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn13 = ((g_Inputs & 0x1000) > 0) ? 1 : 0;
if(g_aIn13 != g_old_aIn13)
{
g_old_aIn13 = g_aIn13;
MqttPublishStatus_aIn13();
}
g_aIn14 = ((g_Inputs & 0x2000) > 0) ? 1 : 0;
if(g_aIn14 != g_old_aIn14)
{
g_old_aIn14 = g_aIn14;
MqttPublishStatus_aIn14();
}
g_aIn15 = ((g_Inputs & 0x4000) > 0) ? 1 : 0;
if(g_aIn15 != g_old_aIn15)
{
g_old_aIn15 = g_aIn15;
MqttPublishStatus_aIn15();
}
g_aIn16 = ((g_Inputs & 0x8000) > 0) ? 1 : 0;
if(g_aIn16 != g_old_aIn16)
{
g_old_aIn16 = g_aIn16;
MqttPublishStatus_aIn16();
}
*****
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
g_aIn12 = ((g_Inputs & 0x0800) > 0) ? 1 : 0;
if(g_aIn12 != g_old_aIn12)
{
g_old_aIn12 = g_aIn12;
MqttPublishStatus_aIn12();
}
***
} 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;
}
}
}
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/