After dabbling in a few projects: a gdax trading bot, mini blockchain and a basic flat file blogging cms I wanted to dive into another hardware project.

You can buy these Belkin IOT smart sockets from Amazon
screenshot-www.amazon.co.uk-2017-08-21-10-39-13

Not bad, for £38 (as of Aug 21st) I can ask alexa to switch on and off my lamp to my hearts content.

The interesting point from a hackers point of view is that these plugs don't require you to set up anything on Amazon Web Services, where normally you would have to develop a lambda function for your own custom skills. Instead there is a closed source protocol that uses Upnp (Universal Plug and Play) that lets an Echo discover the plugs on the local network and interface with them.

I'll cut this short by showing you fauxmo, an open source python library to let a WiFi connected device emulate a Belkin Wemo. Now we're in business.

Shopping list

We'll need to grab a few things for this project.

Most people have a few of these things already, you can also save some cash by buying internationally from Aliexpress or going to a hackspace near you!

Disclaimer

Mains electricity is dangerous, I accept no liability or responsibility for any harm you may cause by attempting this. Please make an effort to learn about the safety issues associated with mains electricity before attempting this project.

Build

1. Powering the project

Let's start with the raspberry pi, power, relay and AC switching system.

First we want to tear apart that cheap USB wall adapter, it should look something like this inside

charger_flat-s400
image credit - righto

You can see the USB port on one side and two contacts on the other. In a UK plug these contacts will correspond to the live and neutral (right and left) pins. On the other side is a USB port, you can either desolder this, or leave it as is if your case has enough space. Here is a wiring diagram to show the outputs

USB

Now you'll want to solder a set of pin headers onto the PI Zero, this means we can remove it later to either replace it or use it for another project!

The don't forget to remove the VCC -> JDVCC jumper from the relay, this uses the PIs 5V rail to power the relay, as the current is super low we instead opt to power it from our USB power adapter. Additionally, these relay boards have optoisolators on board, this means one side of the circuit is electrically isolated from the other. If we do not remove the jumper then the pi is in fact not isolated from the 230VAC side, so if there is a spike or some back current from the relays then bye bye pi!

2. Modify the extension lead

Dismantling the extension lead is deceptively simple, mine had tri-wing screws but you can fit a flathead screwdriver in there for a quick fix. My extension lead gave me this fun warning --

IMG_20170818_160821--1-

Challenge accepted, let's tear this thing open and get to work. But first --

Mains sockets in the UK have three pins, ground to the top, b(r)own to the right and b(l)ue to the left. AC (Alternating current) means the polarity of the left and right pins flips at 50Hz, meaning both of these pins can be deadly when the lead is connected, so please DO NOT plug the extension lead in for the duration of the build.

IMG_20170818_160234

My lead has brass rails running between the sets of pins, the top ground rail can be left as is, so can the left (b(l)ue) rail. Now you'll want to snip the right (b(r)own) rail into sections, so each right pin of the plug is isolated from any other connection. Then epoxy the sections in place to prevent them from coming loose at any point --

IMG_20170818_160305_01

Now we want to solder mains wire to the ground rail, the neutral rail, and ONE WIRE PER PIN to each of the live pins. That should total 6 wires, again use a suitable wire gauge, mine was from a kettle lead. Make sure to leave plenty of length, at least 20cm extra, for the next step.

3. Assemble the IOT device

This diagram explains the full circuit for our fake wemo hardware --
smart-extension-lead--1-

The GPIO wires should be simple female to female pin jumper leads, anything carrying AC should be mains cable. You can also use pin jumper leads for the 5V supply from the USB adapter.

Raspberry-Pi-GPIO-Layout-Model-B-Plus-rotated-2700x900-702x336

I stored all of this hardware inside a PVC project box, you simply drill a hole in either side, letting the mains input pass into one side and the control wires to extension lead through the other. Ideally you should use cable grips or even better, a kettle lead socket.

Grab a box like this one --

61K7GDxdl4L.SY355

4. Programming

First up, thanks to MakerMusings for their work on Fauxmo, a wemo emulator that can run on any device.

As we're using out PI headless, i.e. without a monitor, there is some config we should do before we start.

First, download raspbian lite from here, then using an ISO tool of your choice, burn the image onto a fresh micro sd card.

Next, before plugging the PI in, edit the WiFi config to automatically connect to our wifi network

cd /media/path/to/sdcard
sudo nano ./etc/wpa_supplicant/wpa_supplicant.conf
network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_NETWORK_PASSWORD"
}

Now, plug in the PI and let it boot up, we'll ssh in and download the code for this project

ssh pi@some.ip.here
sudo apt-get update -y
sudo apt-get install -y git
git clone https://github.com/aranscope/alexa-extension

Currently you add your devices in the devices section of run.py, in the future it should be possible to visit a web configuration panel for the device. Be aware that you should set ports for the devices (so you don't need to rediscover on reboot) and they should not interfere with any other devices.

Now we can run the emulator

cd alexa-extension
nohup python run.py &

To get this running automatically when the alexa-extension is rebooted

sudo cp alexa-extension/alexa-extension-startup.sh /etc/init.d
sudo chmod +x /etc/init.d/alexa-extension-startup.sh
update-rc.d alexa-extension-startup.sh defaults 100

5. Device name workaround

A fun workaround to avoid needing to touch any of the existing code is using the 'groups' setting in the Alexa devices app. You can create groups with any number of devices and with any name, so if I have a lamp plugged into the first socket I can simply create a group called 'Lamp' only containing device 1! This saves having to use a custom app as the Wemo or TpLink offerings require and means the only major configurations steps are configuring WiFi to connect automatically and installing the alexa-extension software.

Screenshot_20170821-174604

The finished product

It's been a few days now and no major issues, the extension lead powers my bedroom lamps as well as monitor and speakers. I've set up groups like 'desk' to turn on the monitor and speakers and 'lights' to turn on both lamps. I designed alexa-extension to be unassuming but would love to take this even further by integrating it directly into the extension lead.

I never thought I'd ssh into an extension lead :') --

IMG_20170821_162250

The future

This project was a lot of fun, with a cost totalling around £15 (I already had the PI and tools) it is also over 10x cheaper per socket than a Wemo.

In hindsight there are a few pitfalls to this project

  • Raspberry PI is overkill, we don't need a full linux distro and this can be expensive
  • Relay clicking sounds, these are dampened by the box and I personally don't notice them, but I probably should have used solid state relays
  • User experience, this is the biggest downside, I would like an out of the box easy configuration experience that requires no programming experience
  • Switches, having manual buttons for the sockets would be good if the echo dies!
  • Seperate IOT box, the need for another brick seperate to the extension lead is a little ugly, this could be fixed with a larger extension or smaller components.

So I've ordered some new parts from China --

  • esp8266 + ISP
  • solid state relay breakout board
  • tiny USB AC adapter

And have been learning more web dev in the hopes to have a nice configuration system next time round.

Let's see how the next one turns out.