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

Powering off via UI while using a Mausberry Circuit



  • Is there a way I can make it so the Pi/Mausberry Circuit fully powers down if you use the UI to shutdown the Pi?
    examples:
    "SHUTDOWN SYSTEM" from EmulationStation
    "Power off system" within Kodi
    Menu>Shutdown>Shutdown within LXDE

    Whenever I power the Pi off using these methods the Pi turns off (it takes a lot longer compared to when the button is pressed), but the LED I have connected to the Mausberry Circuit remains lit.

    Is there a way I can make the Pi run the same code that is in the Mausberry script whenever these alternate power down methods are used?

    Thanks, you guys rock :)



  • I will add a “me too” on this point. When I shutdown via the UI, the Pi goes through the motions and shuts down, but I can see that the pi's LED is still lit, not just the Mausberry LED. It also renders the Mausberry unusable for powering back up. I have to pull the power cord, replug, then I can power on with the Mausberry.



  • @caver01 @madmodder123
    That's can be done surly at ease... ;)
    Take a look to the post here. That's the retropie EmulationStation shutdown script. So if you just add one line at the specific if-clause - the shutdown via the mausberry should be initiated.

    sudo echo "1" > /sys/class/gpio/gpio$GPIOpin1/value
    definate GPIOPin1 as done in mausberry... That should be all :)

    I'll be glad for a feedback ;)
    Please remember ... as I don't have a mausberry I can't test.



  • @cyperghost Awesome! I was hoping we could tap into ES somehow like this. I guess I missed that opportunity a couple months ago when you guys were zeroing in on the final version of the MB script.

    Question: are you suggesting this line gets inserted around the "sudo poweroff" in the ES script, or in place of it? My inclination is to replace that line (or comment out and add) with this GPIO variable value command, as we would be just simulating the MB button press by setting the variable to "1" which will get picked up by the sleeping script and cause it to fall out of the "power == 0" sleep loop and so on, just like I pushed the button. Correct?



  • @caver01

    ATTENTION! Definate correct GPIOPin!

    EDIT: I exchanged one line... I would rather do the echo 1 first
    After that es-shutdown can be removed by script...
    You may extent sleep timer?

    The script is located in /opt/retropie/supplementary/emulationstation/emulationstation.sh

    #!/bin/sh
    
    esdir="$(dirname $0)"
    while true; do
        rm -f /tmp/es-restart /tmp/es-sysrestart /tmp/es-shutdown
        "$esdir/emulationstation" "$@"
        ret=$?
        [ -f /tmp/es-restart ] && continue
        if [ -f /tmp/es-sysrestart ]; then
            rm -f /tmp/es-sysrestart
            sudo reboot
            break
        fi
        if [ -f /tmp/es-shutdown ]; then
            sudo echo "1" > /sys/class/gpio/gpio$GPIOpin1/value && sleep 3 #<-- very likely here
            rm -f /tmp/es-shutdown
            sudo poweroff
            break
        fi
        break
    done
    exit $ret
    


  • @cyperghost
    I am away from my pi at the moment so I cannot test, but I got to thinking about this. . . If we are just setting a variable to trigger the sleeping MB script, it will end up sending the same sudo poweroff command from the mausberry script . . so I am going to make a guess here that nothing will change. It will still keep the MB circuit itself in limbo and the Pi will be off/asleep, but not completely powered off. In other words, I am predicting no change.

    Here's why: I suspect the MB circuit itself is monitoring the actual pin voltages, and all we are doing here is triggering the software shutdown. While that's all well and good, we may not be giving the MB hardware any indication that it should be preparing to cut power like it does when we press the button. This is my prediction anyway.

    With that possibility in mind, and recognizing that I am a bit of a GPIO novice, is there a way to force the signal on the GPIO pin itself so that the MB circuit hardware really DOES detect something? This would get picked up by the script too.



  • @caver01

    With that possibility in mind, and recognizing that I am a bit of a GPIO novice, is there a way to force the signal on the GPIO pin itself so that the MB circuit hardware really DOES detect something? This would get picked up by the script too.

    You send a trigger to the MB script! Every second the MB checks the state of the GPIO! So I would extend the sleep timer in the script posted above to at least 2 seconds.

    Type:
    nano /opt/retropie/supplementary/emulationstation/emulationstation.sh
    and just add this one line between the if-then-by es-shutdown clause

    sudo echo "1" > /sys/class/gpio/gpio$GPIOpin1/value && sleep 3
    

    Please definate $GPIOpin1 value! I can't see it as my glass globe is broken now. The modified MB-script checks existance of ES-running and some emulators and then exits and gives command back to the original ES-shutdown script. As there is no emulator running and ES is already shutting down the MB script will take command back to ES and ES ends as usual but the GPIOPin from MB is changed!

    It will still keep the MB circuit itself in limbo and the Pi will be off/asleep, but not completely powered off. In other words, I am predicting no change.

    No - I'm pretty sure it works - maybe not by this attempt but in general it should ;)



  • @cyperghost said in Powering off via UI while using a Mausberry Circuit:

    Please definate $GPIOpin1 value!

    Sorry, I don't know what this means.

    Thanks for the added explanation. I understand how the updated Mausberry script works. It's so great that it looks for emulator PIDs now and does a graceful shutdown.

    My point was that if all we are changing is the value of a variable defined by the script--and not an actual pin voltage-- I wonder how the outcome would be any different--as it still leads to a sudo poweroff.

    But, if changing that variable also triggers a voltage change on the GPIO PIN itself, we are probably good to go.

    What I suspect happens when the physical button is used is more than just falling out of the sleep loop. I may be mistaken, but the mausberry hardware is actually cutting power to the Pi and going into a dormant state itself--something it doesn't seem to do if I just trigger a software shutdown. That makes me think the circuit is doing something in hardware when it detects a voltage change on the pin it is monitoring IN ADDITION to our script getting triggered. Even the LED stays brightly lit from the Mausberry circuit. It "thinks" the Pi is still running. We need to send the signal to the circuit to trick it into thinking the button has been pressed.

    In any case, I won't be able to test this until I get home in 5-6 hours.



  • @caver01 Well it could be that the switch triggers some hardware thing to the mausberry. But keep in mind

    with echo 1 the GPIO sends 3,3V to the mausberry
    with echo 0 the GPIO sends 0,0V to ther mausberry

    You can connect a simple LED to a GPIO and ground directly and just send 1 to let the LED glow and send 0 to switch it off ;)
    I'm pretty sure it works....

    In theory there is no difference if you press the button to send the 1 to the Pie or do it via software and tell the GPIO - set to 1 now ;)
    Maybe you're right and the hardware button does something with the mausberry but as far as I understood the mausberry works with permanent and momentary switches ;) So it just triggers the GPIO ....



  • @cyperghost said in Powering off via UI while using a Mausberry Circuit:

    ATTENTION! Definate correct GPIOPin!

    Are you saying we should adjust the line as it applies to our setup--so if we used pin 23 for the for GPIOpin1 as suggested by Mausberry, the line should be:

    sudo echo "1" > /sys/class/gpio/gpio$GPIOpin23/value && sleep 3 #<-- very likely here



  • No!
    sudo echo "1" > /sys/class/gpio/gpio23/value && sleep 3



  • @cyperghost Ok, so setting this value really does affect the pin voltage. That's was my hope, fearing that it might just be a script variable.



  • @caver01 You can just test without altering any script. Quit ES and type in terminal the command above. Maybe the sudo command is not needed :) (I doubt it but let us find out)



  • @cyperghost said in Powering off via UI while using a Mausberry Circuit:

    No!
    sudo echo "1" > /sys/class/gpio/gpio23/value && sleep 3

    Hahaha. Gotcha. Ok. I will give this a try later. Thanks so much, and especially for your patience. You were way ahead of me, going straight for the pin itself.

    If this works, as I now suspect it will, you just closed the last little loop as I can see with using this circuit on a RetroPie setup. Thanks in advance!

    @madmodder123 I hope you are following along and can try this too. I think we were having the same issue (actually, everyone who uses a MB circuit can benefit).



  • @cyperghost said in Powering off via UI while using a Mausberry Circuit:

    @caver01 You can just test without altering any script. Quit ES and type in terminal the command above. Maybe the sudo command is not needed :) (I doubt it but let us find out)

    Good idea. Consider that my first test. I will report back later.



  • @cyperghost I tried the command at CLI. got an error:
    echo: write error: Operation not permitted



  • @cyperghost after a little reading I tried something else:
    First, I considered the fact that pin 23 is exported and set to input. That may have been preventing me from setting the value, so I tried changing it to output with:
    sudo echo "out" > /sys/class/gpio/gpio23/direction
    then I tried setting the value to 1 with
    sudo echo "1" > /sys/class/gpio/gpio23/value
    this immediately triggered the shutdown. However, the LED wired to the mausberry is still lit, the Pi is sleeping but power still on. And in this state, the power switch is non-functional, as if the MB circuit still thinks the Pi is on, like it has not seen a button press.

    It strikes me that the power button is wired to the MB circuit, not to a GPIO pin. The MB circuit is wired to pins 23 INPUT and 24 output. So, the button tells the circuit to shutdown and the circuit triggers the input on pin 23 which kicks off the script. I honestly don't know what pin 24 is doing. I expect the MB is monitoring this pin, so when the power goes out, it sees that the pin is no longer set to "1", but it appears that the circuit does not bother to look at 24 unless it has received a button press from the wired button.

    I am starting to think I need a transistor in this equation, or that the MB circuit is not as versatile as I wish it was. It should detect if the pi is shutdown, not just when the button gets used.



  • @caver01
    I See .... That is because the switch is labeled as input.
    So there is no chance to with an simple echo AS it expects an input. I will report in a few hours ...
    I have a small test circuit available at home. One LED connected with a button.



  • @caver01 I was on the wrong track - sorry!
    But there is an solution... you need a Solid State Relais like this and connect the output in parallel direction to your switch. So you can use the switch or the SSR...

    The innput for the SSR is annother GPIO in ouput direction... if you set now echo 1 command in this GPIO direction.. LED inside the relais will glow and switch is closed.

    So you have 2 switches ... the physical and one software remoted. I have some of these bought cost 1$ each and work fine.

    @caver01 What happens if you change direction to the second GPIO?



  • @cyperghost

    What happens if you change direction to the second GPIO?

    If we change direction on pin 24, I can’t write to it. I tried change the value too and it does nothing.

    I think I will look into the relay option. Do I need any other components such as resistors connected anywhere? I don’t think I have ever hooked up a relay before.



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.

Looks like your connection to RetroPie Forum was lost, please wait while we try to reconnect.