Raspberry Pi won't run script on boot

847 views Asked by At

The program will use a barcode scanner to verify weather a coupon is good or not and will flash either a red or green light depending on if the coupon is valid.

The way I want this to work is to turn on the Pi and then immediately be able to scan coupons. All the Pi has to do is begin the program once I turn on the Pi and remain on and that's all. I am using crontab to begin the following program:

#Adam Giancola 
#June 5th 2015

#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):
        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()

This codes works fine if I run it through the terminal using "sudo python3 ...blah blah blah" , but I have used crontab to start the program at boot and I am getting no feedback from the LEDs. I know my crontab configuration is working because I have run other programs along side and they are working. Why won't the LEDs respond?

1

There are 1 answers

1
fleed On

The problem is that the barcode scanner is pretending to be a keyboard. When you run it from the console, whatever the barcode scanner outputs will go to your program as it is the one currently reading on stdin for the console.

When you run it in the background, through rc.local, cron, init script or whatever other method, your program will not be on the console, it will be in the background, so it will not "see" what the barcode scanner types into the keyboard.

Probably the best short term solution for you is to enable auto-login on the console (the GUI has to be off) and call your program right after login. Full explanation here, short version:

Edit /etc/inittab and change the corresponding line to:

 1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

Edit /home/pi/.bash_profile and add:

 sudo python3 bla bla bla