-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathESP8266WeMo.ino
270 lines (222 loc) · 7.78 KB
/
ESP8266WeMo.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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUDP.h>
const char* ssid = "---"; // your network SSID (name)
const char* pass = "---"; // your network password
const char* friendlyName = "Arduino"; // Alexa will use this name to identify your device
const char* serialNumber = "221517K0101768"; // anything will do
const char* uuid = "904bfa3c-1de2-11v2-8728-fd8eebaf492d"; // anything will do
// Multicast declarations
IPAddress ipMulti(239, 255, 255, 250);
const unsigned int portMulti = 1900;
const unsigned int webserverPort = 9876;
// Witty Cloud Board specifc pins
const int LED_PIN = 13;
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//int status = WL_IDLE_STATUS;
WiFiUDP Udp;
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
ESP8266WebServer server(webserverPort);
//-----------------------------------------------------------------------
// UDP Multicast Server
//-----------------------------------------------------------------------
char* getDateString()
{
//Doesn't matter which date & time, will work
//Optional: replace with NTP Client implementation
return "Wed, 29 Jun 2016 00:13:46 GMT";
}
void responseToSearchUdp(IPAddress& senderIP, unsigned int senderPort)
{
Serial.println("responseToSearchUdp");
//This is absolutely neccessary as Udp.write cannot handle IPAddress or numbers correctly like Serial.print
IPAddress myIP = WiFi.localIP();
char ipChar[20];
snprintf(ipChar, 20, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
char portChar[7];
snprintf(portChar, 7, ":%d", webserverPort);
Udp.beginPacket(senderIP, senderPort);
Udp.write("HTTP/1.1 200 OK\r\n");
Udp.write("CACHE-CONTROL: max-age=86400\r\n");
Udp.write("DATE: ");
Udp.write(getDateString());
Udp.write("\r\n");
Udp.write("EXT:\r\n");
Udp.write("LOCATION: ");
Udp.write("http://");
Udp.write(ipChar);
Udp.write(portChar);
Udp.write("/setup.xml\r\n");
Udp.write("OPT: \"http://schemas.upnp.org/upnp/1/0/\"); ns=01\r\n");
Udp.write("01-NLS: ");
Udp.write(uuid);
Udp.write("\r\n");
Udp.write("SERVER: Unspecified, UPnP/1.0, Unspecified\r\n");
Udp.write("X-User-Agent: redsonic\r\n");
Udp.write("ST: urn:Belkin:device:**\r\n");
Udp.write("USN: uuid:Socket-1_0-");
Udp.write(serialNumber);
Udp.write("::urn:Belkin:device:**\r\n");
Udp.write("\r\n");
Udp.endPacket();
}
void UdpMulticastServerLoop()
{
int numBytes = Udp.parsePacket();
if (numBytes <= 0)
return;
IPAddress senderIP = Udp.remoteIP();
unsigned int senderPort = Udp.remotePort();
// read the packet into the buffer
Udp.read(packetBuffer, numBytes);
// print out the received packet
//Serial.write(packetBuffer, numBytes);
// check if this is a M-SEARCH for WeMo device
String request = String((char *)packetBuffer);
int mSearchIndex = request.indexOf("M-SEARCH");
int mBelkinIndex = request.indexOf("urn:Belkin:device:"); //optional
if (mSearchIndex < 0 || mBelkinIndex < 0)
return;
// send a reply, to the IP address and port that sent us the packet we received
responseToSearchUdp(senderIP, senderPort);
}
//-----------------------------------------------------------------------
// HTTP Server
//-----------------------------------------------------------------------
void handleRoot()
{
Serial.println("handleRoot");
server.send(200, "text/plain", "Tell Alexa to discover devices");
}
void handleSetupXml()
{
Serial.println("handleSetupXml");
String body = "<?xml version=\"1.0\"?>\r\n";
body += "<root>\r\n";
body += " <device>\r\n";
body += " <deviceType>urn:OriginallyUS:device:controllee:1</deviceType>\r\n";
body += " <friendlyName>";
body += friendlyName;
body += "</friendlyName>\r\n";
body += " <manufacturer>Belkin International Inc.</manufacturer>\r\n";
body += " <modelName>Emulated Socket</modelName>\r\n";
body += " <modelNumber>3.1415</modelNumber>\r\n";
body += " <UDN>uuid:Socket-1_0-";
body += serialNumber;
body += "</UDN>\r\n";
body += " </device>\r\n";
body += "</root>";
String header = "HTTP/1.1 200 OK\r\n";
header += "Content-Type: text/xml\r\n";
header += "Content-Length: ";
header += body.length();
header += "\r\n";
header += "Date: ";
header += getDateString();
header += "\r\n";
header += "X-User-Agent: redsonic\r\n";
header += "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n";
header += "connection: close\r\n";
header += "LAST-MODIFIED: Sat, 01 Jan 2000 00:00:00 GMT\r\n";
header += "\r\n";
header += body;
Serial.println(header);
server.sendContent(header);
}
void handleUpnpControl()
{
Serial.println("handleUpnpControl");
//Extract raw body
//Because there is a '=' before "1.0", it will give the following:
//"1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:SetBinaryState></s:Body></s:Envelope>
String body = server.arg(0);
//Check valid request
boolean isOn = body.indexOf("<BinaryState>1</BinaryState>") >= 0;
boolean isOff = body.indexOf("<BinaryState>0</BinaryState>") >= 0;
boolean isValid = isOn || isOff;
if (!isValid) {
Serial.println("Bad request from Amazon Echo");
Serial.println(body);
server.send(400, "text/plain", "Bad request from Amazon Echo");
return;
}
//On/Off Logic
if (isOn) {
digitalWrite(LED_PIN, 1);
Serial.println("Alexa is asking to turn ON a device");
server.send(200, "text/plain", "Turning ON device");
}
else {
digitalWrite(LED_PIN, 0);
Serial.println("Alexa is asking to turn OFF a device");
server.send(200, "text/plain", "Turning OFF device");
}
}
void handleNotFound()
{
Serial.println("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()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.println();
// Initialize LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, 0);
// setting up Station AP
WiFi.begin(ssid, pass);
// Wait for connect to AP
Serial.print("[Connecting to ");
Serial.print(ssid);
Serial.print("]...");
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
tries++;
if (tries > 50)
break;
}
// print your WiFi info
IPAddress ip = WiFi.localIP();
Serial.println();
Serial.print("Connected to ");
Serial.print(WiFi.SSID());
Serial.print(" with IP: ");
Serial.println(ip);
//UDP Server
Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
Serial.print("Udp multicast server started at ");
Serial.print(ipMulti);
Serial.print(":");
Serial.println(portMulti);
//Web Server
server.on("/", handleRoot);
server.on("/setup.xml", handleSetupXml);
server.on("/upnp/control/basicevent1", handleUpnpControl);
server.onNotFound(handleNotFound);
server.begin();
Serial.print("HTTP server started on port ");
Serial.println(webserverPort);
}
void loop()
{
UdpMulticastServerLoop(); //UDP multicast receiver
server.handleClient(); //Webserver
}