Installare la ventola Melopero su Raspberry Pi 4 con LibreELEC

Ieri un amico mi ha chiesto aiuto per far funzionare la ventola Melopero su un Raspberry Pi 4 con LibreELEC.

La procedura di installazione della ventola Melopero è stata concepita per Raspbian e questo è lo script python che si occupa di controllarla.

Per far funzionare la ventola su LibreELEC occorre eseguire i seguenti step:

  • Installare in LibreELEC l'Addon Raspberry Pi Tools
  • Collegarsi via ssh al Raspberry

ssh root@raspberry_ip

la password di default è: libreelec

  • Verificare che tutte le librerie di Raspberry Pi Tools siano state installate correttamente:

ls -la /storage/.kodi/addons/virtual.rpi-tools/lib

  • Scaricare lo script python che si occupa di avviare e gestire la ventola:

mkdir /storage/.config/fan_controller

cd /storage/.config/fan_controller

wget https://raw.githubusercontent.com/melopero/Melopero_FAN_HAT/master/melopero-fan-hat/fan_controller.py

  • Modificare lo script python per indicargli il percorso delle librerie installate da Raspberry Pi Tools:

nano /storage/.config/fan_controller/fan_controller.py

aggiungendo all'inizio dello script:

import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')

in modo che lo script sia così:

#!/usr/bin/env python3
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import os
import time
import RPi.GPIO as GPIO
    

minimum_always_ON=True
minimum_speed=30
target_temp=52
DEBUG=False
current_speed=0



def getCPUtemperature():
    global current_speed
    res = os.popen('vcgencmd measure_temp').readline()
    temp =(res.replace("temp=","").replace("'C\n",""))
    if(DEBUG):
        print("temp is {0}, current speed: {1}".format(temp,current_speed)) #Uncomment here for testing
    return temp

try:
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18, GPIO.OUT)
    myPWM=GPIO.PWM(18,50)
    myPWM.start(minimum_speed)
    current_speed=minimum_speed
    while True:
        temp = float(getCPUtemperature())
        if(temp<target_temp and not minimum_always_ON):
            myPWM.ChangeDutyCycle(0)
            current_speed=0
            time.sleep(1)
            continue
        if(temp<target_temp and minimum_always_ON):
            myPWM.ChangeDutyCycle(minimum_speed)
            current_speed=minimum_speed
            time.sleep(1)
            continue
        
        if(temp>target_temp and temp<56):
            myPWM.ChangeDutyCycle(40)
            current_speed=40
            time.sleep(1)
            continue
        if(temp>56 and temp<60):
            myPWM.ChangeDutyCycle(50)
            time.sleep(1)
            continue
        if(temp>60 and temp<65):
            myPWM.ChangeDutyCycle(60)
            current_speed=60
            time.sleep(1)
            continue
        if(temp>65 and temp<70):
            myPWM.ChangeDutyCycle(70)
            current_speed=70
            time.sleep(1)
            continue
        if(temp>70 and temp<74):
            myPWM.ChangeDutyCycle(80)
            current_speed=80
            time.sleep(1)
            continue
        if(temp>74 and temp<76):
            myPWM.ChangeDutyCycle(90)
            current_speed=90
            time.sleep(1)
            continue
        if(temp>76):
           #handleFan(100)
            myPWM.ChangeDutyCycle(100)
            current_speed=100
            time.sleep(1)
            continue
            
        
        

except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt 
    GPIO.cleanup() # resets all GPIO ports used by this program
  • Creare il file autostart.sh e fargli avviare lo script python che si occupa di controllare la ventola:

touch /storage/.config/autostart.sh

chmod +x /storage/.config/autostart.sh

nano /storage/.config/autostart.sh

aggiungendo al suo interno:

(python /storage/.config/fan_controller/fan_controller.py)&

  • Ora non resta che riavviare il Raspberry e goodersi la ventola in funzione:

reboot