RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    Multi Switch Shutdown Script!

    Scheduled Pinned Locked Moved Ideas and Development
    shutdown scriptshutdown switchcyperghost
    272 Posts 40 Posters 117.3k 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.
    • J
      julenvitoria Banned @cyperghost
      last edited by julenvitoria

      @cyperghost Hi! Sorry but I was very busy these days... tonight or tomorrow I will upload the code that I use for you to see it. Yes, Python script plus bash is a powerful weapon !!

      1 Reply Last reply Reply Quote 1
      • J
        julenvitoria Banned @cyperghost
        last edited by

        @cyperghost Here are the two scripts... the first pastebin is a script in python and is originally created by George Ilyes (https://github.com/gilyes/pi-shutdown/blob/master/pishutdown.py) and is modified by PlataKoway and me to make it even more complete. Through this script I call version 0.07 of your script (the second one) in order to kill the process of the emulator that is running and then I close emulationstation in a safe way.

        https://pastebin.com/4FgsKK06
        https://pastebin.com/LpfaZh9J

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

          @julenvitoria There are some things that can be improved ;)

          1. Primary: If you use python then make use of interrupt then you can avoid the while(true)-loop.
          2. Secondary: I would rather do a ES-restart than a system reboot
          3. Secondary: I would rather use the poweroff button for an poweroff at short press and would use the 3 second timer to do a restart/reboot ;)
          4. Primary: The killall emulationstation will work in most cases but you will fail if you have scraped large amount of data. Because if after 5 seconds a shutdown is initiated the file system will be set to read only and you will loose data.
          5. Secondary: You use lots of sudo commands. You call the k.script with sudo for ex. this isn't necassary as ES is called by user pi, morover if you call the script via rc.local you are user root (so even the sudo shutdown can be shortend to shutdown only) or if you call it via ES autostart you are user pi, and then sudo shutdown is needed ;)

          All in all if you are happy with your setup and it works it is okay ;)
          Imho take a look to Point 1, this is one of the improvements python can handle at ease ;) Because with this setup it really doesn't matter if you use python or bash ...

          Take a look at this example here - you will see, there is no while-loop, because you can use interrupts. Your script uses polling :) Please don't take this as an offence.

          J 1 Reply Last reply Reply Quote 0
          • J
            julenvitoria Banned @cyperghost
            last edited by

            @cyperghost Oh, thanks very much! I'll take a look to the link. You can be calm, I always receive in a good way a constructive criticism!! My skills of python are not very good but I am learning... :D

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

              @julenvitoria I'm with you.

              1 Reply Last reply Reply Quote 0
              • G
                gollumer
                last edited by

                I just got a nespi+ case, and installed your script (I never installed the one from nespi). It restarts ES fine, but I don't think it's doing the safe shutdown correctly. When I press the power button, it goes straight to a black screen and shuts off. I don't see the screen output doing it's safe shutdown processes.

                I used the instructions here to install: https://retropie.org.uk/forum/topic/17461/nespi-case-reset-works-power-doesnt/16

                I did install raspi-gpio as well.

                If I run this at the command line, I get a pid: ./multi_switch.sh --es-pid
                I tried running the shutdown command from the command line as well (./multi_switch.sh --es-poweroff), but it goes straight to a black screen as well...not a safe shutdown.

                What could be wrong?

                I have an RPi3, running RetroPie 4.3.

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

                  @gollumer Well... did you checked if your favourites are saved? I regard your feedback as the script is doing it's work fine in a NESPI+ case. Let me explain: I tested several times on my system (It's exactly like yours: RPi3 and RetroPie 4.3) and I was able to save favourites and even scraped data with the switches --es-poweroff you used, too. So I see no fails or flaws. The shutdown is incredible fast because it terminates each session as long as the PIDs are not valid anymore.
                  The trick is, that if the ES binary received the -TERM signal annother script (it's internal in ES) takes over and initiates the shutdown. So it behaves exactly as you restart/reboot/shutoff via ES's QUIT from Main Menu!

                  You can view the script here nano /opt/retropie/supplementary/emulationstation/emulationstation.sh - this is responsible for restart ES and other system shutdowns.

                  But please tell me if following things are working (because NESPi+ is still WIP, caused by I don't have this device here in range for testing, so everything is done with code reading and some photographs of the connectivity to the GPIO heads)

                  1. You press POWER ON and the system starts, LED will glow if script is loaded
                  2. You press POWER OFF inside ES (even with emulators running) and the system performs a save shutdown (with metadata, favourites .... saved) - LED blinks 4 times and stays off
                  3. You press RESET with emulator running and you will be returned to ES
                  4. You press RESET within ES and it restarts ES (NO REBOOT!!!)

                  Thanks for your support!

                  G 1 Reply Last reply Reply Quote 1
                  • G
                    gollumer @cyperghost
                    last edited by

                    @cyperghost said in Multi Switch Shutdown Script!:

                    But please tell me if following things are working (because NESPi+ is still WIP, caused by I don't have this device here in range for testing, so everything is done with code reading and some photographs of the connectivity to the GPIO heads)

                    You press POWER ON and the system starts, LED will glow if script is loaded
                    You press POWER OFF inside ES (even with emulators running) and the system performs a save shutdown (with metadata, favourites .... saved) - LED blinks 4 times and stays off
                    You press RESET with emulator running and you will be returned to ES
                    You press RESET within ES and it restarts ES (NO REBOOT!!!)

                    Thank you for your response. Here are the answers to your questions:

                    1. Yes, POWER ON = the system starts, and LED lights up.

                    2. No, POWER OFF inside ES did not perform a safe shutdown, no metadata saved. The LED blinked 4 times, then the screen went black. Prior to pressing POWER OFF, I had added a game to my Favorites collection, and verified that it was there. After turning off, then turning back on, the game was no longer in my Favorites collection.

                    3. No, RESET with emulator running does nothing.

                    4. Yes, RESET within ES (but not emulator running) restarts ES without rebooting.

                    So, as you can see, it's partially working, but not completely (and the most important one...#2 (safe shutdown)...is not working.

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

                      @gollumer Okay thanks.

                      Script works!

                      It is essential to set the switch in the NESPi+ CASE in ON position!

                      1 Reply Last reply Reply Quote 0
                      • G
                        gollumer
                        last edited by

                        Update: I opened up the nespi+, turned the switch from ON to OFF, then back to ON, and now it's saving metadata! Seems that it wasn't completely in the ON position. Also, RESET from within an emulator now works as well. (I think the issue with that before was I was just pressing it quickly, not holding it for a second...)

                        So, problem solved.

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

                          @gollumer Perfect! As I said - some people like @meleu @TMNTturtlguy and me spent tons of time to elaborate a working solution! So I was pretty sure the script works!

                          Annother questions:
                          Do you have a fan installed in your system?
                          Do you load the script per autostart.sh or via rc.local? Both versions do work but I prefer always the autostart solution.

                          1 Reply Last reply Reply Quote 0
                          • G
                            gollumer
                            last edited by

                            Yes, I have a fan. I installed it on the 3.3v connector instead of 5v so that it's not as loud. I'm loading the script in autostart.sh.

                            cyperghostC 1 Reply Last reply Reply Quote 1
                            • C
                              cloudlink
                              last edited by

                              I installed this script with my Nespi+ case and it works perfectly!
                              I also have a fan. If I plug it into a 3.3v pin, what scripts are available to control it?

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

                                @gollumer Good catch ;)
                                But you have to connect it to the FAN connection position in the NESPi+ case
                                Even if it's loud! Directly connected to 3,3V will affect nothing

                                I assume it's not powered down even the Raspberry is shutdown.

                                Therefore... can you exit ES to terminal and then write:
                                raspi-gpio set 4 op dl
                                This will powerdown GPIO 4 and may also shuttoff the fan!
                                But please exit ES to terminal first. I don't want that you will loose data as I don't know what happens.

                                If this works you can uncomment line 184 from script!
                                and then NESPi+ is 100% working

                                    # PowerOff LED, Poweroff PowerCtrl
                                    raspi-gpio set $GPIO_lediodectrl op dl
                                #    raspi-gpio set $GPIO_poweronctrl op dl #Really have no clue what it does!
                                
                                # Initiate Shutdown per ES
                                

                                @cloudlink
                                Same question: I think the fan isn't powered off so it will always run.
                                Maybe that's the magic of GPIO4!

                                But you have to connect it to the FAN connection position in the NESPi+ case
                                Even if it's loud! Directly connected to 3,3V will affect nothing

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

                                  @cloudlink said in Multi Switch Shutdown Script!:

                                  If I plug it into a 3.3v pin, what scripts are available to control it?

                                  Then you can use raspi-gpio to powerdown that PIN - if you use a GPIO that let's the fan run! But this is not the recommended way ;) Usually those devices are connected to 5V rail (because this delivers 2.5A!) and a GPIO controls a switch to powerdown/poweron that device. Keep in mind ... the 3.3V GPIO rail only delivers 0.10 or 0.05A! That's much enough for a few LEDs and lots of microcontrollers but maybe not enough for a fan.

                                  But I assume the NESPI+ fan connector itself is switchable. Because I've really NO idea why we need GPIO 4 (POWERONCTRL?)

                                  If you want to make the fan switchable I would recommend you:

                                  1. Try the connector into the case, this is maybe switchable (maybe GPIO 4 will do it's job)
                                  2. Try to connect your fan to 5.0V or 3.3V line (that's PIN1 = 3.3 and PIN2,4=5V) and then use a MOSFET connected to a GPIO that switches fan on or off. Therefore raspi-gpio can be used.
                                  1 Reply Last reply Reply Quote 0
                                  • G
                                    gollumer
                                    last edited by

                                    Lol. Yeah, I originally had the fan connected to the fan connector, but recently changed it due to the noise. You're right: the fan doesn't shut down. I'm not at home right now, but when I get home I'll try the command you suggest (raspi-gpio set 4 op dl) from the terminal and see what happens.

                                    cyperghostC 1 Reply Last reply Reply Quote 0
                                    • C
                                      cloudlink
                                      last edited by

                                      My fan is connected to GPIO pin 17. Will this make it so the fan runs when the Pi is on but the fan is off when the Pi is off?

                                      G 1 Reply Last reply Reply Quote 0
                                      • G
                                        gollumer @cloudlink
                                        last edited by

                                        @cloudlink said in Multi Switch Shutdown Script!:

                                        My fan is connected to GPIO pin 17. Will this make it so the fan runs when the Pi is on but the fan is off when the Pi is off?

                                        That's the same pin I used...pin 17.

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

                                          @gollumer said in Multi Switch Shutdown Script!:

                                          Lol. Yeah, I originally had the fan connected to the fan connector, but recently changed it due to the noise. You're right: the fan doesn't shut down. I'm not at home right now, but when I get home I'll try the command you suggest (raspi-gpio set 4 op dl) from the terminal and see what happens.

                                          Yes, thank you. I think that's the way the GPIO is intended for.

                                          @cloudlink
                                          About GPIO17, of course both of you can shutoff the fan by uncommenting line 184 in my script and then edit the call in line 311

                                           NESPiPlus 2 3 4 14
                                          
                                          to
                                          
                                           NESPiPlus 2 3 17 14
                                          

                                          But I assume GPIO 4 shutoff the fan if installed in case connector!

                                          1 Reply Last reply Reply Quote 0
                                          • G
                                            gollumer
                                            last edited by

                                            Ok, I tried aspi-gpio set 4 op dl from the terminal, and nothing happened (as expected).

                                            I then uncommented line 184, and edited line 311 as shown (replaced 4 with 17). I rebooted, then shutdown using the button. Shutdown is fine, but the fan is still working. So...perhaps I do need to go back to using fan connector.

                                            cyperghostC 1 Reply Last reply Reply Quote 0
                                            • 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.