0

I have developed a code using Python that generates barcodes and interfaces a thermal printer to it. The barcode generation and then printing it is done using a button attached to the gpio.

Now, I am having trouble in automatically running this py script when i plug in the raspberry pi. I have spent 2 days trying nearly all approaches on the internet including adding the path to the file in

sudo nano /etc/profile

and adding the path to my script in the end. But every time I reboot the pi, the script doesn't run and the OS starts up.

Any suggestions?

1

If your script simply waits for a button to be pressed then does work and you are using Raspbian Jessie or Arch you should be able to accomplish this simply enough with Systemd units. If you follow the answer given in this SE post, correcting the various paths and users with the ones you are using, you might get what you are after.

Put the unit in /etc/systemd/system and start with sudo systemctl start what_you_called_the_unit and give it a test. It can be enabled on start up via sudo systemctl enable what_you_called_the_unit

However if you need the script to interact directly with the user in anyway, such as offering an on-screen menu from them to select from or accepting a command from the keyboard, this approach won't work.

The OS (Linux) will always be running, its what makes the Pi 'do stuff', I'm guessing you mean the desktop UI? If so, it won't hurt to leave it running, you could remove it (not sure what you are running to give any info) or use something like Raspbian Lite which is just a command line shell if you don't need or want it. Maybe get this working before moving on to this though.

I've butchered a GPIO sample to provide a simple example of what I mean.

Python Code put in /opt/myscript.py make executable sudo chmod +x /opt/myscript.py:

#!/usr/bin/python3

''' 
rubbish program that would monitor Button presses, and on the third
press would end will print horses when the button is pressed
sample taken, butchered, from 
https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/
'''

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)

prev_input = 0
presses = 0

while True:

    input = GPIO.input(17)

    if (not prev_input) and input:
        print("horses")
        presses += 1

    prev_input = input

    time.sleep(0.05)

    if presses > 2:
        break

Systemd Unit, put in /etc/systemd/system/myservice.service

[Unit]
Description=My horse printing script

[Service]
ExecStart=/opt/myscript.py

[Install]
WantedBy=multi-user.target

Then start the script via sudo systemctl start myservice if you follow the logs via sudo journalctl -u myservice you should see it write some 'starting' stuff then the word 'horses' for the first 3 times you press the button then it exits. This relies on you have a button connected to GPIO pin 17.

  • So you mean that if the code interacts with the GPIO i.e. user inputs, the method you mentioned in the first two paragraphs won't work? And if I am alright with leaving the os running in the background, what method May i use? will the method that makes use of rc.local help? – shantanu Jan 6 '17 at 10:54
  • That was a bit unclear, by user input i mean them directly interacting with the code, such as entering a command or something like that. If your script is just reacting to a GPIO event then it should be fine. – tobyd Jan 6 '17 at 10:59
  • 1. Just to be more clear, by "unit", you mean my code/path to the code right? 2. And, {/etc/systemd/system} is a folder. Am I to modify the Autologin@service file in that folder? – shantanu Jan 6 '17 at 11:11
  • a 'Unit' is what systemd calls the things it will start as a service, you'd create a new unit file in that folder telling systemd what you want it to do. Don't delete anything from that folder, just add a new file called [your-service].service in there containing the bits from the SE link – tobyd Jan 6 '17 at 11:18
0

this link might be helful to you Auto Login Setup

  • Welcome to Raspberry Pi! Please take the tour and visit the helpcenter. Whilst this may theoretically answer the question, it is preferable to include the essential parts of the answer and provide the link for reference. In fact we're enforcing this policy: please edit so that the answer stands on its own should the link break in the future. If it is not edited within 48 hours it will be converted to Community Wiki. – Ghanima Jan 6 '17 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.