0

I wrote a crappy Python script that should turn on LEDs connected to the GPIO interface.

import RPi.GPIO as GPIO
import time, sys, json

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
gpioPorts = [4, 9, 22]

for i in range(len(gpioPorts)):
    GPIO.setup(gpioPorts[i], GPIO.OUT)

pwm = {}
for i in range(1, len(sys.argv), 2):
    if sys.argv[i] == 4 or 9 or 22:
        if int(sys.argv[i + 1]) >= 5 and int(sys.argv[i + 1]) <= 100: 
            pwm["{0}".format(int(sys.argv[i]))] =  GPIO.PWM(int(sys.argv[i]), 120)
            pwm[sys.argv[i]].start(int(sys.argv[i + 1]))
        elif int(sys.argv[i + 1]) == 0:
            GPIO.output(int(sys.argv[i]), False)

def getStatus(gpioPorts):
    status = {}
    for i in range(len(gpioPorts)):
        if GPIO.input(gpioPorts[i]) == 0:
            status[gpioPorts[i]] = 0
        else:
            status[gpioPorts[i]] = 1
    return status

print(json.dumps(getStatus(gpioPorts)))

When I enter

%Run script.py 4 100 9 20 22 50

in Thonny IDE, it should turn on LED on the GPIO Port 4 with 100% brightness, LED 9 with 20% brightness and LED 22 with 50% brightness. I can change the brightness and everything works fine, but when I enter:

sudo python script.py 4 100 9 20 22 50

in LXTerminal the LEDs flicker and then go out directly. Why does my code work in Thonny but not in the terminal?

EDIT:

import RPi.GPIO as GPIO
import time, sys, json

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
gpioPorts = [4, 9, 22]

for i in range(len(gpioPorts)):
    GPIO.setup(gpioPorts[i], GPIO.OUT)

for i in range(1, len(sys.argv), 2):
    if sys.argv[i] == 4 or 9 or 22:
        if int(sys.argv[i + 1]) == 1: 
            GPIO.output(int(sys.argv[i]), True)
        elif int(sys.argv[i + 1]) == 0:
            GPIO.output(int(sys.argv[i]), False)

def getStatus(gpioPorts):
    status = {}
    for i in range(len(gpioPorts)):
        if GPIO.input(gpioPorts[i]) == 0:
            status[gpioPorts[i]] = 0
        else:
            status[gpioPorts[i]] = 1
    return status

print(json.dumps(getStatus(gpioPorts)))

I don't need

time.sleep(60)

when I'm not using PWM. I can run the same Code in Thonny and in the terminal. Why do I need "time.sleep ()" in the above code but not in the lower code?

0

2 Answers 2

1

You have not told the script to continue running. From the terminal it will run the script commands then stop.

Presumably in the Thonny IDE the script does not shut down properly.

Add something like a time.sleep(60) after the print statement to let the script run for 60 seconds.

6
  • Is there a possibility to keep the script running as long as the pi is turned on? I don't want to stop the script after 60 seconds.
    – Dniel97
    Commented Dec 22, 2017 at 14:59
  • Increase the delay to a large number of seconds or put the delay in a while loop.
    – joan
    Commented Dec 22, 2017 at 15:03
  • When I use a while loop, my php site can't execute the python script, without "crashing"
    – Dniel97
    Commented Dec 22, 2017 at 15:09
  • There is no mention of PHP in your question.
    – joan
    Commented Dec 22, 2017 at 15:29
  • 1
    I am reluctant to go any further down this route as the question is now different. However I will point out that RPi.GPIO PWM is generated by software so will only work while the RPi.GPIO script is running.
    – joan
    Commented Dec 22, 2017 at 17:41
-1

I just found out, that it is not possible to run a PWM python script "forever": https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=124281

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.