Belkin’s WeMo Switch is a cool way of controlling almost any AC mains device from the comfort of your smartphone. The switch has a simple built-in Wi-Fi server and hooks into your wireless home network, allowing you to control its on/off state via an Android or iOS app.
But you can also control the WeMo Switch with an Arduino. Take an Arduino Uno microcontroller board, add an Ethernet Shield, two wires and a momentary push-button switch, plus our source code and you’ve got everything you need to build an Arduino Power Switch.
Get the code
You’ll find the source code you need on our website.
There are no external libraries required, just unzip the file, load up the WeMoSwitch_v1.ino code into the Arduino integrated development environment (IDE), add in the WeMo Switch IP address (more on that later) and flash it to your Arduino board.
Make sure you grab the latest Arduino IDE from here.
How it works
We’ve looked at the W5100 Ethernet Shield previously, but just briefly, it’s an add-on board (they’re called ‘shields’ in Arduino-speak) that allows you to connect an Arduino Uno microcontroller into your wired home network via standard Ethernet cabling.
It also features a microSD card reader, but here, we just need the Ethernet connection.
As for the rest of it, the momentary-on push-button switch acts as our on/off switch and is connected between GND (ground) and digital pin 3 on the Arduino Uno via the Ethernet Shield pin headers.
The pins on the Ethernet Shield connect into the Arduino board underneath, allowing us to maintain access to the Arduino pins. Once pressed, the switch pulls pin 3, which had previously been loosely held to digital ‘1’ by its internal pull-up resistor, to digital ‘zero’ or ground.
We look for that change using the digitalRead() command, triggering the source code to send a message to the WeMo Switch to either turn on or off. It’s long been known the WeMo Switch uses a messaging protocol called Simple Object Access Protocol (SOAP), which is just a mix of HTML and XML code.
GetBinaryState allows you to poll the WeMo Switch for its current power state, SetBinaryState lets you set that state. For simplicity, our source code just uses the SetBinaryState action.
To turn on the WeMo Switch, you use the SetBinaryState action to set the BinaryState attribute to ‘1’, and ‘0’ to turn it off again. This is what we believe the WeMo app on your phone does and it’s what we’re doing here in the Arduino source code.
The great thing about this is it allows you to control mains-powered gear without any direct (and potentially hazardous) connection.
But whether you’re using the smartphone app or your own Arduino control, you should take care not to switch the WeMo Switch too quickly when you have sensitive electrical equipment connected to it, like your TV, for example.
Rapid-switching of these devices will likely cause them to fail — so be careful when using it.
To reduce the risk, the Arduino source code is set so that you can only change the WeMo Switch state once per second (which is about as fast as you can do it with the WeMo app) and you can’t hold the push-button switch down to auto-toggle the WeMo Switch state. You must press it again to change it.
Now there is another issue that’s common to push-button style switches we overcome here, commonly referred to as ‘switch bouncing’.
Switches are mechanical and don’t make a clean instant connection when you press them. Rather, they tend to bounce between ‘on’ and ‘off’ for a fraction of a second as they find their new state.
If you trigger code off a switch state change, you could get multiple triggers during this bounce period. We get around this by detecting the first state change, setting the new state on the WeMo Switch and delaying any further code operation for 500-milliseconds (half a second).
If the button is held down, we delay further until its let go and wait one more half-second. That’s more than enough time for a push-button switch to gets it ‘wobbles’ out of the way.
Finding the WeMo IP address
We’ll assume you’ve gone through the setup process on your phone and you can control it via the WeMo app. That done, go to your Windows computer, launch Windows Explorer, go to Network and you should see the WeMo Switch identified in the list of device icons.
Right-click on the icon, select Properties from the context menu and you’ll see the IP address listed at the bottom of the new window. Write this into the top of the Arduino source code where noted and then flash the source code to the Arduino board.
When you connect in the Ethernet cable and power up the Arduino, open the Serial Monitor (magnifying glass icon top-right of the Arduino IDE) and wait until you see the IP address notifications.
The Arduino board uses the built-in Ethernet library and DHCP (dynamic host configuration protocol) to find a spare IP address slot on your network.
Once the notification appears, you should be right to press the push-button and see the WeMo Switch do its thing.
Need other ideas?
The smartphone app also lets you set a WeMo Switch to turn on (or off) at certain times of the day or have it run in a timer mode, where it switches on for a fixed period and automatically shuts down.
But using Arduino, you can have almost anything fire off a WeMo Switch. For example, plug in a fan and use the DHT22 temperature and humidity sensor we’ve used recently to trigger the WeMo Switch when the temperature reaches a certain level.
You could also trigger it from sound, light, ultrasonic wave, touch switch, humidity — you name it. You just need imagination.
You’ll find the WeMo Switch at most retailers for around $60.