loading

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
<p>I am getting the following error: </p><p>sudo python ./wemo_backend.py</p><p>Traceback (most recent call last):</p><p> File &quot;./wemo_backend.py&quot;, line 222, in &lt;module&gt;</p><p> db.connect()</p><p> File &quot;/usr/local/lib/python2.7/dist-packages/peewee.py&quot;, line 3268, in connect</p><p> self.initialize_connection(self.__local.conn)</p><p> File &quot;/usr/local/lib/python2.7/dist-packages/peewee.py&quot;, line 3185, in __exit__</p><p> reraise(new_type, new_type(*exc_args), traceback)</p><p> File &quot;/usr/local/lib/python2.7/dist-packages/peewee.py&quot;, line 3266, in connect</p><p> **self.connect_kwargs)</p><p> File &quot;/usr/local/lib/python2.7/dist-packages/peewee.py&quot;, line 3485, in _connect</p><p> conn = sqlite3.connect(database, **kwargs)</p><p>peewee.OperationalError: unable to open database file</p><p>##########################</p><p>Please help with this error as I am a novice programmer with python.</p><p>Waiting for replies...</p>
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.<br><br>#old location<br>/home/pi/scripts/WEMO/database.db<br><br>#New location<br>/home/pi/wemo_database.db
<p>This worked!! TY :) </p><p>The database has been created now but, 403 forbidden error is given by server..</p><p>ERROR MESSAGE:</p><p>*************</p><p>FORBIDDEN</p><p>You dont have permission to access /cgi-bin/wemo_control2.py on this server.</p><p>Apache/2.2.22 (debian) Server at 192.168.0.103 Port 80</p><p>***********************</p><p>Is it because of any errors with Apache ?</p><p>I was able to see the test page at the initial step..</p><p> seems like I need your help again ..</p>
<p>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</p>
<p>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...</p>
<p>Basically it is a permission issue.I am now suggesting you a step which worked for me.</p><p>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!!</p>
<p>This is an issue with your apache configuration, visit the link in step 3 to fix your apache config.</p>
<br> <br><strong>&lt;class 'peewee.OperationalError'&gt;</strong>Python 2.7.3: /usr/bin/python<br>Tue Oct 20 18:30:37 2015<p>A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.</p><p>*******************</p><p>errors</p><p>**********************</p><p><strong>&lt;class 'peewee.OperationalError'&gt;</strong>: unable to open database file args = ('unable to open database file',) message = 'unable to open database file'</p><p>#################################################################################################################################</p><p>This is the error page that comes when I am clicking the icons at the wemo webpage. I am attaching the screenshots of the error page. I thought its access issue for db and played with it. but cudnt figure it out.</p>
<p>I have the same issue, but I was only able to get that far when I started doing chmod/chgrp settings, etc. So here is my /usr/lib/cgi-bin:</p><p>pi@raspberrypi:/usr/lib/cgi-bin $ ls -l</p><p>total 8</p><p>-rwxrwxrwx 1 root root 171 Sep 11 00:50 testit.cgi</p><p>-rwxrwxrwx 1 root root 171 Sep 11 00:49 testit.py</p><p>lrwxrwxrwx 1 root root 37 Sep 11 00:40 wemo_control2.py -&gt; /home/pi/wemocontrol/wemo_control2.py</p><p>pi@raspberrypi:/usr/lib/cgi-bin $ ls -ld /home/pi/wemocontrol/wemo_control2.py</p><p>-rwxrwxrwx 1 pi pi 3893 Sep 10 22:39 /home/pi/wemocontrol/wemo_control2.py</p><p>This works:</p><p><a href="http://192.168.1.172/cgi-bin/wemo_control2.py?key=raspi123" rel="nofollow">http://192.168.1.172/cgi-bin/</a>testit.py</p><p>This doesn't:</p><p><a href="http://192.168.1.172/cgi-bin/wemo_control2.py?key=raspi123" rel="nofollow">http://192.168.1.172/cgi-bin/wemo_control2.py?key=...</a></p><p>Any ideas?</p><p>Thanks,</p><p>Scott</p>
<p>Have the same issue, where you able to resolve this?</p>
<p>Thanks @ericpulvino .. :) </p>
<p>My bad.. those errors where because of the permissions issue. I am good to go now. Thanks for your help once again :) Looking forward for more projects from you ..</p>
<p>I have a problem with this step</p><p>&quot;python ./wemo_backend.py </p><p>I get this error</p><p>python: can't open file './wemo_backend.py': [Errno 2] No such file or directory</p><p>./send_wemo_commands.py multiupdate&quot;</p><p>-bash: ./send_wemo_commands.py: No such file or directory</p><p>It seems that I have no database. Is it a SQL database I have to create?</p><p>Run Rasbian jessie</p>
<p>Thank you for this tutorial. This made for fun Sunday afternoon project. </p><p>Some notes:</p><p>1) setting up Apache was a sticky wicket. Ended up piecing together various web sources to solve the issues. For me, the final trick was running these two pieces from <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-an-apache-mysql-and-python-lamp-server-without-frameworks-on-ubuntu-14-04" rel="nofollow">https://www.digitalocean.com/community/tutorials/h...</a></p><p>(note: thought the link above speaks of Python 3, I'm still using 2.7.9)</p><p>&lt;quote from external site&gt;</p><p>--Finally, we must register Python with Apache. To start, we disable multithreading processes.</p><pre><ul><li>sudo a2dismod mpm_event<br></ul></pre><p>--Then, we give Apache explicit permission to run scripts.</p><pre><ul><li>sudo a2enmod mpm_prefork cgi</ul></pre><p>&lt;/quote from external site&gt;</p><p>2) Step 3 above calls for command &quot;sudo python-pip install peewee&quot;. I had to use &quot;sudo pip install peewee&quot;.</p><p>3) I had to relocate the images to an explicit image folder outside the project.</p><p>4) One piece for improvement...after clicking on a device to change its state, the HTML has an autoload which continually loads that device to that state. So if I turn a light off, that light will continue to be turned off every time the page autoloads. This is a problem when you use another device or rule to change the state apart from the Raspberry Pi. The autoload page HTML should always load the main control interface link, which does not automatically set the state on reload.</p><p>Hope this helps the next person!</p>
<p>Hi Eric,</p><p>where did you place the image folder?</p>
<p>Hi,</p><p>I can't get the images to display. Everything else works but I'm getting a 404 not found for all of the images. Permissions and owner are all the same for all files in this project.</p><p>What directory should they be in?</p><p>Thanks.</p>
<p>This was really an amazing stuff.Big thanks@ericpulvino bro for this project...:)</p><p>Almost all the issues I have faced in this project were rectified via the comments section itself.</p><p>sudo chmod 777- Finally the most helpful command in the last step at last...ha..ha...if you give permission access Read,Write,Execute to all the folders involved in this project.you are easy to go....</p><p>If anyone knows how to integrate MySQL with this project to store the data and to view it in phpmyadmin?....kindly share the code and help in this regard.It will be really helpful for many.Thanks in advance.......</p>
<p>Step 1 says this:</p><p>The only two devices which work with this project are Belkin's WEMO Light Switch and Belkin's WEMO Plug.</p><p>The devices I have are WemoSwitch, i.e. Model Number F7C027.</p><p>Will that work?</p>
<p>Hi,</p><p>I was able to get Apache and all working but now I'm getting errors in wemo_backend.py (and AttributeError str object has no attribute commit_Select line 2696) send_wemo_commands.py (AttributeError: str object has no attribute drop_table in line 4606).</p><p>It'd be great if you could help with the scripts.</p>
<p>Hi,</p><p>Nice tutorial, unfortunately, I'm getting the following error:</p><p>NameError: name 'SqliteDatabase' is not deinfed</p><p>Any ideas?</p>
<p>Hello, I am new with Raspberry Pi. And I have a few questions. I want to connect a humidifier to the raspberry pi. So If i purchase a wemo switch, and plug the humidifier to the switch then i can control it with the Raspberry pi? Also when setting up the wemo switch, what are the steps? Like i turn the wemo switch on, then i connect it to the wifi, then i can follow the steps above and it should work on Raspberry Pi? Or do i need to build some sort of circuit? Also would it be easier to buy a humidifier that connects to the raspberry pi by usb instead of a plug? Thank you!</p>
<p>Thanks for this GREAT project.</p>
<p>I am getting the following error </p><p><strong>pi@mwkpi</strong> <strong>~/wemocontrol $</strong> python ./wemo_backend.py</p><p>Traceback (most recent call last):</p><p> File &quot;./wemo_backend.py&quot;, line 3, in &lt;module&gt;</p><p> import peewee</p><p>ImportError: No module named peewee</p><p>but my &quot;pip list&quot; command reveals i have peewee (2.6.4)</p>
<p>sounds like an issue with your python path. When you enter a standard python shell can you run the command &quot;import peewee&quot; if not that is your issue, your python install cannot &quot;see&quot; your installed version of peewee so you need to add your peewee install directory to your python path. This is sort of uncommon and I cannot explain why this might be happening but if you're running Windows I could see this being more likely-- As a long time linux user I'm sort of isolated from much of the Windows world.</p>
<p>eric thanks for the reply. I am using linux based RASPBIAN JESSIE OS. I see import peewee is not working in the python shell . I try to install the peewee using the following commands</p><p>git clone <a href="https://github.com/coleifer/peewee.git" rel="nofollow"> https://github.com/coleifer/peewee.git </a></p><p><br>cd peewee</p><p><br>python setup.py install </p><p>but the installation failed . Thanks wemo controller I will try to figure it out.<br></p>
<p>I would recommend installing via pip.<br>&quot;sudo pip install peewee&quot;<br>You can troubleshoot the failed install using the messages shown during the install there may be a few additional packages to get peewee installed, perhaps &quot;sudo apt-get install build-essential&quot; for starters.</p>
<p>Nice project. I hope you will share more things that you make.</p>

About This Instructable

25,495views

46favorites

License:

More by ericpulvino:Raspberry Pi WEMO Control Center 
Add instructable to: