Controlling Power Sockets using a Raspberry Pi

The subwoofer we had in our home cinema setup died a few weeks ago, so I did my research and found a nice replacement. The only thing I didn’t spot was the fact it never goes into standby if there’s no signal (unlike the old one). I tried using some eco plugs which turn off peripherals when the TV was turned off.. but it learns the TV remote signals, and completely turns the TV off as well.. meaning that you have to hit the power button twice to turn the TV back on; that doesn’t work well with the Harmony all-in-one remote we use.

What I decided to do was buy an Energenie socket, which can be switched on/off wirelessly from a Raspberry Pi. The kit comes with 2 sockets and a transmitter to attach to the GPIO headers on the Pi, and costs about £20.

In my subwoofer scenario I basically want it switched on when the TV is on, and off when the TV is off. The TV has a Chromecast plugged into it which is visible on my local network. If you’ve got a Smart TV on your network, maybe that’ll be visible in the same way. So when the TV/Chromecast appear on the network, we know to switch on the power socket.

energeniesockets

Here’s the small Energenie transmitter attached to the GPIO headers on my B+.. it’s pretty tiny and the case I’ve got still fits over the top. Notice the small hole where you can attach an aerial.. if you want extra range, then you’ll need to solder one on.. I added a 135mm wire, since the range I got out of it just wasn’t enough to get from the dining room cupboard to the living room.

energeniepi

 

To put this together, we can use the Raspberry Pi Network Spy code I wrote in my previous blog posts on element14;

Raspberry Pi Network Spy – Part 1 – Initial Setup + d/b Schema
Raspberry Pi Network Spy – Part 2 – D/b Setup + PHP for the scanner

All we need is a new PHP page that’ll call one of the functions we’ve already written.. we need a list of the MAC addresses that are currently visible on the network, then check whether the Chromecast is there. Once we know whether the TV/Chromecast is on or off, we then call a Python script that will turn the Energenie socket on/off.

Here’s the PHP;

arp-chromecast.php

The Python script could follow the Energenie example script, but there’s actually an even more simple Python package which I’ve used in this project. To install it I did the following;

sudo apt-get install python-pip
sudo pip install energenie

Then I wrote this helper Python script (which we’ll call from PHP) that accepts a couple of parameters, like this callenergenie.py [on|off] [switch_number];

callenergenie.py

from energenie import switch_on, switch_off
import sys

if len(sys.argv)==1:
print ‘Please specify arguments like this callenergenie.py [on|off] [switch_number]’
print ‘eg. callenergenie.py on 1’
print ‘eg. callenergenie.py off 2′
else:
on_or_off = sys.argv[1]
which_switch = int(sys.argv[2])

if (on_or_off==’on’):
print ‘Switching on ‘, which_switch
switch_on(which_switch)

if (on_or_off==’off’):
print ‘Switching off ‘, which_switch
switch_off(which_switch)

Now that we’ve written the PHP & Python, all we need to do is run the PHP every minute to scan the network and do the switching. We’ll do this using another cron job;

crontab -e

*/1 * * * * sudo /usr/bin/php /var/www/arp-chromecast.php

 

Project Mentions

This project of mine has been included in PC Pro magazine, and on one of my favourite tech podcasts, TekThing (at 22min 40sec in the video below)!

Advertisement

ASUS RT-N56U Wireless Router Review

The Belkin 54G wireless access point I’ve been using for a few years has been suffering since we extended the house.. there’s just not enough range on it, and it tends to temporarily lock up if it’s hammered a lot.

Finding a replacement took a lot of research, but I eventually decided on the ASUS RT-N56U, costing about £85, after reading some great reviews, including one in PC Pro. It’s worth reiterating that it doesn’t contain an ADSL router.. so if you’re on that type of broadband you’ll still need one of those in your set up. I’m happy with that, since the master socket isn’t where I want to place the wireless router anyway.

Configuration was straight-forward, aided by a decent web interface. As soon as I plugged it in I upgraded the firmware to the latest version from ASUS. I’d also seen that there are unofficial firmwares from a Russian group which are meant to fix a bunch of stuff & add new features. I’m sticking with the ASUS firmware unless I hit stability problems or find that I want to tinker later on. I certainly like the option to have a customised firmware should the need arise.

After having it running for a few days I’m really impressed with the speed and coverage across the house. It allows you to either use one SSID for 2.4Ghz and one for 5Ghz.. which lets you decide which band you want a device to use. Or set the same SSID for both, and it’ll automatically switch you over to the fastest band according to the signal strength. I was in the kitchen, and it connected on what must have been the 2.4Ghz band (due to the distance), then moved to the living room where the speed shot up & was obviously using the 5Ghz band.

Here’s a table showing how the signal strength has been affected by the upgrade;

Signal Strength (dBm)
Postion ASUS Belkin
Utility Room -70 -75
Kitchen Diner -76 -85
Lounge -45 -63
Bedroom -61 -75
Bathroom -76 -85

 

And here’s my unboxing video;

Using Wireshark and MITM to explore a STB

