Automated mains socket power-off for OSMC on a Raspberry Pi

I’ve chosen to replace an ageing mini-PC which I’ve used since 2010 with a new Raspberry Pi 3 B+ running OSMC. It makes for a really capable media centre which can playback newer h.265 HEVC video files at 1080p without any problems, or it can serve 4K files over NFS to a box with a hardware h.265 chip like the Fire TV Box.

This form factor is easy to take on holiday and you can use an old infrared remote control (or Harmony learning remote) with it too.

However, the one thing I’ve struggled with is how to make it easy for my family to use in regard to switching it on and off. The Pi doesn’t have a power button. Some power supplies have an inline rocker switch, which almost fits the bill. I wanted something more automated.

Fortunately I had a spare Energenie power socket from a previous project where I use one to turn off our bass speaker when the TV isn’t on. These power sockets are controlled remotely (over RF) from a Pi which you attach an Energenie control board/shield to.

What I’ve done with the Pi 3 is have it powered through an Energenie socket, and set up a service that executes when it detects OSMC is shutting down. That service will make a quick HTTP call to the Pi with the Energenie controller shield, which will in turn send an RF signal to turn the mains socket off.

 

osmcshutdown

 

Here’s how you can set it up like I have…

Scripts for the Pi running OSMC

First, add a new service script.. create a new file in this folder;

/etc/systemd/system/callenergenie.service


[Unit]
Description=Energenie Remote Call to Secondary Pi
Before=multi-user.target
After=network.target
Conflicts=shutdown.target
[Service]
ExecStart=/bin/true
ExecStop=/bin/sh /home/osmc/callenergenie.sh
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

Then enable it with;

sudo systemctl enable callenergenie.service

There are a couple of useful things happening in this service, the After parameter makes sure the code runs before the network code is shut down, and Conflicts parameter is looking for OSMC shutting down.

Now add a helper script… this will make the webserver call as a background task, so control will be given back to the service immediately, rather than it waiting for the wget to complete.

/home/osmc/callenergenie.sh


echo Calling energenie socket…
wget –quiet –background –output-document="callenergenie.log" "http://192.168.1.99/callenergenie.php?delay=10&switch=2&state=off"
echo Sleeping for 2 seconds
sleep 2
echo Done.
exit 0

This calls the PHP script, telling it which socket to turn off, and how long to delay before sending the command, which we’re doing so that the Pi has time to shut down before the power is cut.

Scripts for the Pi with the Energenie shield

This is the PHP script I added to the other Pi which was already configured to be a PHP web server.

/var/www/html/callenergenie.php


<?php
/* MattC – Call this with various parameters..
callenergenie.php?
delay = time in seconds to sleep before calling Energenie
switch = which socket to talk to
state = turn socket on/off
e.g. callenergenie.php?delay=5&switch=2&state=off
*/
print ("Waiting ".$_GET['delay']." seconds");
sleep($_GET['delay']);
print ("Switching socket ".$_GET['switch']." ".$_GET['state']);
exec("sudo python /var/www/callenergenie.py ".$_GET['state']." ".$_GET['switch']);
?>

To allow PHP to run the script as root, I needed to add the Apache user to the list of sudo-ers.. not that secure tho :( I’d be interested in anyone who knows how to run the Energenie scripts a regular user.. their Python doesn’t like it when it’s not root.

nano /etc/sudoers

www-data ALL=(ALL) NOPASSWD:ALL

The nice thing about the PHP script is that we can actually call it to turn the Pi on remotely too.. so you could configure that into a widget on your phone, or add it to Alexa.

Advertisement

Portable Raspberry Pi Media Center

We recently went on holiday and I took my laptop & VGA cable with me. It was my intention to hook it up to the TV and play some media on it to keep the kids happy on rainy days. However, It turned out the TV had the VGA port covered up by the wall mounting bracket, and my laptop doesn’t have HDMI.. so we ended up putting the laptop on a chair and watching videos from there; it did the job, but wasn’t ideal.

At home we have a Fire TV Stick that could run Kodi, but the problem with Fire TV is that it has to have an internet connection, otherwise it doesn’t work (you can’t even get to Kodi!). Tethering it to my phone isn’t an option, since there are poor mobile signals in a lot of the places we visit.

Next time I’m going to be more prepared, with a more compact and flexible setup consisting of a Raspberry Pi 2 running OpenElec (and Kodi) together with a set of cables allowing me to hook it up to pretty much any TV. The Pi 2 runs Kodi really well, and the OpenElec distribution boots really quickly & has good Wifi and BlueTooth support. I initially chose a compact/travel USB-based keyboard instead of Bluetooth in case OpenElec ‘forgot’ the keyboard and I’d have nothing to navigate the menus to re-pair it.

Cable-wise, I’ve got a 1m standard HDMI cable, which will be fine in most situations.. with a 2m HDMI extension lead if I can’t get the Pi near enough to the TV (some accomodation doesn’t have power sockets where you’d expect them). I’ve also got a RCA lead, with a SCART adapter as well.. so that helps if we get stuck with an older TV.

For media storage I’ve gone with a USB3 Flash Drive with a capacity of 64Gb, which gives us more to play with than the microSD card, and it’s super-fast for copying media from a PC. As soon as you plug in the flash drive, Kodi will show it in the menus.

So that’s it.. nothing groundbreaking or overly difficult to put together. The whole system is small enough to fit in a small travel bag & gives us a lot of flexibility when dealing with different hotels/accommodation. You may just find the TV accepts the USB flash drive and can play back whatever is on it.. but at least you’ll have all the gear you need if it doesn’t ;)

