0

I've been doing a lot of reading but have not found any answers to my question which is.... How can I fix this script? RE: EP-0152

#!/usr/bin/python3

import RPi.GPIO as GPIO import time import subprocess

GPIO.setmode(GPIO.BCM) GPIO.setup(14, GPIO.OUT) pwm = GPIO.PWM(14,100)

print("\nPress Ctrl+C to quit \n") dc = 0 pwm.start(dc)

try: while True: temp = subprocess.getoutput("vcgencmd measure_temp|sed 's/[^0-9.]//g'") if round(float(temp)) >= 45: dc = 100 pwm.ChangeDutyCycle(dc) print("CPU Temp:",float(temp)," Fan duty cycle:",dc) time.sleep(180.0) if round(float(temp)) >= 40: dc = 85 pwm.ChangeDutyCycle(dc) print("CPU Temp:",float(temp)," Fan duty cycle:",dc) time.sleep(120.0) else: dc = 70 pwm.ChangeDutyCycle(dc) print("CPU Temp:",float(temp)," Fan duty cycle:",dc) time.sleep(60.00)

except KeyboardInterrupt: pwm.stop() GPIO.cleanup() print("Ctrl + C pressed -- Ending program")

I receive multiple errors while trying to run this, such as

error: 'GPIO not allocated'
lgpio.py, line 458+ 

Any help would be very much appreciated.

Greenonline
  • 2,969
  • 5
  • 27
  • 38
Smitty
  • 1
  • 2

1 Answers1

1

Mods are going to grieve me for this post. .. The answer to your question is debugging, and debugging is a process, not a nice clean answer.

which Pi? did you look at line 458 of the lgpio.py script?

There are other means to get cpu temp from your pi, you could run cat /sys/class/thermal/thermal_zone0/temp which returns milli-celcius, and leaning to a more canonical python type approach would be simpler to process without funny bash scripts in your code, as you could just read the file, and process the data.

You could add print("") statements, I don't think your code makes it to temp before it fails. you need to ID the point of your code that is failing.

So you want to dubug your python code. First, open a bash command prompt, normal user, no root. run:

vcgencmd measure_temp

This command running properly as a non root user is the central thesis of your script performing properly, so you should make sure that the command is indeed operational to begin with.

If it returns a valid data output, continue. If it says permission denied, or gives you any weird results, start figuring it out, or just don't use it at all.

A more canonical python approach to getting cpu temp:

#!/usr/bin/python3

#import RPi.GPIO as GPIO import time import subprocess

#GPIO.setmode(GPIO.BCM) #GPIO.setup(14, GPIO.OUT) #pwm = GPIO.PWM(14, 100)

print("\nPress Ctrl+C to quit \n") #pwm.start(0) # Start with fan off

def get_cpu_temp(): try: with open("/sys/class/thermal/thermal_zone0/temp", "r") as f: temp_millicelsius = int(f.read().strip()) return temp_millicelsius / 1000.0 except (IOError, ValueError): print("Error reading temperature") return 0

try: while True: temp = get_cpu_temp()

    if temp >= 45:
        dc = 100
    elif temp >= 40:
        dc = 85
    elif temp >= 35:
        dc = 70
    else:
        dc = 0  # Fan off below 35°C

    #pwm.ChangeDutyCycle(dc)
    print(f"CPU Temp: {temp:.1f}°C, Fan duty cycle: {dc}%")
    time.sleep(10.0)

except KeyboardInterrupt: #pwm.stop() #GPIO.cleanup() print("\nCtrl + C pressed -- Ending program")

See if you can run this script. it just reports cpu temp. if it works ok, then add in the GPIO by commentating the pwm and GPIO related lines, and see if it breaks. Do it nice and systematically, to see where, if it fails.