Advertisement
keks13

Untitled

Jan 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Author: Edoardo Paolo Scalafiotti <edoardo849@gmail.com>
  3. import os
  4. from time import sleep
  5. import signal
  6. import sys
  7. import RPi.GPIO as GPIO
  8. pin = 18 # The pin ID, edit here to change it
  9. maxTMP = 40 # The maximum temperature in Celsius after which we trigger the fan
  10. def setup():
  11. GPIO.setmode(GPIO.BCM)
  12. GPIO.setup(pin, GPIO.OUT)
  13. GPIO.setwarnings(False)
  14. return()
  15. def getCPUtemperature():
  16. res = os.popen(‘vcgencmd measure_temp’).readline()
  17. temp =(res.replace(“temp=”,””).replace(“’C\n”,””))
  18. #print(“temp is {0}”.format(temp)) #Uncomment here for testing
  19. return temp
  20. def fanON():
  21. setPin(True)
  22. return()
  23. def fanOFF():
  24. setPin(False)
  25. return()
  26. def getTEMP():
  27. CPU_temp = float(getCPUtemperature())
  28. if CPU_temp>maxTMP:
  29. fanON()
  30. else:
  31. fanOFF()
  32. return()
  33. def setPin(mode): # A little redundant function but useful if you want to add logging
  34. GPIO.output(pin, mode)
  35. return()
  36. try:
  37. setup()
  38. while True:
  39. getTEMP()
  40. sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
  41. except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
  42. GPIO.cleanup() # resets all GPIO ports used by this program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement