-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonkyo_nodeMCU.ino
403 lines (366 loc) · 10.6 KB
/
onkyo_nodeMCU.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
397
398
399
400
401
402
403
#include "Arduino.h"
#include <map>
#include "config.h"
#include <EEPROM.h>
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266SSDP.h>
#include <uri/UriBraces.h>
//Variables
uint i = 0;
uint statusCode;
String st;
String selectSSID;
String content;
char hexadecimalnum[5];
//Function Decalration
bool testWifi(void);
void launchAPWeb(void);
void setupAP(void);
// Clear EEPROM
void clearEEPROM() {
for (int i = 0; i < 96; i++) {
EEPROM.write(i, 0);
}
EEPROM.commit();
delay(100);
ESP.reset();
}
ESP8266WebServer server(80);
void setup() {
EEPROM.begin(512); //Initialasing EEPROM
delay(10);
#ifdef DEBUG_ON
Serial.begin(115200); // Start serial debug console monitoring
while (!Serial)
;
#endif
DEBUG();
DEBUG("Disconnecting previously connected WiFi");
WiFi.disconnect();
pinMode(ONKYO_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
//digitalWrite(LED_BUILTIN, HIGH);//this will turn off the NodeMCU blue LED
DEBUG();
DEBUG();
DEBUG("Startup");
//-- Read EEPROM for SSID and pass
DEBUG("Reading EEPROM ssid");
String esid;
for (int i = 0; i < 32; ++i) {
esid += char(EEPROM.read(i));
}
DEBUG();
DEB("SSID: ");
DEBUG(esid);
DEBUG("Reading EEPROM pass");
String epass = "";
for (int i = 32; i < 96; ++i) {
epass += char(EEPROM.read(i));
}
DEB("PASS: ");
DEBUG(epass);
WiFi.hostname("Onkyo-NodeMCU");
WiFi.begin(esid.c_str(), epass.c_str());
if (testWifi()) {
DEBUG("Succesfully Connected!!!");
if (MDNS.begin("Onkyo NodeMCU")) {
DEBUG("MDNS responder started");
}
// Set server routing
restServerRouting();
// Set not found response
server.onNotFound(handleNotFound);
// Start server
server.begin();
MDNS.addService("http", "tcp", 80);
DEBUG("Starting SSDP...");
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort(80);
SSDP.setName("Onkyo NodeMCU");
SSDP.setURL("/");
SSDP.setModelName("D1 Mini NodeMCU");
SSDP.begin();
DEBUG("HTTP server started");
return;
} else {
DEBUG("Turning the HotSpot On");
launchAPWeb();
setupAP(); // Setup HotSpot
}
DEBUG();
DEB("Waiting.");
while ((WiFi.status() != WL_CONNECTED)) {
server.handleClient();
delay(1);
}
}
//-- Fuctions used for WiFi credentials saving and connecting to it which you do not need to change
bool testWifi(void) {
uint i = 0;
DEBUG("Waiting for Wifi to connect");
while (i < 20) {
if (WiFi.status() == WL_CONNECTED) {
return true;
}
delay(500);
DEB("*");
i++;
}
DEBUG();
DEBUG("Connect timed out");
return false;
}
void launchAPWeb() {
DEBUG();
if (WiFi.status() == WL_CONNECTED)
DEBUG("WiFi connected");
DEB("Local IP: ");
DEBUG(WiFi.localIP());
DEB("SoftAP IP: ");
DEBUG(WiFi.softAPIP());
createAPWebServer();
if (MDNS.begin("Onkyo NodeMCU")) {
DEBUG("MDNS responder started");
}
server.begin();
MDNS.addService("http", "tcp", 80);
DEBUG("Starting SSDP");
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort(80);
SSDP.setName("Onkyo NodeMCU");
SSDP.setURL("/");
SSDP.setModelName("D1 Mini NodeMCU");
SSDP.begin();
DEBUG("HTTP server started");
}
// Define routing
void restServerRouting() {
server.on(UriBraces("/{}"), HTTP_GET, []() {
DEB("route: " + server.pathArg(0));
String cmd = (server.pathArg(0));
cmd.toLowerCase();
DEBUG(" - lower: " + cmd);
if (cmds.count(cmd)) {
sprintf(hexadecimalnum, "0x%.3X", cmds[cmd]);
content = "{\"route\": \"" + cmd + "\",\"cmd\": \"" + hexadecimalnum + "\"}";
statusCode = 200;
onkyoSend(cmds[cmd]);
server.send(statusCode, "application/json", content);
} else if (cmd == "") {
content = "[";
for (auto const& [key, val] : cmds) {
sprintf(hexadecimalnum, "0x%.3X", val);
content += "{\"route\": \"" + key + "\",\"cmd\": \"" + hexadecimalnum + "\"},";
}
content += "{\"route\": \"initialize\",\"cmd\": \"Reset WiFi settings\"},{\"route\": \"custom\",\"URI parameters\": [{\"cmd\": []}]}]";
statusCode = 200;
server.send(statusCode, "application/json", content);
} else if (cmd == "initialize") {
server.send(200, "application/json", "{\"route\": \"initialize\",\"cmd\": \"EEPROM ssid & pass data cleared!\"}");
DEBUG("EEPROM ssid & pass data cleared!");
clearEEPROM();
} else if (cmd == "custom") {
String custom_cmd = server.arg("cmd");
if (custom_cmd.length() == 0) {
content = "{\"Error\":\"422 required parameter not found\"}";
statusCode = 422;
} else {
int cmd_len = custom_cmd.length() + 1;
char cmd_array[cmd_len];
custom_cmd.toCharArray(cmd_array, cmd_len);
int cmd_int = (int)strtol(cmd_array, 0, 16);
if (cmd_int != 0) {
DEBUG(cmd_array);
DEBUG(cmd_int);
onkyoSend(cmd_int);
content = "{\"route\": \"custom\",\"cmd\": \"" + custom_cmd + "\"}";
statusCode = 200;
} else {
content = "{\"Error\":\"400 Bad request\"}";
statusCode = 400;
}
}
//server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(statusCode, "application/json", content);
} else if (cmd == "description.xml") {
SSDP.schema(server.client());
} else {
handleNotFound();
}
});
}
// Manage not found URL
void handleNotFound() {
String message = "Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setupAP(void) {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
DEBUG("scan done");
if (n == 0)
DEBUG("no networks found");
else {
DEB(n);
DEBUG(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
DEB(i + 1);
DEB(": ");
DEB(WiFi.SSID(i));
DEB(" (");
DEB(WiFi.RSSI(i));
DEB(")");
DEBUG((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
}
}
DEBUG();
st = "<ol>";
selectSSID = "<input type='text' id='ssid' name='ssid' list='ssidList'/><datalist id='ssidList'>";
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
st += "<li>";
st += WiFi.SSID(i);
selectSSID += "<option value='";
selectSSID += WiFi.SSID(i);
selectSSID += "'>";
selectSSID += WiFi.SSID(i);
selectSSID += "</option>";
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
st += "</li>";
}
st += "</ol>";
selectSSID += "</datalist>";
delay(100);
WiFi.softAP("Onkyo NodeMCU");
DEBUG("softap");
launchAPWeb();
DEBUG("over");
}
void createAPWebServer() {
server.on("/description.xml", HTTP_GET, []() {
SSDP.schema(server.client());
});
server.on("/", []() {
IPAddress ip = WiFi.softAPIP();
String ipStr = ip.toString().c_str(); //String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
content = "<!DOCTYPE HTML><html>";
content += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
content += "<link rel=\"icon\" href=\"data:,\">";
content += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}";
content += ".button { background-color: #253342; border: none; color: white; padding: 16px 40px;";
content += "text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}";
content += ".button2 {background-color: #555555;}</style></head>";
content += "<body><h1>Onkyo NodeMCU</h1>";
content += "<form action=\"/scan\" method=\"POST\"><input type=\"submit\" value=\"scan\" class=\"button\"></form>";
// content += ipStr;
content += "<p>";
content += st;
content += "</p><form method='get' action='setting'><label>SSID: </label>";
content += selectSSID;
content += "<br><label>Password: </label><input name='pass' length=64><br><input type=\"submit\" class=\"button\"></form>";
content += "</body></html>";
server.send(200, "text/html", content);
});
server.on("/scan", []() {
IPAddress ip = WiFi.softAPIP();
String ipStr = ip.toString().c_str(); //String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
content = "<!DOCTYPE HTML>\r\n<html><button onclick='window.location.href=\"/\";'>Back</button></html>";
server.send(200, "text/html", content);
});
server.on("/setting", []() {
String qsid = server.arg("ssid");
String qpass = server.arg("pass");
if (qsid.length() > 0 && qpass.length() > 0) {
DEBUG("clearing eeprom");
for (int i = 0; i < 96; ++i) {
EEPROM.write(i, 0);
}
DEBUG(qsid);
DEBUG();
DEBUG(qpass);
DEBUG();
DEB("writing eeprom ssid: ");
for (int i = 0; i < qsid.length(); ++i) {
EEPROM.write(i, qsid[i]);
DEB(qsid[i]);
}
DEBUG();
DEB("writing eeprom pass: ");
for (int i = 0; i < qpass.length(); ++i) {
EEPROM.write(32 + i, qpass[i]);
DEB(qpass[i]);
}
DEBUG();
EEPROM.commit();
content = "{\"Success\":\"saved to eeprom... The device will now reset to boot into new wifi\"}";
statusCode = 200;
} else {
content = "{\"Error\":\"404 not found\"}";
statusCode = 404;
DEBUG("Sending 404");
}
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(statusCode, "application/json", content);
if (statusCode == 200) {
delay(100);
ESP.reset();
}
});
}
/*ONKYO*/
void onkyoSend(int command) {
onkyoWriteHeader();
for (int i = 0; i < 12; i++) {
bool level = command & 0x800;
command <<= 1;
onkyoWriteBit(level);
}
onkyoWriteFooter();
}
void onkyoWriteHeader() {
//DEBUG(micros());
digitalWrite(ONKYO_PIN, HIGH);
delayMicroseconds(3000);
digitalWrite(ONKYO_PIN, LOW);
delayMicroseconds(1000);
//DEBUG(micros());
}
void onkyoWriteBit(bool level) {
digitalWrite(ONKYO_PIN, HIGH);
delayMicroseconds(1000);
digitalWrite(ONKYO_PIN, LOW);
if (level)
delayMicroseconds(2000);
else
delayMicroseconds(1000);
}
void onkyoWriteFooter() {
digitalWrite(ONKYO_PIN, HIGH);
delayMicroseconds(1000);
digitalWrite(ONKYO_PIN, LOW);
delay(20);
}
void loop(void) {
server.handleClient();
delay(1);
}