element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Automatically powering down the mains supply for a Raspberry Pi
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Matt
  • Date Created: 23 Apr 2018 10:14 AM Date Created
  • Views 1518 views
  • Likes 7 likes
  • Comments 1 comment
  • home automation
  • rpibeginner
  • raspberry_pi
  • raspberry pi
  • energenie
  • home_automation
  • raspberry_pi_projects
  • osmc
Related
Recommended

Automatically powering down the mains supply for a Raspberry Pi

Matt
Matt
23 Apr 2018

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;

 

image

 

 

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 image

 

nano /etc/sudoers

 

www-data ALL=(ALL) NOPASSWD:ALL

 

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

  • Sign in to reply
  • amgalbu
    amgalbu over 4 years ago

    Hello Matt

    interesting solution!

    what is the power consumption of the energenie plug? May be it worth comparing the consumption of the socket with the consumption of the Raspberry pi in shutdown state. If the consumption are comparable, you could switch on the raspberry using Gpio 5 (and create a simple script to start the shutdown procedure when the status of another GPIO changes - unfortunately GPIO 5 is used for the I2C interface)

     

    cheers

    Ambrogio

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube