1

I have a python script running with an RFID reader. The script runs fine when ran from terminal. When I add python /home/pi/script.py & to /etc/profile and reboot the Raspberry Pi, the scripts launches fine but gets stuck in this except:

while True:
#Setting up the RFID reader
    try:
        reader = mercury.Reader("tmr:///dev/ttyUSB0")
    break
except TypeError:
    print('Error, manual reset. Retrying')

Why can't I access the USB port when running the python script on boot in /etc/profile?

EDIT

  1. attempt to fix the issue. Instead of adding it to /etc/profile, add it to /etc/rc.local. The script won't run when doing this.

EDIT

I needed to sudo raspi-config and set the RPi to auto boot in desktop mode logged in as user pi. It now works!

Daniel
  • 113
  • 6

3 Answers3

2

There is no relation between /etc/profile and the boot process. Add your script to the init process.

Gerard H. Pille
  • 461
  • 2
  • 5
1

As @jsotola adviced, you could run it later in the boot process.... or you can write your script to keep on trying to open the port over and over again till it's available (with a certain delay of a few seconds between attempts so that you don't go into a tight loop).

Addendum: Apparently the tip that was followed was to start the script from rc.local... this is my full comment, just in case:

Another would be to add it to /etc/rc.local which is run at the end of the boot sequence, if I'm not mistaken. Just make sure that the process actually goes in the background so that the script doesn't hang (run it with &, for example). There might be other details to take care but it should take care of running it veery late on the boot process.

Hope that's good enough to anyone looking for an answer later on.

eftshift0
  • 800
  • 1
  • 7
  • 13
1

Start your script as service. First create a new service with:

rpi3 ~$ sudo systemctl edit --force --full rfid-reader.service

Insert this statements, save them and quit the editor:

[Unit]
Description=Setting up the RFID reader
Wants=sockets.target
After=sockets.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/script.py

[Install]
WantedBy=multi-user.target

Check the new service:

rpi3 ~$ systemctl status rfid-reader.service
● rfid-reader.service - Setting up the RFID reader
   Loaded: loaded (/etc/systemd/system/rfid-reader.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Now you can enable and test your service:

rpi3 ~$ sudo systemctl enable rfid-reader.service
rpi3 ~$ sudo systemctl start rfid-reader.service

I cannot test it because I have no RFID reader. You are alone with this. Here are some commands that may help:

rpi3 ~$ systemctl status rfid-reader.service
rpi3 ~$ systemctl cat rfid-reader.service
rpi3 ~$ sudo systemctl restart rfid-reader.service
rpi3 ~$ sudo systemctl edit --full rfid-reader.service

One problem is that I cannot check out for you the target after that /dev/ttyUSB0 is available. In the example above I use sockets.target. Don't know if this already initialised /dev/ttyUSB0. With

rpi3 ~$ systemctl

you will find what services and targets are active. Only look at the targets. Other candidates for Wants= and After= seem to be:

local-fs.target   Local File Systems
sysinit.target    System Initialization
basic.target      Basic System

If sockets.target does not work I would try other targets in this order.

Ingo
  • 42,961
  • 20
  • 87
  • 207