Hey everyone ! Hope you are well !
I am trying to have a fan control script working at startup. I have a Pi 4 with Libreelec 12.0.1 freshly installed with RPi tools addon. Fan is controlled by PWM on PIN 18.
The control script runs well when I use the command: python /storage/fan_control.py
Python
#! /usr/bin/env python3
import time
import signal
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
from gpiozero import PWMOutputDevice
import os
# Configuration
FAN_PIN = 18 # BCM pin used to drive PWM fan
WAIT_TIME = 2 # [s] Time to wait between each refresh
PWM_FREQ = 25 # [kHz] 25kHz for Noctua PWM control
# Configurable temperature and fan speed
MIN_TEMP = 42 # under this temp value fan is switched to the FAN_OFF speed
MAX_TEMP = 68 # over this temp value fan is switched to the FAN_MAX speed
FAN_LOW = 30 # lower side of the fan speed range during cooling
FAN_HIGH = 99 # higher side of the fan speed range during cooling
FAN_OFF = 20 # fan speed to set if the detected temp is below MIN_TEMP
FAN_MAX = 100 # fan speed to set if the detected temp is above MAX_TEMP
# Get CPU's temperature
def getCpuTemperature():
with open('/sys/class/thermal/thermal_zone0/temp') as f:
return float(f.read()) / 1000
def setFanSpeed(speed):
pwm_fan.value = speed/100 # divide by 100 to get values from 0 to 1
return()
# Handle fan speed
def handleFanSpeed():
temp = float(getCpuTemperature())
#print("cpu temp: {}".format(temp))
# 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:
pwm_fan = PWMOutputDevice(FAN_PIN, initial_value=0,frequency=PWM_FREQ) # initialize FAN_PIN as a pwm out>
setFanSpeed(FAN_OFF) # initially set fan speed to the FAN_OFF value
while True:
handleFanSpeed() # call the function that calculates the target fan speed
time.sleep(WAIT_TIME) # wait for WAIT_TIME seconds before recalculate
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
setFanSpeed(FAN_HIGH)
#finally:
# pwm_fan.close() # in case of unexpected exit, resets pin status (fan will go full speed after exiting)
Display More
I have created an autostart.sh file and it also works (fan is controlled) when I execute sh /storage/.config/autostart.sh
When I boot the Raspberry, the fan is always running at full speed which means the python script isn't launched.
Here is the result of systemctl status kodi-autostart.service:
Code
● kodi-autostart.service - Kodi user autostart script
● kodi-autostart.service - Kodi user autostart script
Loaded: loaded (/usr/lib/systemd/system/kodi-autostart.service; disabled; p
Active: active (exited) since Tue 2024-02-27 17:26:17 UTC; 6 months 25 days
Process: 865 ExecStart=/bin/sh -c . /etc/profile; test -f /storage/.config/a
Main PID: 865 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 1590)
CPU: 572ms
CGroup: /system.slice/kodi-autostart.service
Sep 22 09:56:43 KodiPi sh[899]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sep 22 09:56:43 KodiPi sh[899]: File "/storage/.kodi/addons/virtual.rpi-tools/
Sep 22 09:56:43 KodiPi sh[899]: self.pin.frequency = frequency
Sep 22 09:56:43 KodiPi sh[899]: ^^^^^^^^^^^^^^^^^^
Sep 22 09:56:43 KodiPi sh[899]: File "/storage/.kodi/addons/virtual.rpi-tools/
Sep 22 09:56:43 KodiPi sh[899]: lambda self, value: self._set_frequency(valu
Sep 22 09:56:43 KodiPi sh[899]: ^^^^^^^^^^^^^^^^^^^^^^^^
Sep 22 09:56:43 KodiPi sh[899]: File "/storage/.kodi/addons/virtual.rpi-tools/
Sep 22 09:56:43 KodiPi sh[899]: raise PinPWMUnsupported( # pragma: no cover
Sep 22 09:56:43 KodiPi sh[899]: gpiozero.exc.PinPWMUnsupported: PWM is not suppo
~
~
~
standard input lines 1-19/19 (END)
KodiPi:~ # clear
● kodi-autostart.service - Kodi user autostart script
● kodi-autostart.service - Kodi user autostart script
Loaded: loaded (/usr/lib/systemd/system/kodi-autostart.service; disabled; preset: disabled)
Active: active (exited) since Tue 2024-02-27 17:26:17 UTC; 6 months 25 days ago
Process: 865 ExecStart=/bin/sh -c . /etc/profile; test -f /storage/.config/autostart.sh && exec /bin/sh /storage/.config/autostart
Main PID: 865 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 1590)
CPU: 572ms
CGroup: /system.slice/kodi-autostart.service
Sep 22 09:56:43 KodiPi sh[899]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sep 22 09:56:43 KodiPi sh[899]: File "/storage/.kodi/addons/virtual.rpi-tools/lib/gpiozero/output_devices.py", line 396, in __init__
Sep 22 09:56:43 KodiPi sh[899]: self.pin.frequency = frequency
Sep 22 09:56:43 KodiPi sh[899]: ^^^^^^^^^^^^^^^^^^
Sep 22 09:56:43 KodiPi sh[899]: File "/storage/.kodi/addons/virtual.rpi-tools/lib/gpiozero/pins/__init__.py", line 379, in <lambda>
Sep 22 09:56:43 KodiPi sh[899]: lambda self, value: self._set_frequency(value),
Sep 22 09:56:43 KodiPi sh[899]: ^^^^^^^^^^^^^^^^^^^^^^^^^^
Sep 22 09:56:43 KodiPi sh[899]: File "/storage/.kodi/addons/virtual.rpi-tools/lib/gpiozero/pins/__init__.py", line 374, in _set_freq
Sep 22 09:56:43 KodiPi sh[899]: raise PinPWMUnsupported( # pragma: no cover
Sep 22 09:56:43 KodiPi sh[899]: gpiozero.exc.PinPWMUnsupported: PWM is not supported on pin GPIO18
~
~
~
~
KodiPi:~ #
Display More
I have tried to increase the sleep delay in autostart.sh to 60 and 120 but it does not make any difference.
Any idea ?