-
Notifications
You must be signed in to change notification settings - Fork 0
/
haustuer.ino
365 lines (317 loc) · 8.03 KB
/
haustuer.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
#include <FS.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>
#include <EEPROM.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include "Arduino.h"
/*
* Un-Comment for Serial Debug Output
*/
//#define SerialDebug
extern "C"
{
#include "user_interface.h"
}
/*
* Variables
*/
os_timer_t Timer1;
int Counter = 0;
bool TickOccured = true;
bool doorbellTriggered = false;
bool shouldSaveConfig = false;
char msgToPublish[60];
bool bufState;
long lastDebounceTime = 0;
long debounceDelay = 150;
/*
* Placeholders for MQTT Variables, these are entered in WiFiManager Portal, no need to change it here.
*/
char MQTT_BROKER[40] = "x.x.x.x";
char MQTT_PORT[6] = "1883";
/*
* These are the topics for the doorbell
*/
const char* statusTopic = "/home/haustuer/klingel/status";
const char* doorOpenerTopic = "/home/haustuer/klingel/dooropener";
const char* modeTopic = "/home/haustuer/klingel/mode";
const char* doorbellTopic ="/home/haustuer/klingel";
/*
* Input declarations
*/
const int doorbellInput = D1;
const int modePin = D3;
const int dooropenerPin = D4;
/*
* Handler
*/
WiFiManager wifiManager;
WiFiClient espClient;
PubSubClient client(espClient);
/*
* FUNCTIONS
*/
/*
* Callback for WifiManager Settings
*/
void saveConfigCallback () {
#ifdef SerialDebug
Serial.println("Should save config");
#endif
shouldSaveConfig = true;
}
/*
* Timer Callback for timed Interrupt every 30 seconds (Heartbeat)
*/
void timerCallback(void *pArg)
{
TickOccured = true;
*((int *) pArg) += 1;
}
/*
* Callback function for MQTT Subscribe
*/
void callback(char* topic, byte* payload, unsigned int length) {
#ifdef SerialDebug
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
#endif
if (strcmp(topic,modeTopic) == 0 && (char)payload[0] == '1')
{
digitalWrite(modePin,LOW);
#ifdef SerialDebug
Serial.println("switch to HA");
#endif
EEPROM.write(0, 1);
TickOccured = true;
}
else if (strcmp(topic,modeTopic) == 0 && (char)payload[0] == '0')
{
digitalWrite(modePin,HIGH);
#ifdef SerialDebug
Serial.println("Switch to doorbell");
#endif
EEPROM.write(0, 0);
TickOccured = true;
}
else if (strcmp(topic,doorOpenerTopic) == 0 && (char)payload[0] == '1')
{
#ifdef SerialDebug
Serial.println("door opener triggered");
#endif
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(dooropenerPin, LOW);
delay(4000);
#ifdef SerialDebug
Serial.println("door opener end");
#endif
digitalWrite(BUILTIN_LED, HIGH);
digitalWrite(dooropenerPin, HIGH);
}
EEPROM.commit();
}
/*
* Interrupt function for debounce on Input pin
*/
void ICACHE_RAM_ATTR doorbellDebounce()
{
#ifdef SerialDebug
Serial.println("Interrupt: entered");
#endif
int reading = digitalRead(doorbellInput);
if(reading == bufState) return;
boolean debounce = false;
if((millis() - lastDebounceTime) <= debounceDelay)
{
debounce = true;
}
lastDebounceTime = millis();
if(debounce) return;
bufState = reading;
if (reading==1) doorbellTriggered = true;
#ifdef SerialDebug
Serial.println("Interrupt: button: " + String(reading));
#endif
}
/*
* SETUP ROUTINE
*/
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
pinMode(modePin, OUTPUT);
pinMode(dooropenerPin, OUTPUT);
pinMode(doorbellInput, INPUT_PULLUP);
digitalWrite(BUILTIN_LED, HIGH);
EEPROM.begin(512);
Serial.begin(115200);
#ifdef SerialDebug
Serial.println("DEBUG MODE");
Serial.println("mounting FS...");
wifiManager.setDebugOutput(true);
#else
wifiManager.setDebugOutput(false);
#endif
if (SPIFFS.begin()) {
#ifdef SerialDebug
Serial.println("mounted file system");
#endif
if (SPIFFS.exists("/config.json"))
{
#ifdef SerialDebug
Serial.println("reading config file");
#endif
File configFile = SPIFFS.open("/config.json", "r");
if (configFile)
{
#ifdef SerialDebug
Serial.println("opened config file");
#endif
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if ( ! deserializeError )
{
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success())
{
#endif
#ifdef SerialDebug
Serial.println("\nparsed json");
#endif
strcpy(MQTT_BROKER, json["mqtt_server"]);
strcpy(MQTT_PORT, json["mqtt_port"]);
} else
{
#ifdef SerialDebug
Serial.println("failed to load json config");
#endif
}
configFile.close();
}
}
} else {
#ifdef SerialDebug
Serial.println("failed to mount FS");
#endif
}
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", MQTT_BROKER, 40);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", MQTT_PORT, 6);
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
wifiManager.autoConnect("Haustuer");
strcpy(MQTT_BROKER, custom_mqtt_server.getValue());
strcpy(MQTT_PORT, custom_mqtt_port.getValue());
if (shouldSaveConfig)
{
#ifdef SerialDebug
Serial.println("saving config");
#endif
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
#endif
json["mqtt_server"] = MQTT_BROKER;
json["mqtt_port"] = MQTT_PORT;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile)
{
#ifdef SerialDebug
Serial.println("failed to open config file for writing");
#endif
}
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
serializeJson(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
configFile.close();
}
#ifdef SerialDebug
Serial.println("local ip");
Serial.println(WiFi.localIP());
#endif
ArduinoOTA.setHostname("Haustuer");
ArduinoOTA.begin();
#ifdef SerialDebug
Serial.println("Connecting MQTT");
Serial.print((char *)MQTT_BROKER);
Serial.print(":");
Serial.println(atoi(MQTT_PORT));
#endif
client.setServer((char *)MQTT_BROKER, atoi(MQTT_PORT));
client.setCallback(callback);
client.connect("HaustuerV2");
client.subscribe(modeTopic);
client.subscribe(doorOpenerTopic);
os_timer_setfn(&Timer1, timerCallback, &Counter);
os_timer_arm(&Timer1, 30000, true);
digitalWrite(dooropenerPin, HIGH);
digitalWrite(modePin,!EEPROM.read(0));
attachInterrupt(digitalPinToInterrupt(doorbellInput), doorbellDebounce, CHANGE);
}
/*
* Loop
*/
void loop()
{
if (!client.connected())
{
#ifdef SerialDebug
Serial.println("disconnected, retrying");
#endif
client.connect("HaustuerV2");
client.subscribe(modeTopic);
client.subscribe(doorOpenerTopic);
}
if (TickOccured)
{
#ifdef SerialDebug
Serial.println("Heartbeat Time!");
#endif
if (client.connected())
{
snprintf (msgToPublish, 60, "%d", EEPROM.read(0));
client.publish(statusTopic, msgToPublish);
}
TickOccured = false;
}
if (doorbellTriggered)
{
#ifdef SerialDebug
Serial.println("hat geklingelt");
#endif
client.publish(doorbellTopic, "DingDong");
delay(2000);
client.publish(doorbellTopic, " ");
doorbellTriggered = false;
}
client.loop();
ArduinoOTA.handle();
}
/*
* End.
*/