Now that I have my Raspberry Pi traffic light project auto running from start up. To do this I had to set it so that the Raspberry Pi auto logins. The process for doing this is described here. Then edit .bash_profile so the programs I want to start are started up from there. As I want to demonstrate this without the use of keyboard, mouse or monitor I was faced with the issue of being able to properly shutdown the Pi. I just wanted to start up the demo and be able to shut it down while in the field.
I have found the solution by adding a button to the project that shuts down the pi when pressed.
The Raspberry Pi user guide (issue 1) page 197 has details of the actual circuit and code required to detect a button press.
I then had the issue of how to run both programs at the same time the solution here was to background the shutdown.py process then run the traffic lights program.
To do this I edited the .bash_profile text file and added the following
pi@raspberrypi:~$ cat .bash_profile hostname -I echo "hello, backgrounding shutdown process" sudo python ./RaspberryPI/gpio/shutdown.py & echo "running traffic lights" sudo python ./RaspberryPI/gpio/traffic2.py
This starts up shutdown.py and then backgrounds the task, then starts the traffic lights program up.
so the code I used for the shutdown program is as follows
import RPi.GPIO as GPIO import time from time import sleep import os GPIO.setwarnings(False) GPIO.setmode (GPIO.BOARD ); GPIO.setup(23, GPIO.IN) #sswitch while True: input_value = GPIO.input(23) if input_value == False: print ("shuttimg dpwm pi") #time.sleep(5) os.system("sudo shutdown -h now") while input_value == False:
I found this article to help me with the shutdown routine.
then my traffic lights code is here for completeness
import RPi.GPIO as GPIO from time import sleep GPIO.setwarnings(False) GPIO.setmode (GPIO.BOARD ); GPIO.setup(11, GPIO.OUT) #red led GPIO.setmode (GPIO.BOARD ); GPIO.setup(13, GPIO.OUT) #yellow led GPIO.setmode (GPIO.BOARD ); GPIO.setup(19, GPIO.OUT) #green led #GPIO.output(11,True) #turn off leds off=[11,13,19] on=[11,13,19] #turn off leds for x in off: GPIO.output (x, False) i = 1 while i < 5: GPIO.output (11, True) # turn on red sleep(1) GPIO.output (13,True) # turn on amber sleep(2) GPIO.output (11, False) #turn off red GPIO.output (13, False) #turn off amber GPIO.output (19, True) #turn on green sleep(2) GPIO.output (19, False) #turn off green GPIO.output (13, True) # turn on amber sleep(1) GPIO.output(13, False) #turn off amber GPIO.output(11, True) #Turn on Red sleep(5) GPIO.cleanup()
This all works for me here, there is room for a few tweaks somewhere. But this is for later.