Tkinter GUI does not run properly when auto boot - python

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.

Related

How would I open and run a python program on a raspberry pi from a main python program on a windows pc?

I am running a main python program on a Windows PC that is hooked to equipment that cannot be ran on an Raspberry pi. At a certain point in the main program, I want to call/execute a Rpi program to run. I need the GPIO pins from the Rpi to turn on a relay/s. Is there a way to wirelessly(or serially) open and run the program on the raspberry pi from the main program already running on the Windows PC?
Maybe I am not thinking of something, is there an easier and just as cheap solution to turn on a relay from the Windows PC program?
Any points in the right direction would be greatly appreciated.
The easiest way to do this is with Remote GPIO which is all documented at that link. However, the gist of it is pretty simple as follows.
On the Raspberry Pi, run these steps:
sudo apt install pigpio
sudo raspi-config and enable "Remote GPIO"
sudo systemctl enable pigpiod
On the Windows PC, run these steps:
pip install gpiozero pigpio
Assuming your RaspberryPi has IP address 192.168.1.3, run PIGPIO_ADDR=192.168.1.3 python3 YourScript.py
Your script on Windows PC would then look like this:
from gpiozero import LED
from time import sleep
red = LED(17)
while True:
red.on()
sleep(1)
red.off()
sleep(1)
depending on security requirements. Assuming that the Desktop PC and raspberry pi are on the same network, you could create an HTTP REST endpoint on the pi, you could use flask or fastapi for this. then call that from the app running on the desktop. for help with flask see https://flask.palletsprojects.com/en/2.0.x/ if you are familiar with python flask is fairly simple to get started with.

Unable to start when bootup of raspberry pi a .py program

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."

crontab doesnt run python script on a raspberry pi 4

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.

Set up a Raspberry Pi 3B+ to boot up and run a Python script immediately

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

lightdm startup script to change locale on raspberry pi with curses

I've written a script that uses python curses to allow me to change the locale (edits /etc/default/locale) on the raspberry pi. It has a basic menu that allows me to select the language.
I want to run this script before the lxde session starts. I've tried editing the /etc/lightdm/lightdm.conf file but when I do, the pi doesn't boot at all, it just has a black screen with a blinking cursor.
Am I approaching this the right way? Is there a better way to run an interactive python curses before lxde starts?
Try using crontab in Linux. You could launch the program at booting:
https://en.wikipedia.org/wiki/Cron

Categories