« A Raspberry Pi Stratum 1 NTP Server | Main | About to update to RedHat Enterprise Linux or CentOS 7.7? Beware! »

Sunday, July 14, 2019

Using the Pimoroni Fan Shim with LibreElec


Trying out the latest LibreElec alpha on a Raspberry Pi 4 on the hottest day of the year so far I found the CPU temperature reaching 70ºC. A bit too warm for my liking, so I've added a Pimoroni Fan Shim.

There's no way you can install the Pimoroni python library on LibreElec, but there is an alternative.

First, you need to install the Raspberry Pi Tools addon in LibreElec.

Addons / install from repository / libreelec add-ons / program add-ons / Raspberry Pi Tools.

I found a script on https://forum-raspberrypi.de/forum/thread/43568-fan-shim-steuern/, and edited it slightly. Change the min and max temperatures to suit yourself.

ssh into your LibreElec box as root and

nano /storage/fanshim.py

#!/usr/bin/env python
# https://forum-raspberrypi.de/forum/thread/43568-fan-shim-steuern/
# place command below in /storage/.config/autostart.sh
#   nohup /storage/fanshim.py &
import os
import time
import signal
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import RPi.GPIO as GPIO
import subprocess
Pause = 30
CoreTempMax = 57
CoreTempMin = 46
GPIO_Pin = 18
Run_Fan_function = False
def init():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(GPIO_Pin, GPIO.OUT)
    return()
def Set_Fan_ON():
    GPIO.output(GPIO_Pin, True)
    return()
    
def Set_Fan_OFF():
    GPIO.output(GPIO_Pin, False)
    return()
def get_CPU_Temp():
    temp = subprocess.check_output(['vcgencmd', 'measure_temp'])[5:-3]
    return temp
    
def Watch_Temp():
    global Run_Fan_function
    CPU_Temp = float(get_CPU_Temp())
    if Run_Fan_function==False and CPU_Temp>=CoreTempMax:
        Run_Fan_function = True
        Set_Fan_ON()
    if Run_Fan_function==True and CPU_Temp<=CoreTempMin:
        Run_Fan_function = False
        Set_Fan_OFF()
    return();
try:
    init() 
    while True:
        Watch_Temp()
        time.sleep(Pause)
except KeyboardInterrupt:
    GPIO.cleanup()

Save the edited file, and then:

chmod +x /storage/fanshim.py

Then edit LibreElec's autostart.sh

nano /storage/.config/autostart.sh so it contains the line:

nohup /storage/fanshim.py &

Reboot your Pi 4 for it to take effect.

If you're using a TV Hat, you'll need an extra tall stacking header to give a good gap between the fan and the TV Hat.

Enjoy.

Postscript, October 26, 2019

Someone has done a bit of work on this and turned it into a proper installable Kodi addon:

https://github.com/jane-t/rpi-fanshim

Thanks!


Posted by Phil at 10:06 PM
Edited on: Saturday, October 26, 2019 4:27 PM
Categories: IT, Raspberry Pi