WeMo Light Switch Web Toggler

A while back I bought a WiFi Light Switch so that I could turn off/on the lights in my room with a click of a button on my phone. It was about $50 at Best Buy so I decided to give it a shot. I did it to stroke my curiosity, NOT because I am too lazy to get out of my chair and turn off the lights, I swear.

The initial install was quick and easy and I got it working in about 15 minutes. I was initially happy with the product but I soon came to realize that the app that was developed by WeMo was complete garbage. Each time I wanted to toggle the light it would take upwards of 20 seconds for the app to open, discover my device, and then toggle it. This happened EVERY TIME. After a few months of dealing with this delay I decided I wanted to fix the problem as well as increase functionality.

The idea: Make a website that I can navigate to and have the light toggle on or off, without having to open the app.

This will solve my problem of the slow app, and also add the ability to toggle the light from any device that is connected to my network.

I decided to use my raspberry pi as a web server and use that to talk to the WeMo.

Luckily, the hardest part was already done for me. Morderntoil created a nifty shell script to control the WeMo from the command line.
You can grab the script here.

I threw this script in my /usr/bin directory so that it could be globally executable. Once the script is in the /usr/bin I made another quick helper script in the same directory that calls the original script. This allows me to easily change the IP of the light switch if it ever looses its DHCP bindings.
This is my helper script. Of course you would need to change the IP there to the IP of your own light switch. I haven’t yet found a way to make this static with my network setup.

/usr/bin/wemo_control.sh 192.168.2.55 TOGGLE

Once that was set up, I quickly found out that the only way to run a shell script from a webpage is to run it using CGI access. I installed Apache CGI and then added a VirtualHost to my webserver. Please notice the CGI configuration.
This is located at /etc/apache2/sites-enabled/000-default

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html2

Options FollowSymLinks
AllowOverride None

Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined

After adding the Virtual Host, I went into my /usr/lib/cgi-bin directory and added a file called toggle.cgi. This is the code that will actually display the website and run the shell script.
This is a very simple website that is just used to run the command right when the website loads, no buttons needed. I also decided to run some extra show commands on the raspberry pi just for a little bit of visual info.

Below is toggle.cgi
toggle

Once all the code is in place, all that’s left is to make a shortcut to the webpage. Once I browse to this site (in my case: 192.168.2.100:88/cgi-bin/toggle.cgi) my lights turn off, quite promptly as well!

This is what the webpage looks like:
wemo

Now any device with that URL will be able to toggle the light.

Pro Tip: Make a website shortcut link on your phone’s app page and you effectively have an android widget to toggle the lights.

If anyone would like a full, more in-depth tutorial on how to get this working please let me know. I must say it really helps me get the most out of my Wemo Light Switch.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *