Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import RPi.GPIO as GPIO
  4. import time
  5. import sys
  6. import os
  7. import signal
  8. # Configuration
  9. FAN_PIN = 14 # BCM pin used to drive transistor's base
  10. WAIT_TIME = 5 # [s] Time to wait between each refresh
  11. FAN_LSPD = 20 # [%] Fan minimum speed.
  12. PWM_FREQ = 25 # [Hz] Change this value if fan has strange behavior
  13. # Setup GPIO pin
  14. GPIO.setmode(GPIO.BCM)
  15. GPIO.setup(FAN_PIN, GPIO.OUT, initial=GPIO.LOW)
  16. fan=GPIO.PWM(FAN_PIN,PWM_FREQ)
  17. fan.start(0);
  18.  
  19. def getCPUtemperature():
  20. res = os.popen('/opt/vc/bin/vcgencmd measure_temp').readline()
  21. temp =(res.replace("temp=","").replace("'C\n",""))
  22. print("Temp is {0}°C".format(temp)) #Uncomment here for testing
  23. return temp
  24. def getTEMP():
  25. CPU_temp = float(getCPUtemperature())
  26. return()
  27. i = 0
  28. hyst = 1 # Fan speed will change only of the difference of temperature is higher than hysteresis
  29. tempSteps = [30, 35, 40, 45, 50, 60, 65, 70] # [°C] # Configurable temperature and fan speed steps
  30. speedSteps = [0, 20, 40, 60, 80, 100, 100, 100] # [%]
  31. cpuTempOld=0
  32. fanSpeedOld=0
  33. # We must set a speed value for each temperature step
  34. if(len(speedSteps) != len(tempSteps)):
  35. print("Numbers of temp steps and speed steps are different")
  36. exit(0)
  37.  
  38. try:
  39. while 1:
  40. getTEMP()
  41. time.sleep(5) # Read the temperature every 5 secs
  42. cpuTempFile=open("/sys/class/thermal/thermal_zone0/temp","r")
  43. cpuTemp=float(cpuTempFile.read())/1000
  44. cpuTempFile.close() # Read CPU temperature
  45. if (abs(cpuTemp-cpuTempOld > hyst)): #Calculate desired fan speed
  46. if (cpuTemp < tempSteps[0]): # Below first value, fan will run at min speed.
  47. fanSpeed = speedSteps[0]
  48. elif(cpuTemp >= tempSteps[len(tempSteps)-1]): # Above last value, fan will run at max speed
  49. fanSpeed = speedSteps[len(tempSteps)-1]
  50. else: # If temperature is between 2 steps, fan speed is calculated by linear interpolation
  51. for i in range(0,len(tempSteps)-1):
  52. if((cpuTemp >= tempSteps[i]) and (cpuTemp < tempSteps[i+1])):
  53. fanSpeed = round((speedSteps[i+1]-speedSteps[i])\
  54. /(tempSteps[i+1]-tempSteps[i])\
  55. *(cpuTemp-tempSteps[i])\
  56. +speedSteps[i],1)
  57. if((fanSpeed != fanSpeedOld) ):
  58. if((fanSpeed != fanSpeedOld)\
  59. and ((fanSpeed >= FAN_LSPD) or (fanSpeed == 0))):
  60. fan.ChangeDutyCycle(fanSpeed)
  61. fanSpeedOld = fanSpeed
  62. print("Speed is {0}%".format(fanSpeed))
  63. time.sleep(WAIT_TIME) # Wait until next refresh
  64. except(KeyboardInterrupt): # If a keyboard interrupt occurs (ctrl + c), the GPIO is set to 0 and the program exits.
  65. print("Cancelled... Fan OFF")
  66. GPIO.cleanup()
  67. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement