Skip to content

Commit

Permalink
fixed all issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kakopappa committed Nov 26, 2016
1 parent fe6a299 commit 3d5e84a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 40 deletions.
33 changes: 26 additions & 7 deletions wemos/Switch.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "Switch.h"
#include "CallbackFunction.h"




//<<constructor>>
Switch::Switch(){

Serial.println("default constructor called");
}
//Switch::Switch(String alexaInvokeName,unsigned int port){
Switch::Switch(String alexaInvokeName, unsigned int port, CallbackFunction oncb, CallbackFunction offcb){
Expand All @@ -15,24 +17,35 @@ Switch::Switch(String alexaInvokeName, unsigned int port, CallbackFunction oncb,
(uint16_t) chipId & 0xff);

serial = String(uuid);
persistent_uuid = "Socket-1_0-" + serial +"-"+ String(localPort);
persistent_uuid = "Socket-1_0-" + serial+"-"+ String(port);

device_name = alexaInvokeName;
localPort = port;
onCallback = oncb;
offCallback = offcb;

startWebServer();
}



//<<destructor>>
Switch::~Switch(){/*nothing to destruct*/}

void Switch::serverLoop(){
if (server != NULL) {
server->handleClient();
delay(1);
}
}

void Switch::startWebServer(){
server = new ESP8266WebServer(localPort);

server->on("/", [&]() {
handleRoot();
});


server->on("/setup.xml", [&]() {
handleSetupXml();
Expand All @@ -48,6 +61,9 @@ void Switch::startWebServer(){

//server->onNotFound(handleNotFound);
server->begin();

Serial.println("WebServer started on port: ");
Serial.println(localPort);
}

void Switch::handleEventservice(){
Expand Down Expand Up @@ -109,7 +125,6 @@ void Switch::handleUpnpControl(){
}

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

Expand All @@ -119,7 +134,7 @@ void Switch::handleSetupXml(){
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>"
Expand Down Expand Up @@ -150,6 +165,10 @@ void Switch::handleSetupXml(){
Serial.println(setup_xml);
}

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

void Switch::respondToSearch(IPAddress& senderIP, unsigned int senderPort) {
Serial.println("");
Serial.print("Sending response to ");
Expand All @@ -164,7 +183,7 @@ void Switch::respondToSearch(IPAddress& senderIP, unsigned int senderPort) {
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"
"DATE: Sat, 26 Nov 2016 04:56:29 GMT\r\n"
"EXT:\r\n"
"LOCATION: http://" + String(s) + ":" + String(localPort) + "/setup.xml\r\n"
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
Expand All @@ -174,7 +193,7 @@ void Switch::respondToSearch(IPAddress& senderIP, unsigned int senderPort) {
"USN: uuid:" + persistent_uuid + "::urn:Belkin:device:**\r\n"
"X-User-Agent: redsonic\r\n\r\n";

UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.beginPacket(senderIP, senderPort);
UDP.write(response.c_str());
UDP.endPacket();

Expand Down
7 changes: 5 additions & 2 deletions wemos/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

class Switch {
private:
ESP8266WebServer *server = NULL;
WiFiUDP UDP;
String serial;
String persistent_uuid;
String device_name;
unsigned int localPort;
CallbackFunction onCallback;
CallbackFunction offCallback;
ESP8266WebServer *server = NULL;
WiFiUDP UDP;

void startWebServer();
void handleEventservice();
void handleUpnpControl();
Expand All @@ -27,6 +28,8 @@ class Switch {
Switch();
Switch(String alexaInvokeName, unsigned int port, CallbackFunction onCallback, CallbackFunction offCallback);
~Switch();
String getAlexaInvokeName();
void serverLoop();
void respondToSearch(IPAddress& senderIP, unsigned int senderPort);
};

Expand Down
29 changes: 21 additions & 8 deletions wemos/UpnpBroadcastResponder.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "UpnpBroadcastResponder.h"
#include "Switch.h"

#include <functional>

// Multicast declarations
IPAddress ipMulti(239, 255, 255, 250);
const unsigned int portMulti = 1900;
char packetBuffer[512];

#define MAX_SWITCHES 14
Switch switches[MAX_SWITCHES];
Switch switches[MAX_SWITCHES] = {};
int numOfSwitchs = 0;

#define numOfSwitchs (sizeof(switches)/sizeof(Switch)) //array size
//#define numOfSwitchs (sizeof(switches)/sizeof(Switch)) //array size

//<<constructor>>
UpnpBroadcastResponder::UpnpBroadcastResponder(){
Expand Down Expand Up @@ -39,9 +41,16 @@ bool UpnpBroadcastResponder::beginUdpMulticast(){
return state;
}

void UpnpBroadcastResponder::addDevice(const Switch& device) {
int arrSize = sizeof(switches) / sizeof(Switch);
switches[arrSize + 1] = device;
//Switch *ptrArray;

void UpnpBroadcastResponder::addDevice(Switch& device) {
Serial.print("Adding switch : ");
Serial.print(device.getAlexaInvokeName());
Serial.print(" index : ");
Serial.println(numOfSwitchs);

switches[numOfSwitchs] = device;
numOfSwitchs++;
}

void UpnpBroadcastResponder::serverLoop(){
Expand All @@ -60,12 +69,16 @@ void UpnpBroadcastResponder::serverLoop(){

if(request.indexOf('M-SEARCH') > 0) {
if(request.indexOf("urn:Belkin:device:**") > 0) {

Serial.println("Got UDP Belkin Request..");

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

for(int n = 0; n < numOfSwitchs; n++) {
Switch &sw = switches[n];
sw.respondToSearch(senderIP, senderPort);

if (&sw != NULL) {
sw.respondToSearch(senderIP, senderPort);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion wemos/UpnpBroadcastResponder.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UpnpBroadcastResponder {
~UpnpBroadcastResponder();
bool beginUdpMulticast();
void serverLoop();
void addDevice(const Switch& device);
void addDevice(Switch& device);
};

#endif
52 changes: 30 additions & 22 deletions wemos/wemos.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,69 @@

// prototypes
boolean connectWifi();
void s1On();
void s1Off();
void s2On();
void s2Off();

//on/off callbacks
void officeLightsOn();
void officeLightsOff();
void kitchenLightsOn();
void kitchenLightsOff();

// Change this before you flash
const char* ssid = "Aruna";
const char* password = "********";
const char* password = "*****";

boolean wifiConnected = false;

UpnpBroadcastResponder upnpBroadcastResponder;

// Define all switches here. Max 14
// Format: Alexa invocation name, local port no, on callback, off callback
Switch s1("office lights", 1501, s1On, s1Off);
Switch s2("kitchen lights", 1502, s2On, s2Off);
Switch *office = NULL;
Switch *kitchen = NULL;

void setup()
{
Serial.begin(115200);
Serial.begin(9600);

// Initialise wifi connection
wifiConnected = connectWifi();

if(wifiConnected){
upnpBroadcastResponder.beginUdpMulticast();

// Add your switches here
upnpBroadcastResponder.addDevice(s1);
upnpBroadcastResponder.addDevice(s2);
// Define your switches here. Max 14
// Format: Alexa invocation name, local port no, on callback, off callback
office = new Switch("office lights", 80, officeLightsOn, officeLightsOff);
kitchen = new Switch("kitchen lights", 81, kitchenLightsOn, kitchenLightsOff);

Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*office);
upnpBroadcastResponder.addDevice(*kitchen);
}
}

void loop()
{
if(wifiConnected){
upnpBroadcastResponder.serverLoop();

kitchen->serverLoop();
office->serverLoop();
}
}

void s1On() {
// Switch 1 turn on
void officeLightsOn() {
Serial.print("Switch 1 turn on ...");
}

void s1Off() {
// Switch 1 turn off
void officeLightsOff() {
Serial.print("Switch 1 turn off ...");
}


void s2On() {
// Switch 2 turn on
void kitchenLightsOn() {
Serial.print("Switch 2 turn on ...");
}

void s2Off() {
// Switch 2 turn off
void kitchenLightsOff() {
Serial.print("Switch 2 turn off ...");
}

// connect to wifi – returns true if successful or false if not
Expand Down

0 comments on commit 3d5e84a

Please sign in to comment.