Python3 GPIO issues

Hi there…

So, I’ve just installed DietPi on my Raspberry Pi 4, having run Raspian previously. I have a python script for controlling the fan, but it doesn’t work in DietPi it seems. I have installed the RPi.GPIO module for python. Can anyone suggest what I’ll need to change. Script is below (feel free to steal if useful!)




import RPi.GPIO as GPIO
import time
import subprocess

ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature.
OFF_THRESHOLD = 50 # (degress Celsius) Fan shuts off at this temperature.
ledPin = 13 # define ledPin

def setup():
GPIO.setmode(GPIO.BOARD) # use PHYSICAL GPIO Numbering
GPIO.setup(ledPin, GPIO.OUT) # set the ledPin to OUTPUT mode
GPIO.output(ledPin, GPIO.LOW) # make ledPin output LOW level

def get_temp():
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.’)

def loop():
while True:
temp = get_temp()
strtemp = str(temp)
xtime = time.strftime("%c")
if temp > ON_THRESHOLD:
GPIO.output(ledPin, GPIO.HIGH)
f = open("/home/dietpi/fancontrol/fanlog.txt", “a”)
f.write(xtime)
f.write(" “)
f.write(strtemp)
f.write(“C”)
f.write(”\n")
f.close()
elif temp < OFF_THRESHOLD:
GPIO.output(ledPin, GPIO.LOW)
time.sleep(60) # Wait for x seconds

def destroy():
GPIO.cleanup() # Release all GPIO

if name == ‘main’: # Program entrance
print (‘Program is starting … \n’)
setup()
try:
loop()
except KeyboardInterrupt: # Press ctrl-c to end the program.
destroy()

Hi,

many thanks for your request. Did you activated serial console on dietpi-config?

Not sure if it helps but there was a user who tried to control a specific fan. He found a setup script on vendor page that install some stuff as well as set specific settings. Maybe you can have a look to the script. At the beginning your will find the relevant parts for setup and config.

https://dietpi.com/forum/t/argon-one-case-fan/4264/3