Skip to content

Commit

Permalink
Turning the fork into a library
Browse files Browse the repository at this point in the history
  • Loading branch information
witnessmenow committed Nov 30, 2016
1 parent 7690f57 commit e1cb62a
Show file tree
Hide file tree
Showing 15 changed files with 718 additions and 277 deletions.
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
339 changes: 339 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#Arduino Esp8266 Alexa Multiple Belkin wemo switch emulator
#Arduino Esp8266 Alexa Wemo switch emulator

This project supports emulating upto 14 belkin wemo switches using 1 ESP 8266 chip.
This project is completly based on the [forked repo](https://github.com/kakopappa/arduino-esp8266-alexa-multiple-wemo-switch), all the hard work was done by kakopappa. I just reworked their code to make it consumable as a Arduino library so it could be easily pulled into other projects.

Parts you need:
This library enables your esp8266 to simulate a Belkin Wemo switch. It can be discovered as a device by your Amazon Echo/Dot on the Smart home section. It supports calling the emulated device a custom name e.g. "Alexa, turn off test lights", where test lights is the custom name

WeMos D1-mini ($4.00) http://www.aliexpress.com/store/product/D1-mini-Mini-NodeMcu-4M-bytes-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/1331105_32529101036.html

How to use:

1. Download the code
2. Open wemos.ino in the Arduino editor.
2. Change the WI-FI settings.
3. Define switches and callbacks
3. Flash
4. Enjoy

Previous post on single wemo switch
https://github.com/kakopappa/arduino-esp8266-alexa-wemo-switch/edit/master/README.md
The library supports emulating up to 14 switches using 1 ESP8266 chip.

## Installing

The downloaded code can be included as a new library into the IDE selecting the menu:

Sketch / include Library / Add .Zip library

Restart the Arduino IDE and follow the examples located at

File -> Examples -> Esp8266AlexaWemoEmulator
85 changes: 85 additions & 0 deletions examples/MultiSwitch/MultiSwitch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <ESP8266WiFi.h>

#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"

// prototypes
boolean connectWifi();

//on/off callbacks
void lightOn();
void lightOff();
void secondOn();
void secondOff();

//------- Replace the following! ------
char ssid[] = "xxx"; // your network SSID (name)
char password[] = "yyyy"; // your network key

WemoManager wemoManager;
WemoSwitch *light = NULL;
WemoSwitch *second = NULL;

const int ledPin = BUILTIN_LED;

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

// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);

wemoManager.begin();
// Format: Alexa invocation name, local port no, on callback, off callback
light = new WemoSwitch("test lights", 80, lightOn, lightOff);
second = new WemoSwitch("second lights", 81, secondOn, secondOff);
wemoManager.addDevice(*light);
wemoManager.addDevice(*second);

pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
delay(10);
digitalWrite(ledPin, HIGH); // Wemos BUILTIN_LED is active Low, so high is off
}

void loop()
{
wemoManager.serverLoop();
}

void lightOn() {
Serial.print("Switch 1 turn on ...");
digitalWrite(ledPin, LOW);
}

void lightOff() {
Serial.print("Switch 1 turn off ...");
digitalWrite(ledPin, HIGH);
}

void secondOn() {
Serial.print("Switch 2 turn on ...");
digitalWrite(ledPin, LOW);
}

void secondOff() {
Serial.print("Switch 2 turn off ...");
digitalWrite(ledPin, HIGH);
}
70 changes: 70 additions & 0 deletions examples/SingleSwitch/SingleSwitch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <ESP8266WiFi.h>

#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"

// prototypes
boolean connectWifi();

//on/off callbacks
void lightOn();
void lightOff();

//------- Replace the following! ------
char ssid[] = "xxx"; // your network SSID (name)
char password[] = "yyyy"; // your network key

WemoManager wemoManager;
WemoSwitch *light = NULL;

const int ledPin = BUILTIN_LED;

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

// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);

wemoManager.begin();
// Format: Alexa invocation name, local port no, on callback, off callback
light = new WemoSwitch("test lights", 80, lightOn, lightOff);
wemoManager.addDevice(*light);

pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
delay(10);
digitalWrite(ledPin, HIGH); // Wemos BUILTIN_LED is active Low, so high is off
}

void loop()
{
wemoManager.serverLoop();
}

void lightOn() {
Serial.print("Switch 1 turn on ...");
digitalWrite(ledPin, LOW);
}

void lightOff() {
Serial.print("Switch 1 turn off ...");
digitalWrite(ledPin, HIGH);
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Esp8266AlexaWemoEmulator
version=1.0
author=Brian Lough
maintainer=Brian Lough <[email protected]>
sentence=Use your esp8266 with the Alexa by emulating a Wemo plug
paragraph=Use your esp8266 with the Alexa by emulating a Wemo plug
category=Communication
url=https://github.com/witnessmenow/esp8266-alexa-wemo-emulator.git
architectures=*
File renamed without changes.
96 changes: 96 additions & 0 deletions src/WemoManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "WemoManager.h"
#include "WemoSwitch.h"
#include <functional>

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

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

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

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

}

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

bool WemoManager::begin(){
boolean state = false;

Serial.println("Begin multicast ..");

if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
Serial.print("Udp multicast server started at ");
Serial.print(ipMulti);
Serial.print(":");
Serial.println(portMulti);

state = true;
}
else{
Serial.println("Connection failed");
}

return state;
}

//Switch *ptrArray;

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

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

void WemoManager::serverLoop(){

int packetSize = UDP.parsePacket();
if (packetSize > 0)
{
IPAddress senderIP = UDP.remoteIP();
unsigned int senderPort = UDP.remotePort();

// 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);
// Serial.println("----------");
// Serial.println(request);
// Serial.println("-----------");
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++) {
WemoSwitch &sw = switches[n];

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

for(int i = 0; i < numOfSwitchs; i++) {
WemoSwitch &swit = switches[i];

if (&swit != NULL) {
swit.serverLoop();
}
}
}
20 changes: 20 additions & 0 deletions src/WemoManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef WemoManager_h
#define WemoManager_h

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include "WemoSwitch.h"

class WemoManager {
private:
WiFiUDP UDP;
public:
WemoManager();
~WemoManager();
bool begin();
void serverLoop();
void addDevice(WemoSwitch& device);
};

#endif
Loading

0 comments on commit e1cb62a

Please sign in to comment.