Temperature controlled fan using GPIO on rpi 3

Hello,

I am trying to create a background service which should start when libreelec starts and monitor the CPU temperature. If the temperature goes beyond certain limit switch the fan ON. I have almost got all the hardware ready and connected to the GPIO (pin 18).

Below is the script I manage to create so far. Please let me know if this will work or not and specifically if this is fine or not. I am planning to install it as a service addon uisng zip file.

Lately the temperature on my Rpi 3 (Libreelec 7.0.3) goes above 70 which was not the case before. I am running moderate overclock settings. Also, have been running transmission, couchpotato and sonarr along with Kodi. Also, during startup I get the yellow warning of temperature which I believe is displayed when CPU is above 80. Can anybody point me in correct direction how to find out which service or addon is causing the pi to overheat so much?

I am newbie in linux, but can follow steps if provided. Have learned couple of tricks so far Smile

fan.py

Code:
#!/bin/python
# Author: Arpit

import os
from time import sleep
import signal
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import RPi.GPIO as GPIO

pin = 18 # The pin ID, edit here to change it
maxTMP = 55 # The maximum temperature in Celsius after which we trigger the fan

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pin, GPIO.OUT)
    GPIO.setwarnings(False)
    return()
def fanON():
    GPIO.output(pin, True)
    return()
def fanOFF():
    GPIO.output(pin, False)
    return()
def getTEMP():
    res = os.popen(‘vcgencmd measure_temp’).readline()
    temp =(res.replace(“temp=”,””).replace(“’C\n”,””))
    #print(“temp is {0}”.format(temp)) #Uncomment here for testing
    CPU_temp = float(temp)
    if CPU_temp>maxTMP:
        fanON()
    else:
        fanOFF()
    return()
try:
    setup()
    while True:
        getTEMP()
sleep(10) # Read the temperature every 10 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
    GPIO.cleanup() # resets all GPIO ports used by this program

addon.xml

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.gpio.temperaturecontrolledfan" name="Temperature Controlled Fan" version="1.0.0" provider-name="ArpitG">
    <requires>
<import addon="xbmc.python" version="2.20.0"/>
</requires>
<extension library="fan.py" point="xbmc.service" start="login"/>
<extension point="xbmc.addon.metadata">
<summary lang="en">Temperature Controlled Fan</summary>
<description lang="en">Driver script for GPIO controlled fan</description>
<disclaimer lang="en">This plugin requires hardware fan connected by GPIO.</disclaimer>
<platform>all</platform>
</extension>
</addon>


The circuit diagram is as follows. The DC motor is actually a fan. Never connect motor to rpi like this.
watch gallery


Thank you.

Edit: Cannot install the addon. What am I missing here?

Fanart not working…

Hi last time I dith a fresh kodi 16.1 install on my sd card (using LibreELEC image for my RaspBerry Pi2) Add my movies and tv shows and that works great, but after adding my music collection and update the music library. When playing music most off the...

Maybe a memory leak problem with kodi

Well, this is just a heads up, I will report back later on when Ill get the next reboot on kodi.

Its seems to me that kodi have a mem leak problem, its take about 2-4% of mem everytime you start a new movie, so the memory usage to kodi increase, and when it have increase to a sudden number, kodi crash, and when its starts up again the memory usage is reset.

I will post log, and debug info as soon this happens again. (kodi is not at 1G mem usages), så i guess at 2G usage it crash again.

By the way using the default skin that came with kodi 17

Control Kodi 17 on iPad 1

On my iPad 1 I have the Kodi remote app installed and I can connect to Kodi 16 (on an old laptop) but not to Kodi 17 (on Nvidia Shield). I can control Kodi 17 with an app on my HTC 10. The app cannot be updated on the iPad, so I wonder if it is still ...

Having problems with the new joystick implementation

I am having some difficulties with the new joystick implementation of Kodi 17. I have been using my 360 controller to control Kodi ever since the XBMC days with no major problems ever, but after Kodi 17 I am having a major problem. I have my PC connec...

Kodi for chromebook?

Will they ever add kodi for chromebook because my school blocks everything cant even watch Netflix at home. Thanks for your time

No audio

Suddenly, I no longer have any audio on my KODI.. There is a red speaker in the top right hand corner and when I go into a stream the audio says "audio undetermined". I am not able to change the audio output device in system settings and I have watched...

Black level issues

I calibrated the black level with the AVS 709 disc, and video looks great. However, sometimes, the black level will go haywire, becoming very elevated, and requiring a restart.

After Update on kodi 17 problem with 1080p exodus

Hi guys i have problem, i update my kodi on pc from 16.1 to 17 after that i cant find all the movies and tv series on 1080p resolution like the previous version of Kodi. For example i watching now on exodus the braking bad tv series and i watching it i...

Crashes are all over the place

I am beyond frustrated. I've have spent most of the last three days trying everything I can to figure out how to get Kodi to work. I had little to no problems with Kodi 16, but in a touch of daring I decided to upgrade for the improvements I read so mu...