void alexaOn();
void alexaOff();
void alexaDim();
void alexaInit()
{
if (alexaEnabled && WiFi.status() == WL_CONNECTED)
{
prepareIds();
udpConnected = connectUDP();
if (udpConnected) alexaInitPages();
}
}
void handleAlexa()
{
if (alexaEnabled && WiFi.status() == WL_CONNECTED)
{
if(udpConnected){
// if there’s data available, read a packet
int packetSize = UDP.parsePacket();
if(packetSize) {
IPAddress remote = UDP.remoteIP();
int len = UDP.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
String request = packetBuffer;
if(request.indexOf("M-SEARCH") >= 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();
}
}
}
}
}
}
void alexaOn()
{
if (alexaOnMacro == 255)
{
handleSet((alexaNotify)?"win&T=1&IN":"win&T=1&NN&IN");
} else
{
applyMacro(alexaOnMacro);
}
String body =
"\r\n"
"\r\n"
"1\r\n"
"\r\n"
" ";
server.send(200, "text/xml", body.c_str());
Serial.print("Sending :");
Serial.println(body);
}
void alexaOff()
{
if (alexaOffMacro == 255)
{
handleSet((alexaNotify)?"win&T=0&IN":"win&T=0&NN&IN");
} else
{
applyMacro(alexaOffMacro);
}
String body =
"\r\n"
"\r\n"
"0\r\n"
"\r\n"
" ";
server.send(200, "text/xml", body.c_str());
Serial.print("Sending :");
Serial.println(body);
}
void alexaDim(uint8_t bri)
{
String ct = (alexaNotify)?"win&IN&A=":"win&NN&IN&A=";
ct = ct + bri;
handleSet(ct);
}
void prepareIds() {
escapedMac = WiFi.macAddress();
escapedMac.replace(":", "");
escapedMac.toLowerCase();
}
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"
"EXT:\r\n"
"CACHE-CONTROL: max-age=%u\r\n" // SSDP_INTERVAL
"LOCATION: http://"+ String(s) +":80/description.xml\r\n"
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/1.17.0\r\n" // _modelName, _modelNumber
"hue-bridgeid: "+ escapedMac +"\r\n"
"ST: urn:schemas-upnp-org:device:basic:1\r\n" // _deviceType
"USN: uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"::upnp:rootdevice\r\n" // _uuid::_deviceType
"\r\n";
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(response.c_str());
UDP.endPacket();
Serial.println("Response sent !");
}
void alexaInitPages() {
server.on("/upnp/control/basicevent1", HTTP_POST, []() {
Serial.println("########## Responding to /upnp/control/basicevent1 ... ##########");
String request = server.arg(0);
Serial.print("request:");
Serial.println(request);
if(request.indexOf("SetBinaryState") >= 0) {
if(request.indexOf("1") >= 0) {
Serial.println("Got Turn on request");
alexaOn();
}
if(request.indexOf("0") >= 0) {
Serial.println("Got Turn off request");
alexaOff();
}
}
if(request.indexOf("GetBinaryState") >= 0) {
Serial.println("Got binary state request");
sendState();
}
server.send(200, "text/plain", "");
});
server.on("/eventservice.xml", HTTP_GET, [](){
Serial.println(" ########## Responding to eventservice.xml ... ########\n");
String eventservice_xml = ""
""
""
"SetBinaryState"
""
""
""
"BinaryState"
"BinaryState"
"in"
""
""
""
""
"GetBinaryState"
""
""
""
"BinaryState"
"BinaryState"
"out"
""
""
""
""
""
""
"BinaryState"
"Boolean"
"0"
""
""
"level"
"string"
"0"
""
""
"\r\n"
"\r\n";
server.send(200, "text/plain", eventservice_xml.c_str());
});
server.on("/description.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 = ""
""
"10"
"http://"+ String(s) +":80/"
""
"urn:schemas-upnp-org:device:Basic:1"
"Philips hue ("+ String(s) +")"
"Royal Philips Electronics"
"http://www.philips.com"
"Philips hue Personal Wireless Lighting"
"Philips hue bridge 2012"
"929000226503"
"http://www.meethue.com"
""+ escapedMac +""
"uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +""
"index.html"
""
" "
" image/png"
" 48"
" 48"
" 24"
" hue_logo_0.png"
" "
" "
" image/png"
" 120"
" 120"
" 24"
" hue_logo_3.png"
" "
""
""
"";
server.send(200, "text/xml", setup_xml.c_str());
Serial.print("Sending :");
Serial.println(setup_xml);
});
// openHAB support
server.on("/on.html", HTTP_GET, [](){
Serial.println("on req");
server.send(200, "text/plain", "turned on");
alexaOn();
});
server.on("/off.html", HTTP_GET, [](){
Serial.println("off req");
server.send(200, "text/plain", "turned off");
alexaOff();
});
server.on("/status.html", HTTP_GET, [](){
Serial.println("Got status request");
String statrespone = "0";
if (bri > 0) {
statrespone = "1";
}
server.send(200, "text/plain", statrespone);
});
}
boolean connectUDP(){
boolean state = false;
Serial.println("");
Serial.println("Con UDP");
if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
Serial.println("Con success");
state = true;
}
else{
Serial.println("Con failed");
}
return state;
}
void sendState() {
String body =
"\r\n"
"\r\n"
"";
body += ((bri>0) ? "1" : "0");
body += "\r\n"
"\r\n"
" \r\n";
server.send(200, "text/xml", body.c_str());
}