-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot-hub-c.ino
993 lines (899 loc) · 34.5 KB
/
iot-hub-c.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
#include <SPI.h>
#include <string.h>
#include <Console.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <avr/pgmspace.h>
#include <EEPROM.h>
#include "Shade.h"
#include "ARiF.h"
#include "Settings.h"
#include "WebGUI.h"
#include "Light.h"
#include "Wire.h"
#include "RS485.h"
/*
CONTROLLINO/Arduino based smarthouse and industry low-level control platform.
Used to control outputs by inputs and provide a REST-like API over network to that system.
RELEASE NOTES:
v1.3.0
- Added counter based inputs
- Added RS485 humidity and temperature sensor.
v1.2.2
- Fixed raspy IP change through HB message from a new IP.
BarnGA-0.5 (or 1.2.1)
- Added Controllino build-in relay support
- fixed the tilt and position timers support by ARiF
- Added Shade state stored in EEPROM
- Added "User" indication to shades triggered by physical switch
- issues:
- Problem with lack of time gap between changing direction when shade not in sync
BarnGA-0.4
- Added 1-wire temp sensor
- implemented factory reset on "restore" cmd and on new install.
- tag for centralON feature (each device can be marked for being applicable for centralON), new digitIO device commands:
- devCtrlON
- devCtrlOFF
- use MAC address provided by raspy on registration
- Implemented device settings sent as JSON.
- added uptime in miliseconds to arduino settings sent as JSON.
- Issues/limitations found:
- The ctrlON function works only if the device (last pin) is set to inputRelease mode.
BarnGA-0.3.1
- fixed bug that was shutting down networking after 12 hours
- added MAC printing on init
BarnGA-0.3
- fixed the issue with code getting stuck after initial DHCP fails.
BarnGA-0.2
- Increased the ARiF timer to 10s
- Changed MAC address
BarnGA - initial version to support lights in Libor's Barn
Created 8 Oct 2019
by Maciej
*/
/*
----------------
--- Settings ---
----------------
*/
/* variable controlling if the relays are set to NC or NO
NC - Normally Closed - the digitOUT is by default in LOW state, LightON -> HIGH state (true)
NO - Normally Open - the digitOUT is by default in HIGH state, LightON -> LOW state (false)
*/
bool relaysNC = true;
/* This value is set to:
MODE_LIGHTS - where every digitIN toggled changes the state of its corresponding digitOUT (1-to-1 mapping)
MODE_SHADES - the devices are coupled in 4 for one shade device (separate controls for UP and DOWN directions).
Each digitIN enables the corresponding digitOUT, but at the same time disables the paired digitOUT.
Additional default timer is implemented.
*/
byte funcMode;
/* MAC address used for initiall boot */
byte mac[] = { 0x00, 0xAA, 0xBB, 0x13, 0xF9, 0x76 };
/*
------------------------
--- Global Variables ---
------------------------
*/
/* starting with not registered iot hub */
byte ardID = 0;
byte raspyID = 0;
/* holds information if this arduino is registered */
bool isRegistered = false;
/* holds the IP of the Raspy/iot-gw */
IPAddress iotGwIP;
/* instantiate the shades objects */
Shade shades[SHADES];
/* instantiate the lights objects */
Light lights[LIGHTS];
/* value to control when to print the measurement report */
bool measure;
/*
-----------------
--- Setup ---
-----------------
*/
void setup() {
/* initialize serial interface */
Serial.begin(9600);
Serial.println(F("###------------"));
Serial.println(F("### Setup init!"));
Serial.println(F("###------------"));
Serial.print(F("Version: "));
Serial.println(F(VERSION));
//Platform.EEPROMSetUID();
if (Platform.EEPROMIsUIDSet() == true) {
Serial.println(F("UID set - running normally"));
} else {
Serial.println(F("UID not set - restoring EEPROM to factory defaults"));
Platform.EEPROMRaze();
Platform.EEPROMSetUID();
}
/* Set the default mode of the device based on the EEPROM value */
funcMode = Platform.EEPROMGetMode();
/* assume default mode on platform where it couldn't be read */
if (funcMode != MODE_FAIL && funcMode != MODE_SHADES) {
funcMode = MODE_LIGHTS;
}
if (funcMode == MODE_SHADES) {
initializeShades();
} else if (funcMode == MODE_LIGHTS) {
initializeLights();
}
/* disable SD card on the Ethernet shield */ // to be removed, this is part of the Settings::SDCardInit()
//pinMode(4, OUTPUT);
//digitalWrite(4, HIGH);
/* initialize various platform dependend settings */
Platform.initPlatform();
/* initialize the SD Card */
if (Platform.SDCardInit() == SD_INIT_SUCCESS) {
WebGUI.setSDStatusAvailable();
} else {
WebGUI.setSDStatusUnavailable();
}
/* get the registration data from EEPROM */
isRegistered = Platform.EEPROMIsRegistered();
//isRegistered = false;
/*Serial.print("Using MAC address: ");
char macbuff[17];
sprintf(macbuff, "%x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(macbuff);*/
//Platform.EEPROMSetUseDefMAC(EEPROM_FLG_DEF_MAC);
if (isRegistered) {
Serial.print("Arduino registered with ardID: ");
ardID = Platform.EEPROMGetArdID();
Serial.println(ardID);
Serial.print("Raspy IP: ");
iotGwIP = Platform.EEPROMGetRaspyIP();
Serial.println(iotGwIP);
Serial.print("RaspyID: ");
raspyID = Platform.EEPROMGetRaspyID();
Serial.println(raspyID);
ARiF.begin(VER_SHD_1, mac, iotGwIP, ardID, raspyID);
WebGUI.setInfoRegistered(ardID, raspyID, iotGwIP);
} else {
Serial.println(F("Not registered within any iot-gw"));
ARiF.begin(VER_SHD_1, mac);
WebGUI.setInfoDeregistered();
}
/* set ARiF mode */
if (funcMode == MODE_SHADES) {
ARiF.setMode(M_SHADES);
} else if (funcMode == MODE_LIGHTS) {
ARiF.setMode(M_LIGHTS);
}
/* Read the light Central Control setting and set Light class global mode */
byte centralCtrlMode;
centralCtrlMode = Platform.EEPROMGetLightCentral();
if (centralCtrlMode == DIGITOUT_CENTRAL_CTRL_ENABLE) {
Light::enableCentralCtrl();
ARiF.setCtrlON(ARIF_CTRLON_ENABLED);
} else if (centralCtrlMode == DIGITOUT_CENTRAL_CTRL_DISABLE) {
Light::disableCentralCtrl();
ARiF.setCtrlON(ARIF_CTRLON_DISABLED);
} else {
Light::disableCentralCtrl();
ARiF.setCtrlON(ARIF_CTRLON_DISABLED);
}
/* initialize 1-Wire sensors */
Wire.begin();
/* initialize RS485 sensors */
RS485.begin();
/* initilize the webUI */
WebGUI.begin();
Serial.println(F("Setup complete!"));
}
/*
-----------------
--- Main Loop ---
-----------------
*/
void loop() {
/* -- measurement code start -- */
measure = false;
TCCR1A = 0;
TCCR1B = 1;
uint16_t start = TCNT1;
/* -- measurement code end -- */
/*
----------------------------
--- Shade mode operation ---
----------------------------
*/
if (funcMode == MODE_SHADES) {
for (int i = 0; i < SHADES; i++) {
shades[i].update();
byte devID = shades[i].getDevID();
byte upPressResult = shades[i].isUpPressed();
byte downPressResult = shades[i].isDownPressed();
if (upPressResult == PHY_MOMENTARY_PRESS) {
shades[i].setUserPressed();
if (shades[i].isMoving()) {
shades[i].stopWithTilt();
} else {
shades[i].toggleTiltUp();
}
measure = true;
} else if (upPressResult == PHY_PRESS_MORE_THAN_2SEC) {
shades[i].setUserPressed();
if (shades[i].isMoving()) {
shades[i].stopWithTilt();
} else {
shades[i].up();
}
}
if (downPressResult == PHY_MOMENTARY_PRESS) {
shades[i].setUserPressed();
if (shades[i].isMoving()) {
shades[i].stopWithTilt();
} else {
shades[i].toggleTiltDown();
}
measure = true;
} else if (downPressResult == PHY_PRESS_MORE_THAN_2SEC) {
shades[i].setUserPressed();
if (shades[i].isMoving()) {
shades[i].stopWithTilt();
} else {
shades[i].down();
}
}
if (shades[i].justStoppedTilt()) { /* executed on shade stopped tilt movement */
Serial.println("just stopped tilt!");
if (shades[i].getUserPressed()) {
ARiF.sendUserShadeTilt(Settings::shadeIDs[i], shades[i].getTilt());
} else {
ARiF.sendShadeTilt(Settings::shadeIDs[i], shades[i].getTilt());
}
WebGUI.shadeSetTilt(devID, shades[i].getTilt());
WebGUI.shadeSetDirection(devID, S_WEBGUI_STOP);
Platform.EEPROMSetShadeTilt(devID, shades[i].getTilt());
}
if (shades[i].justStopped()) { /* executed on shade stopped vertical movement */
if (shades[i].getUserPressed()) {
ARiF.sendUserShadeStop(Settings::shadeIDs[i]);
shades[i].clearUserPressed();
} else {
ARiF.sendShadeStop(Settings::shadeIDs[i]);
}
ARiF.sendShadePosition(Settings::shadeIDs[i], shades[i].getCurrentPosition());
WebGUI.shadeSetPosition(devID, shades[i].getCurrentPosition());
Platform.EEPROMSetShadePosition(devID, shades[i].getPosition());
Platform.EEPROMSetShadeReachedPosition(devID, shades[i].getCurrentPosition());
if (shades[i].isSynced()) {
Platform.EEPROMSetShadeSyncFlag(devID);
}
}
if (shades[i].justStartedDown()) { /* executed on shade started moving down */
Platform.EEPROMClearShadeSyncFlag(Settings::shadeIDs[i]);
if (shades[i].getUserPressed()) {
ARiF.sendUserShadeDown(Settings::shadeIDs[i]);
shades[i].clearUserPressed();
} else {
ARiF.sendShadeDown(Settings::shadeIDs[i]);
}
WebGUI.shadeSetDirection(devID, S_WEBGUI_DOWN);
}
if (shades[i].justStartedUp()) { /* executed on shade started moving up */
Platform.EEPROMClearShadeSyncFlag(Settings::shadeIDs[i]);
if (shades[i].getUserPressed()) {
ARiF.sendUserShadeUp(Settings::shadeIDs[i]);
shades[i].clearUserPressed();
} else {
ARiF.sendShadeUp(Settings::shadeIDs[i]);
}
WebGUI.shadeSetDirection(devID, S_WEBGUI_UP);
}
if (shades[i].justSynced()) {
Serial.println(F("Just synced!"));
Platform.EEPROMSetShadeSyncFlag(devID);
}
}
/*
-----------------------------
--- Lights mode operation ---
-----------------------------
*/
} else if (funcMode == MODE_LIGHTS) {
byte devID;
byte pressResult;
for (int i = 0; i < LIGHTS; i++) {
devID = lights[i].getDevID();
pressResult = PHY_NO_PRESS;
pressResult = lights[i].isPressed();
if (pressResult == PHY_MOMENTARY_PRESS) {
lights[i].toggle();
} else if (pressResult == PHY_PRESS_MORE_THAN_2SEC) {
lights[i].toggle();
} else if (pressResult == PHY_CENTRAL_CTRL_MOMENTARY_PRESS) {
byte devIDCentralPress;
for (int j = 0; j < LIGHTS; j++) {
if (lights[j].getType() == DIGITOUT_ONOFF) {
devIDCentralPress = lights[j].getDevID();
if (lights[j].getCtrlON() == DIGITOUT_CTRLON_ON) {
lights[j].setON();
WebGUI.lightSetON(devIDCentralPress);
ARiF.sendUserLightON(devIDCentralPress);
delay(DIGITOUT_CENTRAL_CTRL_DELAY);
}
}
}
} else if (pressResult == PHY_CENTRAL_CTRL_PRESS_MORE_THAN_2SEC) {
byte devIDCentralPress;
for (int k = 0; k < LIGHTS; k++) {
if (lights[k].getType() == DIGITOUT_ONOFF) {
devIDCentralPress = lights[k].getDevID();
if (lights[k].getCtrlON() == DIGITOUT_CTRLON_ON) {
lights[k].setOFF();
WebGUI.lightSetOFF(devIDCentralPress);
ARiF.sendUserLightOFF(devIDCentralPress);
delay(DIGITOUT_CENTRAL_CTRL_DELAY);
}
}
}
} else if (pressResult == PHY_COUNTER_TIME_TRIGGER) {
unsigned long counter;
if (ARiF.isARiFConnected()) {
Serial.println(F("Sending counter value"));
counter = lights[i].getCounterAndReset();
ARiF.sendCounter(devID, counter);
} else {
Serial.println(F("ARiF Disconnected, not sending counter."));
}
}
if (lights[i].justTurnedON()) {
WebGUI.lightSetON(devID);
ARiF.sendUserLightON(devID);
}
if (lights[i].justTurnedOFF()) {
WebGUI.lightSetOFF(devID);
ARiF.sendUserLightOFF(devID);
}
}
}
/*
----------------------------------
--- Handling of WebGUI actions ---
----------------------------------
*/
byte webGuiRet = WebGUI.update();
switch (webGuiRet) {
case CMD_WEBGUI_DEREGISTER: /* Deregister button pressed */
ARiF.deregister();
Platform.EEPROMDeregister();
break;
case CMD_WEBGUI_SET_M_LIGHTS: /* Switch Mode to Lights */
setModeLights();
break;
case CMD_WEBGUI_SET_M_SHADES: /* Switch Mode to Shades */
setModeShades();
break;
case CMD_WEBGUI_NOTHING: /* Do nothing */
break;
}
/*
--------------------------------
--- Handling of ARiF actions ---
--------------------------------
*/
byte ret = ARiF.update();
byte lastDevID;
byte lastLightType;
unsigned long lastLightTimer;
int lastShadePositionTimer;
int lastShadeTiltTimer;
switch (ret) {
case U_CONNECTED: /* ARiF connection with the Raspy has been re-established (or established for the first time) */
Serial.println(F("Connected back!"));
if (funcMode == MODE_SHADES) {
sendShadeStatus();
} else if (funcMode == MODE_LIGHTS) {
sendLightStatus();
}
ARiF.sendSettings();
break;
case U_NOTHING: /* Do nothing */
break;
case CMD_REGISTER: /* Registered to a Raspy */
Serial.println(F("Registered!"));
ardID = ARiF.getArdID();
raspyID = ARiF.getRaspyID();
iotGwIP = ARiF.getRaspyIP();
WebGUI.setInfoRegistered(ardID, raspyID, iotGwIP);
Platform.EEPROMRegister(ardID, raspyID, iotGwIP);
break;
case CMD_SHADEUP: /* shadeUP command received */
Serial.print(F("ShadeUP received for: "));
Serial.println(ARiF.getLastDevID());
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == lastDevID) shades[i].up();
}
break;
case CMD_SHADEDOWN: /* shadeDOWN command received */
Serial.print(F("ShadeDOWN received for: "));
Serial.println(ARiF.getLastDevID());
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == lastDevID) shades[i].down();
}
break;
case CMD_SHADEPOS: /* shadePOS command received */
Serial.print(F("Shadepos received with value: "));
Serial.println(ARiF.getLastShadePosition());
lastDevID = ARiF.getLastDevID();
//Serial.println(s.getDevID()); // why s.toPosition() doesn't work??
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == lastDevID) shades[i].toPosition(ARiF.getLastShadePosition());
}
break;
case CMD_SHADETILT: /* shadeTILT command received */
Serial.print(F("Shadetilt received: "));
Serial.println(ARiF.getLastShadeTilt());
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == lastDevID) shades[i].setTilt(ARiF.getLastShadeTilt());
}
break;
case CMD_SHADESTOP: /* shadeSTOP command received */
Serial.println(F("ShadeSTOP received"));
lastDevID = ARiF.getLastDevID();
//Serial.println(s.getDevID()); // why s.toPosition() doesn't work??
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == lastDevID) {
shades[i].stopWithTilt();
}
}
break;
case CMD_LIGHTON: /* lightON command received */
Serial.println(F("Received lightON command"));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setON();
}
}
break;
case CMD_LIGHTOFF: /* lightOFF command received */
Serial.println(F("Received lightOFF command"));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setOFF();
}
}
break;
case CMD_LIGHT_TYPE: /* lightType command received */
Serial.print(F("Received lightType command: "));
lastDevID = ARiF.getLastDevID();
lastLightType = ARiF.getLastLightType();
Serial.println(lastLightType);
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setType(lastLightType);
Platform.EEPROMSetLightType(lastDevID, lastLightType);
ARiF.sendLightSettings(lastDevID, lights[i].getTimer(), lastLightType, lights[i].getInputType(), lights[i].getCtrlON());
}
}
WebGUI.lightSetType(lastDevID, ARiF.getLastLightType());
break;
case CMD_LIGHT_TIMER: /* lightTimer command received */
Serial.print(F("Received lightTimer command: "));
lastDevID = ARiF.getLastDevID();
lastLightTimer = ARiF.getLastLightTimer();
Serial.println(lastLightTimer);
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setTimer(lastLightTimer);
Platform.EEPROMSetLightTimer(lastDevID, lastLightTimer);
ARiF.sendLightSettings(lastDevID, lastLightTimer, lights[i].getType(), lights[i].getInputType(), lights[i].getCtrlON());
}
}
WebGUI.lightSetTimer(lastDevID, ARiF.getLastLightTimer());
break;
case CMD_MODE_LIGHTS: /* modeLights command received */
Serial.println(F("Received modeLights command"));
setModeLights();
break;
case CMD_MODE_SHADES: /* modeShades command received */
Serial.println(F("Received modeShades command"));
setModeShades();
break;
case CMD_TIMER_POS: /* shadePTimer command received */
Serial.print(F("Received shadePTimer command: "));
lastDevID = ARiF.getLastDevID();
lastShadePositionTimer = ARiF.getLastShadePositionTimer();
Platform.EEPROMSetShadePosTimer(lastDevID, lastShadePositionTimer);
Serial.println(lastShadePositionTimer);
/* validate if the received timer is within range, if not ignore */
if (lastShadePositionTimer >= SHADE_POSITION_TIMER_MIN && lastShadePositionTimer <= SHADE_POSITION_TIMER_MAX) {
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == lastDevID) {
shades[i].setPositionTimer(lastShadePositionTimer);
Platform.EEPROMSetShadePosTimer(lastDevID, lastShadePositionTimer);
ARiF.sendShadeSettings(lastDevID, lastShadePositionTimer, shades[i].getTiltTimer());
}
}
} else {
Serial.println(F("Received shadePTimer value out of range. Ignoring"));
}
break;
case CMD_TIMER_TILT: /* shadeTTimer command received */
Serial.print(F("Received shadeTTimer command: "));
lastDevID = ARiF.getLastDevID();
lastShadeTiltTimer = ARiF.getLastShadeTiltTimer();
Platform.EEPROMSetShadeTiltTimer(lastDevID, lastShadeTiltTimer);
Serial.println(lastShadeTiltTimer);
/* validate if the received timer is within range, if not ignore */
if (lastShadeTiltTimer >= SHADE_TILT_TIMER_MIN && lastShadeTiltTimer <= SHADE_TILT_TIMER_MAX) {
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == lastDevID) {
shades[i].setTiltTimer(lastShadeTiltTimer);
Platform.EEPROMSetShadeTiltTimer(lastDevID, lastShadeTiltTimer);
ARiF.sendShadeSettings(lastDevID, shades[i].getPositionTimer(), lastShadeTiltTimer);
}
}
} else {
Serial.println(F("Received shadeTTimer value out of range. Ignoring"));
}
break;
case CMD_INPUT_HOLD: /* inputHold command received */
Serial.println(F("Received inputHold command. "));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setInputTypeHold();
Platform.EEPROMSetLightInputType(lastDevID, DIGITOUT_SWITCH_PRESS_HOLD);
ARiF.sendLightSettings(lastDevID, lights[i].getTimer(), lights[i].getType(), DIGITOUT_SWITCH_PRESS_HOLD, lights[i].getCtrlON());
}
}
break;
case CMD_INPUT_REL: /* inputRelease command received */
Serial.println(F("Received inputRelease command. "));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setInputTypeRelease();
Platform.EEPROMSetLightInputType(lastDevID, DIGITOUT_SWITCH_PRESS_RELEASE);
ARiF.sendLightSettings(lastDevID, lights[i].getTimer(), lights[i].getType(), DIGITOUT_SWITCH_PRESS_RELEASE, lights[i].getCtrlON());
}
}
break;
case CMD_INPUT_OVERRIDE_ON: /* inputOverrideOn command received */
Serial.println(F("Received inputOverrideOn command. "));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setInputTypeSimpleHeatOverrideOn();
Platform.EEPROMSetLightInputType(lastDevID, DIGITOUT_SWITCH_HEAT_OVERRIDE_ON);
ARiF.sendLightSettings(lastDevID, lights[i].getTimer(), lights[i].getType(), DIGITOUT_SWITCH_HEAT_OVERRIDE_ON, lights[i].getCtrlON());
}
}
break;
case CMD_INPUT_OVERRIDE_OFF: /* inputOverrideOff command received */
Serial.println(F("Received inputOverrideOff command. "));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setInputTypeSimpleHeatOverrideOff();
Platform.EEPROMSetLightInputType(lastDevID, DIGITOUT_SWITCH_HEAT_OVERRIDE_OFF);
ARiF.sendLightSettings(lastDevID, lights[i].getTimer(), lights[i].getType(), DIGITOUT_SWITCH_HEAT_OVERRIDE_OFF, lights[i].getCtrlON());
}
}
break;
case CMD_CTRL_DEV_ON: /* devCtrlON command received */
Serial.println(F("Received devCtrlON command. "));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setCtrlONEnabled();
Platform.EEPROMSetLightCtrlON(lastDevID, DIGITOUT_CTRLON_ON);
Platform.EEPROMGetLightCtrlON(lastDevID);
ARiF.sendLightSettings(lastDevID, lights[i].getTimer(), lights[i].getType(), lights[i].getInputType(), DIGITOUT_CTRLON_ON);
}
}
break;
case CMD_CTRL_DEV_OFF: /* devCtrlOFF command received */
Serial.println(F("Received devCtrlOFF command. "));
lastDevID = ARiF.getLastDevID();
for (int i = 0; i < LIGHTS; i++) {
if (lights[i].getDevID() == lastDevID) {
lights[i].setCtrlONDisabled();
Platform.EEPROMSetLightCtrlON(lastDevID, DIGITOUT_CTRLON_OFF);
Platform.EEPROMGetLightCtrlON(lastDevID);
ARiF.sendLightSettings(lastDevID, lights[i].getTimer(), lights[i].getType(), lights[i].getInputType(), DIGITOUT_CTRLON_OFF);
}
}
break;
case CMD_CTRL_ON: /* ctrlON command received */
Serial.println(F("ctrlON received"));
Light::enableCentralCtrl();
ARiF.setCtrlON(ARIF_CTRLON_ENABLED);
Platform.EEPROMSetLightCentral(DIGITOUT_CENTRAL_CTRL_ENABLE);
ARiF.sendSettings();
break;
case CMD_CTRL_OFF: /* ctrlOFF command received */
Serial.println(F("ctrlOFF received"));
Light::disableCentralCtrl();
ARiF.setCtrlON(ARIF_CTRLON_DISABLED);
Platform.EEPROMSetLightCentral(DIGITOUT_CENTRAL_CTRL_DISABLE);
ARiF.sendSettings();
break;
case CMD_RESTORE:
Serial.println(F("Received restore cmd"));
Platform.EEPROMClearUID();
ARiF.sendSettings();
break;
case CMD_DEREGISTER:
Serial.println(F("Received deregister cmd"));
ARiF.deregister();
Platform.EEPROMDeregister();
break;
case U_RASPYIPCHGD:
Serial.println(F("Raspy IP changed"));
iotGwIP = ARiF.getRaspyIP();
Platform.EEPROMSetRaspyIP(iotGwIP);
break;
case CMD_UNKNOWN: /* unknown command received */
Serial.print(F("Received unknown command from: "));
Serial.print(ARiF.getLastDevID());
break;
}
/* universal float var for 1-Wire and RS485 data */
float floatValue;
/*
-----------------------------------
--- Handling of oneWire actions ---
-----------------------------------
*/
Wire.update();
byte deviceCount = Wire.getDeviceCount();
for (byte i = 0; i < deviceCount; i++) {
if (!Wire.isTemperatureRead(40 + i)) {
floatValue = Wire.getTemperature(40 + i);
ARiF.sendTempStatus(40 + i, floatValue);
}
}
/*
---------------------------------
--- Handling of RS485 actions ---
---------------------------------
*/
RS485.update();
if (!RS485.isTemperatureRead(50)) {
floatValue = RS485.getTemperature(50);
Serial.print("Temp: ");
Serial.println(floatValue);
ARiF.sendTempStatus(50, floatValue);
}
if (!RS485.isHumidityRead(51)) {
floatValue = RS485.getHumidity(51);
Serial.print("Humidity: ");
Serial.println(floatValue);
ARiF.sendHumidityStatus(51, floatValue);
}
/* -- measurement code start -- */
uint16_t finish = TCNT1;
uint16_t overhead = 8;
uint16_t cycles = finish - start - overhead;
if (measure) {
Serial.print("cycles: ");
Serial.println(cycles);
}
/* -- measurement code end -- */
}
/*
-----------------
--- Functions ---
-----------------
*/
/* return the shade object with the given devID */
Shade getShade(byte devID) {
for (int i = 0; i < SHADES; i++) {
if (shades[i].getDevID() == devID) return shades[i];
}
}
/* set mode into shades */
void setModeShades() {
funcMode = MODE_SHADES;
WebGUI.setSystemMode(M_WEBGUI_SHADES);
Platform.EEPROMSetMode(MODE_SHADES);
ARiF.setMode(M_SHADES);
ARiF.sendSettings();
for (int i = 0; i < LIGHTS; i++) {
lights[i].reset();
}
initializeShades();
sendShadeStatus();
}
/* set mode into lights */
void setModeLights() {
byte devID;
funcMode = MODE_LIGHTS;
WebGUI.setSystemMode(M_WEBGUI_LIGHTS);
Platform.EEPROMSetMode(MODE_LIGHTS);
ARiF.setMode(M_LIGHTS);
ARiF.sendSettings();
for (int i = 0; i < SHADES; i++) {
shades[i].reset();
}
initializeLights();
sendLightStatus();
}
/* initialize all Lights from the EEPROM */
void initializeLights() {
Serial.println(F("Initialize system as: MODE_LIGHTS"));
byte type;
unsigned long timer;
byte inputType;
byte overrideFlag;
byte devCtrlON;
for (int i = 0; i < LIGHTS; i++) {
type = Platform.EEPROMGetLightType(Platform.lightIDs[i]);
timer = Platform.EEPROMGetLightTimer(Platform.lightIDs[i]);
if (type == DIGITOUT_ONOFF) {
lights[i].init(Platform.lightIDs[i], DIGITOUT_ONOFF);
Serial.print(F("Light: "));
Serial.print(Platform.lightIDs[i]);
Serial.print(F(" set to: DIGITOUT_ONOFF"));
WebGUI.lightInit(i, Platform.lightIDs[i], S_WEBGUI_L_ONOFF);
} else if (type == DIGITOUT_TIMER) {
Serial.print(F("Light: "));
Serial.print(Platform.lightIDs[i]);
Serial.print(F(" set to: DIGITOUT_TIMER, with timer: "));
Serial.print(timer);
lights[i].init(Platform.lightIDs[i], DIGITOUT_TIMER);
WebGUI.lightInit(i, Platform.lightIDs[i], S_WEBGUI_L_TIMER);
WebGUI.lightSetTimer(Platform.lightIDs[i], timer);
} else if (type == DIGITOUT_SIMPLE_HEAT) {
Serial.print(F("Device: "));
Serial.print(Platform.lightIDs[i]);
Serial.print(F(" set to: DIGITOUT_SIMPLE_HEAT"));
lights[i].init(Platform.lightIDs[i], DIGITOUT_SIMPLE_HEAT);
} else if (type == DIGITOUT_COUNTER) {
Serial.print(F("Device: "));
Serial.print(Platform.lightIDs[i]);
Serial.print(F(" set to: DIGITOUT_COUNTER, with timer: "));
Serial.print(timer);
lights[i].init(Platform.lightIDs[i], DIGITOUT_COUNTER);
} else {
/* if type cannot be recognized set it by default to DIGITOUT_ONOFF */
lights[i].init(Platform.lightIDs[i], DIGITOUT_ONOFF);
Serial.print(F("Light: "));
Serial.print(Platform.lightIDs[i]);
Serial.print(F(" set to: DIGITOUT_ONOFF (defaulting)"));
WebGUI.lightInit(i, Platform.lightIDs[i], S_WEBGUI_L_ONOFF);
Platform.EEPROMSetLightConfig(Platform.lightIDs[i], DIGITOUT_ONOFF, DIGITOUT_DEFAULT_TIMER);
}
/* timer must be set after init() */
lights[i].setTimer(timer);
inputType = Platform.EEPROMGetLightInputType(Platform.lightIDs[i]);
Serial.print(F(" , input type: "));
if (inputType == DIGITOUT_SWITCH_PRESS_HOLD) {
lights[i].setInputTypeHold();
Serial.print(F("DIGITOUT_SWITCH_PRESS_HOLD"));
} else if (inputType == DIGITOUT_SWITCH_PRESS_RELEASE) {
lights[i].setInputTypeRelease();
Serial.print(F("DIGITOUT_SWITCH_PRESS_RELEASE"));
} else if (inputType == DIGITOUT_SWITCH_HEAT_OVERRIDE_ON) {
lights[i].setInputTypeSimpleHeatOverrideOn();
Serial.print(F("DIGITOUT_SWITCH_HEAT_OVERRIDE_ON"));
} else if (inputType == DIGITOUT_SWITCH_HEAT_OVERRIDE_OFF) {
lights[i].setInputTypeSimpleHeatOverrideOff();
Serial.print(F("DIGITOUT_SWITCH_HEAT_OVERRIDE_OFF"));
} else if (inputType == DIGITOUT_SWITCH_HEAT_TEMP_SENSOR) {
lights[i].setInputTypeSimpleHeatTempSensor();
Serial.print(F("DIGITOUT_SWITCH_HEAT_TEMP_SENSOR"));
} else {
/* if inputType cannot be recognized set it by default to DIGITOUT_SWITCH_PRESS_RELEASE */
lights[i].setInputTypeRelease();
Platform.EEPROMSetLightInputType(Platform.lightIDs[i], DIGITOUT_SWITCH_PRESS_RELEASE);
Serial.println(F("DIGITOUT_SWITCH_PRESS_RELEASE (defaulting)"));
}
devCtrlON = Platform.EEPROMGetLightCtrlON(Platform.lightIDs[i]);
Serial.print(F(" CtrlON: "));
if (devCtrlON == DIGITOUT_CTRLON_ON) {
lights[i].setCtrlONEnabled();
Serial.print(F("Enabled, "));
} else if (devCtrlON == DIGITOUT_CTRLON_OFF) {
lights[i].setCtrlONDisabled();
Serial.print(F("Disabled, "));
} else {
lights[i].setCtrlONDisabled();
Serial.print(F("Disabled (defaulting), "));
Platform.EEPROMSetLightCtrlON(Platform.lightIDs[i], DIGITOUT_CTRLON_OFF);
}
Serial.print(F("timer: "));
Serial.println(lights[i].getTimer());
}
for (int i = 0; i < LIGHTS; i++) {
WebGUI.lightInit(i, Platform.lightIDs[i], S_WEBGUI_L_TIMER);
}
WebGUI.setSystemMode(M_WEBGUI_LIGHTS);
}
/* Initialize all Shades from EEPROM */
void initializeShades() {
Serial.println(F("Initialize system as: MODE_SHADES"));
int posTimer;
int tiltTimer;
bool synced;
int position;
int tilt;
byte reachedPosition;
for (int i = 0; i < SHADES; i++) {
posTimer = Platform.EEPROMGetShadePosTimer(Platform.shadeIDs[i]);
tiltTimer = Platform.EEPROMGetShadeTiltTimer(Platform.shadeIDs[i]);
synced = Platform.EEPROMGetShadeSyncFlag(Platform.shadeIDs[i]);
/* if recorded EEPROM value is different than the allowed ones, overwrite EEPROM with default */
/*if (posTimer != Shade::validatePositionTimer(posTimer)) {
Platform.EEPROMSetShadePosTimer(Platform.shadeIDs[i], Shade::validatePositionTimer(posTimer));
posTimer = Platform.EEPROMGetShadePosTimer(Platform.shadeIDs[i]);
}
if (tiltTimer != Shade::validateTiltTimer(tiltTimer)) {
Platform.EEPROMSetShadeTiltTimer(Platform.shadeIDs[i], Shade::validateTiltTimer(tiltTimer));
tiltTimer = Platform.EEPROMGetShadeTiltTimer(Platform.shadeIDs[i]);
}*/
if (synced) {
position = Platform.EEPROMGetShadePosition(Platform.shadeIDs[i]);
tilt = Platform.EEPROMGetShadeTilt(Platform.shadeIDs[i]);
reachedPosition = Platform.EEPROMGetShadeReachedPosition(Platform.shadeIDs[i]);
shades[i].init(Settings::shadeIDs[i], synced, tilt, position, reachedPosition, posTimer, tiltTimer);
} else {
shades[i].init(Settings::shadeIDs[i], synced, TILT_H_CLOSED, 0, 0, posTimer, tiltTimer);
}
WebGUI.shadeInit(i, Settings::shadeIDs[i]);
Serial.print(F("Shade: "));
Serial.print(shades[i].getDevID());
Serial.print(F(", posTimer: "));
Serial.print(posTimer);
Serial.print(F(", tiltTimer: "));
Serial.print(tiltTimer);
Serial.print(F(", synced: "));
if (synced) {
Serial.print(synced);
Serial.print(F(", position: "));
Serial.print(position);
Serial.print(F(", tilt: "));
Serial.println(tilt);
} else {
Serial.println(synced);
}
//shades[i].setPositionTimer(posTimer);
//shades[i].setTiltTimer(tiltTimer);
}
for (int i = 0; i < SHADES; i++) {
WebGUI.shadeInit(i, Settings::shadeIDs[i]);
}
WebGUI.setSystemMode(M_WEBGUI_SHADES);
}
/* ARiF send all Shades statuses and settings */
void sendShadeStatus() {
for (int i = 0; i < SHADES; i++) {
if (shades[i].isSynced()) {
ARiF.sendShadeSynced(Settings::shadeIDs[i]);
ARiF.sendShadeTilt(Settings::shadeIDs[i], shades[i].getTilt());
ARiF.sendShadePosition(Settings::shadeIDs[i], shades[i].getCurrentPosition());
} else {
ARiF.sendShadeUnsynced(Settings::shadeIDs[i]);
}
ARiF.sendShadeSettings(Settings::shadeIDs[i], shades[i].getPositionTimer(), shades[i].getTiltTimer());
}
}
/* ARiF send all Lights statuses and settings */
void sendLightStatus() {
byte devID;
byte lightType;
byte lightInputType;
for (int i = 0; i < LIGHTS; i++) {
devID = lights[i].getDevID();
lightType = lights[i].getType();
lightInputType = lights[i].getInputType();
if (lights[i].getStatus()) {
ARiF.sendLightON(devID);
} else {
ARiF.sendLightOFF(devID);
}
ARiF.sendLightSettings(devID, lights[i].getTimer(), lightType, lightInputType, lights[i].getCtrlON());
}
}