After I made the video, I bought a USB numeric keyboard from eBay for a paltry £2.. that’s compacted the kit even further, allowing it to fit in an old camera bag.

The keypad isn’t instantly recognised by Kodi, but an easy way to get it up and running is to use the Keymap Add-on. Attach a normal USB keyboard and the keypad at the same time.. start the add-on and use the keyboard to activate the remap process. From there, it’s dead simple to map the keypad to the different Kodi functions.

Here’s the full kit list;

1 x Raspberry Pi 2
1 x 8Gb MicroSD card
1 x 2m HDMI extension cable
1 x 1m HDMI cable
1 x USB numeric keypad
1 x RCA to SCART adapter
1 x 3.5mm plug to RCA lead
1 x 64Gb USB3 Flash Drive
1 x USB power supply + cable

Update 1:

Just got back from a week at Center Parcs (Woburn) and was really pleased to find a HDMI socket on the wall, and a power socket.. it made it super easy to hook up the Pi to the TV :)

Update 2:

We spent a week near Blackpool, and the accommodation we stayed in had a patch panel as well! Seems like they’re quite common these days :)

 

Building a Zotac ION-based HTPC

I’ve been looking to replace my ageing Xbox which has reliably run XBMC for over 7 years with a box that can handle HD content, as well as acting as a NAS & download server. The Zotac ION box caught my eye, since the latest media center apps now support hardware acceleration for video playback & the nVidia ION GPU is supported.

Components

I bought the kit as a bundle from Mini-ITX.com and fitted a 32Gb SSD for the operating system, and a 1TB Western Digital Essentials HDD which would be attached via USB & powers down when not in use. The shopping list looks like this;

– Zotac ION-ITX-A Motherboard with Dual Core 1.6GHz Atom N330
– 2GB or 4GB DDR2 800 RAM included
– M350 Universal Mini-ITX Enclosure
(bundle costing £233 from Mini-ITX)

– OCZ Onyx 32GB SATA II 2.5in Solid State Disk
(about £50 from Amazon)

– 1TB Western Digital Essentials
(about £45 from Amazon)

OS

For the OS I wanted to use Windows 7; that’s what I’m most comfortable writing scripts + code for. The Linux distros for XBMC Live look pretty good, but there was too much of a learning curve involved for adding in the extra features I wanted.

Installing OS From USB

Since there’s no optical drive, I opted to install Windows 7 from a USB key. I actually used a 4Gb SD card from my camera & a SD card reader, and followed these instructions to make it bootable & have the Windows 7 installation on it. Installation went without a hitch.

Media Player

After installing the OS, I updated with the latest nVidia drivers, and started installing apps like XBMC. The latest Dharma release of XBMC supports hardware acceleration using DXVA2, however I was unable to get it working with 1080p content & gave up after a couple of hours. I then tried Media Portal which worked perfectly with whatever I threw at it. There’s a brilliant guide on how to configure Windows 7 and MediaPortal by Rhys Jones, which is useful to follow if you want to optimise your HTPC setup.

Remote Control

Next up was getting the remote control working correctly. I had a cheapo CyberLink remote, plus a Harmony 600.. I used the Cyberlink IR Reciever & mapped up keys into the Harmony 600. Some of the buttons worked, some didn’t. I ended up re-mapping the numeric keys to certain MediaPortal features using one of the plugins. I also re-mapped the MCE Green Button so that it runs MediaPortal (see this guide.. scroll to the Green Button heading).

Scripts

The HTPC also acts as a NAS + handles any downloads, so I’ve written a bunch of scripts to handle certain situtations.

– Timed execution of certain download tasks using Windows Scheduler. Making sure that the tasks don’t bring the HTPC out of sleep.
– Execution of scripts when the HTPC is brought out of sleep (like making sure MediaPortal is running)

Keyboard + Mouse Control

For times when I need a keyboard, I’m running Unified Remote on the HTPC and the client on my Android phone. This allows me to control most aspects of the HTPC via my phone, without having to buy extra hardware like a wireless mouse/keyboard.

XBMC Script – Caller Id 1.3 Released

This script for XBMC allows you to display details of incoming calls. Full details on how to get this up and running are on this page; you’ll need the right kind of modem attached to a PC running software like YAC.

The latest versions of the Caller Id script now include:

  • Play a sound when a call is displayed (optional)
  • Support for Switchboard by James Traynor merged into this release
  • Small bugfixes around which name to display
  • Better logging so it’s easier to debug when things go wrong :)
  • Pause playback option (optional)
  • When YAC has passed a name, display that rather than the number
  • Display an initialisation message when script starts (optional)
  • Code to allow this to run in Python emulator on a PC

You can find the script at XBMCScripts.com.

%d bloggers like this: