-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266_door_system.ino
213 lines (177 loc) · 5.18 KB
/
esp8266_door_system.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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WifiClient.h>
#include <uptime_formatter.h>
#include <LittleFS.h>
#include <ArduinoJson.h>
#define LED 5
#define RELAY 4
// PIN Number to control system
int pin = 0000;
// What the system thinks the door state is
bool doorOpen = false;
// What our sensor actually says
bool sensorDoorState = false;
// Is the relay activated
bool relaySwitched = false;
// Checking relay activation duration
unsigned long relayLastSwitch = 0;
// The last time an operation occured from the web
unsigned long lastWebOperation = 0;
ESP8266WebServer server(80);
void activateRelay() {
relayLastSwitch = millis();
relaySwitched = true;
digitalWrite(RELAY, HIGH);
}
void checkRelay() {
if(relaySwitched) {
if((relayLastSwitch + 2000) < millis()) {
relaySwitched = false;
digitalWrite(RELAY, LOW);
}
}
}
void doorLED() {
if(doorOpen) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
void webUptime() {
server.send(200, "text/html", uptime_formatter::getUptime());
}
void webPageNotFound() {
server.send(404, "text/html", "404: Not Found");
}
bool rateLimitWeb(int seconds) {
long milliseconds = (seconds * 1000);
if((lastWebOperation + milliseconds) < millis()) {
return true;
} else {
return false;
}
}
String loadSsid() {
if(LittleFS.exists("/settings.json")) {
File settings = LittleFS.open("/settings.json", "r");
DynamicJsonDocument jsonData(256);
deserializeJson(jsonData, settings.readString());
return jsonData["ssid"];
} else {
return "none";
}
}
String loadKey() {
if(LittleFS.exists("/settings.json")) {
File settings = LittleFS.open("/settings.json", "r");
DynamicJsonDocument jsonData(256);
deserializeJson(jsonData, settings.readString());
return jsonData["key"];
} else {
return "none";
}
}
void loadPin() {
if(LittleFS.exists("/settings.json")) {
File settings = LittleFS.open("/settings.json", "r");
DynamicJsonDocument jsonData(256);
deserializeJson(jsonData, settings.readString());
pin = jsonData["pin"].as<int16_t>();
} else {
pin = 0000;
}
}
int verifySentPin() {
if(server.hasArg("plain")) {
if(server.arg("plain").length() <= 28) {
DynamicJsonDocument jsonData(32);
deserializeJson(jsonData, server.arg("plain"));
const int sentPin = jsonData["pin"].as<int16_t>();
return sentPin;
}
} else {
return 0000;
}
}
void webOpenDoor() {
Serial.println("Door open requested");
if(rateLimitWeb(10)) {
if(verifySentPin() == pin) {
if(doorOpen) {
// Door already open
server.send(200, "application/json", "{\"query\" : \"open\", \"result\":\"failed\", \"reason\" : \"invalid\"}");
} else {
// Door now opening
server.send(200, "application/json", "{\"query\" : \"open\", \"result\":\"success\", \"reason\" : \"opening\"}");
doorOpen = true;
activateRelay();
}
} else {
server.send(200, "application/json", "{\"query\" : \"open\", \"result\":\"failed\", \"reason\" : \"invalid-pin\"}");
}
} else {
server.send(200, "application/json", "{\"query\" : \"open\", \"result\":\"failed\", \"reason\" : \"ratelimited\"}");
}
}
void webCloseDoor() {
Serial.println("Door close requested");
if(rateLimitWeb(10)) {
if(verifySentPin() == pin) {
lastWebOperation = millis();
if(doorOpen) {
// Door now closing
server.send(200, "application/json", "{\"query\" : \"close\", \"result\":\"success\", \"reason\" : \"closing\"}");
// TODO: Change to magnetic contact checking
doorOpen = false;
activateRelay();
} else {
// Door already closed
server.send(200, "application/json", "{\"query\" : \"close\", \"result\":\"failed\", \"reason\" : \"invalid\"}");
}
} else {
server.send(200, "application/json", "{\"query\" : \"open\", \"result\":\"failed\", \"reason\" : \"invalid-pin\"}");
}
} else {
server.send(200, "application/json", "{\"query\" : \"close\", \"result\":\"failed\", \"reason\" : \"ratelimited\"}");
}
}
void webStatusDoor() {
Serial.println("Door status requested");
if(doorOpen) {
server.send(200, "application/json", "{\"query\" : \"status\", \"result\":\"success\", \"reason\" : \"open\"}");
} else {
server.send(200, "application/json", "{\"query\" : \"status\", \"result\":\"success\", \"reason\" : \"closed\"}");
}
}
void setup() {
pinMode(LED, OUTPUT);
pinMode(RELAY, OUTPUT);
Serial.begin(115200);
delay(5);
Serial.println('\n');
if(!LittleFS.begin()) {
Serial.println("Error enabling file system");
return;
}
WiFi.begin(loadSsid(), loadKey());
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.println("System starting...");
Serial.println(WiFi.localIP());
loadPin();
server.serveStatic("/", LittleFS, "/web/index.html");
server.on("/open/", webOpenDoor);
server.on("/close/", webCloseDoor);
server.on("/status/", webStatusDoor);
server.on("/uptime/", webUptime);
server.onNotFound(webPageNotFound);
server.begin();
}
void loop() {
doorLED();
checkRelay();
server.handleClient();
}