Reputation: 1369
I'm controlling a LED on a raspberry pi 2 with Python. I want the LED to go on for x seconds. When I set an environment variable in Linux. For example, export t=5
. The LED goes on but won't go off.
If I just set the variable in the python script everything works fine.
I'm setting an environment variable in Linux like so:
export t=5
sudo python test.py
And getting it in Python like so:
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(12,GPIO.OUT)
GPIO.output(12,0)
s = 0
t = os.environ.get('t')
while s <= t:
if (GPIO.input(11) == 1):
GPIO.output(12, 1)
time.sleep(0.1)
s += 0.1
else:
GPIO.output(12, 0)
GPIO.output(12, 0)
Upvotes: 2
Views: 1815
Reputation: 57610
The values of environment variables — and thus the values of os.environ
— are stored as strings. Thus, you need to convert t
to a number in order for comparison with s
to do what you want:
t = int(os.environ.get('t'))
Upvotes: 5