RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    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

    Scheduled Pinned Locked Moved Help and Support
    mausberrypower offcircuitmenuled
    72 Posts 4 Posters 17.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cyperghostC
      cyperghost @caver01
      last edited by cyperghost

      @caver01 But the mausberry works with permanent switches?
      But I think the real button won't work anymore ;(

      So use a GPIO to set event on/off ;)

      cyperghostC caver01C 2 Replies Last reply Reply Quote 0
      • cyperghostC
        cyperghost @cyperghost
        last edited by cyperghost

        @caver01
        So you soldering the SSR next to your switch. So you have two switches running.
        I'm guessing the hardware switch is a momentary one - with normal state off?
        So the SSR can also act as a momentary, unpowered via GPIO it is off, powerd via GPIO it is on....

        So it think it should work ;)

        caver01C 1 Reply Last reply Reply Quote 0
        • caver01C
          caver01 @cyperghost
          last edited by

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

          @caver01 But the mausberry works with permanent switches?
          But I think the real button won't work anymore ;(

          So use a GPIO to set event on/off ;)

          Yep. use it to trigger an actual (parallel) button press.

          What I can't figure out is why, even after the pi shuts down with a soft shutdown, the mausberry ignores my button presses. It is like it locks into a powered state. It must be checking the state of Pin 2:

          If Pin2 = 0 and power button is closed, DO POWER ON
          if Pin 2 = 1 and power button is closed, DO POWER OFF

          so, after a soft shutdown, the mausberry thinks I am trying to turn the pi back on, but it is already on, so it does nothing. It's like an anti-bounce state, the time right after a POWER ON, but before the script is running and can set Pin 2 = 1.

          My 4-player cocktail style cabinet built as a custom "roadcase"

          1 Reply Last reply Reply Quote 0
          • caver01C
            caver01 @cyperghost
            last edited by

            @cyperghost If I get one of these to test, I will need help. Which pin on the SSR goes to what, and which pin gets the resistor?

            My 4-player cocktail style cabinet built as a custom "roadcase"

            cyperghostC 1 Reply Last reply Reply Quote 0
            • cyperghostC
              cyperghost @caver01
              last edited by

              @caver01 page 7 shows connection diagramm
              Pin 1 goes to positive value (= GPIO Pin), Pin 2 is ground, set a resistor with right value (330ohm for 3.3V).

              Pin 3 and 4 is the switch ... Connection does not matter here see page 4, it is independent if you switch the ground or positive value.

              caver01C 1 Reply Last reply Reply Quote 0
              • caver01C
                caver01 @cyperghost
                last edited by

                @cyperghost Looks like the diagram has the resistor on Pin 1. Does it matter?

                My 4-player cocktail style cabinet built as a custom "roadcase"

                1 Reply Last reply Reply Quote 0
                • cyperghostC
                  cyperghost
                  last edited by cyperghost

                  @caver01 No but the resistor has to be placed somewhere in the line (+) LED (-)
                  So (+) R LED (-) or (+) LED R (-) doesn't matter

                  caver01C 1 Reply Last reply Reply Quote 0
                  • caver01C
                    caver01 @cyperghost
                    last edited by

                    @cyperghost Oh I get it. . . because the device itself uses an LED internally for actuation.

                    Hey, thanks for your input here and the fun conversation about this. @madmodder123 I hope I didn't overstep by totally dominating your original post. I think we were on the same page with this, and it looks like a little testing may prove that while the Mausberry circuit is an excellent solution for an add-on power switch (especially when coupled with the updated script), it comes with an assumption that the button you add will be the only shutdown method used. It is not setup with awareness of additional soft shutdown procedures that aren't triggered by the button and the circuit itself.

                    My 4-player cocktail style cabinet built as a custom "roadcase"

                    1 Reply Last reply Reply Quote 1
                    • cyperghostC
                      cyperghost
                      last edited by cyperghost

                      @caver01 I'm pretty sure that a normally opened ssr works together with a momentary switch!

                      Annother suggestion. I think if we use a normally closed SSR and connect it to the $GPIOPin2 so there is no current as long as the Pie is active. But if is inactive it would close the reset pads of the mausberry... do you think that may work?

                      cyperghostC 1 Reply Last reply Reply Quote 0
                      • cyperghostC
                        cyperghost @cyperghost
                        last edited by cyperghost

                        Some "improvments" to the original script! It might be a bit faster as there is no if-then-else....

                        #!/bin/bash
                        #this is the GPIO pin connected to the lead on switch labeled OUT
                        GPIOpin1=23
                        
                        #this is the GPIO pin connected to the lead on switch labeled IN
                        GPIOpin2=24
                        
                        echo "$GPIOpin1" > /sys/class/gpio/export
                        echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
                        echo "$GPIOpin2" > /sys/class/gpio/export
                        echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
                        echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
                        power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
                        
                        until [ $power = 1 ]; do
                            power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
                            sleep 1
                        done
                        
                        [ $power = 1 ]  && echo "Shutdown now!" && poweroff
                        
                        caver01C 1 Reply Last reply Reply Quote 0
                        • caver01C
                          caver01 @cyperghost
                          last edited by

                          @cyperghost
                          Nice.

                          I used to use a python script that did edge detection. I was led to believe that keeping a python script active in the background would be even less of a CPU burden than a polling bash script. But I changed to the bash script you helped build when I saw the better commands. It would be nice to convert it to python.

                          My 4-player cocktail style cabinet built as a custom "roadcase"

                          cyperghostC 1 Reply Last reply Reply Quote 0
                          • cyperghostC
                            cyperghost @caver01
                            last edited by

                            @caver01 can you post python code please?
                            I think python is the better choice - if you enable IRQ mode, then it's the fastest alternative ;)
                            But the old bash script looked a bit weird so I made it a bit cleaner looking

                            caver01C 1 Reply Last reply Reply Quote 0
                            • caver01C
                              caver01 @cyperghost
                              last edited by

                              @cyperghost I will post it later when I am back to my Pi.

                              My 4-player cocktail style cabinet built as a custom "roadcase"

                              1 Reply Last reply Reply Quote 0
                              • cyperghostC
                                cyperghost
                                last edited by cyperghost

                                @caver01 I will be online in a few hours again. At work I asked a colleague (circuit designer) how this issue can be solved. It's possible to switch the event just via a transistor - but I forget which type ... I think N-type, but you need some resistors (at lease one at base) and best a diode (for current switchbacks) the make sure switch behaviour.

                                On the other hand he said, that the optical-mosfet is also a nice method to trigger that and it's really fail safe as the circuits are isolated.

                                Then I asked the method with the reset trigger... Clear answer: NO! A reset is a reset as you don't know what there really happens. It can be named as "emergency exit" if the µc is trapped but never should be used as regular switch.

                                caver01C 1 Reply Last reply Reply Quote 0
                                • caver01C
                                  caver01 @cyperghost
                                  last edited by

                                  @cyperghost I am glad you confirmed the transistor idea. I was actually going to try that first based on other things I have read. I believe I need to use an NPN transistor, and I have a bunch of them collected from old hardware. Here is an example where someone is using a transistor to trigger a button press for a camera trigger. The SSR is suggested first, but someone concludes that a transistor method would be adequate.

                                  Most discussions elsewhere online regarding this topic mention the use of the opto-coupler SSR as a safer method, as it isolates both sides and you don't have to worry about linking the grounds. However, I am not that worried about isolation, and I won't have to buy anything to try it.

                                  I didn't get a chance to lookup my python script last night. I will post something soon, and I will definitely try the transistor trick.

                                  My 4-player cocktail style cabinet built as a custom "roadcase"

                                  cyperghostC 1 Reply Last reply Reply Quote 1
                                  • cyperghostC
                                    cyperghost @caver01
                                    last edited by

                                    @caver01 Thank you for the linked document with the transistor. As you use the same potential there should be no drawbacks with connecting the Pie and the mausberry.

                                    As said, the SSR is the easiest method at all.

                                    caver01C 1 Reply Last reply Reply Quote 0
                                    • caver01C
                                      caver01 @cyperghost
                                      last edited by

                                      @cyperghost The only risk I see with the transistor here is if the momentary button wired to the Mausberry is not connected to ground on one of its poles. Maybe even that does not matter, but the diagrams I have seen including the one linked above has the emitter connected to ground that is common to both the switch and the Pi. I may look at the Mausberry circuit closely to see if I can trace one of the button terminals to ground. If not, I may just try it anyway.

                                      My 4-player cocktail style cabinet built as a custom "roadcase"

                                      cyperghostC 1 Reply Last reply Reply Quote 0
                                      • cyperghostC
                                        cyperghost @caver01
                                        last edited by

                                        @caver01 Well take a voltmeter and measure switch pins against ground. I hope that there are 3.3V ;)

                                        caver01C 2 Replies Last reply Reply Quote 0
                                        • caver01C
                                          caver01 @cyperghost
                                          last edited by

                                          @cyperghost Good idea!

                                          My 4-player cocktail style cabinet built as a custom "roadcase"

                                          1 Reply Last reply Reply Quote 0
                                          • caver01C
                                            caver01 @cyperghost
                                            last edited by

                                            @cyperghost well, that NPN transistor totally worked!

                                            First, I modified my mausberry script by adding another variable for GPIOpin3 which I set to 18.
                                            Then, I added two more echo lines to export the pin and to set its direction to OUT:

                                            #!/bin/bash
                                            
                                            # need to call this from /etc/rc.local
                                            
                                            # End Emulationstation if condition of running binary is true (v1.59)
                                            # v1.0 07/21/17 by cyperghost - Inital run 
                                            # v1.1 07/22/17 - Added chown command to set right user permission for creating es-shutdown // cyperghost
                                            # v1.2 07/23/17 - Some small improvments, easier to maintain, removed echo, removed else branch // cyperghost
                                            # v1.5 07/27/17 - Great step to exit ES even if emulators is running by runcommand.sh are started // meleu
                                            # v1.55 07/29/17 - all kudos go to @meleu for his alltime genious RegEx hack! // meleu
                                            # v1.56 07/30/17 - All emulators will be detected. // meleu
                                            # v1.58 08/02/17 - generel method: Use PPID to detect child PIDs now (ScummVM fix) // cyperghost
                                            # v1.59 08/03/17 - nothing new, just polishing the code // meleu
                                            # I just checked with SSH command - it saved my metadata! Maybe you need to extend sleeptimer!
                                            # greetings @pjft for his famous favorits and @meleu for his RegEx sniplets and his constant help! 
                                            
                                            #this is the GPIO pin connected to the lead on switch labeled OUT
                                            GPIOpin1=23
                                            
                                            #this is the GPIO pin connected to the lead on switch labeled IN
                                            GPIOpin2=24
                                            
                                            #this is the GPIO pin connected to the 1k-ohm resistor, leading to the base of NPN transistor (emitter and collector to power switch poles)
                                            GPIOpin3=18
                                            
                                            echo "$GPIOpin1" > /sys/class/gpio/export
                                            echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
                                            echo "$GPIOpin2" > /sys/class/gpio/export
                                            echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
                                            echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
                                            echo "$GPIOpin3" > /sys/class/gpio/export
                                            echo "out" > /sys/class/gpio/gpio$GPIOpin3/direction
                                            
                                            
                                            while true; do
                                                power="$(cat /sys/class/gpio/gpio$GPIOpin1/value)"
                                                if [[ "$power" == 0 ]]; then
                                                    sleep 1
                                                else
                                                    emucall="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')"
                                                    if [[ -n "$emucall" ]]; then
                                                        emupid="$(pgrep -f "$emucall")" 
                                                        pkill -P "$emupid"
                                                        kill "$emupid"
                                                        sleep 4
                                                    fi    
                                            
                                                    espid="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")"
                                                    if [[ -n "$espid" ]]; then
                                                        touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown
                                                        kill "$espid"
                                                        exit
                                                    fi
                                                    # End Emulationstation if condition of running binary is true
                                            
                                                    sudo poweroff
                                                fi
                                            done
                                            

                                            Finally, I added the line:
                                            echo "1" > /sys/class/gpio/gpio18/value to the emulationstation.sh in place of the sudo poweroff in
                                            that file.

                                            I soldered up two components, an NPN transistor and a 1k-ohm resistor. The resistor goes on the middle pin of the transistor, and the outside pins go in parallel to the power switch connected to the mausberry. Then, on the opposite end of the resistor, I connected this to GPIO18 as specified in my updated script.

                                            Now, when I select SHUTDOWN in the ES menu, it completely powers off.

                                            Thanks for all of your help!

                                            My 4-player cocktail style cabinet built as a custom "roadcase"

                                            meleuM 1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post

                                            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.