fancontrol.py
· 1.9 KiB · Python
Raw
#! /usr/bin/python
# PWM fan speed controller
import RPi.GPIO as GPIO
import time
import signal
import sys
import os
# Configuration
FAN_PIN = 14 # BCM pin used to drive PWM fan
WAIT_TIME = 10 # [s] Time to wait between each refresh
PWM_FREQ = 25 # [kHz] 25kHz for Noctua PWM control
# Configurable temperature and fan speed
MIN_TEMP = 35
MAX_TEMP = 65
FAN_LOW = 40
FAN_HIGH = 100
FAN_OFF = 0
FAN_MAX = 100
# Get CPU's temperature
def getCpuTemperature():
res = os.popen('vcgencmd measure_temp').readline()
temp =(res.replace("temp=","").replace("'C\n",""))
#print("temp is {0}".format(temp)) # Uncomment for testing
print("temp:", temp)
return temp
# Set fan speed
def setFanSpeed(speed):
print("speed:", speed)
fan.start(speed)
return()
# Handle fan speed
def handleFanSpeed():
temp = float(getCpuTemperature())
# Turn off the fan if temperature is below MIN_TEMP
if temp < MIN_TEMP:
setFanSpeed(FAN_OFF)
#print("Fan OFF") # Uncomment for testing
# Set fan speed to MAXIMUM if the temperature is above MAX_TEMP
elif temp > MAX_TEMP:
setFanSpeed(FAN_MAX)
#print("Fan MAX") # Uncomment for testing
# Caculate dynamic fan speed
else:
step = (FAN_HIGH - FAN_LOW)/(MAX_TEMP - MIN_TEMP)
temp -= MIN_TEMP
setFanSpeed(FAN_LOW + ( round(temp) * step ))
#print(FAN_LOW + ( round(temp) * step )) # Uncomment for testing
return ()
try:
# Setup GPIO pin
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(FAN_PIN, GPIO.OUT, initial=GPIO.LOW)
fan = GPIO.PWM(FAN_PIN,PWM_FREQ)
setFanSpeed(FAN_OFF)
# Handle fan speed every WAIT_TIME sec
while True:
handleFanSpeed()
time.sleep(WAIT_TIME)
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
setFanSpeed(FAN_HIGH)
#GPIO.cleanup() # resets all GPIO ports used by this function
| 1 | #! /usr/bin/python |
| 2 | # PWM fan speed controller |
| 3 | |
| 4 | import RPi.GPIO as GPIO |
| 5 | import time |
| 6 | import signal |
| 7 | import sys |
| 8 | import os |
| 9 | |
| 10 | # Configuration |
| 11 | FAN_PIN = 14 # BCM pin used to drive PWM fan |
| 12 | WAIT_TIME = 10 # [s] Time to wait between each refresh |
| 13 | PWM_FREQ = 25 # [kHz] 25kHz for Noctua PWM control |
| 14 | |
| 15 | # Configurable temperature and fan speed |
| 16 | MIN_TEMP = 35 |
| 17 | MAX_TEMP = 65 |
| 18 | FAN_LOW = 40 |
| 19 | FAN_HIGH = 100 |
| 20 | FAN_OFF = 0 |
| 21 | FAN_MAX = 100 |
| 22 | |
| 23 | # Get CPU's temperature |
| 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 for testing |
| 28 | print("temp:", temp) |
| 29 | return temp |
| 30 | |
| 31 | # Set fan speed |
| 32 | def setFanSpeed(speed): |
| 33 | print("speed:", speed) |
| 34 | fan.start(speed) |
| 35 | return() |
| 36 | |
| 37 | # Handle fan speed |
| 38 | def handleFanSpeed(): |
| 39 | temp = float(getCpuTemperature()) |
| 40 | # Turn off the fan if temperature is below MIN_TEMP |
| 41 | if temp < MIN_TEMP: |
| 42 | setFanSpeed(FAN_OFF) |
| 43 | #print("Fan OFF") # Uncomment for testing |
| 44 | # Set fan speed to MAXIMUM if the temperature is above MAX_TEMP |
| 45 | elif temp > MAX_TEMP: |
| 46 | setFanSpeed(FAN_MAX) |
| 47 | #print("Fan MAX") # Uncomment for testing |
| 48 | # Caculate dynamic fan speed |
| 49 | else: |
| 50 | step = (FAN_HIGH - FAN_LOW)/(MAX_TEMP - MIN_TEMP) |
| 51 | temp -= MIN_TEMP |
| 52 | setFanSpeed(FAN_LOW + ( round(temp) * step )) |
| 53 | #print(FAN_LOW + ( round(temp) * step )) # Uncomment for testing |
| 54 | return () |
| 55 | |
| 56 | try: |
| 57 | # Setup GPIO pin |
| 58 | GPIO.setwarnings(False) |
| 59 | GPIO.setmode(GPIO.BCM) |
| 60 | GPIO.setup(FAN_PIN, GPIO.OUT, initial=GPIO.LOW) |
| 61 | fan = GPIO.PWM(FAN_PIN,PWM_FREQ) |
| 62 | setFanSpeed(FAN_OFF) |
| 63 | # Handle fan speed every WAIT_TIME sec |
| 64 | while True: |
| 65 | handleFanSpeed() |
| 66 | time.sleep(WAIT_TIME) |
| 67 | |
| 68 | except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt |
| 69 | setFanSpeed(FAN_HIGH) |
| 70 | #GPIO.cleanup() # resets all GPIO ports used by this function |
fancontrol.sh
· 380 B · Bash
Raw
#! /bin/sh
# fancontrol starter script
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting fancontrol.py"
/usr/local/bin/fancontrol.py &
;;
stop)
echo "Stopping fancontrol.py"
pkill -f /usr/local/bin/fancontrol.py
;;
*)
echo "Usage: /etc/init.d/fancontrol.sh {start|stop}"
exit 1
;;
esac
exit 0
| 1 | #! /bin/sh |
| 2 | # fancontrol starter script |
| 3 | |
| 4 | # Carry out specific functions when asked to by the system |
| 5 | case "$1" in |
| 6 | start) |
| 7 | echo "Starting fancontrol.py" |
| 8 | /usr/local/bin/fancontrol.py & |
| 9 | ;; |
| 10 | stop) |
| 11 | echo "Stopping fancontrol.py" |
| 12 | pkill -f /usr/local/bin/fancontrol.py |
| 13 | ;; |
| 14 | *) |
| 15 | echo "Usage: /etc/init.d/fancontrol.sh {start|stop}" |
| 16 | exit 1 |
| 17 | ;; |
| 18 | esac |
| 19 | |
| 20 | exit 0 |