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?
os.systemcall, sudo waits for the admin password to be entered, but it can't since it has no pipe to receive it from.sudoinstead 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.ifblock way down and the global var declared in the beginning...sudofrom those commands.