-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCS_ESP.ino
269 lines (255 loc) · 7.19 KB
/
TCS_ESP.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
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
// PubSubClient muss für längere Payloads verändert werden!
volatile uint32_t lastInt;
volatile uint32_t code;
volatile bool length;
volatile uint32_t _code;
volatile uint8_t _pos;
volatile bool _crc;
volatile bool _length;
volatile bool _valid;
const uint8_t _OUT = 15;
uint32_t last_door;
uint32_t last_ring;
uint32_t last_light;
uint16_t light_info;
const uint32_t UID = 0x12345; // Seriennummer (in 16 Byte Nachrichten xSSSSSxxl
const uint16_t LIGHT_DUR = 130000; // Licht aktiv für x ms
const char* ssid = "ssid";
const char* password = "password";
IPAddress mqtt_server(10, 0, 0, 1);
//const char* mqtt_server = "mqtt.local"
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
Serial.println("WiFi Init");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.write('.');
delay(100);
}
Serial.println("Interrupt init");
attachInterrupt(4, inputChange, CHANGE);
Serial.println("OTA");
ArduinoOTA.setPort(8266);
ArduinoOTA.setPassword((const char *)"totalcontrolsystem");
ArduinoOTA.setHostname("TCS");
ArduinoOTA.begin();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.println("MQTT connecting");
mqttCon(true);
pinMode(_OUT, OUTPUT);
Serial.println("Ready.");
}
void mqttCon(boolean block) {
while (!client.connected()) {
yield();
if (client.connect("TCS", "tcs/available", 0, (boolean)true, "offline")) {
client.subscribe("tcs/do_open");
client.subscribe("tcs/light/set");
client.subscribe("tcs/cmd/send");
client.publish("homeassistant/light/tcs/config", "{\"name\":\"TCS Light\",\"stat_t\":\"tcs/light\",\"json_attr_t\":\"tcs/light/info\",\"avty_t\":\"tcs/available\",\"cmd_t\":\"tcs/light/set\",\"qos\":0}", (bool)true);
client.publish("homeassistant/binary_sensor/tcs/config", "{\"name\":\"TCS Ring\",\"stat_t\":\"tcs/ring\",\"avty_t\":\"tcs/available\",\"json_attr_t\":\"tcs/ring/info\",\"qos\":0,\"dev_cla\":\"occupancy\"}", (bool)true);
client.publish("homeassistant/switch/tcs/config", "{\"name\":\"TCS Opener\",\"stat_t\":\"tcs/opener\",\"avty_t\":\"tcs/available\",\"cmd_t\":\"tcs/do_open\",\"qos\":0,\"opt\":false,\"ic\":\"mdi:lock-open-outline\"}", (bool)true);
client.publish("tcs/available", "online", (boolean)true);
}
if (!block) break;
}
}
void callback(char* topic, byte* payload, unsigned int length) {
if (String("tcs/do_open").equals(topic)) {
if (String((char*)payload).substring(0,length).equals("ON")) {
sendCMD((uint16_t)0x1100);
} else {
Serial.println("Content mismatch");
}
} else if (String("tcs/light/set").equals(topic)) {
if (String((char*)payload).substring(0,length).equals("ON")) {
sendCMD((uint16_t)0x1200);
} else {
Serial.println("Content mismatch");
}
} else if (String("tcs/cmd/send").equals(topic)) {
if (length == 4) {
sendCMD((uint16_t)strtol(String((char*)payload).substring(0,length).c_str(), NULL, 16));
} else if (length == 8) {
sendCMD((uint32_t)strtol(String((char*)payload).substring(0,length).c_str(), NULL, 16));
}
}
}
void sendCMD(uint32_t cmd, uint8_t length) {
if (length != 16 && length != 32) return;
bool crc = 1;
digitalWrite(_OUT, 1);
delay(6);
digitalWrite(_OUT, 0);
delay(length >> 3);
for (uint8_t i = 0; i < length; i++) {
digitalWrite(_OUT, !(i & 1));
bool b = cmd >> (length - 1 - i) & 1;
crc ^= b;
delay(b ? 4 : 2);
}
digitalWrite(_OUT, 1);
delay(crc ? 4 : 2);
digitalWrite(_OUT, 0);
}
void sendCMD(uint16_t cmd) {
sendCMD(cmd, 16);
}
void sendCMD(uint32_t cmd) {
sendCMD(cmd, 32);
}
ICACHE_RAM_ATTR void inputChange() {
uint32_t curInt = micros();
uint32_t t = curInt - lastInt;
if (lastInt > curInt) {
t = 0xFFFFFFFF - lastInt + curInt;
}
if (t < 1000) {
Serial.write('I');
} else if (t < 5000) {
bool b = 1;
if (del < 3000) {
b = 0;
}
if (!_pos) {
_length = b;
Serial.print(b ? "32b " : "16b ");
} else if (_pos <= (_length ? 32 : 16)) {{
_code <<= 1;
_code |= b;
}
_crc ^= b;
Serial.write(b + '0');
} else if (_pos == (_length ? 33 : 17)) {
Serial.print("CRC ");
if (b == _crc) {
Serial.println("OK");
code = _code;
length = _length;
} else {
Serial.println("NOK");
}
} else {
Serial.println("W");
}
_pos++;
} else if (t < 7000) {
Serial.println();
Serial.println(_pos);
Serial.write('B');
_pos = 0;
_crc = 1;
_code = 0;
} else if (t < 24000) {
Serial.write('R');
} else {
Serial.println("TOUT");
}
lastInt = curInt;
}
void loop() {
if (code != 0) {
Serial.println("CODE:");
Serial.println(code, HEX);
char h[10];
if (length) {
switch (code >> 28) {
case 0x0:
switch ((code >> 8) & 0xFFFFF) {
case UID:
last_ring = millis();
client.publish("tcs/ring/info", "{\"Source\":\"external\"}", (bool)true);
client.publish("tcs/ring", "ON", (bool)true);
Serial.println("Klingel unten");
break;
default:
// Andere Klingel
break;
}
break;
case 0x1:
Serial.println("Klingelt");
switch ((code >> 8) & 0xFFFFF) {
case UID:
last_ring = millis();
client.publish("tcs/ring/info", "{\"Source\":\"internal\"}", (bool)true);
client.publish("tcs/ring", "ON", (bool)true);
Serial.println("Eigene Etagenklingel");
break;
default:
// Andere Klingel
break;
}
break;
case 0x3:
// Eventuell muss man auf die 2 Byte ACK Response achten, da bei NACK kein 0x30XX geschickt wird beim Auflegen
client.publish("tcs/call", "ON", (bool)true);
switch ((code >> 8) & 0xFFFFF) {
case UID:
//Eigenes Telefon
default:
break;
}
break;
default:
Serial.println("UNKOWN");
}
sprintf(h, " %08X", code);
} else {
switch (code >> 8) {
case 0x11:
client.publish("tcs/opener", "ON", (bool)true);
last_door = millis();
Serial.println("Tueroffner");
break;
case 0x12:
last_light = millis();
client.publish("tcs/light", "ON", (bool)true);
Serial.println("Licht an");
break;
case 0x22:
client.publish("tcs/ring", "OFF", (bool)true);
Serial.println("Klingeln beendet");
// Nur gesendet, wenn von einem TCS Gerät geklingelt wurde
break;
case 0x30:
client.publish("tcs/call", "OFF", (bool)true);
Serial.println("Ruf beendet");
break;
}
sprintf(h, " %04X", code);
}
client.publish("tcs/cmd", h);
code = 0;
}
if (last_door && last_door + 7000 <= millis()) {
last_door = 0;
client.publish("tcs/opener", "OFF", (bool)true);
}
if (last_ring && last_ring + 5000 <= millis()) {
// Timout, wenn nicht über offizielle Wege geklingel wurde
last_ring = 0;
client.publish("tcs/ring", "OFF", (bool)true);
}
if (last_light) {
uint16_t cl = max((int16_t)0, (int16_t)((last_light + LIGHT_DUR - millis()) / 1000));
if (cl != light_info) {
client.publish("tcs/light/info", (String("{\"Remaining\":") + cl + "}").c_str(), (bool)true);
if (!cl) {
last_light = 0;
client.publish("tcs/light", "OFF", (bool)true);
}
light_info = cl;
}
}
ArduinoOTA.handle();
mqttCon(false);
client.loop();
}