• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
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 109.5k 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.
  • C
    cyperghost @mrfalk
    last edited by 10 Nov 2018, 08:36

    @mrfalk

    boot to text console auto login as pi

    I think it is enough to just write ./multi_switch --generic & to send the process to background. Then you avoid the second text console ;)

    1 Reply Last reply Reply Quote 1
    • M
      mrfalk
      last edited by 12 Nov 2018, 11:39

      It works great it boots in to EmulationStation and turns On/Off

      But I encounter a problem it randomly starts it self when projector is still off and dont give out signal to relay wich means circut is open

      C 1 Reply Last reply 12 Nov 2018, 19:03 Reply Quote 0
      • C
        cyperghost @mrfalk
        last edited by 12 Nov 2018, 19:03

        @mrfalk Then there is a kind of "backstroke" somewhere. Maybe it's the relay coil itself, or the relais falls back in closed state somewhere.... Really have no idea why this is happening.

        1 Reply Last reply Reply Quote 1
        • M
          mrfalk
          last edited by 12 Nov 2018, 20:43

          Yeah going to do some more bug searching I’ll get back if I’ll find what causes it //

          1 Reply Last reply Reply Quote 0
          • J
            jandalf81
            last edited by 27 Nov 2018, 15:03

            @cyperghost First of all, thank you for this script. It works great with the RetroFlag SuperPi case. But I do have a feature request...

            Would it be possible (and sensible?) to add fan control to this? What I mean is a way to disable the fan if the temperatures are below a certain threshold. If the temperature rises, enable the fan until the Pi has cooled down again.

            I know the Kintaru Super Ursus 9000 has something like this. I don't know how they've done it, though. I looked at their installer, which executes two BaSh scripts and installs a DEB package:

            • https://www.dropbox.com/s/gmziwqpipzpe38l/kintaroinstaller.sh (this script is executed first, it executes the next script)
            • https://packagecloud.io/install/repositories/kintaro/pcb/script.deb.sh (seems to install some pre-requisites for the DEB package)
            • last, the package kintarosnes is installed

            I tried to control the fan via raspi-gpio but have not been successful, yet. What I was successful at is reading the CPU and GPU temperature and making the PowerLED switch on and off:

            #!/bin/bash
            
            function measureTemps ()
            {
            	local cpuTempC
            	local gpuTempC
            
            	# header
            	printf "DATE\t\t\t\tCPU\t\tGPU\n"
            
            	while [ true ]
            	do
            		if [[ -f "/sys/class/thermal/thermal_zone0/temp" ]]
            		then
            			cpuTempCINT=$(cat /sys/class/thermal/thermal_zone0/temp)
            			cpuTempC="${cpuTempCINT:0:2}.${cpuTempCINT:2:1}"
            		else
            			cpuTempC="n/a"
            		fi
            		
            		if [[ -f "/opt/vc/bin/vcgencmd" ]]
            		then
            			gpuTempC=$(/opt/vc/bin/vcgencmd measure_temp)
            			gpuTempC=${gpuTempC:5:4}
            		else
            			gpuTempC="n/a"
            		fi
            		
            		printf "$(date +%FT%T%:z):\t${cpuTempC}\t\t${gpuTempC}\n"
            		
            		sleep 5
            	done
            }
            
            measureTemps
            
            # RetroFlag SuperPi Case
            # 	PowerLED OFF
            # 	raspi-gpio set 14 op pn dl
            
            # 	PowerLED ON
            # 	raspi-gpio set 14 op pn dh
            

            Is my request even possible? I know the fan is connected to the case's PCB, but so is the PowerLED and that can be controlled!

            C 1 Reply Last reply 27 Nov 2018, 20:25 Reply Quote 0
            • C
              cyperghost @jandalf81
              last edited by cyperghost 27 Nov 2018, 20:25

              @jandalf81 said in Multi Switch Shutdown Script!:

              Would it be possible (and sensible?) to add fan control to this?

              Well the best solution would be a PWM fan control but I'm not very familar with such things. There are so much possibilities to control fans the easiest way to do this is due an opto-isolator. Then you add an additional GPIO to just power on/off the fan. This works same like the LED control.

              But to add this in the script.... No I don't think so. Because you need additional hardware. But your function could be invoked to the loop and will check button action and output CPU temperature.

              Sadly you can't control the fan directly through the case.... so as I wrote. You need always additional hardware and I would stay away to directly use GPIOs as power source. The 3,3V line is only capable of 100mA current. Enough for a few LEDs, microcontrollers ... but near the edge to power a 5V fan (imho)

              I know the Kintaru Super Ursus 9000 has something like this. I don't know how they've done it, though. I looked at their installer, which executes two BaSh scripts and installs a DEB package:

              Looks like PWM control... take a look here
              Here someone did PWM control into NESPi case

              Is my request even possible? I know the fan is connected to the case's PCB, but so is the PowerLED and that can be controlled!

              After long reading here is the short answer: No!
              Because:
              The LED is directly connected to GPIO14 so this can be controlled
              The fan is directly connected to main power.... Can only be controlled like the complete case -- Power Cut!

              But:
              With additional hardware and some new wiring: Yes!

              J 1 Reply Last reply 27 Nov 2018, 20:53 Reply Quote 1
              • J
                jandalf81 @cyperghost
                last edited by 27 Nov 2018, 20:53

                @cyperghost

                Thank you very much for your reply! Although that's not the answer I had hoped for I appreciate your time and effort!
                Well, I'll just turn the volume up, then... ;-)

                C 1 Reply Last reply 27 Nov 2018, 20:59 Reply Quote 0
                • C
                  cyperghost @jandalf81
                  last edited by cyperghost 27 Nov 2018, 20:59

                  @jandalf81 said in Multi Switch Shutdown Script!:

                  Well, I'll just turn the volume up, then... ;-)

                  If the fan is to loud then use following tricks:

                  1. You can try to connect to 3,3Voltage (that is PIN 1 on Raspberry GPIO header - I don't recommend this)

                  2. Let it connected to 5V and add two Si-diodes to this
                    like:

                  +5V --->|---->|------ FAN
                  

                  each diode consumes 0,6V-0,7V so you've a drop to 3,8-3,6V and the fan is more silent ;) You can also use resistors but this is a bad way I do not recommend.

                  1 Reply Last reply Reply Quote 1
                  • D
                    denisuu
                    last edited by denisuu 17 Dec 2018, 22:33

                    This might not the right forum to be posting this, but no one else seems to want to help and I don't have enough scripting/programming skills to understand your script.

                    I have my pi partitioned with PINN (Retropie, OSMC and Raspbian) However your script only works on Retropie. I found another script that does work on both OSMC & Raspbian but doesn't shut down the PI completely. The standby light stays on and the fans keep spinning.

                    I want to add the shutdown_fan part from your script to this script: LINK

                    #!/usr/bin/python
                    import RPi.GPIO as GPIO
                    import os
                    
                    # hide warning
                    GPIO.setwarnings(0)
                    
                    # connect RPi numbered pin
                    button = 5
                    
                    # hide warning
                    GPIO.setwarnings(0)
                    
                    # pins setup
                    GPIO.setmode(GPIO.BOARD)
                    GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
                    
                    # wait for button press
                    print('Wait for button press...')
                    GPIO.wait_for_edge(button, GPIO.FALLING)
                    
                    # activate system command
                    # Updated to make true safe shutdown w/emustation (incase OSMC has been added)
                    os.system('sudo sleep 5s & sudo shutdown -h now')
                    

                    From what I understand I need parts from these parts?:

                    #From shutdown_fan script 
                    [ "$1" = "poweroff" ] && raspi-gpio set 4 op pn dl
                    
                    #From multi_switch.sh scipt
                    echo "    PowerOnControl GPIO 4 (BCM 4), output, high, power on control!"
                    
                            pack_check raspi-gpio
                            cli_parameter resetbtn= powerbtn= powerctrl= ledctrl=
                            NESPiPlus ${call[@]}
                    
                    function cli_parameter() {
                        unset call
                        local PARAMETER=$@
                        for i in ${PARAMETER[@]}; do
                            value="${CLI#*$i}"
                            [[ $value != $PARAMETER ]] && value="${value%% *}" || value="-1"
                            [[ $value =~ ^[0-9]{1,2}$ ]] || value="-1"
                            call+=("$value")
                        done
                    }
                    
                    1 Reply Last reply Reply Quote 0
                    • C
                      cyperghost
                      last edited by 18 Dec 2018, 07:00

                      @denisuu Take a look here SHIM ON OFF cut power PART 1
                      Use this script and set the correct Pins in script. This works

                      D 1 Reply Last reply 18 Dec 2018, 18:51 Reply Quote 0
                      • D
                        denisuu @cyperghost
                        last edited by 18 Dec 2018, 18:51

                        @cyperghost

                        I tried for more than an hour I really can't get this to work.

                        In THIS tutorial: Do I copy the content of SHIM ON OFF - PART ONE in part 2.a or 3.a?

                        I'm also confused with the pin numbers, in the other tutorial he uses pin 5 and in your script it's pin 4 or am I confusing the pin to power down with the button?

                        C 1 Reply Last reply 18 Dec 2018, 19:12 Reply Quote 0
                        • C
                          cyperghost @denisuu
                          last edited by cyperghost 18 Dec 2018, 19:12

                          @denisuu I don't use OSMC so make sure the directory /lib/systemd/system-shutdown/ is available (I think it is available)

                          So in your tutorial follow steps till 2b

                          1. sudo nano /etc/rc.local
                            3.1 add above line exit 0 python /home/osmc/shutdown.py &
                            3.2 or /home/osmc/shutdown.py & alone can also work ;)

                          2. setup script according to link from me
                            4.1 change led_pin from 17 to 14
                            4.2 poweroff_pin = 4 is okay

                          No... 5 is physical pin 5 = GPIO 3 so magic are the lines import RPi.GPIO as GPIO ..... GPIO.setmode(GPIO.BOARD) in the python script.

                          D 1 Reply Last reply 18 Dec 2018, 20:26 Reply Quote 0
                          • D
                            denisuu @cyperghost
                            last edited by denisuu 18 Dec 2018, 20:26

                            Thanks for the help, really appreciate it!!!

                            It's partially working now, now it goes to sleep and the fans keep spinning like in the original script.
                            I actually have a Retroflag SUPERpi case (on the other 2 scripts it dind't make a difference).

                            the power-switch acts a little funny now. Up is reboot and down is sleep.

                            All steps done: https://pastebin.com/4QcLQY7M

                            Edit: It's reacts exactly the same on Raspbian.

                            C 1 Reply Last reply 18 Dec 2018, 20:59 Reply Quote 0
                            • C
                              cyperghost @denisuu
                              last edited by cyperghost 18 Dec 2018, 20:59

                              @denisuu Does the LED blink 3 times? You have to make the gpio shutoff script executable with chmod command. This is essential forget this to mention

                              Edit it is command
                              sudo chmod +x /lib/systemd/system-shutdown/gpio-shutoff

                              D 1 Reply Last reply 18 Dec 2018, 21:21 Reply Quote 0
                              • D
                                denisuu @cyperghost
                                last edited by 18 Dec 2018, 21:21

                                Yes it blinks 3 times and powers off then after af few seconds turns on again,
                                or goes to sleep (depending on the possition of the switch).

                                I did make the script executable I forgot to add it to the pastbin. (added it now)

                                C 1 Reply Last reply 18 Dec 2018, 21:37 Reply Quote 0
                                • C
                                  cyperghost @denisuu
                                  last edited by 18 Dec 2018, 21:37

                                  @denisuu are there any other scripts running? It is a bit hard to guess but you can try to use the original fan_shutdown script. Please install raspi-gpio with sudo apt-get install raspi-gpio

                                  D 1 Reply Last reply 19 Dec 2018, 01:18 Reply Quote 0
                                  • D
                                    denisuu @cyperghost
                                    last edited by denisuu 19 Dec 2018, 01:18

                                    Ok I removed the old script on Raspbian and installed yours, it works perfectly now!

                                    Only OSMC left, it's really sad it doens't work on OSMC since that's what I use my pi most often for. OSMC doesn't find the package raspi-gpio or npm. Can I just disable the FAN on that partition? Or get raspi-gpio from somewhere else?

                                    Raspbian:

                                      296  cd /home/pi/
                                      297  ls -alF
                                      298  rm reset.py
                                      299  rm shutdown.py
                                      300  rm /lib/systemd/system/shutdown.service
                                      301  apt install raspi-gpio
                                      302  mkdir /home/pi/scripts
                                      303  cd /home/pi/scripts
                                      304  wget https://raw.githubusercontent.com/crcerror/ES-generic-shutdown/master/multi_switch.sh && chmod +x multi_switch.sh                                                                                                         r/multi_switch.sh && chmod +x multi_switch.sh
                                      305  ls -alF
                                      306  sudo nano /etc/rc.local     (/home/pi/scripts/multi_switch.sh --nespi+ & before exit 0)
                                      307  cd /lib/systemd/system-shutdown/ && sudo wget https://raw.githubusercontent.com/crcerror/ES-generic-shutdown/master/shutdown_fan && sudo chmod +x shutdown_fan                                                                                                         nt.com/crcerror/ES-generic-shutdown/master/shutdown_fan && sudo chmod +x shutdownfan
                                    
                                    C 1 Reply Last reply 19 Dec 2018, 08:58 Reply Quote 0
                                    • C
                                      cyperghost @denisuu
                                      last edited by 19 Dec 2018, 08:58

                                      @denisuu I think in OSMC it is easily possible to add that function, too. You can try wiringPi instead - I think that should be available. So every call to raspi-gpio must be translated to a wPi call.

                                      EDIT:
                                      I think you should use sudo apt-get install build-essential and then you can further start your build

                                      D 1 Reply Last reply 19 Dec 2018, 12:18 Reply Quote 0
                                      • D
                                        denisuu @cyperghost
                                        last edited by denisuu 19 Dec 2018, 12:18

                                        I deleted half of the scipt and it still works, would it be possible to delete everything I don't need from this? That would make it a lot easier to translate it to WiringPi, since I don't understand half of what the script does.

                                        C 1 Reply Last reply 19 Dec 2018, 15:59 Reply Quote 0
                                        • C
                                          cyperghost @denisuu
                                          last edited by 19 Dec 2018, 15:59

                                          @denisuu You can delete 95% if the script and it will still work. Instead of the raspi-gpio try to translate to wpi calls. Did you try to compile raspi-gpio? https://github.com/RPi-Distro/raspi-gpio

                                          D 1 Reply Last reply 19 Dec 2018, 16:30 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.

                                            This community forum collects and processes your personal information.
                                            consent.not_received