-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebRemote.ino
276 lines (246 loc) · 7.62 KB
/
WebRemote.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
/*
MIT License
Copyright (c) 2021 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define DEBUG_ON 0
#if DEBUG_ON
#define Dbg_begin(...) Serial.begin(__VA_ARGS__)
#define Dbg_print(...) Serial.print(__VA_ARGS__)
#define Dbg_println(...) Serial.println(__VA_ARGS__)
#define Dbg_printf(...) Serial.printf(__VA_ARGS__)
#else
#define Dbg_begin(...)
#define Dbg_print(...)
#define Dbg_println(...)
#define Dbg_printf(...)
#endif
#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <ESP8266mDNS.h>
#endif // ESP8266
#if defined(ESP32)
#include <ESPmDNS.h>
#include <WebServer.h>
#include <WiFi.h>
#include <Update.h>
#endif // ESP32
#include <WiFiClient.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <IRsend.h>
#include "index_html.h"
#include "xbox_media.h"
#if defined(ESP8266)
ESP8266WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
#endif // ESP8266
#if defined(ESP32)
WebServer server(80);
#endif // ESP32
MDNSResponder mdns;
WiFiManager wifiManager;
#include <PubSubClient.h>
const char mqtt_server[] = "raspi2";
WiFiClient espClient;
PubSubClient mqttClient(espClient);
// ESP GPIO pin to use. Recommended: 4 (D2).
IRsend irsend(4); // Set the GPIO to be used to sending the message.
// mDSN hostname is webremote.local
#define HOSTNAME "WebRemote"
void handleRoot(void){
server.send(200, "text/html", INDEX_HTML);
}
void handleNotFound(void) {
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 Json_touch_grid(uint8_t client, Touch_Page_t &page)
{
// Build remote control grid for web socket client. Encoded in JSON format.
String json = "[";
for (int r = 0; r < page.rows; r++) {
json.concat('[');
for (int c = 0; c < page.cols; c++) {
Touch_IRcode_t *ir = (Touch_IRcode_t *)page.IR_cells + ((r * page.cols) + c);
if (ir->cell_name != NULL) {
Dbg_printf("c = %d, r = %d, cell_name = %s colSpan = %d\n", c, r, ir->cell_name, ir->colSpan);
if (c != 0) json.concat(',');
json.concat("{\"cellLabel\":\"");
json.concat(ir->cell_name);
json.concat('"');
if (ir->colSpan > 0) {
json.concat(",\"colSpan\":");
json.concat(ir->colSpan);
}
json.concat('}');
}
}
if (r == (page.rows - 1)) {
json.concat(']');
}
else {
json.concat("],");
}
}
json.concat(']');
Dbg_println(json);
webSocket.sendTXT(client, json.c_str(), json.length());
}
void sendIRcode(Touch_Page_t &page, uint8_t row, uint8_t col)
{
Dbg_printf("sendIRCode (%d,%d)\n", row, col);
if (row >= page.rows) {
Dbg_printf("row=%d too high\n", row);
row = page.rows - 1;
}
if (col >= page.cols) {
Dbg_printf("col=%d too high\n", col);
col = page.cols - 1;
}
Touch_IRcode_t *ir = (Touch_IRcode_t *)page.IR_cells + ((row * page.cols) + col);
Dbg_printf("type= %d code= 0x%llx\n", ir->IRType, ir->IRCode);
if (ir->IRType == WALL_POWER_ON) {
mqttClient.publish("x10/A1", "On");
}
else if (ir->IRType == WALL_POWER_OFF) {
mqttClient.publish("x10/A1", "Off");
}
else if (ir->IRType == NEC) {
if (!irsend.send(ir->IRType, ir->IRCode, ir->nbits, ir->repeat)) {
Dbg_println(F("sendIRCode fail"));
}
}
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
Dbg_printf("webSocketEvent(%d, %d, ...)\r\n", num, type);
switch(type) {
case WStype_DISCONNECTED:
Dbg_printf("[%u] Disconnected!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Dbg_printf("[%u] Connected from %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// Send touch grid in JSON format
Json_touch_grid(num, XBox_Media);
}
break;
case WStype_TEXT:
{
Dbg_printf("[%u] get Text: %s\r\n", num, payload);
StaticJsonDocument<64> doc;
DeserializationError error = deserializeJson(doc, payload, length);
if (error) {
Dbg_print(F("deserializeJson() failed: "));
Dbg_println(error.f_str());
return;
}
const char * event = doc["event"];
if (strcmp(event, "touch start") == 0) {
sendIRcode(XBox_Media, doc["row"], doc["col"]);
}
}
break;
case WStype_BIN:
Dbg_printf("[%u] get binary length: %u\r\n", num, length);
hexdump(payload, length);
break;
default:
Dbg_printf("Invalid WStype [%d]\r\n", type);
break;
}
}
void setup()
{
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
WiFi.mode(WIFI_STA);
irsend.begin();
Dbg_begin(115200);
bool res = wm.autoConnect("WebRemote");
if(!res) {
Dbg_println(F("Failed to connect"));
delay(1000);
ESP.restart();
}
#if defined(ESP8266)
if (mdns.begin(HOSTNAME, WiFi.localIP())) {
#else // ESP8266
if (mdns.begin(HOSTNAME)) {
#endif // ESP8266
Dbg_println(F("MDNS responder started"));
mdns.addService("http", "tcp", 80); // Announce http tcp service on port 80
mdns.addService("ws", "tcp", 81); // Announce web sock tcp service on port 81
}
// Start web socket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);
// Media Remote Control is the home page
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
// Setup MQTT broker/server
mqttClient.setServer(mqtt_server, 1883);
}
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (mqttClient.connect(clientId.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop()
{
#if defined(ESP8266)
mdns.update();
#endif
if (!mqttClient.connected()) {
reconnect();
}
else {
mqttClient.loop();
}
webSocket.loop();
server.handleClient();
}