This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
WemosD1_HomeMatic_RFID.ino
296 lines (265 loc) · 7.97 KB
/
WemosD1_HomeMatic_RFID.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
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <ESP8266HTTPClient.h>
#include <MFRC522.h>
#include <ArduinoOTA.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <ArduinoJson.h>
#include <WiFiManager.h>
#define SS_PIN D8
#define RST_PIN D3
#define BUZZER_PIN D1
#define SWITCHPIN D2
#define LEDPIN D4
#define RFIDDETECTLOOPS 60
#define IPSIZE 16
#define CCUVARSIZE 255
#define HTTPTIMEOUT 3000
#define CONFIGPORTALTIMEOUT 180
#define UDPPORT 6673
enum rfid_mode_e { RM_MFRC522, RM_RDM6300 };
struct hmconfig_t {
char CCUIP[IPSIZE] = "";
char VAR_RFID_command[CCUVARSIZE] = "";
char VAR_Alarmanlage_scharf[CCUVARSIZE] = "";
char VAR_Alarmanlage_wird_scharf[CCUVARSIZE] = "";
char VAR_Alarmanlage_scharfschaltbar[CCUVARSIZE] = "";
} HomeMaticConfig;
struct rfid_t {
int detectCount = 0;
int noDetectCount = 0;
String chipId = "";
byte Mode = RM_MFRC522;
} RFID;
struct systemzustand_t {
bool Scharf = false;
bool Scharfschaltbar = false;
bool AlarmAusgeloest = false;
} Systemzustand;
//WifiManager - don't touch
bool shouldSaveConfig = false;
const String configJsonFile = "config.json";
#define wifiManagerDebugOutput true
bool startWifiManager = false;
struct wemosnetconfig_t {
char IP[IPSIZE] = "0.0.0.0";
char NETMASK[IPSIZE] = "0.0.0.0";
char GW[IPSIZE] = "0.0.0.0";
} WemosNetConfig;
struct udp_t {
WiFiUDP UDP;
char incomingPacket[255];
} UDPClient;
unsigned long oldStandbyBlinkMillis = 0;
unsigned long oldBeepMillis = 0;
unsigned long lastRDM6300ChipIdMillis = 0;
String result1 = "";
String result2 = "";
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(SWITCHPIN, INPUT_PULLUP);
Serial.println("\nConfig-Modus aktivieren?");
for (int i = 0; i < 20; i++) {
if (digitalRead(SWITCHPIN) == LOW) {
startWifiManager = true;
break;
}
digitalWrite(LEDPIN, HIGH);
delay(100);
digitalWrite(LEDPIN, LOW);
delay(100);
}
loadSystemConfig();
doWifiConnect();
startOTAhandling();
UDPClient.UDP.begin(UDPPORT);
SPI.begin();
mfrc522.PCD_Init();
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
if (getStateCCU(String(HomeMaticConfig.VAR_Alarmanlage_scharf), "State") == "false") {
Systemzustand.Scharf = false;
digitalWrite(LED_BUILTIN, HIGH);
} else {
Systemzustand.Scharf = true;
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println("Ready!");
}
void loop() {
ArduinoOTA.handle();
//Millisekundenüberlauf nach 49... Tagen abfangen
if (oldStandbyBlinkMillis > millis())
oldStandbyBlinkMillis = millis();
if (oldBeepMillis > millis())
oldBeepMillis = millis();
if (RFID.Mode == RM_MFRC522) {
result1 = handleMFRC522();
result2 = handleMFRC522();
}
if (RFID.Mode == RM_RDM6300) {
delay(20);
String rdm_result = handleRDM6300();
if (rdm_result.length() > 10) {
result1 = rdm_result;
Serial.println("RDM6300: " + result1);
delay(20);
lastRDM6300ChipIdMillis = millis();
}
if (millis() - lastRDM6300ChipIdMillis > 200) {
if (lastRDM6300ChipIdMillis > 0)
Serial.println("lastRDM6300ChipIdMillis > 200");
lastRDM6300ChipIdMillis = 0;
result1 = "";
}
}
if (result1 != result2) {
RFID.detectCount++;
if (RFID.detectCount == 1) {
Serial.println("BEEP");
beep(70);
}
RFID.chipId = result1 + result2;
//Wenn Chip LANGE gehalten wird
if (RFID.detectCount == RFIDDETECTLOOPS) {
Serial.println("\nLONG HOLD (" + String(RFID.detectCount) + ") " + RFID.chipId);
//prüfen, ob sich Alarm scharf schalten lässt
if (getStateCCU(String(HomeMaticConfig.VAR_Alarmanlage_scharfschaltbar), "State") == "true") {
Serial.println("Scharfschaltbar!");
Systemzustand.Scharfschaltbar = true;
//Sende Befehl zum Scharfschalten
setStateCCU(HomeMaticConfig.VAR_RFID_command, "\"1;" + RFID.chipId + "\"");
} else {
//Wenn nicht scharfschaltbar, dann nur Piepton ausgeben
Systemzustand.Scharfschaltbar = false;
Serial.println("Nicht scharfschaltbar!");
for (int i = 0; i < 10; i++) {
beep(50);
delay(10);
}
}
RFID.chipId = "";
if (Systemzustand.Scharfschaltbar == true) {
delay(150); // <- Wartepause wegen lahmer CCU2, bei RaspberryMatic nicht erforderlich
//anschließend prüfen, ob Anlage scharf geschaltet wird
if (getStateCCU(String(HomeMaticConfig.VAR_Alarmanlage_wird_scharf), "State") == "true") {
digitalWrite(LED_BUILTIN, LOW);
Systemzustand.Scharf = true;
beep(1500);
}
else {
//Chip nicht zugelassen
Serial.println("Chip nicht zugelassen!");
for (int i = 0; i < 2; i++) {
beep(50);
delay(10);
}
}
}
}
} else {
RFID.noDetectCount++;
if (RFID.noDetectCount == 4) {
//Wenn Chip KURZ gehalten wird
if (RFID.detectCount > 0 && RFID.detectCount < (RFIDDETECTLOOPS) && RFID.chipId != "") {
Serial.println("\nSHORT HOLD (" + String(RFID.detectCount) + ") " + RFID.chipId);
//Sende Befehl für unscharf
setStateCCU(HomeMaticConfig.VAR_RFID_command, "\"0;" + RFID.chipId + "\"");
RFID.chipId = "";
delay(150); // <- Wartepause wegen lahmer CCU2, bei RaspberryMatic nicht erforderlich
//Prüfe, ob tatsächlich unscharf
if (getStateCCU(HomeMaticConfig.VAR_Alarmanlage_scharf, "State") == "false") {
digitalWrite(LED_BUILTIN, HIGH);
Systemzustand.Scharf = false;
Systemzustand.AlarmAusgeloest = false;
beep(150);
}
else {
//Sonst ist da was faul
for (int i = 0; i < 2; i++) {
beep(50);
delay(10);
}
}
}
RFID.noDetectCount = 0;
RFID.detectCount = 0;
}
}
String udpMessage = handleUDP();
if (udpMessage != "") {
bool oldScharf = Systemzustand.Scharf;
Serial.println("UDP Info erhalten: " + udpMessage);
if (udpMessage == "0") Systemzustand.Scharf = false;
if (udpMessage == "1") Systemzustand.Scharf = true;
digitalWrite(LED_BUILTIN, (udpMessage == "0"));
if (oldScharf != Systemzustand.Scharf) {
beep(800);
delay(50);
beep(100);
delay(50);
beep(100);
}
Systemzustand.AlarmAusgeloest = (udpMessage == "2");
}
standByBlink();
if (Systemzustand.AlarmAusgeloest) beep(50, 250);
}
String handleMFRC522() {
if (!mfrc522.PICC_IsNewCardPresent()) return "";
if (!mfrc522.PICC_ReadCardSerial()) return "";
return printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
}
String handleRDM6300() {
if (Serial.available()) {
String rdm6300_serial = "";
while (Serial.available()) {
char inChr = Serial.read();
switch (inChr) {
case 0x02:
rdm6300_serial = "";
case 0x03:
break;
default:
rdm6300_serial += inChr;
break;
}
}
return rdm6300_serial;
}
return "";
}
String printHex(byte *buffer, byte bufferSize) {
String id = "";
for (byte i = 0; i < bufferSize; i++) {
id += buffer[i] < 0x10 ? "0" : "";
id += String(buffer[i], HEX);
}
id.toUpperCase();
return id;
}
void beep(int msec) {
beep(msec, 0);
}
void beep(int msec, int interval) {
if ((millis() - oldBeepMillis) > interval) {
oldBeepMillis = millis();
digitalWrite(BUZZER_PIN, HIGH);
delay(msec);
digitalWrite(BUZZER_PIN, LOW);
}
}
void standByBlink() {
if ((millis() - oldStandbyBlinkMillis > 4000) && !Systemzustand.Scharf) {
oldStandbyBlinkMillis = millis();
digitalWrite(LED_BUILTIN, LOW);
delay(80);
digitalWrite(LED_BUILTIN, HIGH);
delay(70);
}
}