I’ve recently bought a new Freeview HD box from 3view. It has a whole host of features and can be considered a “connected” device. As with most high-end set-top-boxes (STB) it pulls software firmware updates from the web, and I was interested to see where it went to get these updates & how it knew they were available.

I know about using tools to sniff network traffic, but have only done this to sniff traffic coming directly out of the PC I’m running the capture software on. Buying this 3view box gave me more of an inventive to expand my knowledge & figure out how to capture the traffic from other devices.

It was actually relatively easy. I decided to do a Machine in the Middle (MITM) ‘attack’ which was documented over at the Wireshark wiki.

In my case I didn’t have 2 network cards, but did have a laptop with one network card, and a wireless card. In Windows XP I bridged the NIC to the Wireless adapter, then plugged in a cross-over cable that linked the 3view box to my laptop. Then, after a bit of messing about with IP addresses it started working.. my 3view box was accessing my wireless router via my laptop.

Now that all the traffic from the 3view box was going via my laptop, all I then needed to do was fire up Wireshark & take a look at the packets.

Intercepting the traffic allowed me to see where the box was going for it’s updates, and the User Agent. That’s been documented over at the 3viewer community website I set up for 3view owners.

http://3viewer.elementfx.com/

Ethernet Camera Review – Edimax IC-1310

Last week I bought one of these cameras to keep an eye on the house. After initially thinking I wanted the old D-Link DCS-900 I stumbled across the Edimax range of cameras. Here’s a summary of what they have available:

  • Edimax IC-1500 – Older model
  • Edimax IC-1510 – Updated version of the IC-1500 – ~£40
  • Edimax IC-1510wg – As per IC-1510 but adds wireless – ~£65
  • Edimax IC-1310 – Same as the IC-1510 but adds audio – ~£48
  • Edimax IC-1310wg – As per IC-1310 but adds wireless – ~£78

I went for the IC-1310 because I thought the audio might be useful, even if I didn’t use it at first. I also couldn’t justify the more expensive wireless model when I’ve got a couple of Belkin wireless APs in the loft that could be hooked up to add wireless at zero cost to me.

One week on, this camera is great, especially for the price! In low light the images are pretty decent, and in daylight they’re great. Some of the features require that you use Internet Explorer; e.g. getting audio as well as video requires their ActiveX control, as does the initial setup of the motion detection. However, most other features work through other web browsers like Firefox (I don’t think some people read all the documentation!) it’s all in the ‘CGI Commands’ manual on the Edimax website. You can get straight to the MJPG video stream or individual JPG images through whatever browser you like. However, I’ve not yet been able to get the audio and video stream mixed together on other browsers. The ActiveX control also doesn’t seem to work through corporate proxies.

Network setup is relatively trivial for anyone who knows their way around port forwarding in a router. It took me about 10 minutes to set everything up & make it available using a DynDNS host name for external viewing.

It hooked up to my Android phone without any problems, streaming the video over 3G using apps like ‘Tiny DVR‘ (which I’d recommend) or ‘IP Cam Viewer Lite‘. Over 3G the frame rate was about 1-2fps.. enough for a quick check on the house. Edimax have their own app for those with iPhones.

If you don’t need audio then go for the cheaper Edimax IC-1510 (which is currently about £40). I’ve had mine wired through a ethernet cable so can’t comment on the wireless side of things; if you want wireless then you need to see whether it’s worth the extra £30 to go from the IC-3010 to the IC-3010wg.

URLs that work through most non-IE web browsers are:

Single-image, not logged in (you define the file name to use);
http://IP:web_port/loginfree.jpg

MJPG Stream
http://IP:web_port/mjpg/video.mjpg

MJPG Stream with authentication
http://admin:password@IP:web_port/mjpg/video.mjpg

Flat Cat 6

128381306-40 This year I’m moving to HD at home, but streaming HD over a wireless connection is far from ideal. The next step up from that is to use the Ethernet over power adapters (like Homeplug) which are supposed to be pretty decent, yet expensive. However, I’ve gone for a wired connection using some really flat Cat 6 gigabit Ethernet cable.

Usually it’s a really hassle running cable from your router to the device, but because this cable is so flat it’ll go easily under most doors and you can then run it under the carpet.

At about £14 from CPC (for 15 meters) it’s not a bad price compared to buying wireless or power adapter kits.

Drive Map Pro 1.6 Released

Drive Map Pro is a great piece of Windows software for organising all the servers you connect to. It’s primary purpose is to give you easy access to mapping a network drive with the minimum of fuss, however it does a lot more that that, including managing your VNC/Radmin connections too!

The latest version of Drive Map Pro (version 1.6) is now available to registered users and includes the following:

  • Lots of bug-fixes :-D
  • Ability to have multiple UNC drive mappings under one entry, syntax is ‘\serverc$,d$,whatever’, which gives you the ability to map to any/all 3 of those drives without clogging up your list of servers!
  • Middle-click launches the application you’ve associated with the server.. saves a click or two!
  • Store notes against each entry.. handy for storing box info, like who’s the administrator
  • More application preferences (like auto-hide DriveMapPRO after launching an application)

Click here for more information on Drive Map Pro.

Drive Map Pro 1.6

Mapping a drive has never been easier!!

%d bloggers like this: