-
Notifications
You must be signed in to change notification settings - Fork 7
/
irServer.ino
490 lines (418 loc) · 14.5 KB
/
irServer.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRrecv.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <FS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
WiFiManager wifiManager;
MDNSResponder mdns;
ESP8266WebServer server(80);
int SERIAL_SPEED = 115200;
int RECV_PIN = 5; //an IR detector/demodulator is connected to GPIO //D1
int SEND_PIN = 4; // D2 paired with a 1.6 Ohm resistor
char * CONFIG_PATH = "config.txt";
char * CONFIG_BACKUP_PATH = "config.bak";
File fsUploadFile;
IRrecv irrecv(RECV_PIN);
IRsend irsend(SEND_PIN);
decode_results results1; // Somewhere to store the results
decode_results results; // Somewhere to store the results
void handleRoot() {
server.send(200, "text/html", "Please specify command! Form: /ir?code=xxx&bits=xx&protocol=x");
}
void handleIr() {
String codestring = server.arg("code");
String protocol = server.arg("protocol");
String bitsstring = server.arg("bits");
String deviceCode = server.arg("deviceCode");
String subDeviceCode = server.arg("subDeviceCode");
String obc = server.arg("obc");
String pronto = server.arg("pronto");
// String webOutput = "Protocol: "+protocol+"; Code: "+codestring+"; Bits: "+bitsstring + " - ("+deviceCode + subDeviceCode + obc +")";
String webOutput = "";
Serial.println(webOutput);
unsigned long code = 0;
int rc5_control_bit = 0;
if ((codestring != "") && (bitsstring != "")) {
//unsigned long code = codestring.toInt();
char tarray[15];
codestring.toCharArray(tarray, sizeof(tarray));
code = strtoul(tarray, NULL, 16);
// unsigned long code = atol(codestring);
}
int bits = bitsstring.toInt();
if ((obc != "") && (deviceCode != "")) {
//convert OBC & deviceCode to hex CodeString
int iDeviceCode = StrToUL(deviceCode);
int iDeviceCodeLSB = flipBits(iDeviceCode);
int iDeviceCodeLSB_INV = iDeviceCodeLSB ^ 0xFF;
dump(iDeviceCode, iDeviceCodeLSB, iDeviceCodeLSB_INV);
int iSubDeviceCode;
int iSubDeviceCodeLSB;
int iSubDeviceCodeLSB_INV;
if ((subDeviceCode == "")) {
iSubDeviceCode = iDeviceCode;
iSubDeviceCodeLSB = iDeviceCodeLSB;
iSubDeviceCodeLSB_INV = iDeviceCodeLSB_INV;
} else {
iSubDeviceCode = StrToUL(subDeviceCode);
iSubDeviceCodeLSB = flipBits(iSubDeviceCode);
iSubDeviceCodeLSB_INV = iSubDeviceCodeLSB ^ 0xFF;
}
int iOBC = StrToUL(obc);
int iOBCLSB = flipBits(iOBC);
int iOBCLSB_INV = iOBCLSB ^ 0xFF;
dump(iOBC, iOBCLSB, iOBCLSB_INV);
Serial.println(iDeviceCodeLSB, HEX);
Serial.println(iOBCLSB, HEX);
Serial.println("----");
if (protocol == "Samsung") {
code = combineBytes(iDeviceCodeLSB, iSubDeviceCodeLSB, iOBCLSB, iOBCLSB_INV);
} else if (protocol == "NEC" || protocol == "NECx2") {
code = combineBytes(iDeviceCodeLSB, iDeviceCodeLSB_INV, iOBCLSB, iOBCLSB_INV);
} else if (protocol == "RC6") {
/*NOT TESTED*/
code = combineBytes(0, 0, iDeviceCode, iOBC);
bits = 20;
} else if (protocol == "RC5") {
/*NOT TESTED*/
/*control=1,device=5,command=6*/
rc5_control_bit = abs(rc5_control_bit - 1);
code = rc5_control_bit;
code += code << 5 + iDeviceCodeLSB;
code += code << 6 + iOBCLSB;
bits = 12;
} else if (protocol == "JVC") {
/*NOT TESTED*/
code = combineBytes(0, 0, iDeviceCodeLSB, iOBCLSB);
bits = 16;
} else if (protocol == "Sony") {
/*NOT TESTED & highly suspect, need to seem some example codes*/
code = iOBCLSB;
if (subDeviceCode != "") {
bits = 20;
code = code << 5 + iDeviceCodeLSB;
code = code << 8 + iSubDeviceCodeLSB;
} else if (iDeviceCode > 2 ^ 5) {
bits = 15;
code = code << 8 + iDeviceCodeLSB;
} else {
bits = 12;
code = code << 5 + iDeviceCodeLSB;
}
} else {
code = 0;
server.send(404, "text/html", "Protocol not implemented for OBC!");
}
Serial.println("---");
Serial.println(code, HEX);
Serial.println("---");
}
if (code != 0) {
Serial.println(code, HEX);
if (protocol == "NEC") {
server.send(200, "text/html", webOutput);
irsend.sendNEC(code, bits);
}
else if (protocol == "Sony") {
server.send(200, "text/html", webOutput);
irsend.sendSony(code, bits);
}
else if (protocol == "Whynter") {
server.send(200, "text/html", webOutput);
irsend.sendWhynter(code, bits);
}
else if (protocol == "LG") {
server.send(200, "text/html", webOutput);
irsend.sendLG(code, bits);
}
else if (protocol == "RC5") {
server.send(200, "text/html", webOutput);
irsend.sendRC5(code, bits);
}
else if (protocol == "RC6") {
server.send(200, "text/html", webOutput);
irsend.sendRC6(code, bits);
}
else if (protocol == "DISH") {
server.send(200, "text/html", webOutput);
irsend.sendDISH(code, bits);
}
else if (protocol == "SharpRaw") {
server.send(200, "text/html", webOutput);
irsend.sendSharpRaw(code, bits);
}
else if (protocol == "Samsung") {
server.send(200, "text/html", webOutput);
irsend.sendSAMSUNG(code, bits);
}
else {
server.send(404, "text/html", "Protocol not implemented!");
}
} else if (pronto != "") {
//pronto code
//blocks of 4 digits in hex
//preample is 0000 FREQ LEN1 LEN2
//followed by ON/OFF durations in FREQ cycles
//Freq needs a multiplier
//blocks seperated by %20
//we are ignoring LEN1 & LEN2 for this use case as not allowing for repeats
//just pumping all
int spacing = 5;
int len = pronto.length();
int out_len = ((len - 4) / spacing) - 3;
uint16_t prontoCode[out_len];
unsigned long timeperiod;
unsigned long multiplier = .241246 ;
int pos = 0;
unsigned long hz;
if (pronto.substring(pos, 4) != "0000") {
server.send(404, "text/html", "unknown pronto format!");
//unknown pronto format
} else {
pos += spacing;
hz = strtol(pronto.substring(pos, pos + 4).c_str(), NULL, 16);
hz = (hz * .241246);
hz = 1000000 / hz;
//XXX TIMING IS OUT
timeperiod = 1000000 / hz;
pos += spacing; //hz
pos += spacing; //LEN1
pos += spacing; //LEN2
delay(0);
for (int i = 0; i < out_len; i++) {
prontoCode[i] = (strtol(pronto.substring(pos, pos + 4).c_str(), NULL, 16) * timeperiod) + 0.5;
pos += spacing;
}
//sendRaw
yield();
irsend.sendRaw(prontoCode, out_len, hz / 1000);
server.send(200, "text/html", "pronto code!");
}
} else {
server.send(404, "text/html", "Missing code or bits!");
}
}
unsigned long StrToUL (String str) {
char tarray[15];
str.toCharArray(tarray, sizeof(tarray));
unsigned long code = strtoul(tarray, NULL, 10);
return (code);
}
unsigned long combineBytes(int a1, int a2, int a3, int a4) {
unsigned long code = 0;
code = code + a1;
code = (code << 8) + a2;
code = (code << 8) + a3;
code = (code << 8) + a4;
return (code);
}
int flipBits(unsigned char b) {
return ( (b * 0x0202020202ULL & 0x010884422010ULL) % 1023);
}
void dump(int a1, int a2, int a3) {
Serial.print(a1, HEX);
Serial.print("-");
Serial.print(a2, HEX);
Serial.print("-");
Serial.println(a3, HEX);
}
void handleNotFound() {
server.send(404, "text/plain", "404");
}
void handleReset() {
wifiManager.resetSettings();
}
void handleLoadConfig() {
File f;
if (!SPIFFS.exists(CONFIG_PATH)) {
//doesn't exist create blank config
f = SPIFFS.open(CONFIG_PATH, "w");
f.println("{\"pages\":[{\"name\":\"New Page\",\"buttons\":[]}]}");
Serial.println("CREATED");
f.close();
}
f = SPIFFS.open(CONFIG_PATH, "r");
String s = f.readStringUntil('\n');
Serial.println(s);
f.close();
String callback = server.arg("callback");
server.send(200, "text/plain", callback + "(" + s + ")");
}
void handleLoadBackupConfig() {
File f;
if (!SPIFFS.exists(CONFIG_BACKUP_PATH)) {
f = SPIFFS.open(CONFIG_BACKUP_PATH, "w");
f.println("{\"pages\":[{\"name\":\"New Page\",\"buttons\":[]}]}");
Serial.println("CREATED");
f.close();
}
f = SPIFFS.open(CONFIG_BACKUP_PATH, "r");
String s = f.readStringUntil('\n');
Serial.println(s);
f.close();
String callback = server.arg("callback");
server.send(200, "text/plain", callback + "(" + s + ")");
}
void handleSaveConfig() {
Serial.println("Saving");
File f;
File f2;
if (SPIFFS.exists(CONFIG_PATH)) {
f = SPIFFS.open(CONFIG_PATH, "r");
f2 = SPIFFS.open(CONFIG_BACKUP_PATH, "w");
String s = f.readStringUntil('\n');
f2.println(s);
Serial.println("BACKED UP");
f.close();
f2.close();
Serial.println("Config backuped");
}
f = SPIFFS.open(CONFIG_PATH, "w");
String newConfig = server.arg("config");
f.println(newConfig);
Serial.println(newConfig);
f.close();
String callback = server.arg("callback");
server.send(200, "text/plain", callback + "('SAVED')");
}
void handleDeleteConfig() {
File f;
String callback = server.arg("callback");
if (!SPIFFS.exists(CONFIG_PATH)) {
Serial.println("FILE NOT FOUND");
} else {
SPIFFS.remove(CONFIG_PATH);
Serial.println("FILE DELETED");
}
server.send(200, "text/plain", callback + "(\"File Deleted\")");
};
void learnHandler() {
Serial.println("In Learning Handling");
String callback = server.arg("callback");
String proto = "";
{ // Grab an IR code
// dumpInfo(&results); // Output the results
switch (results.decode_type) {
default:
case UNKNOWN: proto = ("UNKNOWN"); break ;
case NEC: proto = ("NEC"); break ;
case SONY: proto = ("Sony"); break ;
case RC5: proto = ("RC5"); break ;
case RC6: proto = ("RC6"); break ;
case DISH: proto = ("DISH"); break ;
case SHARP: proto = ("SHARP"); break ;
case JVC: proto = ("JVC"); break ;
case SANYO: proto = ("Sanyo"); break ;
case MITSUBISHI: proto = ("MITSUBISHI"); break ;
case SAMSUNG: proto = ("Samsung"); break ;
case LG: proto = ("LG"); break ;
case WHYNTER: proto = ("Whynter"); break ;
// case AIWA_RC_T501: Serial.print("AIWA_RC_T501"); break ;
case PANASONIC: proto = ("PANASONIC"); break ;
}
//results->value
//Serial.print(results->value, HEX);
Serial.println("Here"); // Blank line between entries
irrecv.resume(); // Prepare for the next value
String output = callback + "({protocol:\"" + proto + "\", value:\"" + String((unsigned long)results.value, HEX) + "\", bits:\"" + String(results.bits) + "\"})";
Serial.println(output);
server.send(200, "text/html", output);
}
}
void handleUploadRequest() {
Serial.println("in Upload Request");
server.send(200, "text/html", "");
}
void handleFileUpload() { // upload a new file to the SPIFFS
//if (server.uri() != "/upload") return;
Serial.println("in upload");
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) filename = "/" + filename;
Serial.print("handleFileUpload Name: "); Serial.println(filename);
fsUploadFile = SPIFFS.open(filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist)
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
} else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile) { // If the file was successfully created
fsUploadFile.close(); // Close the file again
Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
server.sendHeader("Location", "/success.html"); // Redirect the client to the success page
server.send(303);
} else {
server.send(500, "text/plain", "500: couldn't create file");
}
}
}
void setup(void) {
irsend.begin();
Serial.begin(SERIAL_SPEED);
SPIFFS.begin();
Serial.println("v5.2");
wifiManager.autoConnect("IRSVR");
if (mdns.begin("irsvr", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
//OTA
// No authentication by default
// ArduinoOTA.setPassword((const char *)"irsvr");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
server.on("/ir", handleIr);
server.on("/reset", handleReset);
server.on("/learn", learnHandler);
server.on("/test", handleRoot);
server.on("/loadConfig", handleLoadConfig);
server.on("/loadBackupConfig", handleLoadBackupConfig);
server.on("/saveConfig", handleSaveConfig);
server.on("/deleteConfig", handleDeleteConfig);
server.onFileUpload(handleFileUpload );
server.on("/upload", HTTP_POST, handleUploadRequest, handleFileUpload );
server.serveStatic("/", SPIFFS, "/index.html");
server.serveStatic("/index.html", SPIFFS, "/index.html");
server.serveStatic("/app.js", SPIFFS, "/app.js");
server.serveStatic("/manage.html", SPIFFS, "/manage.html");
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
irrecv.enableIRIn();
}
void loop(void) {
server.handleClient();
ArduinoOTA.handle();
if (irrecv.decode(&results1)) {
Serial.println("Signal recveived");
if(results1.value==0xffffffff){
Serial.println("ffffffff recieved ignoring");
} else {
irrecv.decode(&results);
}
irrecv.resume();
}
}