-
Notifications
You must be signed in to change notification settings - Fork 7
/
esp8266_ir_blaster.ino
146 lines (124 loc) · 4.1 KB
/
esp8266_ir_blaster.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
/*
* This programm runs a webserver on the chip providing to main routes:
* POST /play
* This will trigger a 38khz PWM signal on the IR LED with the given timings
* Parameters: expects form encoded parameter 'timings' as comma separated list of integers
*
* GET /record
* This will probe the IR sensor and return the timings in the response body.
* The probing call is not blocking apparently, so it may have to be triggered again, to
* actually show a result.
* If nothing was receive the response will contain the string 'empty'.
*
* All neccessary configuration parameters are defined in the included config.h
* If you copied/cloned this project for the first time you may have to copy
* the config.h.example -> config.h and update the parameters with the matching values.
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#include <IRsend.h>
#include "config.h"
IRrecv irrecv(RECV_PIN);
IRsend irsend(IR_LED); // Set the GPIO to be used to sending the message.
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "NodeMCU infrared remote");
}
#define MAX_DATA_LENGTH 100
uint16_t signal_data[MAX_DATA_LENGTH];
int current_data_length = -1;
void readCSV(String s) {
int numStart = 0;
current_data_length = 0;
for (int i = 0; i <= s.length(); ++i) {
if ( (s.charAt(i) == ',') || ( i == (s.length()) )) {
signal_data[current_data_length++] = s.substring(numStart, i).toInt();
numStart = i + 1;
}
}
}
void handlePlay() {
String response = "POSTED";
response += server.arg("timings");
readCSV(server.arg("timings"));
for (int i = 0; i < current_data_length; ++i) {
Serial.println(signal_data[i]);
}
irsend.sendRaw(signal_data, current_data_length, 38); // Send a raw data capture at 38kHz.
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", response);
}
#define CAPTURE_BUFFER_SIZE 1024
#if DECODE_AC
#define TIMEOUT 50U // Some A/C units have gaps in their protocols of ~40ms.
// e.g. Kelvinator
// A value this large may swallow repeats of some protocols
#else // DECODE_AC
#define TIMEOUT 15U // Suits most messages, while not swallowing many repeats.
#endif // DECODE_AC
decode_results results;
void handleRecord() {
String output = "";
if (irrecv.decode(&results)) {
output += resultToSourceCode(&results);
irrecv.resume(); // Receive the next value
} else {
output = "empty";
}
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/html", output);
}
void handleNotFound(){
String message = "File 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 setup(void){
Serial.begin(115200);
irrecv.enableIRIn(); // Start the receiver
irsend.begin(); // Start IR sender
while (!Serial) // Wait for the serial connection to be establised.
delay(50);
Serial.println();
Serial.print("IRrecvDemo is now running and waiting for IR message on Pin ");
Serial.println(RECV_PIN);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(SSID);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/record", handleRecord);
server.on("/play", HTTP_POST, handlePlay);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}