-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sonoff-HA.ino
396 lines (342 loc) · 12.5 KB
/
Sonoff-HA.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <FS.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
//--------------------------------------------------------------------------------
char ssid[40];
char pwd[40];
int restore_state = 1; //restore state flag
char savedstate[2] = "0"; //saved state of 1 pin
int gpio13Led = 13;
int gpio12Relay = 12;
WiFiClient client;
IPAddress myIP; //wemos ip address
const char *ap_ssid = "RelayBoard";
const char *ap_pwd = "";
AsyncWebServer server(80);
//--------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(100);
initGPIO();
delay(100);
Serial.println("\n\n\nRelay Program Started");
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
if(json.containsKey("ssid") )
strcpy(ssid, json["ssid"]);
if(json.containsKey("pwd") )
strcpy(pwd, json["pwd"]);
if(json.containsKey("restore_state") )
restore_state = json["restore_state"];
if(json.containsKey("savedstate") )
strcpy(savedstate, json["savedstate"]);
} else {
Serial.println("failed to load json config");
}
}
}
} else {
Serial.println("failed to mount FS");
}
//restore state or OFF
if(restore_state) {
restoreState();
} else {
saveConfig();
digitalWrite(gpio13Led, HIGH);
digitalWrite(gpio12Relay, LOW);
}
//if no ssid, start as AP mode
if(ssid == "" ) {
Serial.println("Starting Access Point ...");
WiFi.mode(WIFI_AP);
WiFi.softAP(ap_ssid);
myIP = WiFi.softAPIP();
Serial.print("Access Point IP address: ");
Serial.println(myIP);
Serial.println("Access the relay board at http://relay.local/");
} else {
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pwd);
int ctr = 0;
while (WiFi.status() != WL_CONNECTED && ctr < 30) {
delay(500);
Serial.print(".");
ctr++;
}
if(WiFi.status() == WL_CONNECTED) {
myIP = WiFi.localIP();
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(myIP);
} else {
//failed to connect first time, so retry once more
Serial.println(".");
Serial.print("Could not connect to WiFi: "); Serial.println(ssid);
Serial.println("*** Retrying ***");
WiFi.mode(WIFI_OFF);
delay(3000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pwd);
int ctr = 0;
while (WiFi.status() != WL_CONNECTED && ctr < 30) {
delay(500);
Serial.print(".");
ctr++;
}
if(WiFi.status() == WL_CONNECTED) {
myIP = WiFi.localIP();
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(myIP);
} else {
//could not connect for second time also, probable issue in ssid/network, start in AP mode
Serial.println();
Serial.print("Could not connect to WiFi: "); Serial.println(ssid);
Serial.println("Starting Access Point ...");
WiFi.mode(WIFI_AP);
WiFi.softAP(ap_ssid);
myIP = WiFi.softAPIP();
Serial.print("Access Point IP address: ");
Serial.println(myIP);
Serial.println("Access the relay board at http://relay.local/");
}
}
}
if (!MDNS.begin("relay")) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
//http server routes - UI
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
String html = ""
"<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Wi-Fi Relay Board</title>"
"<script>"
"loading = true;"
"var xhttp = new XMLHttpRequest();"
"xhttp.onreadystatechange = function() {"
" if (this.readyState == 4 && this.status == 200) {"
" var resp = JSON.parse(xhttp.responseText);"
" document.getElementById('ssid').value = resp['ssid'];"
" document.getElementById('pwd').value = resp['pwd'];"
" if(resp['restore_state'] === '1') document.getElementsByName('restore_state')[0].checked = true;"
" else document.getElementsByName('restore_state')[1].checked = true;"
" document.getElementById('msg').innerHTML = resp['msg'];"
" }"
"};"
"function getConfig() {"
"xhttp.open('GET', '/getconfig', true);"
"xhttp.send();"
"}"
"function restart() {"
"xhttp.open('GET', '/restart', true);"
"xhttp.send();"
"} "
"function on() {"
"xhttp.open('GET', '/on', true);"
"xhttp.send();"
"} "
"function off() {"
"xhttp.open('GET', '/off', true);"
"xhttp.send();"
"} "
"function clearmsg() {"
" document.getElementById('msg').innerHTML = '';"
"} "
"function save() {"
" loading=true;"
" var ssid = document.getElementById('ssid').value;"
" var pwd = document.getElementById('pwd').value;"
" var restore_state = document.getElementsByName('restore_state')[0].checked;"
" xhttp.open('GET', '/save?ssid=' + ssid "
"+ '&pwd=' + pwd "
"+ '&restore_state=' + (restore_state? '1' : '0') "
", true);"
" xhttp.send();"
"}"
"</script>"
"</head><body onLoad='getConfig()'><h3 style='color:green'>Wi-Fi Relay Board</h3>"
"<table>"
"<tr><td><B>SSID</B></td><td colspan='2'> : <input type='text' name='ssid' id='ssid' onChange='clearmsg();'></td></tr>"
"<tr><td><B>Password</B></td><td colspan='2'> : <input type='password' name='pwd' id='pwd' onChange='clearmsg();'></td></tr>"
"<tr><td><B>State on Boot</B></td><td>: <input type='radio' name='restore_state' value='1' checked onClick='clearmsg();'> RESTORE </td><td><input type='radio' name='restore_state' value='0' onClick='clearmsg();'> OFF </td></tr>"
"<tr><td> </td><td> </td></tr>"
"<tr><td></td><td><input type='button' value='Restart' onClick='restart()'> <input type='button' value='Save' onClick='save()'></td><td id='msg'></td></td></tr>"
"</table><BR>"
"<HR>"
"<table>"
"<tr><td><input type='button' name='alloff' value='ON' onClick='on();'> <input type='button' name='allon' value='OFF' onClick='off();'> </td></tr>"
"</table><BR>"
"<HR>"
"</body></html>";
request->send(200, "text/html", html);
});
//relay-control route takes query string l=load,state or t=load (load is the pin index)
server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(gpio13Led, LOW);
digitalWrite(gpio12Relay, HIGH);
delay(500);
String resp = "{ \"ssid\" : \"" + String(ssid)
+ "\" , \"pwd\" : \"" + String(pwd)
+ "\" , \"restore_state\" : \"" + String(restore_state)
+ "\" , \"savedstate\" : \"" + "1"
+ "\" , \"msg\" : \""
+ "\" }";
request->send(200, "application/json", resp);
saveConfig();
});
server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(gpio13Led, HIGH);
digitalWrite(gpio12Relay, LOW);
delay(500);
String resp = "{ \"ssid\" : \"" + String(ssid)
+ "\" , \"pwd\" : \"" + String(pwd)
+ "\" , \"restore_state\" : \"" + String(restore_state)
+ "\" , \"savedstate\" : \"" + "0"
+ "\" , \"msg\" : \""
+ "\" }";
request->send(200, "application/json", resp);
saveConfig();
});
//save route to save config
server.on("/save", HTTP_GET, [](AsyncWebServerRequest *request){
if(request->hasParam("ssid")) {
AsyncWebParameter* p = request->getParam("ssid");
String ssid1 = String(p->value()).c_str();
ssid1.toCharArray(ssid, 1+ssid1.length());
}
if(request->hasParam("pwd")) {
AsyncWebParameter* p = request->getParam("pwd");
String pwd1 = String(p->value()).c_str();
pwd1.toCharArray(pwd, 1+pwd1.length());
}
if(request->hasParam("restore_state")) {
AsyncWebParameter* p = request->getParam("restore_state");
String restore_state1 = String(p->value()).c_str();
restore_state = restore_state1.equals("1");
}
String msg = "<font color='red'>Saved!</font>";
//String currentstate = getStatus();
String st = "{ \"ssid\" : \"" + String(ssid)
+ "\" , \"pwd\" : \"" + String(pwd)
+ "\" , \"restore_state\" : \"" + String(restore_state)
+ "\" , \"savedstate\" : \"" + String(savedstate)
+ "\" , \"msg\" : \"" + msg
+ "\" }";
request->send(200, "application/json", st);
saveConfig();
});
//restart the wemos d1 mini
server.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(gpio13Led, HIGH);
digitalWrite(gpio12Relay, LOW);
delay(5000);
ESP.restart();
});
//getconfig gets you the current configuration as well current state of relays/pins
server.on("/getconfig", HTTP_GET, [](AsyncWebServerRequest *request){
//String currentstate = getStatus();
String st = "{ \"ssid\" : \"" + String(ssid)
+ "\" , \"pwd\" : \"" + String(pwd)
+ "\", \"restore_state\" : \"" + String(restore_state)
+ "\", \"savedstate\" : \"" + String(savedstate)
+ "\", \"msg\" : \""
+ "\" }";
request->send(200, "application/json", st);
});
//reset the config file to an empty file
server.on("/resetconfig", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "application/json", "Config Reset");
Serial.println("deleting config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
ESP.reset();
});
//handle file not found error
server.onNotFound([](AsyncWebServerRequest *request){
Serial.printf("NOT_FOUND: ");
Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
request->send(404);
});
//start the http server
server.begin();
delay(1000);
Serial.end(); // Have to end() it as RX and TX are being used for Digital IO
delay(100);
}
//--------------------------------------------------------------------------------
void restoreState() {
if(String(savedstate).toInt() == 0){
digitalWrite(gpio13Led, HIGH);
digitalWrite(gpio12Relay, LOW);
} else {
digitalWrite(gpio13Led, LOW);
digitalWrite(gpio12Relay, HIGH);
}
}
//--------------------------------------------------------------------------------
void saveConfig() {
String sts = getStatus();
strcpy(savedstate, sts.c_str());
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["ssid"] = ssid;
json["pwd"] = pwd;
json["restore_state"] = restore_state;
json["savedstate"] = savedstate;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
}
//--------------------------------------------------------------------------------
void initGPIO() {
pinMode(gpio13Led, OUTPUT);
digitalWrite(gpio13Led, HIGH);
pinMode(gpio12Relay, OUTPUT);
digitalWrite(gpio12Relay, HIGH);
}
//--------------------------------------------------------------------------------
void loop(){
}
//--------------------------------------------------------------------------------
String getStatus() {
return String(digitalRead(gpio12Relay)) ;
}