Plataoroms

fanbytemperature.sh

May 27th, 2018
289
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Author: Edoardo Paolo Scalafiotti <edoardo849@gmail.com>
  4. # Modificado por: CpA - Raspito's Family
  5.  
  6. import os
  7. from time import sleep
  8. import signal
  9. import sys
  10. import RPi.GPIO as GPIO
  11.  
  12. pin = 18  # The pin ID, edit here to change it
  13. maxTMP = 60  # Temperatura máxima donde arrancará el ventilador
  14. freshTMP = 45  # Temperatura de rebote, donde se apagará el ventilador
  15.  
  16.  
  17. def setup():
  18.     GPIO.setmode(GPIO.BCM)
  19.     GPIO.setup(pin, GPIO.OUT)
  20.     GPIO.setwarnings(False)
  21.     return ()
  22.  
  23.  
  24. def getCPUtemperature():
  25.     res = os.popen('vcgencmd measure_temp').readline()
  26.     temp = (res.replace("temp=", "").replace("'C\n", ""))
  27.     # print(“temp is {0}”.format(temp)) #Uncomment here for testing
  28.     return temp
  29.  
  30.  
  31. def fanON():
  32.     setPin(True)
  33.     return ()
  34.  
  35.  
  36. def fanOFF():
  37.     setPin(False)
  38.     return ()
  39.  
  40.  
  41. def getTEMP(switch):
  42.     CPU_temp = float(getCPUtemperature())
  43.     # La temperatura alcanza MaxTemp
  44.     if CPU_temp > maxTMP and not switch:
  45.         fanON()
  46.         return True
  47.     # La temperatura es menor de MaxTMP, y menor de feshTMP, pero está encendido
  48.     elif switch and CPU_temp < freshTMP:
  49.         fanOFF()
  50.         return False
  51.     # Si el ventilador está encendido
  52.     elif switch:
  53.         return True
  54.     else:
  55.         return False
  56.  
  57.  
  58. def setPin(mode):  # A little redundant function but useful if you want to add logging
  59.     GPIO.output(pin, mode)
  60.     return ()
  61.  
  62.  
  63. try:
  64.     setup()
  65.     ventilador_on = False
  66.     while True:
  67.         ventilador_on = getTEMP(ventilador_on)
  68.         sleep(5)  # Read the temperature every 5 sec, increase or decrease this limit if you want
  69. except KeyboardInterrupt:  # trap a CTRL+C keyboard interrupt
  70.     GPIO.cleanup()  # resets all GPIO ports used by this program
RAW Paste Data