Hi,
I have installed Libreelec 9.2.1 on Pi 4 with cooling fan that is connected to GPIO 4 (red) and 6 (black) directly without a transistor. I wanted to control this fan depending on the temperature of CPU. Found some information on internet, and based on these i programmed the Pi as the following:
Created a file fancontrol.py in /storage/.config/ with access 755.
It contains the following script
Python
#!/usr/bin/env python3
import subprocess
import time
from gpiozero import OutputDevice
ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature.
OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature.
SLEEP_INTERVAL = 5 # (seconds) How often we check the core temperature.
GPIO_PIN = 17 # Which GPIO pin you're using to control the fan.
def get_temp():
"""Get the core temperature.
Run a shell script to get the core temp and parse the output.
Raises:
RuntimeError: if response cannot be parsed.
Returns:
float: The core temperature in degrees Celsius.
"""
output = subprocess.run(['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 parse temperature output.')
if __name__ == '__main__':
# Validate the on and off thresholds
if OFF_THRESHOLD >= ON_THRESHOLD:
raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD')
fan = OutputDevice(GPIO_PIN)
while True:
temp = get_temp()
# Start the fan if the temperature has reached the limit and the fan
# isn't already running.
# NOTE: `fan.value` returns 1 for "on" and 0 for "off"
if temp > ON_THRESHOLD and not fan.value:
fan.on()
# Stop the fan if the fan is running and the temperature has dropped
# to 10 degrees below the limit.
elif fan.value and temp < OFF_THRESHOLD:
fan.off()
time.sleep(SLEEP_INTERVAL)
Display More
Then i made another file named fancontrol.sh in /storage/.config/ with access 755
Code
#! /bin/sh
### BEGIN INIT INFO
# Provides: fancontrol.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting fancontrol.py"
/storage/.config/fancontrol.py &
;;
stop)
echo "Stopping fancontrol.py"
pkill -f /storage/.config/fancontrol.py
;;
*)
echo "Usage: /storage/.config/fancontrol.sh {start|stop}"
exit 1
;;
esac
exit 0
Display More
Lastly i created an autostart.sh file in /storage/.config/ with
I have reboot the pi, but the fan is not controlled, it is non stop works with cpu temperature 35.
What did i do wrong? How to make it work?
thank you