Introduction: Raspberry Pi WEMO Control Center
This project provides a webpage and automated interface for controlling Belkin's WEMO light switches and plug switches using the Miranda library.
At the end of the project you will have a webpage that can show real time state of all WEMO switches. Clicking the lightbulbs will toggle the state of the WEMO switch.
The backend sqlite database which stores the states of the WEMO switches can pave the way for all kinds of automated triggers including turning switches on/off at sunset or sunrise as shown in the last step.
Step 1: Setup WEMO Switches and Plugs
The only two devices which work with this project are Belkin's WEMO Light Switch and Belkin's WEMO Plug; all other WEMO devices are unsupported at this time, mainly because full automation is available via this app without the need for the WEMO motion sensors etc.
Record the MAC addresses which are physically printed on each of the switches/plugs and use those MAC address to setup a static IP address for each WEMO device on your Wireless router.
Note: the process for configuring a static IP address is going to be different on each router.
Once static IP assignment has been setup for the WEMOs proceed to setup your WEMOs via manufacturer recommended WEMO app for IOS or android.
Step 2: Install Raspbian on Your Raspberry Pi
Download the image provided here -->
https://www.raspberrypi.org/downloads/raspbian/
Follow the instructions to install the image and get your raspberry pi booted which is outside of the scope of this document.
Step 3: Setup Apache to Execute Python Scripts
Install the following dependencies:
sudo apt-get install apache sudo easy_install pip sudo pip install pip --upgrade sudo pip install peewee
Read the document here:
https://docs.python.org/2/howto/webservers.html
Confirm that your apache installation can handle python scripts by referencing the sample script at the link above.
The CGI-BIN directory used to place your test python scripts can be found at "/usr/lib/cgi-bin" on Raspbian, Debian, and Ubuntu systems.
Note: this part can be tricky in some cases. There are some good suggestions in the comments.
Step 4: Download GitHub Code
If you do not have GIT installed already, install it with:
sudo apt-get install git
Then download the code using:
git clone https://github.com/ericpulvino/wemocontrol
Make sure all python files are executable with:
chmod 755 ./wemocontrol/*.py
Step 5: Configure WEMO Information
Edit the top of your ./wemo_backend.py file to setup a storage location for database and also make nicknames for your WEMOs and their IP addresses.
db = SqliteDatabase('/home/pi/wemocontrol/database.db') # IP address, shortname (for internal references), Long Name (For Display) wemos = [ ("192.168.1.6","driveway","Driveway"), ("192.168.1.7","kitchensink","Kitchen Sink"), ("192.168.1.8","musicroom","Music Room"), ("192.168.1.9","frontporch","Front Porch"), ("192.168.1.10","backporch","Back Porch"),]
Note: You should have already setup your WEMOs to have static IP addresses that do not change
Run the following commands to setup the database and populate it with some initial values:
python ./wemo_backend.py ./send_wemo_commands.py multiupdate
Execute the ./wemo_backend.py file, this will create and initialize the backend database at the specified location while the next command will update the database with the state of each of the switches.
Step 6: Setup WEMO Control Center Website
Place a symbolic link to the wemo_control2.py file in your cgi-bin folder:
sudo ln -s -T /home/pi/wemocontrol/wemo_control2.py /usr/lib/cgi-bin/wemo_control2.py
Step 7: Browse to Website
Access Pi's website at:
http://[IP of Raspberry Pi]/cgi-bin/wemo_control2.py?key=raspi123
Confirm that control of the WEMO switches is possible by clicking on various lightbulbs.
Yellow means switch is On.
Black means switch is Off.
Red means that switch state cannot be determined.
Note: The webpage is refreshed periodically. Combined with the crontab on the following instruction the website will be refreshed to reflect the freshly collected state of the WEMO switch.
Step 8: Optional: Setup Automation
Setup a Crontab to update state of various WEMOs at your interval of choosing.
My sample crontab is provided below.
----------------------------- ### SAMPLE CRONTAB ### -----------------------------
# m h dom mon dow command ################################## #Update WEMO States (Every Minute) ################################## * * * * * /home/pi/wemocontrol/send_wemo_commands.py multiupdate
############################### #TURN ON and Failsafes (For Power Outtage) ############################### #00 16 * * * sunwait sun down 34.918917N 79.455301W && /home/pi/wemocontrol/send_wemo_commands.py frontporch.on kitchensink.on driveway.on 00 21 * * * /home/pi/wemocontrol/send_wemo_commands.py frontporch.on kitchensink.on driveway.on 00 18 * * * /home/pi/wemocontrol/send_wemo_commands.py frontporch.on kitchensink.on driveway.on backporch.on musicroom.on 00 00 * * * /home/pi/wemocontrol/send_wemo_commands.py backporch.on
############################## #TURNING OFF (and failsafes) ############################## 30 1 * * * /home/pi/wemocontrol/send_wemo_commands.py kitchensink.off 00 5 * * * /home/pi/wemocontrol/send_wemo_commands.py frontporch.off kitchensink.off driveway.off backporch.off musicroom.off #00 4 * * * sunwait sun up -0:15:00 34.918917N 79.455301W && /home/pi/wemocontrol/send_wemo_commands.py frontporch.off kitchensink.off driveway.off backporch.off musicroom.off
You may notice above that I occasionally make calls to a program called sunwait; this program takes latitude and longitude coordinates as arguments and can calculate the sunrise/sunset and a whole slew of other important times given the a date and location. I am using the original version hosted here.:
https://www.risacher.org/sunwait/
This can be very easily downloaded and compiled for the Raspberry Pi with the following commands.
wget https://www.risacher.org/sunwait/sunwait-20041208....
sudo apt-get install build-essential
tar -zxvf ./sunwait-20041208.tar.gz
cd ./sunwait-20041208/
make
sudo ln -s -T /home/pi/sunwait-20041208/sunwait /bin/sunwait