Skip to content

Commit

Permalink
Logging enhancements
Browse files Browse the repository at this point in the history
Additional logging to help validate functionality; Extra info on origin
of UDP discovery requests
  • Loading branch information
cosmocracy committed Dec 31, 2016
1 parent 3bdaf05 commit 7b5f223
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
40 changes: 24 additions & 16 deletions Switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ void Switch::startWebServer(){
//server->onNotFound(handleNotFound);
server->begin();

Serial.println("WebServer started on port: ");
Serial.println(localPort);
Serial.print("WebServer started on port tcp/");
Serial.print(localPort);
Serial.print(" for alexa device named '");
Serial.print(device_name);
Serial.println("'");
}

void Switch::handleEventservice(){
Serial.println(" ########## Responding to eventservice.xml ... ########\n");
Serial.print("Responding to eventservice.xml for device named '");
Serial.print(device_name);
Serial.println("'");

String eventservice_xml = "<?scpd xmlns=\"urn:Belkin:service-1-0\"?>"
"<actionList>"
Expand Down Expand Up @@ -101,35 +106,39 @@ void Switch::handleEventservice(){
}

void Switch::handleUpnpControl(){
Serial.println("########## Responding to /upnp/control/basicevent1 ... ##########");
Serial.print("Responding to /upnp/control/basicevent1 for device named '");
Serial.print(device_name);
Serial.println("'");

//for (int x=0; x <= HTTP.args(); x++) {
// Serial.println(HTTP.arg(x));
//}

String request = server->arg(0);
Serial.print("request:");
Serial.print("HTTP request:");
Serial.println(request);

if(request.indexOf("<BinaryState>1</BinaryState>") > 0) {
Serial.println("Got Turn on request");
Serial.println("Requested state change: TURN ON");
onCallback();
}

if(request.indexOf("<BinaryState>0</BinaryState>") > 0) {
Serial.println("Got Turn off request");
Serial.println("Requested state change: TURN OFF");
offCallback();
}

server->send(200, "text/plain", "");
}

void Switch::handleRoot(){
server->send(200, "text/plain", "You should tell Alexa to discover devices");
server->send(200, "text/plain", "You should tell Alexa to 'discover devices'");
}

void Switch::handleSetupXml(){
Serial.println(" ########## Responding to setup.xml ... ########\n");
Serial.print("Responding to setup.xml for device named '");
Serial.print(device_name);
Serial.println("'");

IPAddress localIP = WiFi.localIP();
char s[16];
Expand Down Expand Up @@ -161,19 +170,18 @@ void Switch::handleSetupXml(){

server->send(200, "text/xml", setup_xml.c_str());

Serial.print("Sending :");
Serial.println(setup_xml);
Serial.print("Sending XML: ");
Serial.print(setup_xml);
}

String Switch::getAlexaInvokeName() {
return device_name;
}

void Switch::respondToSearch(IPAddress& senderIP, unsigned int senderPort) {
Serial.println("");
Serial.print("Sending response to ");
Serial.println(senderIP);
Serial.print("Port : ");
Serial.print("Sending UDP discovery response to IP ");
Serial.print(senderIP);
Serial.print(" on port udp/");
Serial.println(senderPort);

IPAddress localIP = WiFi.localIP();
Expand All @@ -197,5 +205,5 @@ void Switch::respondToSearch(IPAddress& senderIP, unsigned int senderPort) {
UDP.write(response.c_str());
UDP.endPacket();

Serial.println("Response sent !");
Serial.println("Discovery response sent");
}
17 changes: 9 additions & 8 deletions UpnpBroadcastResponder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,29 @@ void UpnpBroadcastResponder::serverLoop(){
int packetSize = UDP.parsePacket();
if (packetSize <= 0)
return;
Serial.print("UDP packet encountered, size: ");
Serial.print(packetSize);

IPAddress senderIP = UDP.remoteIP();
unsigned int senderPort = UDP.remotePort();

Serial.print("UDP packet encountered from IP ");
Serial.print(senderIP);
Serial.print(" to port udp/");
Serial.print(senderPort);
Serial.print(" with size (bytes) ");
Serial.println(packetSize);

// read the packet into the buffer
UDP.read(packetBuffer, packetSize);

// check if this is a M-SEARCH for WeMo device
String request = String((char *)packetBuffer);

if(request.indexOf('M-SEARCH') > 0) {
Serial.println("Found 'M-SEARCH' in request");
Serial.println(" Discovery request detected");
if(request.indexOf("urn:Belkin:device:**") > 0) {
Serial.println("Got UDP Belkin Request..");

// int arrSize = sizeof(switchs) / sizeof(Switch);

Serial.println(" Belkin-compatible device discovery request detected");
for(int n = 0; n < numOfSwitchs; n++) {
Switch &sw = switches[n];

if (&sw != NULL) {
sw.respondToSearch(senderIP, senderPort);
}
Expand Down

0 comments on commit 7b5f223

Please sign in to comment.