RPi running script at boot then stops

220 views Asked by At

I am running the following program when I boot up my Pi:

#This program will scan a bar code and if it matches a good bar code will         flash a light
#green or red depending on the validity of the coupon.

import sys, select, os
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

greenLED = 16
redLED = 12

GPIO.setup(greenLED, GPIO.OUT)
GPIO.setup(redLED, GPIO.OUT)
GPIO.output(greenLED, GPIO.LOW)
GPIO.output(redLED, GPIO.LOW)
goodBarCode = "0827112134023"

try:
    #Flash LED to test if script is running at RPi boot

    GPIO.output(greenLED, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(greenLED, GPIO.LOW)
    time.sleep(0.5)

    while(1):
        print ("Program is running")
        userBarCode = input("")

        if userBarCode == goodBarCode:
            GPIO.output(greenLED, GPIO.HIGH)    
            time.sleep(0.5)
            GPIO.output(greenLED, GPIO.LOW)
            time.sleep(0.5)

        else:
            GPIO.output(redLED, GPIO.HIGH)
            time.sleep(0.5)
            GPIO.output(redLED, GPIO.LOW)
            time.sleep(0.5)

except:
    GPIO.cleanup()

The objective I am trying to achieve is to turn on the Pi and have this script running and ready to scan barcodes without ever interacting with the Pi. I have successfully added the program to /etc/rc.local and the program begins on boot up. The issue I am having is that it seems to just immediately close the program after printing "Program is running" instead of waiting for an input, any advice would be great.

3

There are 3 answers

3
Ostap On

I think you just need a

raw_input()

instead of input(). See official docs, what you do is just like eval(raw_input("")). Also, any other details to help you solve? Does it just quit silently?

2
zezollo On

Well how do you know the programm really does close?

My guess is: the line:

userBarCode = input("")

is just waiting for you to type something, and nothing happens before you'll type something.

0
r_ahlskog On

/etc/rc.local is not really the place for that I think. rc.local is meant to run things that terminate, your program won't terminate, worse is that your program seems to expect keyboard input.

I would suggest sticking your program in /etc/inittab instead. replace the first line there with your program instead of /sbin/getty 38400 tty1

#
# Note that on most Debian systems tty7 is used by the X Window System,
# so if you want to add more getty's go ahead but skip tty7 if you run X.
#
1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
6:23:respawn:/sbin/getty 38400 tty6