Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca63427

Browse files
authored
Merge pull request #5 from expert-m/master
Read the core temperature from the file
2 parents a51a851 + d91a04f commit ca63427

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

fancontrol.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env python3
22

3-
import subprocess
43
import time
54

65
from gpiozero import OutputDevice
76

87

9-
ON_THRESHOLD = 65*1000 # (degrees Celsius) Fan kicks on at this temperature.
10-
OFF_THRESHOLD = 55*1000 # (degress Celsius) Fan shuts off at this temperature.
8+
ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature.
9+
OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature.
1110
SLEEP_INTERVAL = 5 # (seconds) How often we check the core temperature.
1211
GPIO_PIN = 17 # Which GPIO pin you're using to control the fan.
1312

@@ -20,10 +19,13 @@ def get_temp():
2019
Returns:
2120
int: The core temperature in thousanths of degrees Celsius.
2221
"""
23-
with open("/sys/class/thermal/thermal_zone0/temp") as f:
24-
temp_data = f.read()
25-
26-
return int(temp_data)
22+
with open('/sys/class/thermal/thermal_zone0/temp') as f:
23+
temp_str = f.read()
24+
25+
try:
26+
return int(temp_str) / 1000
27+
except (IndexError, ValueError,) as e:
28+
raise RuntimeError('Could not parse temperature output.') from e
2729

2830
if __name__ == '__main__':
2931
# Validate the on and off thresholds

0 commit comments

Comments
 (0)