-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsonoff-knxd.ino
1075 lines (896 loc) · 41.4 KB
/
sonoff-knxd.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
/*
* Notwendige Einstellungen in der Arduino IDE
*
* Boardverwalter-URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
*
* Sonoff S20
* Board: Generic ESP8266 Module
* Flash Mode: DOUT
* Flash Size: 1M (64K SPIFFS) [Notwendig für Updates über die Weboberfläche]
*
* Sonoff 4CH
* Board: Generic ESP8285 Module
* Flash Size: 1M (64K SPIFFS) [Notwendig für Updates über die Weboberfläche]
*/
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
// https://github.com/knxd/knxd/blob/master/src/include/eibtypes.h
#include "eibtypes.h"
/*
* *******************************
* *** Laden der Konfiguration ***
* *******************************
*/
#include "Configuration.h"
#if SCD30_ENABLE == true
// https://www.arduino.cc/en/reference/wire
#include <Wire.h>
// https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library
#include <SparkFun_SCD30_Arduino_Library.h>
#if LCD_ENABLE == true
// https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight
#include <rgb_lcd.h>
#endif
#endif
/*
* *************************
* *** Interne Variablen ***
* *************************
*/
const char *SOFTWARE_VERSION = "2021-12-03",
*LOG_WLAN_CONNECTION_INITIATED = "WLAN-Verbindung initiiert",
*LOG_WLAN_CONNECTED = "WLAN-Verbindung hergestellt",
*LOG_WLAN_DHCP_COMPLETED = "IP-Adresse per DHCP erhalten",
*LOG_WLAN_DISCONNECTED = "WLAN-Verbindung getrennt",
*LOG_WLAN_CONNECTION_TIMEOUT = "Zeitüberschreitungen beim Aufbau der WLAN-Verbindung",
*LOG_WLAN_DHCP_TIMEOUT = "DHCP-Zeitüberschreitungen",
*LOG_KNXD_CONNECTION_INITIATED = "Initiale Verbindung zum knxd hergestellt",
*LOG_KNXD_CONNECTION_HANDSHAKE_TIMEOUT = "Zeitüberschreitungen bei der Verbindungsbestätigung durch den knxd",
*LOG_KNXD_CONNECTION_CONFIRMED = "Verbindung vom knxd bestätigt",
*LOG_KNXD_DISCONNECTED = "Verbindung zum knxd getrennt",
*LOG_MISSING_TELEGRAM_TIMEOUT = "Verbindungsabbruch wegen Zeitüberschreitung zwischen zwei Telegrammen",
*LOG_INCOMPLETE_TELEGRAM_TIMEOUT = "Verbindungsabbruch wegen unvollständig empfangenem Telegramm",
*SWITCH_LOG_OFF = "Ausgeschaltet",
*SWITCH_LOG_ON = "Eingeschaltet",
*SWITCH_LOG_LOCK = "Gesperrt",
*SWITCH_LOG_UNLOCK = "Entsperrt",
*SWITCH_LOG_BUTTON = "Taster",
*SWITCH_LOG_ON_BY_LOCK = "Sperrautomatik",
*SWITCH_LOG_OFF_BY_UNLOCK = "Entsperrautomatik",
*SWITCH_LOG_AUTO_OFF_TIMER = "Zeitschalter",
*SWITCH_LOG_AUTO_UNLOCK_TIMER = "Zeitschalter",
*SWITCH_LOG_WEBSERVER = "Webserver";
const uint8_t GA_SWITCH_COUNT = sizeof(GA_SWITCH[0]) / 3,
GA_LOCK_COUNT = sizeof(GA_LOCK[0]) / 3,
KNXD_GROUP_CONNECTION_REQUEST[] = {0x00, 0x05, EIB_OPEN_GROUPCON >> 8, EIB_OPEN_GROUPCON & 0xFF, 0x00, 0x00, 0x00};
const boolean GA_DATE_VALID = GA_DATE[0] + GA_DATE[1] + GA_DATE[2] > 0,
GA_TIME_VALID = GA_TIME[0] + GA_TIME[1] + GA_TIME[2] > 0;
uint8_t messageLength = 0,
messageResponse[32],
wifiConnected = 0,
timeWeekday = 0,
timeHours = 0,
timeMinutes = 0,
timeSeconds = 0,
dateDay = 0,
dateMonth = 0;
uint16_t dateYear = 0;
time_t bootTime = 0;
boolean knxdConnectionInitiated = false,
knxdConnectionConfirmed = false,
lockActive[] = {false, false, false, false},
buttonLastState[] = {true, true, true, true},
buttonDebouncedState[] = {true, true, true, true},
autoOffTimerActive[] = {false, false, false, false},
autoUnlockTimerActive[] = {false, false, false, false},
relayStatus[] = {false, false, false, false},
ledBlinkStatus = false,
timeValid = false,
dateValid = false,
missingTelegramTimeoutEnabled = false,
incompleteTelegramTimeoutEnabled = false;
uint32_t knxdConnectionInitiatedCount = 0,
knxdConnectionFailedCount = 0,
knxdConnectionHandshakeTimeouts = 0,
knxdConnectionConfirmedCount = 0,
receivedTelegrams = 0,
missingTelegramTimeouts = 0,
incompleteTelegramTimeouts = 0,
wifiDisconnections = 0,
knxdDisconnections = 0,
// Variablen zur Zeitmessung
currentMillis = 0,
currentMillisTemp = 0,
millisOverflows = 0,
buttonDebounceMillis[] = {0, 0, 0, 0},
autoOffTimerStartMillis[] = {0, 0, 0, 0},
autoUnlockTimerStartMillis[] = {0, 0, 0, 0},
knxdConnectionInitiatedMillis = 0,
knxdConnectionFailedMillis = CONNECTION_LOST_DELAY_S * 1000,
ledBlinkLastSwitch = 0,
lastTelegramReceivedMillis = 0,
lastTelegramHeaderReceivedMillis = 0,
timeTelegramReceivedMillis = 0,
dateTelegramReceivedMillis = 0,
wifiConnectionInitiatedMillis = 0,
wifiDisconnectedMillis = WIFI_CONNECTION_LOST_DELAY_S * 1000,
lastAirMeasurementReceivedMillis = 0,
lastAirSensorPolledMillis = 0,
airCO2LastSentMillis = 0,
airTemperatureLastSentMillis = 0,
airHumidityLastSentMillis = 0;
#if SCD30_ENABLE == true
// Objekt for SDC30 environment sensor
SCD30 airSensorSCD30;
#if LCD_ENABLE == true
// Object for LCD
rgb_lcd lcd;
#endif
#endif
// Variables for air quality
float airTemperature = 0.0,
airTemperatureLastSent = 0.0,
airHumidity = 0.0,
airHumidityLastSent = 0.0;
uint16_t airCO2 = 0,
airCO2LastSent = 0;
// WLAN-Client
WiFiClient client;
WiFiEventHandler connectHandler,
disconnectHandler,
gotIpHandler,
dhcpTimeoutHandler;
// Webserver
ESP8266WebServer webServer(80);
ESP8266HTTPUpdateServer httpUpdateServer;
// Variablen zur Ereignisspeicherung
const uint32_t LOG_SIZE = 100;
uint32_t connectionLogEntries = 0;
uint32_t switchLogEntries = 0;
struct logConnectionEvent {
const char *message;
uint32_t entry,
uptimeSeconds;
time_t timestamp;
boolean timestampValid;
int32_t wlanChannel;
uint8_t wlanBssid[6];
} connectionLogRingbuffer[LOG_SIZE];
struct logSwitchEvent {
const uint8_t *ga;
const char *type,
*message;
uint8_t channel;
uint32_t entry,
uptimeSeconds;
time_t timestamp;
boolean timestampValid;
} switchLogRingbuffer[LOG_SIZE];
void setup() {
Serial.begin(115200);
delay(10);
pinMode(GPIO_LED, OUTPUT);
digitalWrite(GPIO_LED, !relayStatus[0]);
Serial.print("\n\n");
Serial.print(HOST_NAME);
Serial.print(" (");
Serial.print(HOST_DESCRIPTION);
Serial.println(")");
Serial.print("Software-Version: ");
Serial.print(SOFTWARE_VERSION);
Serial.println("\n");
for (uint8_t ch=0; ch<CHANNELS; ch++){
pinMode(GPIO_BUTTON[ch], INPUT_PULLUP);
pinMode(GPIO_RELAY[ch], OUTPUT);
if (RELAY_NORMALLY_OPEN[ch])
digitalWrite(GPIO_RELAY[ch], relayStatus[ch]);
else
digitalWrite(GPIO_RELAY[ch], !relayStatus[ch]);
Serial.print("Kanal ");
Serial.print(ch + 1);
Serial.print(" GAs schalten: ");
for (uint8_t i=0; i<GA_SWITCH_COUNT; i++){
if (GA_SWITCH[ch][i][0] + GA_SWITCH[ch][i][1] + GA_SWITCH[ch][i][2] > 0) {
Serial.print(GA_SWITCH[ch][i][0]);
Serial.print("/");
Serial.print(GA_SWITCH[ch][i][1]);
Serial.print("/");
Serial.print(GA_SWITCH[ch][i][2]);
Serial.print(" ");
}
}
Serial.println();
Serial.print("Kanal ");
Serial.print(ch + 1);
Serial.print(" GAs sperren: ");
for (uint8_t i=0; i<GA_LOCK_COUNT; i++){
if (GA_LOCK[ch][i][0] + GA_LOCK[ch][i][1] + GA_LOCK[ch][i][2] > 0) {
Serial.print(GA_LOCK[ch][0][0]);
Serial.print("/");
Serial.print(GA_LOCK[ch][0][1]);
Serial.print("/");
Serial.print(GA_LOCK[ch][0][2]);
Serial.print(" ");
}
}
Serial.println();
Serial.print("Kanal ");
Serial.print(ch + 1);
Serial.print(" GA Status: ");
if (GA_STATUS[ch][0] + GA_STATUS[ch][1] + GA_STATUS[ch][2] > 0) {
Serial.print(GA_STATUS[ch][0]);
Serial.print("/");
Serial.print(GA_STATUS[ch][1]);
Serial.print("/");
Serial.print(GA_STATUS[ch][2]);
}
Serial.println();
}
Serial.print("GA Zeit: ");
Serial.print(GA_TIME[0]);
Serial.print("/");
Serial.print(GA_TIME[1]);
Serial.print("/");
Serial.println(GA_TIME[2]);
Serial.print("GA Datum: ");
Serial.print(GA_DATE[0]);
Serial.print("/");
Serial.print(GA_DATE[1]);
Serial.print("/");
Serial.println(GA_DATE[2]);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.hostname(HOST_NAME);
WiFi.persistent(false);
WiFi.setAutoReconnect(false);
setupWebServer();
connectHandler = WiFi.onStationModeConnected(onWifiConnected);
disconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
gotIpHandler = WiFi.onStationModeGotIP(onWifiGotIP);
dhcpTimeoutHandler = WiFi.onStationModeDHCPTimeout(onWifiDhcpTimeout);
#if SCD30_ENABLE == true
// Initialize I2C bus
Wire.begin();
if (Wire.status() != I2C_OK) Serial.println("Something wrong with I2C");
// Initialize SCD30 environment sensor
if (airSensorSCD30.begin() == false) {
Serial.println("The SCD30 did not respond. Please check wiring.");
while(true) {
yield();
delay(1);
}
}
// Sensirion no auto calibration
airSensorSCD30.setAutoSelfCalibration(false);
airSensorSCD30.setTemperatureOffset(AIR_SENSOR_TEMPERATURE_OFFSET_K);
airSensorSCD30.setAltitudeCompensation(AIR_SENSOR_ALTITUDE_COMPENSATION_M);
// Measure air every AIR_SENSOR_MEASUREMENT_INTERVAL_S seconds
airSensorSCD30.setMeasurementInterval(AIR_SENSOR_MEASUREMENT_INTERVAL_S);
Wire.setClock(100000L); // 100 kHz SCD30
Wire.setClockStretchLimit(200000L);// CO2-SCD30
#if LCD_ENABLE == true
// Initialize LCD with 16 columns and 2 rows
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Initialization");
#endif
#endif
}
void onWifiConnected(const WiFiEventStationModeConnected& event) {
wifiConnected = 2;
logConnectionEvent(LOG_WLAN_CONNECTED);
Serial.print("WLAN-Verbindung hergestellt mit ");
Serial.print(WiFi.BSSIDstr());
Serial.print(" auf Kanal ");
Serial.println(WiFi.channel());
}
void onWifiGotIP(const WiFiEventStationModeGotIP& event) {
wifiConnected = 3;
logConnectionEvent(LOG_WLAN_DHCP_COMPLETED);
Serial.print("IP-Adresse: ");
Serial.println(WiFi.localIP());
}
void onWifiDhcpTimeout() {
wifiConnected = 0;
wifiDisconnectedMillis = currentMillis;
WiFi.disconnect();
logConnectionEvent(LOG_WLAN_DHCP_TIMEOUT);
Serial.println(LOG_WLAN_DHCP_TIMEOUT);
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
wifiConnected = 0;
wifiDisconnectedMillis = currentMillis;
wifiDisconnections++;
resetKnxdConnection();
logConnectionEvent(LOG_WLAN_DISCONNECTED);
Serial.print(LOG_WLAN_DISCONNECTED);
Serial.print(": ");
Serial.println(event.reason);
}
void loop() {
currentMillisTemp = millis();
// Überlauf von millis()
if (currentMillis > currentMillisTemp) {
millisOverflows++;
}
currentMillis = currentMillisTemp;
for (uint8_t ch=0; ch<CHANNELS; ch++) {
// Check hardware buttons
checkButton(ch);
// Check if a channel has to be switched off by a timer
if (relayStatus[ch] && autoOffTimerActive[ch] && (currentMillis - autoOffTimerStartMillis[ch]) >= (AUTO_OFF_TIMER_S[ch] * 1000)){
Serial.print("Auto off timer for channel ");
Serial.print(ch + 1);
Serial.println(" has expired!");
// Set to false even if the channel cannot be switched off due to an active lock.
autoOffTimerActive[ch] = false;
switchRelay(ch, false, AUTO_OFF_TIMER_OVERRIDES_LOCK, SWITCH_LOG_AUTO_OFF_TIMER, 0);
}
// Check if a channel has to be unlocked by a timer
if (lockActive[ch] && autoUnlockTimerActive[ch] && (currentMillis - autoUnlockTimerStartMillis[ch]) >= (AUTO_UNLOCK_TIMER_S[ch] * 1000)){
Serial.print("Auto unlock timer for channel ");
Serial.print(ch + 1);
Serial.println(" has expired!");
autoUnlockTimerActive[ch] = false;
lockRelay(ch, false, SWITCH_LOG_AUTO_UNLOCK_TIMER, 0);
}
}
if (WiFi.status() != WL_CONNECTED || wifiConnected < 3) {
// Wifi connection not yet initialized or delay has expired
if (wifiConnected == 0 && (currentMillis - wifiDisconnectedMillis) >= WIFI_CONNECTION_LOST_DELAY_S * 1000) {
wifiConnected = 1;
wifiConnectionInitiatedMillis = currentMillis;
WiFi.begin(SSID, PASSWORD);
logConnectionEvent(LOG_WLAN_CONNECTION_INITIATED);
Serial.print("Verbinde mit WLAN '");
Serial.print(SSID);
Serial.println("'");
}
// The connection is already initialized
else if (wifiConnected > 0 && (currentMillis - wifiConnectionInitiatedMillis) >= WIFI_CONNECTION_TIMEOUT_S * 1000) {
wifiConnected = 0;
wifiDisconnectedMillis = currentMillis;
WiFi.disconnect();
logConnectionEvent(LOG_WLAN_CONNECTION_TIMEOUT);
Serial.println(LOG_WLAN_CONNECTION_TIMEOUT);
}
}
// WLAN connected and IP address received
else if (wifiConnected == 3) {
// KNX-Kommunikation
knxLoop();
// Webserver
webServer.handleClient();
// Falls eine Verbindung zum EIBD/KNXD aufgebaut ist, blinkt die LED sofern gewünscht.
if (LED_BLINKS_WHEN_CONNECTED)
ledBlink();
}
#if SCD30_ENABLE == true
if ((currentMillis - lastAirSensorPolledMillis) >= AIR_SENSOR_POLL_INTERVAL_S * 1000)
measureAir();
#endif
}
void knxLoop(){
// Keine Verbindung zum knxd
if (!knxdConnectionInitiated){
// Pause zwischen zwei Verbindungsversuchen abgelaufen
if ((currentMillis - knxdConnectionFailedMillis) >= CONNECTION_LOST_DELAY_S * 1000){
connectToKnxd();
}
}
// Die Verbindung zum knxd wurde unterbrochen
else if (!client.connected()){
Serial.println("Die Verbindung zum knxd wurde getrennt.");
logConnectionEvent(LOG_KNXD_DISCONNECTED);
knxdDisconnections++;
resetKnxdConnection();
}
// Der Verbindungsaufbau zum knxd wurde nicht rechtzeitig bestätigt
else if (!knxdConnectionConfirmed && (currentMillis - knxdConnectionInitiatedMillis) >= CONNECTION_CONFIRMATION_TIMEOUT_MS){
Serial.print("Der Verbindungsaufbau zum knxd wurde nicht innerhalb von ");
Serial.print(CONNECTION_CONFIRMATION_TIMEOUT_MS);
Serial.println(" ms bestätigt.");
logConnectionEvent(LOG_KNXD_CONNECTION_HANDSHAKE_TIMEOUT);
knxdConnectionHandshakeTimeouts++;
resetKnxdConnection();
}
// Die Verbindung steht prinzipiell, aber seit MISSING_TELEGRAM_TIMEOUT_MIN wurde kein Telegramm mehr empfangen und deshalb wird ein Timeout ausgelöst
else if (missingTelegramTimeoutEnabled && MISSING_TELEGRAM_TIMEOUT_MIN > 0 && (currentMillis - lastTelegramReceivedMillis) >= MISSING_TELEGRAM_TIMEOUT_MIN * 60000){
Serial.print("Timeout: No telegram received during ");
Serial.print(MISSING_TELEGRAM_TIMEOUT_MIN);
Serial.println(" minutes");
logConnectionEvent(LOG_MISSING_TELEGRAM_TIMEOUT);
missingTelegramTimeouts++;
resetKnxdConnection();
}
// Die Verbindung ist etabliert
else {
// Die Länge einer neuen Nachricht lesen
if (messageLength == 0 && client.available() >= 2){
messageLength = (((int) client.read()) << 8) + client.read();
lastTelegramHeaderReceivedMillis = currentMillis;
incompleteTelegramTimeoutEnabled = true;
}
// Die Nutzdaten einer Nachricht lesen
if (messageLength > 0 && client.available() >= messageLength){
client.read(messageResponse, messageLength);
// Prüfen, ob der initiale Verbindungsaufbau korrekt bestätigt wurde
if (!knxdConnectionConfirmed && messageLength == 2 && messageResponse[0] == KNXD_GROUP_CONNECTION_REQUEST[2] && messageResponse[1] == KNXD_GROUP_CONNECTION_REQUEST[3]){
Serial.println("EIBD/KNXD Verbindung hergestellt");
logConnectionEvent(LOG_KNXD_CONNECTION_CONFIRMED);
knxdConnectionConfirmed = true;
knxdConnectionConfirmedCount++;
// Auch wenn bisher kein echtes Telegramm vom Bus kam, die Variablen hier bereits entsprechend setzen,
// da ansonsten ein dauerhaft toter Bus nicht über den Timeout erkannt würde.
lastTelegramReceivedMillis = currentMillis;
missingTelegramTimeoutEnabled = true;
// Die Status-GA senden, sobald die Verbindung steht. Dadurch wird sie beim ersten Start nach einem Spannungsausfall gesendet.
for (uint8_t ch= 0; ch<CHANNELS; ch++)
writeGA(GA_STATUS[ch], relayStatus[ch]);
if (REQUEST_DATE_AND_TIME_INITIALLY){
if (GA_DATE_VALID && !dateValid)
readGA(GA_DATE);
if (GA_TIME_VALID && !timeValid)
readGA(GA_TIME);
}
}
if ( messageLength >= 8
&& messageResponse[0] == EIB_GROUP_PACKET >> 8
&& messageResponse[1] == EIB_GROUP_PACKET & 0xFF){
receivedTelegrams++;
lastTelegramReceivedMillis = currentMillis;
missingTelegramTimeoutEnabled = true;
// Serial.printf("Received group packet telegram (#%d): ", receivedTelegrams);
// for (uint8_t i=0; i<messageLength; i++){
// Serial.printf("%02X", messageResponse[i]);
// Serial.print(" ");
// }
// Serial.printf("(%d.%d.%d -> ", messageResponse[2] >> 4, messageResponse[2] & 0xF, messageResponse[3]);
// Serial.printf("%d/%d/%d, ", messageResponse[4] >> 3, messageResponse[4] & 0x7, messageResponse[5]);
// if (messageResponse[6] == 0x00 && (messageResponse[7] & 0x80))
// Serial.print("GroupValueWrite");
// else if (messageResponse[6] == 0x00 && (messageResponse[7] & 0x40))
// Serial.print("GroupValueResponse");
// else if (messageResponse[6] == 0x00 && messageResponse[7] == 0x00)
// Serial.print("GroupValueRead");
// Serial.println(")");
// Schalten oder sperren
if ( messageLength == 8
&& messageResponse[6] == 0x00
&& (messageResponse[7] & 0x80) == 0x80){
// Die übertragene Gruppenadresse mit denen aller Kanäle vergleichen
for (uint8_t ch=0; ch<CHANNELS; ch++){
for (uint8_t i=0; i<GA_SWITCH_COUNT; i++){
// Schalten
if (GA_SWITCH[ch][i][0] + GA_SWITCH[ch][i][1] + GA_SWITCH[ch][i][2] > 0
&& messageResponse[4] == (GA_SWITCH[0][i][0] << 3) + GA_SWITCH[0][i][1]
&& messageResponse[5] == GA_SWITCH[0][i][2]){
switchRelay(ch, messageResponse[7] & 0x0F, false, 0, &GA_SWITCH[ch][i][0]);
}
}
for (uint8_t i=0; i<GA_LOCK_COUNT; i++){
// Sperren
if (GA_LOCK[ch][i][0] + GA_LOCK[ch][i][1] + GA_LOCK[ch][i][2] > 0
&& messageResponse[4] == (GA_LOCK[0][i][0] << 3) + GA_LOCK[0][i][1]
&& messageResponse[5] == GA_LOCK[0][i][2]){
lockRelay(ch, (messageResponse[7] & 0x0F) ^ LOCK_INVERTED[ch], 0, &GA_LOCK[ch][i][0]);
}
}
}
}
// Status lesen
else if (messageLength == 8
&& messageResponse[6] == 0x00
&& messageResponse[7] == 0x00){
// Die übertragene Gruppenadresse denen aller Kanäle vergleichen
for (uint8_t ch=0; ch<CHANNELS; ch++){
if (GA_STATUS[ch][0] + GA_STATUS[ch][1] + GA_STATUS[ch][2] > 0
&& messageResponse[4] == (GA_STATUS[ch][0] << 3) + GA_STATUS[ch][1] && messageResponse[5] == GA_STATUS[ch][2]) {
Serial.print("Leseanforderung Status Kanal ");
Serial.println(ch + 1);
responseGA(GA_STATUS[ch], relayStatus[ch]);
}
}
#if SCD30_ENABLE == true
// Read GA CO2
if (GA_AIR_CO2[0] + GA_AIR_CO2[1] + GA_AIR_CO2[2] > 0
&& messageResponse[4] == (GA_AIR_CO2[0] << 3) + GA_AIR_CO2[1] && messageResponse[5] == GA_AIR_CO2[2]) {
responseGA(GA_AIR_CO2, encodeDpt9Int(airCO2));
}
// Read GA Temperature
if (GA_AIR_TEMPERATURE[0] + GA_AIR_TEMPERATURE[1] + GA_AIR_TEMPERATURE[2] > 0
&& messageResponse[4] == (GA_AIR_TEMPERATURE[0] << 3) + GA_AIR_TEMPERATURE[1] && messageResponse[5] == GA_AIR_TEMPERATURE[2]) {
responseGA(GA_AIR_TEMPERATURE, encodeDpt9Float(airTemperature));
}
// Read GA Humidity
if (GA_AIR_HUMIDITY[0] + GA_AIR_HUMIDITY[1] + GA_AIR_HUMIDITY[2] > 0
&& messageResponse[4] == (GA_AIR_HUMIDITY[0] << 3) + GA_AIR_HUMIDITY[1] && messageResponse[5] == GA_AIR_HUMIDITY[2]) {
responseGA(GA_AIR_HUMIDITY, encodeDpt9Float(airHumidity));
}
#endif
}
// Time or Date received
else if (messageLength == 11
&& messageResponse[6] == 0x00
&& (messageResponse[7] & 0xC0)){ // Accept GroupValueWrite (0x80) and GroupValueResponse (0x40)
// Date telegram
if (GA_DATE_VALID && messageResponse[4] == (GA_DATE[0] << 3) + GA_DATE[1] && messageResponse[5] == GA_DATE[2]) {
dateDay = messageResponse[8] & 0x1F;
dateMonth = messageResponse[9] & 0x0F;
dateYear = (messageResponse[10] & 0x7F);
dateYear = dateYear >= 90 ? 1900 + dateYear : 2000 + dateYear;
dateTelegramReceivedMillis = currentMillis;
Serial.printf("Date telegram received: %04d-%02d-%02d\n", dateYear, dateMonth, dateDay);
Serial.printf("Old date and time: %04d-%02d-%02d %02d:%02d:%02d (%1d)\n", year(), month(), day(), hour(), minute(), second(), timeStatus());
setTime(hour(), minute(), second(), dateDay, dateMonth, dateYear);
Serial.printf("New date and time: %04d-%02d-%02d %02d:%02d:%02d (%1d)\n", year(), month(), day(), hour(), minute(), second(), timeStatus());
if (!dateValid || !timeValid){
bootTime = now() - getUptimeSeconds();
}
dateValid = true;
}
// Time telegram
if (GA_TIME_VALID && messageResponse[4] == (GA_TIME[0] << 3) + GA_TIME[1] && messageResponse[5] == GA_TIME[2]) {
timeWeekday = messageResponse[8] >> 5;
timeHours = messageResponse[8] & 0x1F;
timeMinutes = messageResponse[9] & 0x3F;
timeSeconds = messageResponse[10] & 0x3F;
timeTelegramReceivedMillis = currentMillis;
Serial.printf("Time telegram received: %02d:%02d:%02d\n", timeHours, timeMinutes, timeSeconds);
Serial.printf("Old date and time: %04d-%02d-%02d %02d:%02d:%02d (%1d)\n", year(), month(), day(), hour(), minute(), second(), timeStatus());
setTime(timeHours, timeMinutes, timeSeconds, day(), month(), year());
Serial.printf("New date and time: %04d-%02d-%02d %02d:%02d:%02d (%1d)\n", year(), month(), day(), hour(), minute(), second(), timeStatus());
if (!dateValid || !timeValid){
bootTime = now() - getUptimeSeconds();
}
timeValid = true;
}
}
}
// Für die nächste Nachricht zurücksetzen
messageLength = 0;
}
// Incomplete telegram received timeout
else if (incompleteTelegramTimeoutEnabled && INCOMPLETE_TELEGRAM_TIMEOUT_MS > 0 && messageLength > 0 && (currentMillis - lastTelegramHeaderReceivedMillis) >= INCOMPLETE_TELEGRAM_TIMEOUT_MS){
Serial.print("Timeout: Incomplete received telegram after ");
Serial.print(INCOMPLETE_TELEGRAM_TIMEOUT_MS);
Serial.println(" ms");
logConnectionEvent(LOG_INCOMPLETE_TELEGRAM_TIMEOUT);
incompleteTelegramTimeouts++;
resetKnxdConnection();
}
}
}
boolean connectToKnxd(){
resetKnxdConnection();
Serial.print("Verbinde mit knxd auf ");
Serial.println(KNXD_IP);
if (client.connect(KNXD_IP, KNXD_PORT)) {
client.setNoDelay(true);
client.keepAlive(KA_IDLE_S, KA_INTERVAL_S, KA_RETRY_COUNT);
client.write(KNXD_GROUP_CONNECTION_REQUEST, sizeof(KNXD_GROUP_CONNECTION_REQUEST));
knxdConnectionInitiated = true;
knxdConnectionInitiatedMillis = currentMillis;
knxdConnectionInitiatedCount++;
logConnectionEvent(LOG_KNXD_CONNECTION_INITIATED);
return client.connected();
}
else{
Serial.println("Verbindung fehlgeschlagen!");
knxdConnectionFailedMillis = currentMillis;
knxdConnectionFailedCount++;
return false;
}
}
void resetKnxdConnection(){
client.stop();
knxdConnectionInitiated = false;
knxdConnectionConfirmed = false;
missingTelegramTimeoutEnabled = false;
incompleteTelegramTimeoutEnabled = false;
knxdConnectionFailedMillis = currentMillis;
messageLength = 0;
}
void checkButton(uint8_t ch){
// Button pressed (true = released, false = pressed)
if (digitalRead(GPIO_BUTTON[ch]) == BUTTON_INVERTED){
// Button state changed, start debouncing timer
if (buttonLastState[ch] != BUTTON_INVERTED)
buttonDebounceMillis[ch] = currentMillis;
// Check debouncing timer
else if (buttonDebouncedState[ch] == !BUTTON_INVERTED && (currentMillis - buttonDebounceMillis[ch]) >= BUTTON_DEBOUNCING_TIME_MS) {
buttonDebouncedState[ch] = BUTTON_INVERTED;
if (BUTTON_TOGGLE)
switchRelay(ch, !relayStatus[ch], false, SWITCH_LOG_BUTTON, 0);
else
switchRelay(ch, true, false, SWITCH_LOG_BUTTON, 0);
}
buttonLastState[ch] = BUTTON_INVERTED;
}
// Button released
else {
// Button state changed, start debouncing timer
if (buttonLastState[ch] == BUTTON_INVERTED)
buttonDebounceMillis[ch] = currentMillis;
// Check debouncing timer
else if (buttonDebouncedState[ch] == BUTTON_INVERTED && (currentMillis - buttonDebounceMillis[ch]) >= BUTTON_DEBOUNCING_TIME_MS) {
buttonDebouncedState[ch] = !BUTTON_INVERTED;
// Switch relay off if button is not in toggle mode
if (!BUTTON_TOGGLE && relayStatus[ch])
switchRelay(ch, false, false, SWITCH_LOG_BUTTON, 0);
}
buttonLastState[ch] = !BUTTON_INVERTED;
}
}
void ledBlink(){
// Falls die Verbindung steht, die LED blinken lassen.
if (knxdConnectionConfirmed) {
// LED-Blinkstatus ist zurzeit an
if (ledBlinkStatus) {
if ((currentMillis - ledBlinkLastSwitch) >= LED_BLINK_ON_TIME_MS){
// LED invertieren
if (LED_SHOWS_RELAY_STATUS && relayStatus[0])
digitalWrite(GPIO_LED, LOW);
else
digitalWrite(GPIO_LED, HIGH);
ledBlinkStatus = false;
ledBlinkLastSwitch = currentMillis;
}
}
// LED-Blinkstatus ist zurzeit aus
else {
if ((currentMillis - ledBlinkLastSwitch) >= LED_BLINK_OFF_TIME_MS){
// LED invertieren
if (LED_SHOWS_RELAY_STATUS && relayStatus[0])
digitalWrite(GPIO_LED, HIGH);
else
digitalWrite(GPIO_LED, LOW);
ledBlinkStatus = true;
ledBlinkLastSwitch = currentMillis;
}
}
}
// Die Verbindung ist unterbrochen, auf den Zustand des Relais setzen
else if (LED_SHOWS_RELAY_STATUS)
digitalWrite(GPIO_LED, !relayStatus[0]);
// Die LED wird nicht für den Relais-Status verwendet, ausschalten
else
digitalWrite(GPIO_LED, HIGH);
}
#if SCD30_ENABLE == true
void measureAir(){
if (airSensorSCD30.dataAvailable()) {
airCO2 = airSensorSCD30.getCO2();
airTemperature = airSensorSCD30.getTemperature();
airHumidity = airSensorSCD30.getHumidity();
lastAirMeasurementReceivedMillis = currentMillis;
#if LCD_ENABLE == true
static char lcdRow1[17],
lcdRow2[17];
snprintf(lcdRow1, 17, "%02d:%02d %6d ppm", hour(), minute(), airCO2);
snprintf(lcdRow2, 17, "%4.1f %cC %5.1f %%", airTemperature, 223, airHumidity);
lcd.setCursor(0,0);
lcd.print(lcdRow1);
lcd.setCursor(0,1);
lcd.print(lcdRow2);
#endif
// Send values to KNX
if (client.connected() && knxdConnectionConfirmed){
uint16_t airCO2Diff = airCO2LastSent > airCO2 ? airCO2LastSent - airCO2 : airCO2 - airCO2LastSent;
float airTemperatureDiff = airTemperatureLastSent > airTemperature ? airTemperatureLastSent - airTemperature : airTemperature - airTemperatureLastSent,
airHumidityDiff = airHumidityLastSent > airHumidity ? airHumidityLastSent - airHumidity : airHumidity - airHumidityLastSent;
if ((currentMillis - airCO2LastSentMillis) >= KNX_SEND_INTERVAL_CO2_S * 1000 || airCO2Diff >= KNX_SEND_DIFFERENCE_VALUE_CO2){
writeGA(GA_AIR_CO2, encodeDpt9Int(airCO2));
airCO2LastSent = airCO2;
airCO2LastSentMillis = currentMillis;
}
if ((currentMillis - airTemperatureLastSentMillis) >= KNX_SEND_INTERVAL_TEMPERATURE_S * 1000 || airTemperatureDiff >= KNX_SEND_DIFFERENCE_VALUE_TEMPERATURE){
writeGA(GA_AIR_TEMPERATURE, encodeDpt9Float(airTemperature));
airTemperatureLastSent = airTemperature;
airTemperatureLastSentMillis = currentMillis;
}
if ((currentMillis - airHumidityLastSentMillis) >= KNX_SEND_INTERVAL_HUMIDITY_S * 1000 || airHumidityDiff >= KNX_SEND_DIFFERENCE_VALUE_HUMIDITY){
writeGA(GA_AIR_HUMIDITY, encodeDpt9Float(airHumidity));
airHumidityLastSent = airHumidity;
airHumidityLastSentMillis = currentMillis;
}
}
}
lastAirSensorPolledMillis = currentMillis;
}
#endif
void switchRelay(const uint8_t ch, const boolean on, const boolean overrideLock, const char *source, const uint8_t *ga){
if (ch >= CHANNELS){
Serial.print("Ungültiger Kanal: ");
Serial.println(ch + 1);
}
else if (relayStatus[ch] != on){
if (!overrideLock && lockActive[ch]){
Serial.print("Schaltkommando wird ignoriert, Kanal ");
Serial.print(ch + 1);
Serial.println(" ist gesperrt!");
}
else{
relayStatus[ch] = on;
if (on){
if (AUTO_OFF_TIMER_S[ch] > 0){
autoOffTimerStartMillis[ch] = currentMillis;
autoOffTimerActive[ch] = true;
}
logSwitchEvent(ch, SWITCH_LOG_ON, source, ga);
Serial.print("Kanal ");
Serial.print(ch + 1);
Serial.println(" wird eingeschaltet");
}
else{
autoOffTimerActive[ch] = false;
logSwitchEvent(ch, SWITCH_LOG_OFF, source, ga);
Serial.print("Kanal ");
Serial.print(ch + 1);
Serial.println(" wird ausgeschaltet");
}
if (RELAY_NORMALLY_OPEN[ch])
digitalWrite(GPIO_RELAY[ch], relayStatus[ch]);
else
digitalWrite(GPIO_RELAY[ch], !relayStatus[ch]);
// Die LED zeigt immer den Zustand des ersten Relais an
if (LED_SHOWS_RELAY_STATUS)
digitalWrite(GPIO_LED, !relayStatus[0]);
writeGA(GA_STATUS[ch], relayStatus[ch]);
}
}
}
void lockRelay(const uint8_t ch, const boolean lock, const char *source, const uint8_t *ga){
if (ch >= CHANNELS){
Serial.print("Ungültiger Kanal: ");
Serial.println(ch + 1);
}
else {
if (lock && !lockActive[ch]){
Serial.print("Kanal ");
Serial.print(ch + 1);
Serial.println(" wird gesperrt!");
logSwitchEvent(ch, SWITCH_LOG_LOCK, source, ga);
if (AUTO_UNLOCK_TIMER_S[ch] > 0){
autoUnlockTimerStartMillis[ch] = currentMillis;
autoUnlockTimerActive[ch] = true;
}
if (SWITCH_OFF_WHEN_LOCKED[ch])
switchRelay(ch, false, false, SWITCH_LOG_ON_BY_LOCK, 0);
else if (SWITCH_ON_WHEN_LOCKED[ch])
switchRelay(ch, true, false, SWITCH_LOG_ON_BY_LOCK, 0);
lockActive[ch] = true;
}
else if (!lock && lockActive[ch]){
lockActive[ch] = false;
autoUnlockTimerActive[ch] = false;
Serial.print("Kanal ");
Serial.print(ch + 1);
Serial.println(" wird entsperrt!");
logSwitchEvent(ch, SWITCH_LOG_UNLOCK, source, ga);
if (SWITCH_OFF_WHEN_UNLOCKED[ch])
switchRelay(ch, false, false, SWITCH_LOG_OFF_BY_UNLOCK, 0);
else if (SWITCH_ON_WHEN_UNLOCKED[ch])
switchRelay(ch, true, false, SWITCH_LOG_OFF_BY_UNLOCK, 0);
}
}
}
void writeGA(const uint8_t ga[], const boolean status){
if (client.connected()){
const uint8_t groupValueWrite[] = {0x00, 0x06, EIB_GROUP_PACKET >> 8, EIB_GROUP_PACKET & 0xFF, (ga[0] << 3) + ga[1], ga[2], 0x00, 0x80 | status};
client.write(groupValueWrite, sizeof(groupValueWrite));
}
}
void writeGA(const uint8_t ga[], const uint16_t data){
if (client.connected()){
const uint8_t groupValueWrite[] = {0x00, 0x08, EIB_GROUP_PACKET >> 8, EIB_GROUP_PACKET & 0xFF, (ga[0] << 3) + ga[1], ga[2], 0x00, 0x80, (data >> 8) & 0xFF, data & 0xFF};
client.write(groupValueWrite, sizeof(groupValueWrite));
}
}
void responseGA(const uint8_t ga[], const boolean status){
if (client.connected()){
const uint8_t groupValueResponse[] = {0x00, 0x06, EIB_GROUP_PACKET >> 8, EIB_GROUP_PACKET & 0xFF, (ga[0] << 3) + ga[1], ga[2], 0x00, 0x40 | status};
client.write(groupValueResponse, sizeof(groupValueResponse));
}
}
void responseGA(const uint8_t ga[], const uint16_t data){
if (client.connected()){
const uint8_t groupValueWrite[] = {0x00, 0x08, EIB_GROUP_PACKET >> 8, EIB_GROUP_PACKET & 0xFF, (ga[0] << 3) + ga[1], ga[2], 0x00, 0x40, (data >> 8) & 0xFF, data & 0xFF};
client.write(groupValueWrite, sizeof(groupValueWrite));
}
}
void readGA(const uint8_t ga[]){
if (client.connected()){
const uint8_t groupValueRequest[] = {0x00, 0x06, EIB_GROUP_PACKET >> 8, EIB_GROUP_PACKET & 0xFF, (ga[0] << 3) + ga[1], ga[2], 0x00, 0x00};
client.write(groupValueRequest, sizeof(groupValueRequest));
}
}
uint32_t getUptimeSeconds(){
return (currentMillis / 1000) + (millisOverflows * (0xFFFFFFFF / 1000));
}
char* getUptimeString(uint32_t totalSeconds){
uint32_t seconds = totalSeconds % 60,
minutes = (totalSeconds / 60) % 60,
hours = (totalSeconds / (60 * 60)) % 24,
days = totalSeconds / (60 * 60 * 24);
static char timeString[22];
if (days == 0) snprintf(timeString, 9, "%02d:%02d:%02d", hours, minutes, seconds);
else if (days == 1) snprintf(timeString, 22, "1 Tag, %02d:%02d:%02d", hours, minutes, seconds);
else snprintf(timeString, 22, "%d Tage, %02d:%02d:%02d", days, hours, minutes, seconds);
return timeString;
}
char* getTimeString(time_t timestamp){
static char timeString[9];
snprintf(timeString, 9, "%02d:%02d:%02d", hour(timestamp), minute(timestamp), second(timestamp));
return timeString;
}
char* getDateString(time_t timestamp){
static char dateString[11];
snprintf(dateString, 11, "%04d-%02d-%02d", year(timestamp), month(timestamp), day(timestamp));
return dateString;
}
char* getWeekdayString(time_t timestamp){
static char weekdayString[11];
switch (weekday(timestamp)){
case 1 : strcpy(weekdayString, "Sonntag"); break;
case 2 : strcpy(weekdayString, "Montag"); break;
case 3 : strcpy(weekdayString, "Dienstag"); break;
case 4 : strcpy(weekdayString, "Mittwoch"); break;
case 5 : strcpy(weekdayString, "Donnerstag"); break;