-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRController_arcam
351 lines (306 loc) · 10.3 KB
/
IRController_arcam
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
```
#include <IRremoteESP8266.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
//+=============================================================================
// Please customize the following settings
//
const char* ssid = "YOUR-SSID"; // WiFi SSID
const char* password = "YOUR-WIFI-PASSWORD"; // WiFi password
const char* passcode = "pass"; // Access code to send IR commands
const int port = YOUR-PORT; // Receiving HTTP port
IPAddress ip(192, 168, XX, YY); // ESP8266 IP Address
IPAddress dns(192, 168, XX, YY); // DNS Server
IPAddress gw(192, 168, XX, YY); // Gateway
IPAddress subnet(255, 255, 255, 0); // Subnet
IRrecv irrecv(5); // Receiving pin
IRsend irsend1(4); // Transmitting preset 1
IRsend irsend2(0); // Transmitting preset 2
IRsend irsend3(12); // Transmitting preset 3
IRsend irsend4(13); // Transmitting preset 4
//
// End configuration area
//+=============================================================================
ESP8266WebServer server(port);
HTTPClient http;
//+=============================================================================
// Setup web server and IR receiver/blaster
//
void setup() {
// Initialize serial
Serial.begin(115200);
Serial.println("ESP8266 IR Controller");
// Begin WiFi
WiFi.config(ip, dns, gw, subnet); // Enable to hardcode an IP address
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
// Configure the server
// Setup simple msg server to mirror version 1.0 functionality
server.on("/msg", []() {
Serial.println("Connection received - MSG");
if (server.arg("pass") != passcode) {
Serial.println("Unauthorized access");
server.send(401, "text/html", "Unauthorized");
} else {
String type = server.arg("type");
String data = server.arg("data");
String ip = server.arg("ip");
int len = server.arg("length").toInt();
long address = (server.hasArg("address")) ? server.arg("address").toInt() : 0;
int rdelay = (server.hasArg("delay")) ? server.arg("rdelay").toInt() : 1000;
int pulse = (server.hasArg("pulse")) ? server.arg("pulse").toInt() : 2;
int pdelay = (server.hasArg("pdelay")) ? server.arg("pdelay").toInt() : 100;
int repeat = (server.hasArg("repeat")) ? server.arg("repeat").toInt() : 1;
int out = (server.hasArg("out")) ? server.arg("out").toInt() : 0;
if (server.hasArg("code")) {
String code = server.arg("code");
char separator = ':';
data = getValue(code, separator, 0);
type = getValue(code, separator, 1);
len = getValue(code, separator, 2).toInt();
}
irblast(type, data, len, rdelay, pulse, pdelay, repeat, address, pickIRsend(out));
server.send(200, "text/html", "Sending code");
}
});
server.on("/", []() {
Serial.println("Connection received");
server.send(200, "text/html", "Server is running");
});
server.begin();
Serial.println("HTTP Server started on port " + String(port));
irsend1.begin();
irsend2.begin();
irsend3.begin();
irsend4.begin();
irrecv.enableIRIn();
Serial.println("Ready to send and receive IR signals");
}
//+=============================================================================
// Split string by character
//
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
//+=============================================================================
// Display IR code
//
void ircode (decode_results *results)
{
// Panasonic has an Address
if (results->decode_type == PANASONIC) {
Serial.print(results->panasonicAddress, HEX);
Serial.print(":");
}
// Print Code
Serial.print(results->value, HEX);
}
//+
// Return which IRsend object to act on
//
IRsend pickIRsend (int out) {
switch (out) {
case 1: return irsend1;
case 2: return irsend2;
case 3: return irsend3;
case 4: return irsend4;
default: return irsend1;
}
}
//+=============================================================================
// Display encoding type
//
void encoding (decode_results *results)
{
switch (results->decode_type) {
default:
case UNKNOWN: Serial.print("UNKNOWN"); break ;
case NEC: Serial.print("NEC"); break ;
case SONY: Serial.print("SONY"); break ;
case RC5: Serial.print("RC5"); break ;
case RC6: Serial.print("RC6"); break ;
case DISH: Serial.print("DISH"); break ;
case SHARP: Serial.print("SHARP"); break ;
case JVC: Serial.print("JVC"); break ;
case SANYO: Serial.print("SANYO"); break ;
case MITSUBISHI: Serial.print("MITSUBISHI"); break ;
case SAMSUNG: Serial.print("SAMSUNG"); break ;
case LG: Serial.print("LG"); break ;
case WHYNTER: Serial.print("WHYNTER"); break ;
case PANASONIC: Serial.print("PANASONIC"); break ;
}
}
//+=============================================================================
// Single line compact code
//
void fullCode (decode_results *results)
{
Serial.print(results->value, HEX);
Serial.print(":");
encoding(results);
Serial.print(":");
Serial.print(results->bits, DEC);
Serial.println("");
}
//+=============================================================================
// Dump out the decode_results structure.
//
void dumpInfo (decode_results *results)
{
// Show Encoding standard
Serial.print("Encoding : ");
encoding(results);
Serial.println("");
// Show Code & length
Serial.print("Code : ");
ircode(results);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
}
//+=============================================================================
// Dump out the decode_results structure.
//
void dumpRaw (decode_results *results)
{
// Print Raw data
Serial.print("Timing[");
Serial.print(results->rawlen - 1, DEC);
Serial.println("]: ");
for (int i = 1; i < results->rawlen; i++) {
unsigned long x = results->rawbuf[i] * USECPERTICK;
if (!(i & 1)) { // even
Serial.print("-");
if (x < 1000) Serial.print(" ") ;
if (x < 100) Serial.print(" ") ;
Serial.print(x, DEC);
} else { // odd
Serial.print(" ");
Serial.print("+");
if (x < 1000) Serial.print(" ") ;
if (x < 100) Serial.print(" ") ;
Serial.print(x, DEC);
if (i < results->rawlen - 1) Serial.print(", "); //',' not needed for last one
}
if (!(i % 8)) Serial.println("");
}
Serial.println(""); // Newline
}
//+=============================================================================
// Dump out the decode_results structure.
//
void dumpCode (decode_results *results)
{
// Start declaration
Serial.print("unsigned int "); // variable type
Serial.print("rawData["); // array name
Serial.print(results->rawlen - 1, DEC); // array size
Serial.print("] = {"); // Start declaration
// Dump data
for (int i = 1; i < results->rawlen; i++) {
Serial.print(results->rawbuf[i] * USECPERTICK, DEC);
if ( i < results->rawlen - 1 ) Serial.print(","); // ',' not needed on last one
if (!(i & 1)) Serial.print(" ");
}
// End declaration
Serial.print("};");
// Comment
Serial.print(" // ");
encoding(results);
Serial.print(" ");
ircode(results);
// Newline
Serial.println("");
// Now dump "known" codes
if (results->decode_type != UNKNOWN) {
// Some protocols have an address
if (results->decode_type == PANASONIC) {
Serial.print("unsigned int addr = 0x");
Serial.print(results->panasonicAddress, HEX);
Serial.println(";");
}
// All protocols have data
Serial.print("unsigned int data = ");
Serial.print(results->value, HEX);
Serial.println(";");
}
}
//+=============================================================================
// Convert string to hex, borrowed from ESPBasic
//
unsigned long HexToLongInt(String h)
{
// this function replace the strtol as this function is not able to handle hex numbers greather than 7fffffff
// I'll take char by char converting from hex to char then shifting 4 bits at the time
int i;
unsigned long tmp = 0;
unsigned char c;
int s = 0;
h.toUpperCase();
for (i = h.length() - 1; i >= 0 ; i--)
{
// take the char starting from the right
c = h[i];
// convert from hex to int
c = c - '0';
if (c > 9)
c = c - 7;
// add and shift of 4 bits per each char
tmp += c << s;
s += 4;
}
return tmp;
}
//+=============================================================================
// Send IR codes to variety of sources
//
void irblast(String type, String dataStr, int len, int rdelay, int pulse, int pdelay, int repeat, long address, IRsend irsend) {
Serial.println("Blasting off");
type.toLowerCase();
long data = HexToLongInt(dataStr);
// Repeat Loop
for (int r = 0; r < repeat; r++) {
// Pulse Loop
for (int p = 0; p < pulse; p++) {
Serial.print(data, HEX);
Serial.print(":");
Serial.print(type);
Serial.print(":");
Serial.println(len);
irsend.sendRC5(data, len);
delay(pdelay);
}
delay(rdelay);
}
}
void loop() {
server.handleClient();
decode_results results; // Somewhere to store the results
if (irrecv.decode(&results)) { // Grab an IR code
fullCode(&results); // Print the singleline value
//dumpInfo(&results); // Output the results
//dumpRaw(&results); // Output the results in RAW format
dumpCode(&results); // Output the results as source code
Serial.println(""); // Blank line between entries
irrecv.resume(); // Prepare for the next value
}
}
```