-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESPLock-homebridge.ino
71 lines (58 loc) · 1.83 KB
/
ESPLock-homebridge.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
/*
ESP8266 HTTP Lock
Make a Siri HomeKit enabled lock with ESPea
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#define RELAY_PIN 5 // wire D1 to relay
const char* ssid = "YOUR-SSID"; // тут ваша сеть
const char* password = "YOUR-PASSWORD"; // тут ваш пароль от wi-fi
const long lockTimeout = 3000;
ESP8266WebServer server(80);
long unlockedTime = 0;
void handleQuery() {
char rsp[255];
sprintf(rsp, "{\"state\":\"%s\",\"statusCode\":200}", digitalRead(RELAY_PIN) ? "unlocked" : "locked");
server.send(200, "text/plain", rsp);
}
void handleLock() {
String state = server.arg("state");
if (state == "locked") {
digitalWrite(RELAY_PIN, 0);
} else {
// There's a bug for ESP8266 Arduino core version 2.3.0 that it can't get param state
digitalWrite(RELAY_PIN, 1);
// Lock again after 3 seconds
unlockedTime = millis();
}
server.send(200, "text/plain", "{\"battery\":100,\"statusCode\":200}");
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, handleQuery); // Query lock status
server.on("/", HTTP_POST, handleLock); // Set lock status
server.begin();
}
void loop() {
server.handleClient();
// Restore lock status
if ((unlockedTime > 0) && (millis() - unlockedTime > lockTimeout)) {
unlockedTime = 0;
digitalWrite(RELAY_PIN, 0);
}
}