Python3 GPIO issues

Have some feedback, questions, suggestions, or just fancy a chat? Pop it in here.
Post Reply
jlkpregan
Posts: 1
Joined: Thu Jul 09, 2020 3:43 pm

Python3 GPIO issues

Post by jlkpregan »

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()
User avatar
Joulinar
Posts: 2020
Joined: Sat Nov 16, 2019 12:49 am

Re: Python3 GPIO issues

Post by Joulinar »

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.

viewtopic.php?p=25852#p25852
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
Post Reply