One of the few things that bugs me about shutting down the the Raspberry Pi is how I need to toggle the power supply to start it back up. There are a few solutions around, like an inline power switch, or the MoPi shield.. or just pull the micro USB plug out of the socket & plug it back in again. None of these I was particularly impressed with, and I wanted something a bit more intuitive & automated (which makes it easier for the kids and wife to use).

 

In a previous project, I'd used an Energenie power socket to turn a bass speaker off/on with the TV, and I had a spare socket (there are 2 in the box) doing nothing. What I've been able to do is set up a PHP script on the Pi that has the Energenie controller & have that control a socket that another Pi is connected to. When the other Pi is shutting down, I can call that PHP script and tell it to turn off the socket after a few seconds (giving the Pi enough time to shut down).

 

The process looks like this;

 

 

 

Here’s how you can set it up the same system…

 

Scripts for the Pi (which is 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

 

Enable the service with;

 

sudo systemctl enable callenergenie.service

 

That service calls this new helper script which will call some PHP running on our Pi server;

 

/home/osmc/callenergenie.sh

 

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

 

Scripts for the Pi server with the Energenie shield/controller

 

/var/www/html/callenergenie.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']);

?>

 

I needed to add the Apache user to the list of SUDO'ers, which I'd rather not have done, but the Energenie Python script supplied by the manufacturer didn't like being run as a regular user

 

nano /etc/sudoers

 

www-data ALL=(ALL) NOPASSWD:ALL

 

If there's a better way to do this, please let me know.