from enum import Enum
import time
TEMP_PATH = "/sys/devices/virtual/thermal/thermal_zone0/temp"
FAN_PATH = "/sys/class/thermal/cooling_device0/cur_state"
class FanSpeed(Enum):
OFF = 0
LOW = 1
MEDIUM = 2
HIGH = 3
FULL = 4
def main():
while True:
temp = get_temp()
if temp > 70:
speed = FanSpeed.FULL
elif temp > 65:
speed = FanSpeed.HIGH
elif temp > 60:
speed = FanSpeed.MEDIUM
elif temp > 40:
speed = FanSpeed.LOW
else:
speed = FanSpeed.OFF
formatted_temp = "{:.2f}".format(temp) # Format temperature with two decimal places
print("=>", formatted_temp, speed)
set_fan_speed(speed)
time.sleep(2)
def get_temp() -> float:
with open(TEMP_PATH, "r") as f:
data = f.read()
temp = int(data) / 1000.0 # Convert the temperature data to a float before dividing
return temp
def set_fan_speed(speed: FanSpeed):
with open(FAN_PATH, "w") as f:
f.write(str(speed.value))
if __name__ == "__main__":
main()
python3 /home/pi/fan.py > /home/pi/fan.txt &