-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRServer.cpp
205 lines (179 loc) · 4.99 KB
/
IRServer.cpp
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
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include "webpage.hpp"
#include "password.hpp"
#define NUM_CMD 20
MDNSResponder mdns;
ESP8266WiFiMulti WifiMulti;
#define TIMEOUT 5000
ESP8266WebServer server(80);
#undef HOSTNAME
#define HOSTNAME "esp8266"
const uint16_t IRLed = 16; // D0 PIN //ESP GPIO pin to use. Recommended: 4 (D2).
IRsend irsend(IRLed); // Set the GPIO to be used to sending the message.
#define RED 14
#define GREEN 12
#define BLUE 13
inline void blinkLED(uint8_t pin, uint16_t time_ms) {
digitalWrite(pin, 1);
//esp_try_delay(time_ms, NULL);
delay(time_ms);
digitalWrite(pin, 0);
delay(time_ms);
}
static char send_buf[4096] = { 0 };
void handleRoot() {
digitalWrite(BLUE,1);
sprintf(send_buf, INDEX, WiFi.localIP().toString().c_str(),"");
server.send(200, "text/html", send_buf);
digitalWrite(BLUE,0);
}
void handleIr() {
digitalWrite(BLUE,1);
if (!server.hasArg("code")) {
server.send(401, "text/plain", "missing ?code=");
}
uint8_t code = server.arg(0).toInt();
uint16_t msg = 0;
if (code <= 63) {
msg = irsend.encodeRC5X(20, code, true);
} else {
msg = irsend.encodeRC5X(16, code, true);
}
irsend.sendRC5(msg);
char temp[16];
sprintf(temp, "Sent: %x", msg);
//sprintf(send_buf, PAGE, WiFi.localIP().toString().c_str(),temp);
server.send(200, "text/html", String(send_buf));
digitalWrite(BLUE, 0);
}
void handleRC5X() {
digitalWrite(BLUE,1);
bool release = false;
if (!server.hasArg("dev")) {
server.send(401, "text/plain", "missing ?dev=");
}
if (!server.hasArg("code")) {
server.send(401, "text/plain", "missing ?code=");
}
if (server.hasArg("release")) {
release = true;
}
uint16_t dev = server.arg(0).toInt();
uint64_t code = server.arg(1).toInt();
uint16_t msg = irsend.encodeRC5X(dev, code, release);
irsend.sendRC5(msg);
char temp[16] = { 0 };
snprintf(temp,16, "Sent: %x", msg);
server.send(200, "text/plain", temp);
digitalWrite(BLUE,0);
}
void handleRC5() {
digitalWrite(BLUE,1);
bool release = false;
if (!server.hasArg("dev")) {
server.send(401, "text/plain", "missing ?dev=");
}
if (!server.hasArg("code")) {
server.send(401, "text/plain", "missing ?code=");
}
if (server.hasArg("release")) {
release = true;
}
uint16_t dev = server.arg(0).toInt();
uint64_t code = server.arg(1).toInt();
uint16_t msg = irsend.encodeRC5(dev, code, release);
irsend.sendRC5(msg);
char temp[16] = { 0 };
snprintf(temp,16, "Sent: %x", msg);
server.send(200, "text/plain", temp);
digitalWrite(BLUE,0);
}
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) {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
irsend.begin();
Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
while (!Serial) {}
WiFi.mode(WIFI_STA);
for (int i = 0; i < NUM_CONN; i++) {
WifiMulti.addAP(SSID_Arr[i], PASSWORD_Arr[i]);
}
char buf[16];
for (uint8_t i = 20; i <= 20; i++) {
break;
for (uint8_t j = 0; j <= 127; j++) {
if (j == 36 || j == 12 || j == 53||j == 45) continue;
//uint16_t msg = irsend.encodeRC5(i, j, true);
uint16_t msg = irsend.encodeRC5X(i, j, true);
sprintf(buf, "i: %d j:%d msg: %x", i, j, msg);
Serial.println(buf);
irsend.sendRC5(msg);
delay(1000);
//}
}
}
while (1) {
break;
char buf[64];
//uint16_t msg = irsend.encodeRC5(17, 70, true);
uint16_t msg = irsend.encodeRC5X(20, 26, true);
//uint16_t msg = 0xd4F;
//uint16_t msg = 0xc2d;
sprintf(buf, "msg: %x", msg);
Serial.println(buf);
irsend.sendRC5(msg);
delay(3000);
}
/*
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(WiFi.SSID() );
Serial.print("IP address: ");
Serial.println(WiFi.localIP().toString());
*/
if (mdns.begin(HOSTNAME)) {
Serial.println("MDNS responder started");
// Announce http tcp service on port 80
mdns.addService("http", "tcp", 80);
}
server.on("/", HTTP_GET, handleRoot);
server.on("/ir",HTTP_GET, handleIr);
server.on("/rc5x",HTTP_GET, handleRC5X);
server.on("/rc5",HTTP_GET, handleRC5);
//server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
mdns.update();
server.handleClient();
if (WifiMulti.run(5000) != WL_CONNECTED) {
blinkLED(RED, 500);
}
//if (WiFi.status() == WL_CONNECTED) blinkLED(GREEN, 80);
}