Raspberry Pi WEMO Control Center

29,764

46

30

Posted

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.

https://www.raspberrypi.org/documentation/installa...

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

Share

Recommendations

  • Space Challenge

    Space Challenge
  • Microcontroller Contest

    Microcontroller Contest
  • Spotless Contest

    Spotless Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

29 Comments

They will not let me retire this article. It is very out of date. Self support only :(

Hi, i can run :
/cgi-bin/wemo_control2.py?key=raspi123

but when i click on WEMO icon i get a ""Internal Server Error"" on this url
/cgi-bin/wemo_control2.py?key=raspi123&allof=0

any advice ?

Did you ever figure it out? I have the same problem...

Hi, no i didn't, let's keep in touch... Maybe it can come from the WEMO connection not the program.

I am getting the following error:

sudo python ./wemo_backend.py

Traceback (most recent call last):

File "./wemo_backend.py", line 222, in <module>

db.connect()

File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3268, in connect

self.initialize_connection(self.__local.conn)

File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3185, in __exit__

reraise(new_type, new_type(*exc_args), traceback)

File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3266, in connect

**self.connect_kwargs)

File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3485, in _connect

conn = sqlite3.connect(database, **kwargs)

peewee.OperationalError: unable to open database file

##########################

Please help with this error as I am a novice programmer with python.

Waiting for replies...

Inside the wemo_backend.py file there is a location specified for the database file.... this location may not exist on your box. I would modify this to wherever you would like to store the database. You could try the location I specified below.

#old location
/home/pi/scripts/WEMO/database.db

#New location
/home/pi/wemo_database.db

This worked!! TY :)

The database has been created now but, 403 forbidden error is given by server..

ERROR MESSAGE:

*************

FORBIDDEN

You dont have permission to access /cgi-bin/wemo_control2.py on this server.

Apache/2.2.22 (debian) Server at 192.168.0.103 Port 80

***********************

Is it because of any errors with Apache ?

I was able to see the test page at the initial step..

seems like I need your help again ..

bro....could you please tell me how did you solve this permission issue for wemo_control2.py? which you have mentioned here...thanks in advance

Did you ever fix this issue? I have this issue too and have not found how to fix it. Ridiculous that there's no one who knows how to fix it...

Basically it is a permission issue.I am now suggesting you a step which worked for me.

1.Instead of giving symbolic link to the wemo_control2.py file in cgi-bin folder in step 6,copy and paste the entire files in the home/wemocontrol folder to usr/lib/cgi-bin folder.This fixed the permission issue for me.Thanks!!