Controlling Belkin WeMo switch with a Raspberry Pi

Here's a simple example of a way to use the ouimeaux library. The RPi's running a server that discovers a WeMo switch by name (specified on the command line), then essentially forwards switch input from the GPIO pin to the WeMo. With a power supply circuit to let it run off 110 AC and a nicer switch, you could mount this in the wall to control your WeMo a little quicker than pulling out your phone, for those cases when you're already in the room. Said my sister: "Congratulations, you've reinvented the light switch."

The switch circuit is the simplest possible; example circuitry and code can be found here.

6 comments

  • Anonymous  
    February 24, 2013 at 1:26 PM

    Tried it on my Raspberry Pi running Arch Linux and it works great! However, when I switched to Raspbian, I'm not able to discover my WeMo anymore ("wemo list" comes up empty, even when I set the timeout to a huge number). I tried it again on Arch and it worked fine.

    Any ideas why this might be happening?

  • Ian McCracken  
    February 25, 2013 at 10:39 AM

    Hmm, no. I used Occidentalis myself, so should be roughly identical to Raspbian in this respect. Try running "wemo --timeout 100 list", like you say, then while it's running check "netstat -an | grep 1900" to see if you're listening for responses properly. Also verify that, for example, iptables isn't running (don't know why it would be, though).

  • Anonymous  
    February 27, 2013 at 7:30 PM

    Running "netstat -an | grep 1900" while listening gives the following:

    udp 0 0 127.0.1.1:1900 0.0.0.0:*
    udp 0 0 0.0.0.0:1900 0.0.0.0:*

    Checking my hosts file, it looks like raspbian redirects "raspberrypi" to 127.0.1.1 by default:

    127.0.0.1 localhost
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters

    127.0.1.1 raspberrypi

    Running wemo list after commenting out the last line of /etc/hosts results in this error:

    Traceback (most recent call last): File "/usr/local/bin/wemo", line 9, in load_entry_point('ouimeaux==0.1', 'console_scripts', 'wemo')() File "/usr/local/lib/python2.7/dist-packages/ouimeaux/cli.py", line 48, in wemo Environment(on_switch, on_motion).discover(args.timeout) File "/usr/local/lib/python2.7/dist-packages/ouimeaux/environment.py", line 20, in __init__ self.upnp = UPnP(self._found_device) File "/usr/local/lib/python2.7/dist-packages/ouimeaux/upnp.py", line 18, in __init__ self.ip = socket.gethostbyname(socket.gethostname()) File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 613, in gethostbyname return get_hub().resolver.gethostbyname(hostname) File "/usr/local/lib/python2.7/dist-packages/gevent/resolver_thread.py", line 28, in gethostbyname return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args) File "/usr/local/lib/python2.7/dist-packages/gevent/threadpool.py", line 225, in apply_e raise result socket.gaierror: [Errno -5] No address associated with hostname

  • Anonymous  
    February 27, 2013 at 7:48 PM

    Instead of commenting out the 127.0.1.1 line in /etc/hosts, I replaced it with my pi's LAN IP address (eg. 192.168.0.103) and "wemo list" works now! Would there be a different solution to this so I don't have to edit my hosts file whenever my IP changes?

  • Ian McCracken  
    February 27, 2013 at 8:56 PM

    Yes, I ran into the same problem, I remember now. I think I did the same thing you did, and since my RPi booted fine after that, apparently removing the loopback was ok. However, I'll start working on a more reliable way to get the RPi's non-loopback IP address.

  • Guo Jing  
    June 15, 2013 at 12:53 PM

    The comments helped me a lot! I'm trying to fix why raspberry pi can not search upnp...

    Thanks both all of your guys!

Post a Comment