-
Notifications
You must be signed in to change notification settings - Fork 252
/
sinric.ino
426 lines (352 loc) · 13 KB
/
sinric.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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
void prepareIds();
boolean connectWifi();
boolean connectUDP();
void startHttpServer();
void turnOnRelay();
void turnOffRelay();
void sendRelayState();
const char* ssid = "************"; // CHANGE: Wifi name
const char* password = "********"; // CHANGE: Wifi password
String friendlyName = "tv"; // CHANGE: Alexa device name
const int relayPin = 5; // D1 pin. More info: https://github.com/esp8266/Arduino/blob/master/variants/d1_mini/pins_arduino.h#L49-L61
WiFiUDP UDP;
IPAddress ipMulti(239, 255, 255, 250);
ESP8266WebServer HTTP(80);
boolean udpConnected = false;
unsigned int portMulti = 1900; // local port to listen on
unsigned int localPort = 1900; // local port to listen on
boolean wifiConnected = false;
boolean relayState = false;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
String serial;
String persistent_uuid;
boolean cannotConnectToWifi = false;
void setup() {
Serial.begin(115200);
// Setup Relay
pinMode(relayPin, OUTPUT);
prepareIds();
// Initialise wifi connection
wifiConnected = connectWifi();
// only proceed if wifi connection successful
if(wifiConnected){
Serial.println("Ask Alexa to discover devices");
udpConnected = connectUDP();
if (udpConnected){
// initialise pins if needed
startHttpServer();
}
}
}
void loop() {
HTTP.handleClient();
delay(1);
// if there's data available, read a packet
// check if the WiFi and UDP connections were successful
if(wifiConnected){
if(udpConnected){
// if there’s data available, read a packet
int packetSize = UDP.parsePacket();
if(packetSize) {
//Serial.println("");
//Serial.print("Received packet of size ");
//Serial.println(packetSize);
//Serial.print("From ");
IPAddress remote = UDP.remoteIP();
for (int i =0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(UDP.remotePort());
int len = UDP.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
String request = packetBuffer;
//Serial.println("Request:");
//Serial.println(request);
// Issue https://github.com/kakopappa/arduino-esp8266-alexa-wemo-switch/issues/24 fix
if(request.indexOf("M-SEARCH") >= 0) {
// Issue https://github.com/kakopappa/arduino-esp8266-alexa-multiple-wemo-switch/issues/22 fix
//if(request.indexOf("urn:Belkin:device:**") > 0) {
if((request.indexOf("urn:Belkin:device:**") > 0) || (request.indexOf("ssdp:all") > 0) || (request.indexOf("upnp:rootdevice") > 0)) {
Serial.println("Responding to search request ...");
respondToSearch();
}
}
}
delay(10);
}
} else {
Serial.println("Cannot connect to Wifi");
}
}
void prepareIds() {
uint32_t chipId = ESP.getChipId();
char uuid[64];
sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff);
serial = String(uuid);
persistent_uuid = "Socket-1_0-" + serial;
}
void respondToSearch() {
Serial.println("");
Serial.print("Sending response to ");
Serial.println(UDP.remoteIP());
Serial.print("Port : ");
Serial.println(UDP.remotePort());
IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
String response =
"HTTP/1.1 200 OK\r\n"
"CACHE-CONTROL: max-age=86400\r\n"
"DATE: Fri, 15 Apr 2016 04:56:29 GMT\r\n"
"EXT:\r\n"
"LOCATION: http://" + String(s) + ":80/setup.xml\r\n"
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
"01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
"ST: urn:Belkin:device:**\r\n"
"USN: uuid:" + persistent_uuid + "::urn:Belkin:device:**\r\n"
"X-User-Agent: redsonic\r\n\r\n";
// Try changing to this if you have problems discovering
/* https://github.com/kakopappa/arduino-esp8266-alexa-wemo-switch/issues/26
String response =
"HTTP/1.1 200 OK\r\n"
"CACHE-CONTROL: max-age=86400\r\n"
"DATE: Fri, 15 Apr 2016 04:56:29 GMT\r\n"
"EXT:\r\n"
"LOCATION: http://" + String(s) + ":80/setup.xml\r\n"
"OPT: "http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
"01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
"ST: ssdp:all\r\n"
"USN: uuid:" + persistent_uuid + "::upnp:rootdevice\r\n"
"X-User-Agent: redsonic\r\n\r\n";
*/
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(response.c_str());
UDP.endPacket();
/* add yield to fix UDP sending response. For more informations : https://www.tabsoverspaces.com/233359-udp-packets-not-sent-from-esp-8266-solved */
yield();
Serial.println("Response sent !");
}
void startHttpServer() {
HTTP.on("/index.html", HTTP_GET, [](){
Serial.println("Got Request index.html ...\n");
HTTP.send(200, "text/plain", "Hello World!");
});
HTTP.on("/upnp/control/basicevent1", HTTP_POST, []() {
Serial.println("########## Responding to /upnp/control/basicevent1 ... ##########");
//for (int x=0; x <= HTTP.args(); x++) {
// Serial.println(HTTP.arg(x));
//}
String request = HTTP.arg(0);
Serial.print("request:");
Serial.println(request);
if(request.indexOf("SetBinaryState") >= 0) {
if(request.indexOf("<BinaryState>1</BinaryState>") >= 0) {
Serial.println("Got Turn on request");
turnOnRelay();
}
if(request.indexOf("<BinaryState>0</BinaryState>") >= 0) {
Serial.println("Got Turn off request");
turnOffRelay();
}
}
if(request.indexOf("GetBinaryState") >= 0) {
Serial.println("Got binary state request");
sendRelayState();
}
HTTP.send(200, "text/plain", "");
});
HTTP.on("/eventservice.xml", HTTP_GET, [](){
Serial.println(" ########## Responding to eventservice.xml ... ########\n");
String eventservice_xml = "<scpd xmlns=\"urn:Belkin:service-1-0\">"
"<actionList>"
"<action>"
"<name>SetBinaryState</name>"
"<argumentList>"
"<argument>"
"<retval/>"
"<name>BinaryState</name>"
"<relatedStateVariable>BinaryState</relatedStateVariable>"
"<direction>in</direction>"
"</argument>"
"</argumentList>"
"</action>"
"<action>"
"<name>GetBinaryState</name>"
"<argumentList>"
"<argument>"
"<retval/>"
"<name>BinaryState</name>"
"<relatedStateVariable>BinaryState</relatedStateVariable>"
"<direction>out</direction>"
"</argument>"
"</argumentList>"
"</action>"
"</actionList>"
"<serviceStateTable>"
"<stateVariable sendEvents=\"yes\">"
"<name>BinaryState</name>"
"<dataType>Boolean</dataType>"
"<defaultValue>0</defaultValue>"
"</stateVariable>"
"<stateVariable sendEvents=\"yes\">"
"<name>level</name>"
"<dataType>string</dataType>"
"<defaultValue>0</defaultValue>"
"</stateVariable>"
"</serviceStateTable>"
"</scpd>\r\n"
"\r\n";
HTTP.send(200, "text/plain", eventservice_xml.c_str());
});
HTTP.on("/setup.xml", HTTP_GET, [](){
Serial.println(" ########## Responding to setup.xml ... ########\n");
IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
String setup_xml = "<?xml version=\"1.0\"?>"
"<root>"
"<device>"
"<deviceType>urn:Belkin:device:controllee:1</deviceType>"
"<friendlyName>"+ friendlyName +"</friendlyName>"
"<manufacturer>Belkin International Inc.</manufacturer>"
"<modelName>Socket</modelName>"
"<modelNumber>3.1415</modelNumber>"
"<modelDescription>Belkin Plugin Socket 1.0</modelDescription>\r\n"
"<UDN>uuid:"+ persistent_uuid +"</UDN>"
"<serialNumber>221517K0101769</serialNumber>"
"<binaryState>0</binaryState>"
"<serviceList>"
"<service>"
"<serviceType>urn:Belkin:service:basicevent:1</serviceType>"
"<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>"
"<controlURL>/upnp/control/basicevent1</controlURL>"
"<eventSubURL>/upnp/event/basicevent1</eventSubURL>"
"<SCPDURL>/eventservice.xml</SCPDURL>"
"</service>"
"</serviceList>"
"</device>"
"</root>\r\n"
"\r\n";
HTTP.send(200, "text/xml", setup_xml.c_str());
Serial.print("Sending :");
Serial.println(setup_xml);
});
// openHAB support
HTTP.on("/on.html", HTTP_GET, [](){
Serial.println("Got Turn on request");
HTTP.send(200, "text/plain", "turned on");
turnOnRelay();
});
HTTP.on("/off.html", HTTP_GET, [](){
Serial.println("Got Turn off request");
HTTP.send(200, "text/plain", "turned off");
turnOffRelay();
});
HTTP.on("/status.html", HTTP_GET, [](){
Serial.println("Got status request");
String statrespone = "0";
if (relayState) {
statrespone = "1";
}
HTTP.send(200, "text/plain", statrespone);
});
HTTP.begin();
Serial.println("HTTP Server started ..");
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting ...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 10){
state = false;
break;
}
i++;
}
if (state){
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
boolean connectUDP(){
boolean state = false;
Serial.println("");
Serial.println("Connecting to UDP");
if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
Serial.println("Connection successful");
state = true;
}
else{
Serial.println("Connection failed");
}
return state;
}
void turnOnRelay() {
digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
relayState = true;
String body =
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body>\r\n"
"<u:SetBinaryStateResponse xmlns:u=\"urn:Belkin:service:basicevent:1\">\r\n"
"<BinaryState>1</BinaryState>\r\n"
"</u:SetBinaryStateResponse>\r\n"
"</s:Body> </s:Envelope>";
HTTP.send(200, "text/xml", body.c_str());
Serial.print("Sending :");
Serial.println(body);
}
void turnOffRelay() {
digitalWrite(relayPin, LOW); // turn off relay with voltage LOW
relayState = false;
String body =
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body>\r\n"
"<u:SetBinaryStateResponse xmlns:u=\"urn:Belkin:service:basicevent:1\">\r\n"
"<BinaryState>0</BinaryState>\r\n"
"</u:SetBinaryStateResponse>\r\n"
"</s:Body> </s:Envelope>";
HTTP.send(200, "text/xml", body.c_str());
Serial.print("Sending :");
Serial.println(body);
}
void sendRelayState() {
String body =
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body>\r\n"
"<u:GetBinaryStateResponse xmlns:u=\"urn:Belkin:service:basicevent:1\">\r\n"
"<BinaryState>";
body += (relayState ? "1" : "0");
body += "</BinaryState>\r\n"
"</u:GetBinaryStateResponse>\r\n"
"</s:Body> </s:Envelope>\r\n";
HTTP.send(200, "text/xml", body.c_str());
}