-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gatekeeper.ino
201 lines (176 loc) · 6.31 KB
/
Gatekeeper.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
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com/esp8266-nodemcu-access-point-ap-web-server/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "Logic.h"
#include "GatekeeperWiFi.h"
long interval = 1000;
int inputPin = 14;
int outputPin = 12;
bool soundOn = true;
bool doorIsOpen;
Logic logic(inputPin, outputPin);
GatekeeperWiFi wifi;
bool apModeOn = true;
AsyncWebServer server(80);
unsigned long previousMillis = 0;
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.pad {
padding: 1em;
}
</style>
</head>
<body>
<h2>ESP8266 Gatekeeper</h2>
<div class="pad">
<span class="dht-labels">door</span>
<span id="door"></span>
</div>
<div class="pad">
<button id="toggle-sound"></button>
</div>
%SETUP_HTML%
<div id="message"></div>
</body>
<script>
%SETUP_JS%
function request ({ url, method = "GET", body }, callback) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callback({text: this.responseText})
}
};
req.open(method, url, true);
req.send();
}
request({url: '/sound'}, ({text}) => {
document.getElementById("toggle-sound").innerText = "Sound: " + text;
})
document.getElementById("toggle-sound").addEventListener('click', function () {
request({url: '/toggle-sound', method: 'POST'}, ({text}) => {
document.getElementById("toggle-sound").innerText = "Sound is " + text;
})
})
setInterval(function ( ) {
request({url: '/door'}, ({text}) => {
document.getElementById("door").innerHTML = text;
document.getElementById("door").style.backgroundColor = (text == "open") ? "red" : "green";
document.getElementById("door").style.color = "white";
})
}, 1000 ) ;
</script>
</html>)rawliteral";
String processor(const String& var){
if(var == "SETUP_HTML"){
if (apModeOn) {
return R"rawliteral(<form>
<small>only needed in AP mode </small>
<div>
<label for="ssid">SSID</label>
<input id="ssid" name="ssid" placeholder="Enter name of WiFi Network"/>
</div>
<div>
<label for="password">Password</label>
<input id="password" name="password" placeholder="The password to connect to WiFi" autofocus/>
</div>
<button type="submit">Submit</button>
</form>)rawliteral";
}
}
if(var == "SETUP_JS"){
if (apModeOn) {
return R"rawliteral(const form = document.querySelector('form');
form.addEventListener('submit', submit);
function submit() {
var ssid = document.getElementById("ssid").value;
var password = document.getElementById("password").value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
debugger
document.getElementById("message").innerText = "Connect to " + ssid;
if (this.readyState == 4 && this.status == 200) {
}
};
xhttp.open("POST", "/setup");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "ssid": ssid, "password": password }));
event.preventDefault();
})rawliteral";
}
}
return String();
}
void setup() {
EEPROM.begin(512);
Serial.begin(9600);
Serial.setDebugOutput(true);
pinMode(outputPin, OUTPUT);
pinMode(inputPin, INPUT);
wifi.createAP("Gatekeeper");
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", index_html, processor);
});
server.on("/door", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", logic.doorIsOpen() ? "open" : "closed");
});
server.on("/sound", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", soundOn ? "on" : "off");
});
server.on("/toggle-sound", HTTP_POST, [](AsyncWebServerRequest * request) {
soundOn = !soundOn;
request->send_P(200, "text/plain", soundOn ? "on" : "off");
});
server.on("/setup",
HTTP_POST,
[](AsyncWebServerRequest * request) {},
NULL,
[](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) {
DynamicJsonDocument doc(1024);
deserializeJson(doc, data);
request->send(200);
String hostSSID = doc["ssid"];
String hostPassword = doc["password"];
Serial.println("setup to connect to");
Serial.println(hostSSID);
Serial.println(hostPassword);
wifi.connectWifi(hostSSID, hostPassword);
apModeOn = false;
/*
EEPROM.write(0, hostSSID);
EEPROM.write(1, hostPassword);
EEPROM.commit();
*/
});
server.begin();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
logic.run(soundOn);
}
}