2023-04-30 15:00:54 +03:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2023-04-30 15:11:52 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
servo = 18
|
|
|
|
|
|
|
|
if true: # set to false to skip using dotenv
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
servo = int(os.getenv('FANCTL_SERVO_PIN'))
|
|
|
|
|
2023-04-30 15:00:54 +03:00
|
|
|
import RPi.GPIO as IO
|
|
|
|
import time
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
IO.setwarnings(False)
|
|
|
|
IO.setmode (IO.BCM)
|
|
|
|
IO.setup(servo,IO.OUT)
|
|
|
|
fan = IO.PWM(servo,2000)
|
|
|
|
fan.start(0)
|
|
|
|
|
|
|
|
def get_temp():
|
|
|
|
output = subprocess.run(['/opt/vc/bin/vcgencmd', 'measure_temp'], capture_output=True)
|
|
|
|
temp_str = output.stdout.decode()
|
|
|
|
try:
|
|
|
|
return float(temp_str.split('=')[1].split('\'')[0])
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
raise RuntimeError('Could not get temperature')
|
|
|
|
|
|
|
|
def set_speed(fan, speed):
|
|
|
|
fan.ChangeDutyCycle(speed)
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
temp = get_temp() # Get the current CPU temperature
|
|
|
|
if temp > 70: # Check temperature threshhold, in degrees celcius
|
|
|
|
set_speed(fan, 100) # Set fan duty based on temperature, 100 is max speed and 0 is min speed or off.
|
|
|
|
elif temp > 60:
|
|
|
|
set_speed(fan, 85)
|
|
|
|
elif temp > 50:
|
|
|
|
set_speed(fan, 60)
|
|
|
|
elif temp > 40:
|
|
|
|
set_speed(fan, 50)
|
|
|
|
elif temp > 32:
|
|
|
|
set_speed(fan, 45)
|
|
|
|
elif temp > 25:
|
|
|
|
set_speed(fan, 40)
|
|
|
|
else:
|
|
|
|
set_speed(fan, 0)
|
|
|
|
time.sleep(5) # Sleep for 5 seconds
|