0

I want to show the status on a Raspberry PI using a LED. To do this I have a program in Python that will run class methods in background using threads.

This is the code I have written:

#!/usr/bin/python

import time
import os
import threading

status = 0

class LEDStatus(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        global status
        self.current_value = None
        self.running = True 
    def run(self):
        global status

        os.system("echo gpio | sudo tee /sys/class/leds/led0/trigger")

        while True:

            for i in xrange(0, status):
                os.system("echo 1 | sudo tee /sys/class/leds/led0/brightness")

                time.sleep(0.25)

                os.system("echo 0 | sudo tee /sys/class/leds/led0/brightness")

                time.sleep(0.25)

            time.sleep(2)

        os.system("echo mmc0 | sudo tee /sys/class/leds/led0/trigger")



if __name__ == '__main__':
    leds = LEDStatus()
    try:
        leds.status = 3
        leds.start()

        while True:
            print "X"
            time.sleep(2)

    except (KeyboardInterrupt, SystemExit): #Al pulsar ctrl+c
        print "\nFinish"
        leds.running = False
        leds.join()

After the program comes to this line os.system("echo gpio | sudo tee /sys/class/leds/led0/trigger"), it doesn't execute anything in the run method.

What am I doing wrong? How can I change the value of the status variable from the main method?

15
  • You are trying to use sudo in a os.system call, sudo waits for the admin password to be entered, but it can't since it has no pipe to receive it from. Commented Mar 15, 2016 at 6:47
  • Depends on sudo configuration, NOPASSWD might be at play. Or not. It does sound like it is the sudo though. Commented Mar 15, 2016 at 6:49
  • 1
    If all the stuff behind the first call requiring sudo is not necessary, it's not a minimal example. Reduce your code to one before posting. Now, two more things: Firstly, you could call your program with sudo instead of trying to elevate privileges in the middle. Secondly, you should be able to configure access to the GPIOs so that you don't need root privileges. Also, upgrade to Python 3, it's stable and well-supported. Commented Mar 15, 2016 at 6:57
  • 1
    @UlrichEckhardt OP did mention that the sudo actually worked fine. The problem turned out to be the mixture of global variables and instance variables. That mix was visible outside of the "infinite loop", since said variable was set in the if block way down and the global var declared in the beginning... Commented Mar 15, 2016 at 7:13
  • 1
    @ozat especially do not remove the code that demonstrates the problem. I've rollbacked your edit that removed sudo from those commands. Commented Mar 15, 2016 at 7:15

1 Answer 1

4

Running sudo in os.system() call may block. It will sit there waiting for user input (the password). This depends on how sudoers file has been set up.

Your code also mixes global variables and class attributes. The status global variable is never updated, since you assign self.status = 3 instead of status = 3.

Using global variables like this is a bit frowned upon, since you might as well really use a class attribute here. Change your __init__ to take initial status as an argument.

def __init__(self, status):
    threading.Thread.__init__(self)
    self.status = status
    self.current_value = None
    self.running = True

and in your run method use self.status:

for i in xrange(0, self.status):
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Ilja , I was a little confused , it works fine now
The problem was caused by the global variable status

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.