Skip to content

Commit

Permalink
on/off callbacks added
Browse files Browse the repository at this point in the history
  • Loading branch information
Aruna Tennakoon committed Nov 24, 2016
1 parent cb7d9dc commit 42e5931
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
8 changes: 8 additions & 0 deletions wemos/CallbackFunction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef CALLBACKFUNCTION_H
#define CALLBACKFUNCTION_H

#include <Arduino.h>

typedef void (*CallbackFunction) ();

#endif
11 changes: 8 additions & 3 deletions wemos/Switch.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "Switch.h"

#include "CallbackFunction.h"

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

}
Switch::Switch(String alexaInvokeName,unsigned int port){
//Switch::Switch(String alexaInvokeName,unsigned int port){
Switch::Switch(String alexaInvokeName, unsigned int port, CallbackFunction oncb, CallbackFunction offcb){
uint32_t chipId = ESP.getChipId();
char uuid[64];
sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
Expand All @@ -17,7 +18,9 @@ Switch::Switch(String alexaInvokeName,unsigned int port){
persistent_uuid = "Socket-1_0-" + serial +"-"+ String(localPort);
device_name = alexaInvokeName;
localPort = port;

onCallback = oncb;
offCallback = offcb;

startWebServer();
}

Expand Down Expand Up @@ -94,10 +97,12 @@ void Switch::handleUpnpControl(){

if(request.indexOf("<BinaryState>1</BinaryState>") > 0) {
Serial.println("Got Turn on request");
onCallback();
}

if(request.indexOf("<BinaryState>0</BinaryState>") > 0) {
Serial.println("Got Turn off request");
offCallback();
}

server->send(200, "text/plain", "");
Expand Down
6 changes: 5 additions & 1 deletion wemos/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUDP.h>
#include "CallbackFunction.h"


class Switch {
private:
String serial;
String persistent_uuid;
String device_name;
unsigned int localPort;
CallbackFunction onCallback;
CallbackFunction offCallback;
ESP8266WebServer *server = NULL;
WiFiUDP UDP;
void startWebServer();
Expand All @@ -21,7 +25,7 @@ class Switch {
void handleSetupXml();
public:
Switch();
Switch(String alexaInvokeName, unsigned int port);
Switch(String alexaInvokeName, unsigned int port, CallbackFunction onCallback, CallbackFunction offCallback);
~Switch();
void respondToSearch(IPAddress& senderIP, unsigned int senderPort);
};
Expand Down
30 changes: 26 additions & 4 deletions wemos/wemos.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"

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

const char* ssid = "Aruna";
const char* password = "********";
Expand All @@ -15,13 +21,13 @@ boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;

// Define all switches here. Max 14
// Format: Alexa invocation name, local port no.
Switch s1("office lights", 1501);
Switch s2("kitchen lights", 1502);
// 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);

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

// Initialise wifi connection
wifiConnected = connectWifi();
Expand All @@ -42,6 +48,22 @@ void loop()
}
}

void s1On() {
// Switch 1 turn on
}

void s1Off() {
// Switch 1 turn off
}


void s2On() {
// Switch 2 turn on
}

void s2Off() {
// Switch 2 turn off
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
Expand Down

0 comments on commit 42e5931

Please sign in to comment.