Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

[Tutorial] OnOff SHIM exposed - neat Powerswitch from Pimoroni



  • Hello fellows,

    as the Raspberry lacks on support for an on/off switch solution many people stepped into to establish this on our beloved mini PC. The most famous is the Powerblock from @petrockblog and others like the Mausberry or the ATXRaspi ...

    I would like to introduce the OnOff SHIM from Pimoroni. This little device is directly plugged onto the PIs GPIO HAT and provides an momentary push button with a chance to add a second switch. A LED indicates the button push and blinks 3 times if a complete poweroff is performed.

    Besides the neat form factor is the HOT price .... 6.0 GBP! This is amazing and are round about 8.1 $

    Install steps if you follow the Pimoroni tutorial...

    1. Solder the header to the SHIM
    2. Connect to your PIE and enter a URL to download and execute a install script (but we want more.... won't we?)

    If you follow the Pimoroni tut you are restricted to a software deamon hell.... we are modders then let us expose this neat thing!

    TOC

    1. Hardware Installation

    2. Software Installation



  • 1. Hardware build

    I created a small sketch for this. You see the SHIM on the pictures without soldered header on it. You can directly solder wires to the pointed connections and plug them into your PIE

    SHIM Hardware

    2. Software part

    I created this because of three reasons. First: I don't like processes I don't fully understand. So I exposed them and make them more configurable. This two shell-files are far away from perfection, they are just examples and do their job. The original software deamon is fully configurable via conf-files but the whole thing is nested deep in a system and would prevent unexpericend users from getting good results.

    Second: The orignial software can only use momentary switch buttons! Of course you can modify the original script provided from Pimoroni but good luck in getting a working result!

    Third: Keep it clean! The original script makes use of external programm packages that has to be installed. We keep it simple working

    SHIM Hardware



  • Software part Tutorial in text form ;)

    Part I: The GPIO Shutoff to cut power from SHIM

    1. Create gpio-shutoff with sudo nano /lib/systemd/system-shutdown/gpio-shutoff
    2. Enter code from box below
    3. Make the script executable with sudo chmod +x /lib/systemd/system-shutdown/gpio-shutoff
    #!/bin/sh
    #
    # OnOff SHIM exposed by cyperghost for retropie.org.uk
    # This is mandatory for proper SHIM shutdown!
    
    	
    poweroff_pin="4"	
    led_pin="17"	
    	
    if [ "$1" = "poweroff" ]; then	
    	
        /bin/echo $led_pin > /sys/class/gpio/export	
        /bin/echo out > /sys/class/gpio/gpio$led_pin/direction	
    	
            for iteration in 1 2 3; do	
                /bin/echo 0 > /sys/class/gpio/gpio$led_pin/value	
                /bin/sleep 0.2	
                /bin/echo 1 > /sys/class/gpio/gpio$led_pin/value	
                /bin/sleep 0.2	
           done	
    	
        /bin/echo $poweroff_pin > /sys/class/gpio/export	
        /bin/echo out > /sys/class/gpio/gpio$poweroff_pin/direction	
        /bin/echo 0 > /sys/class/gpio/gpio$poweroff_pin/value
    fi
    

    Part II: The Trigger-script to initiate shutdown by Button press

    1. Create switch.sh with mkdir /home/pi/RetroPie/scripts && nano /home/pi/RetroPie/scripts/switch.sh
    2. Fill code in textbox below in
    3. Make script executable with chmod +x /home/pi/RetroPie/scripts/switch.sh
    4. Add script to RetroPie Autostart with nano /opt/retropie/configs/all/autostart.sh
    5. Add line sudo /home/pi/RetroPie/scripts/switch.sh & to autostart shellscript. switch script should called BEFORE emulationstation #auto so it look like ....
    sudo /home/pi/RetroPie/scripts/switch.sh &
    mpg123 -Z /media/usb0/BGM/*.mp3 >/dev/null 2>&1 &
    emulationstation #auto
    

    Example of switch.sh

    #!/bin/sh
    #
    # OnOff SHIM exposed by cyperghost for retropie.org.uk
    # This is optional as you can use any button trigger script as you like
    # See this as a working example
    
    
    trigger_pin="17"
    /bin/echo $trigger_pin > /sys/class/gpio/export
    /bin/echo in > /sys/class/gpio/gpio$trigger_pin/direction
    
    power=$(cat /sys/class/gpio/gpio$trigger_pin/value)
    [ $power = 0 ] && switchtype="1" #Not a momentary button
    [ $power = 1 ] && switchtype="0" #Momentary button
    
    until [ $power = $switchtype ]; do
        power=$(cat /sys/class/gpio/gpio$trigger_pin/value)
        sleep 1
    done
    
    poweroff
    

    Thx to @meleu as mentor for bash scripting - Hello bro'
    @obsidianspider @TMNTturtlguy @Rion @Hex @pjft @retroprogrammer @lilbud @mattrixk @mitu @rbaker @caver01 @ruckage @jdrassa @all adminstrators and everyone I forgot here to mention.

    Happy new year and good luck my friends

    Feel free to use this!



  • @cyperghost I am not familiar with RetroPie autostart. Is that script one that will get updated during a RetroPie update? I might be inclined to start this with /etc/rc.local.



  • @caver01 afaik the autostart feature won't be overwritten after an update but of course you can use /etc/rc.localfor this purposes, too. I think that the ES autostart script is more intuitive for beginners because the rc.local needs to end with exit 0 phrase.

    The button script here is just an example of this howto you can use python or the pimoroni deamon or whatever you want.
    But again you NEED the gpio-shutoff script located to /lib/systemd/system-shutdown/. This sets the poweroff pin to 0 and only this shuts off the switch completly for any powerdown actions!

    EDIT: Altered one line in the script 01/09/18



  • @cyperghost Interesting. Well, I finally picked up a Nespi case, so I am going to start doing mods to it over the next few days to get my mausberry connected. It looks like I have some choices to make.



  • @caver01 Well the Mausberry Switch is also a good choice imho ... the SHIM is just extremly cheap in priece. Good luck with your build and make some photos ;)



  • The SHIM is available again ;)
    https://shop.pimoroni.de/products/onoff-shim

    But it's now 7 GBP!



  • @cyperghost First of all thks for the Tutorial.

    But it doesn't work fine for me ...

    • The gpio-shutoffscript doesn't run when the Pi turns off ... (I make the script executable with sudo chmod +x /lib/systemd/system-shutdown/gpio-shutoff command line but it's still not working)

    • The switch.sh script is not running at the start ... However, I add line sudo /home/pi/RetroPie/scripts/switch.sh & to /opt/retropie/configs/all/autostart.sh and to /opt/retropie/configs/autostart.sh scripts and it's not runnig. But if I use sudo /home/pi/RetroPie/scripts/switch.sh in the terminal and test the switch, it's working ...

    Thanks for helping.



  • @necsus I tried this on two builds. On a Raspberry 2 and a Raspberry 3 RPi0, both with Jessie image. The call to gpio-shutoff is hidden and you won't see a message. Can you check your wiring? The LED should blink 3 times but only if you wired it to PIN 1 (3,3V)

    So what happens if the Pie is shutdown? Can you repower it by pressing the switch button?

    It's definitly /opt/retropie/configs/all/autostart.sh- thank you

    It looks like

    sudo /home/pi/RetroPie/scripts/switch.sh &
    mpg123 -Z /media/usb0/BGM/*.mp3 >/dev/null 2>&1 &
    emulationstation #auto
    

    So the switch-script must be executed BEFORE emulationstation call!
    I wasn't exact in this point - sorry!

    I've no clue why this does not work in your case ;) Give more information about your system - I think you are on a wrong system as the shutoff script is a rip of the original script from pimoroni.



  • @cyperghost Thks for your quick response !

    I have a Raspberry Pi 3 with the Pre-made image RetroPie v4.3.

    The modification of the autostart.sh script works fine !

    But when the Pie is shutdown I can't repower it by pressing the button ... The Red Power LED from the Pie and my switch LED (wired on the PIN 1) remain ON ...

    I have check my wiring, and it's ok !
    But I've find a new problem, in the folder /lib/systemd/system-shutdown/ I have 2 scripts : gpio-shutoff.saveand gpio-shutoff.sh. I've test to delete with rm or modify the script but I get the following message error : "Permission denied"

    Did I do something wrong ?



  • @necsus use sudo rm /lib/systemd/system-shutdown/gpio-shutoff.save

    and remove the .sh file, too

    then create the file exactly as I wrote in the tutorial .... WITHOUT .sh just gpio-shutoff and make it executable ;) ... that should do the trick. Seems your script wasn't made executable because of the extension.



  • @cyperghost Thks for the help, I succeeded to delete the two files, create the gpio-shutoff script (I make it executable with sudo chmod +x ;) ).

    But it's still not working, I think the PCB has a problem. I will test if the script run by putting a LED on the GPIO 17 when the Pie shutoff.



  • @necsus Well... that's weird!

    Try to install the official from Pimoroni

    type curl https://get.pimoroni.com/onoffshim | bash

    But I really don't know why this is not working for you... see here
    But seems you are not alone ... from runeaudio.com - I think I will dig deeper in this!
    Hell! There is something going wrong here .... See the official Pimoroni-board - the guy is reporting the same as you! I will make a test on the new Stretch image - keep you updated. That's really strange now!



  • @cyperghost It's very strange, because I install Raspbian Stretch Lite Kernel version: 4.9 on my pi zero w, and use curl https://get.pimoroni.com/onoffshim | bashcommand and it works ...
    I buy 5 OnOff Shim board at the same time on Pimoroni.
    Do you think the OnOff Shim board use on my Rpi 3 has a problem ?



  • SOLVED! Proceed with Tutorial as descriped!



  • @cyperghost Ok that's very strange ! And thanks for the explaination.
    I will do a test on the Pi3 with Raspbian OS on a new SD card, if it's working.

    Which OS can I test to help us understanding the problem ?



  • @necsus The RetroPie image is built on top of Raspbian, so you could keep using Raspbian to test things.



  • @mitu Yes that's right! Raspbian would be the first choice.
    The reason why I ripped this out of the PIMORONI SHIM service is, that the installer will likely not work on systems like RECALBOX, LibreELEC or OSMC.

    But the two bash files and the explaination to setup them would likely work. I think I can test just on weekend so I would be glad if someone else could take over.



  • @mitu @Necsus

    Updated scripts... It will now work! Thanks for your support!
    This will now work on RPi0/1, RPi2/3

    Tested also with Raspbian light Stretch on 4.9 Kernel! Works fine, too :D



Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.