Recalbox Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • recalbox.com
    • Gitlab repository
    • Documentation
    • Discord

    Unsolved Script to turn off cooler

    Recalbox General
    script turn cooler
    7
    24
    9319
    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.
    • Substring
      Substring @azvarel last edited by

      @azvarel Setting a GPIO to a logical 1 level means outputting 3.3V i think. Beware of the current level, a GPIO can't deliver much

      Former dev - Please reply with @substring so that i am notified when you answer me
      Ex dev - Merci de me répondre en utilisant @substring pour que je sois notifé

      1 Reply Last reply Reply Quote 0
      • azvarel
        azvarel last edited by

        @lackyluuk said in Push button (ON/OFF) script problems?:

        @PenPen: I am sorry for this way too late answer. Not sure if you need an answer anymore

        Here is an example with GPIO pin 14 for controlling the fan. The indents might not be correct 😉 I had a little trouble with copy/paste the code 😄
        Hope I can help you with this

        #!/usr/bin/env python

        import os
        import time
        import RPi.GPIO as GPIO

        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        #Set max. temperature of CPU
        TEMP_MAX = 67
        ON_TIME = 200
        #Set pin 14 as transistor gate
        GATE = 14
        #Set pin 14 as an output and shut down
        GPIO.setup(GATE, GPIO.OUT)
        GPIO.output(GATE, False)

        def getCPUtemperature():
        res = os.popen('vcgencmd measure_temp').readline()
        return(res.replace("temp=","").replace("'C\n",""))

        while True:

          	temp_float = float(getCPUtemperature())
        
          try:
          	if (temp_float > TEMP_MAX):
          		# ein
          		GPIO.output(GATE, True)
          		time.sleep(ON_TIME)
          		# aus
          		GPIO.output(GATE, False)
          	else:
          		GPIO.output(GATE, False)
        

        except KeyboardInterrupt:
        print float(getCPUtemperature())
        print "power off fan..."
        GPIO.output(GATE, False)
        print "cancelling..."

        Forgot to attach a short drawing. I used the official RaspberryPI 5V DC fan.

        0_1474305851135_Unbenannt.PNG

        @lackyluuk
        I will ask the questions here instead of being there.

        So I understood your script when it reaches a temperature it turns on the cooler, when it cools it turns off. it is?

        @Substring I'm not a programmer, I'm afraid to do something and go wrong.

        Zodian Theme | #SNESProject

        Substring 1 Reply Last reply Reply Quote 0
        • ian57
          ian57 Staff last edited by

          A more simple solution is to plug the fan cooler on the USB Connector. These one are stopped when the system stops.

          "UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." Dennis Ritchie

          1 Reply Last reply Reply Quote 0
          • lackyluuk
            lackyluuk Translator last edited by

            @azvarel yes exactly, this script controls the fan depending on the temperature. If you don't need this you can just turn the fan ON at startup and OFF at shutdown.

            Or as @ian57 alrrady mentioned, to use a USB slot if you have one free. They deliver also 5V DC.

            NintendoRS | Nintendo Adapter Box | Cardboard Recalbox

            1 Reply Last reply Reply Quote 0
            • azvarel
              azvarel last edited by

              @ian57 This idea is great, but I use the other ports for usb controllers, xbox360 receiver and are on the front of SNES 😞

              If I soldered the wires at the voltage points of the USB, would it have problem when plugging a controller or a usb key? If it does not give you trouble, I do it even easier for me.

              If I do not have to follow the script of @lackyluuk, I just did not understand there in the diagram the why of having a transistor, for safety?

              Zodian Theme | #SNESProject

              1 Reply Last reply Reply Quote 0
              • ian57
                ian57 Staff last edited by ian57

                @azvarel; the transitor is for switch and safety. I don't think that standard GPIO can supply enough power to run your fan correctly. So we use the +5V directly from the power supply to power the fan, and the GPIO + transitor are making the script controllable switch.

                Soldering to get only the power supply is a bit risky for the plugged element I think. A fan, is a motor, and it can have bad effects on it's power circuit. And the usb deliver at max 500mA. If you try to do that .. not on an usb key slot... try it on the usb snes controller slots... it uses less current ...

                If someone elsehas an idea about that?

                "UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." Dennis Ritchie

                azvarel 1 Reply Last reply Reply Quote 0
                • azvarel
                  azvarel @ian57 last edited by

                  @ian57 so we went back to the script.

                  Zodian Theme | #SNESProject

                  1 Reply Last reply Reply Quote 0
                  • Substring
                    Substring @azvarel last edited by

                    @azvarel the script looks like it takes 100% CPU, but it looks logically correct.
                    The electronic schematic is very simple and could help someone like @ian57 who wants to drive a 12V power supply 😄

                    Former dev - Please reply with @substring so that i am notified when you answer me
                    Ex dev - Merci de me répondre en utilisant @substring pour que je sois notifé

                    1 Reply Last reply Reply Quote 1
                    • lackyluuk
                      lackyluuk Translator last edited by

                      @azvarel @Substring just add a time.sleep(1) or (2) into the while() loop

                      NintendoRS | Nintendo Adapter Box | Cardboard Recalbox

                      1 Reply Last reply Reply Quote 0
                      • tikiandskull
                        tikiandskull last edited by

                        Well, maybe i say a stupidity but with a thermal switch? It avoids the question of the script and you can choose one with a temperature of closing and opening of your choice.

                        azvarel 1 Reply Last reply Reply Quote 0
                        • azvarel
                          azvarel @tikiandskull last edited by

                          @tikiandskull nothing is stupid, any idea is valid, where would you find this thermal switch?

                          Zodian Theme | #SNESProject

                          tikiandskull 1 Reply Last reply Reply Quote 0
                          • tikiandskull
                            tikiandskull @azvarel last edited by tikiandskull

                            @azvarel maybe something like this link text It can be found on amazon, conrad, etc some works in 5v others in 12v.

                            Afterwards, I do not know how it could be positioned, but probably with the probe as close as possible to cpu

                            (sorry for my poor English 🙂 )

                            1 Reply Last reply Reply Quote 0
                            • Pdagarcia
                              Pdagarcia last edited by Pdagarcia

                              #!/usr/bin/env python3

                              import os
                              from time import sleep
                              import signal
                              import sys
                              import RPi.GPIO as GPIO

                              pin = 27 # The pin ID, edit here to change it
                              maxTMP = 50 # The maximum temperature in Celsius after which we trigger the fan
                              sleepTime = 30 # The time to sleep between temp checks

                              def setup():
                              GPIO.setmode(GPIO.BCM)
                              GPIO.setup(pin, GPIO.OUT)
                              GPIO.setwarnings(False)
                              return()

                              def getCPUtemperature():
                              res = os.popen('vcgencmd measure_temp').readline()
                              temp =(res.replace("temp=","").replace("'C\n",""))
                              print("temp is {0}".format(temp)) #Uncomment here for testing
                              return temp

                              def fanON():
                              print("Fan on ...")
                              setPin(True)
                              return()

                              def fanOFF():
                              print("Fan off ...")
                              setPin(False)
                              return()

                              def getTEMP():
                              CPU_temp = float(getCPUtemperature())
                              if CPU_temp > maxTMP:
                              fanON()
                              sleep(sleepTime) # Sleep additional time in order to ensure that we stay below max temp ...
                              else:
                              fanOFF()
                              return()

                              def setPin(mode): # A little redundant function but useful if you want to add logging
                              GPIO.output(pin, mode)
                              return()

                              try:
                              setup()
                              while True:
                              getTEMP()
                              sleep(sleepTime) # Read the temperature every x amount of seconds ...
                              except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
                              GPIO.cleanup() # resets all GPIO ports used by this script

                              1 Reply Last reply Reply Quote 0
                              • Pdagarcia
                                Pdagarcia last edited by Pdagarcia

                                With this transistor 2N4401 600 mA, 40V and 5v fan Works perfectly.

                                azvarel 1 Reply Last reply Reply Quote 0
                                • azvarel
                                  azvarel @Pdagarcia last edited by

                                  @Pdagarcia where would you put this script?

                                  Zodian Theme | #SNESProject

                                  1 Reply Last reply Reply Quote 0
                                  • lackyluuk
                                    lackyluuk Translator last edited by lackyluuk

                                    @azvarel I would place the script into /recalbox/scripts and call the script as a background process from the startup directory /etc/init.d for example as S32StartFanControl
                                    This could look like this (just one proposal, you can do it however you like it):

                                    #!/usr/bin/python
                                    
                                    import subprocess
                                    
                                    try:
                                        subprocess.Popen(["python","/recalbox/scripts/fanControl.py"])
                                    
                                    except KeyboardInterrupt:
                                        print "Quit"
                                    

                                    I have called my script that I start as fanControl.py

                                    NintendoRS | Nintendo Adapter Box | Cardboard Recalbox

                                    azvarel 1 Reply Last reply Reply Quote 0
                                    • azvarel
                                      azvarel @lackyluuk last edited by

                                      @lackyluuk thanks 😃

                                      Zodian Theme | #SNESProject

                                      1 Reply Last reply Reply Quote 0
                                      • marcelogarbin
                                        marcelogarbin last edited by

                                        @azvarel I'm with the same problem, did you work? which method did you use?

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

                                        Want to support us ?

                                        72
                                        Online

                                        99.6k
                                        Users

                                        28.1k
                                        Topics

                                        187.1k
                                        Posts

                                        Copyright © 2021 recalbox.com