I have been struggling for a while trying to make a .py file start on a raspberry pi bootup. the file includes opencv, tensorflow and picamera usage. When i try to start the program at boot using different methods it wont start.
The methods i've tried are:
/etc/rc.local (both trying to start the program directly or a .sh script that boots the program itself)
/home/pi/.config/autostart (either starting directly the program with "Exec= '/usr/bin/python3 /pathtoprogram.py') (or using an extern program called "xterm" to try and boot up the program but it fails to start the program)
.bashrc with (sudo python3 /pathtoprogram.py)
I'm atually looking for new methods. the program boots perfectly when i start it myself on the raspberry pi.
Additional info:
my program is a modified version of this one: https://www.tomshardware.com/how-to/raspberry-pi-facial-recognition
using the picam version.
Try systemd:
https://www.raspberrypi.org/documentation/linux/usage/systemd.md
"In order to have a command or program run when the Pi boots, you can add it as a service. Once this is done, you can start/stop enable/disable from the linux prompt."
Related
I'm trying to run a python script automatically when the raspberry pi 4 turns on, and I'm using crontab to do it. The script is a GUI that helps fill and search a database. the raspberry pi doesnt give any errors or exceptions, it just turns on normally and gives no signal of running the script.
the command I used in crontab is:
#reboot python3 /home/pi/Desktop/folder/gui.py &
the libraries that the script uses are:
tkinter
subprocess
pyserial
csv
thank you for your time, this is my first time working in the raspberry pi or a linux environment so I dont understand many things about it.
On the Raspberry Pi, the service that your program needs to use may not be activated. In your example, the Display service may not be activated.
Try to do the following:
#reboot sleep 60 && python3 /home/pi/Desktop/folder/gui.py &
Wait 60s (or more) after the Raspberry Pi starts, which may prevent the code from failing and help solve your problem.
Initial conditions: Raspberry Pi 3B running on latest Raspbian OS
Application: Python script recording audio through a USB Microphone and lighting up LEDS on the GPIOs ports.
Currently working: We need to wait for the Raspberry Pi OS fully up and running and then we launch the Python script from the command line.
What we need: As soon as we power up the Raspberry, we would like the unit to boot up and run our Python script directly without any extra UI input.
There are quite a few ways, see here. One of the ways would be to add the program to be run at startup to the init.d directory using the following lines:
sudo cp /home/pi/sample.py /etc/init.d/
Move to the init directory and open the sample script
cd /etc/init.d
sudo nano sample.py
My question regards SSH on Raspberry Pi.
I am able to successfully ssh on to the Pi using the command:
sudo ssh pi#<ipaddress>
and then entering the password.
Let's say I have a Python script file on the Pi that I execute over SSH. Let's say the script reads:
import time
while True:
print('Hello')
time.sleep(1)
This will print 'Hello' every second whilst the terminal/command prompt window is still open (that is, the computer I am using to access the Pi is running and the SSH session remains open). If I close the connection, then the code will stop being executed on the Pi.
Is there a way I can use SSH to keep the code running on the Pi even when I close the window running SSH on the computer I am using to access the Pi? As in the Pi will keep printing 'Hello' even after I shut down my computer. Maybe by entering a command to open a terminal window on the Pi itself and running the script in that terminal window?
Is there a way this can be done?
Thanks
There are two options I can think of:
create a cron job. This method is usually used to execute scripts/programs repeatedly. The job is triggered by the cron program, so it doesn't matter whether or not you are connected to the Pi, as long as it runs. You just have to connect once and setup the job (typically using crontab -e).
use screen (on Wikipedia) or tmux (on Wikipedia). Those are called terminal multiplexers, and allow you to keep shells (and thus any script/program) running although you aren't connected. Note that, in this case, you will have to manually start your script each time, so this solution is well-suited to scripts that run for a long time but are not started too often.
this is my first time using Raspberry Pi 3 with Python Tkinter to make a GUI.
I have written my script using Python. My code uses Raspberry Pi 3 GPIO 8 and GPIO 11 as inputs. I have 2 relay connected to the 2 inputs terminal. The relay will act as a switch. My code will count how many times the relay have switched on and display in the GUI.
I have tested it using Raspberry Pi 3 LxTerminal with
sudo python /home/pi/Desktop/test.py
The GUI opens and it works perfectly. Every time the relay switches, the count +1.
Then I decided to make it auto boot every time I reboot my Pi. I uses the following technique.
sudo nano /etc/profile
Then add the following code to the final line and save it.
sudo python /home/pi/Desktop/test.py
I reboot my Raspberry Pi and the GUI boot automatically. But the GUI does not response to the relay switching.
I tried rebooting a few times and it worked after a few reboot. Then I tried to reboot again and the next time it does not work again. This steps keep repeating over and over again. Some times the GUI work, some times it doesn't.
Anyone have come across this sorts of problem? What should I do to make the GUI auto boot to works 100%?
NOTE:
Having 5V 1A phone charger to supply power to Pi. (Tried using 5V 2.1A as well)
Connected a HDMI to VGA converter to connect to a monitor
Does not have keyboard and mouse when the GUI is set to auto boot
I have a tkinter GUI that boots on startup on my Raspberry Pi. I did it by adding it to autostart file.
First, make the script executable. Add
#!/usr/bin/python
as the first line of your python script.
Then use this command to make the file executable
chmod +x /home/pi/Desktop/test.py
Now you should be able to start your program without the "sudo python" prefix. Just typing /home/pi/Desktop/test.py in the terminal should boot your program.
Then open autostart file for editing with
nano /home/pi/.config/lxsession/LXDE-pi/autostart
and add
#/home/pi/Desktop/test.py
BTW: you are using "sudo" way to much. You should use sudo very rarely. If you edit user files with sudo you can screw up your file permissions, which leads to very strange bugs. You should need "sudo" when you are installing something, and that's about it.
First of all a Hardware recommendation: sorry for quoting a german page but as shown here https://www.elektronik-kompendium.de/sites/raspberry-pi/1912111.htm your raspi 3 should have minimun a power supply from 5v and 2A but it would be perfect if it supports up to 3A. But thats just some side info.
For me this question PyQt: How to run GUI on Raspberry Pi desktop startup? helped me solving the GUI autostart problem.
I am working on a robotics project with my lab. (Github) Most of the main components are done and documented. The setup is as follows:
We run opstn on the base station. It has a server used to communicate controller position to the robot.
We run rpi on a raspberry pi running on the robot. The two communicate via wifi and have a static address. The raspberry pi has a client program which requests the controller positions when it has time, decides what to run the motor.
The raspberry pi communicates with an mbed via USB to allow for hardware PWM.
The raspberry pi will also be working with I2C devices for monitoring data, and communicating it back to the base station for processing. This will eventually be run as a separate server process, and the base station will have a client process communicating with it.
The base station might need to have a client module to interpret the device data, running as a separate process.
The code does work, but we have to run it in a somewhat longwinded way:
On the base station, cd into kaginawa and python -m opstn, running opstn/__main__.py
SSH into the raspberry pi, cd into kaginawa and python -m rpi, running rpi/__main__.py
I was wondering how to properly launch submodules. For the rpi/__main__.py, I was thinking of providing two functions (the motor-controlling client and the device server) in __main__.py, and use multiprocessing to run them. Experiments seem to indicate that this would work.
What I want to do, though, is zip my directory into an executable, and then simply ./start_robot. This is what the Makefile does, and it works well. However, whatever I try, I seem to be unable to start opstn at all. And I will also need to work with the SSH too, since I will need to launch rpi with sudo. I can probably do that with paramiko using this reply.
If it is possible, I am thinking of using os.uname()[1] to check if it is on the raspberry pi and launch rpi, and opstn otherwise.
tl;dr How do you launch submodules that contain a __main__.py from the root __main__.py of a python package?
The toplevel directory in the project should not be a Python package. It could be a directory with setup.py instead.
You shouldn't cd inside Python package. Put your zip somewhere in PYTHONPATH (check by print(sys.path)) and use absolute names to run submodules e.g., run:
$ python -m kaginawa.opstn
to run kaginawa/opstn/__main__.py script or run:
$ python -m kaginawa.rpi
to run kaginawa/rpi/__main__.py.
If you want to choose what to run at runtime then you could call kaginawa.opstn.main() or kaginawa.rpi.main() functions in kaginawa/__main__.py. And run it:
$ python -mkaginawa
You could run the zip directly.
To simplify including dependencies, you could use PyInstaller, cx_Freeze to bundle your code.