kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

run script automatic

Thu Sep 27, 2018 9:57 am

How do i get my script to run when i open it, right now it open in Thonny where i need to click on the green arrow.
I trying to get the program to run automatic when the pi is connected to power.

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: run script automatic

Thu Sep 27, 2018 10:44 am

There are several ways. Can you say more about the nature of the script? There are several options but some are only appropriate for certain activities or timings without graphics:

cron: @reboot
/etc/rc.local
systemd unit files

If you need to run as a particular user in those contexts, "sudo -u whomever ..." works for the first two of those.

If you need graphics, which Window Manager or Desktop Environment do you have?

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Thu Sep 27, 2018 10:56 am

It is a counter program, I use 2 ir sensors to count on a racetrack, and some buttons to reset.
I have a Pi3b with a 3.5inch screen mounted on the racetrack.

The start of the program.

Code: Select all

import RPi.GPIO as GPIO
import time
import pygame

pygame.init()

win = pygame.display.set_mode((480, 320), pygame.FULLSCREEN)
bg = pygame.image.load('counterbgr.png')

GPIO.setmode(GPIO.BOARD)
GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)	#L1 time reset
GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 	#L2 time reset
GPIO.setup(35, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)	#start new race
GPIO.setup(37, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 	#stop program
GPIO.setup(38, GPIO.IN)                             	#L1 Count
GPIO.setup(40, GPIO.IN)					#L2 Count
 

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Thu Sep 27, 2018 11:00 am

I did try this:
Step 1– Create A Unit File
Open a sample unit file using the command as shown below:

sudo nano /lib/systemd/system/sample.service
Add in the following text :

[Unit]
Description=My Sample Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/sample.py

[Install]
WantedBy=multi-user.target
You should save and exit the nano editor.

Configure systemd Run a Program On Your Raspberry Pi At Startup

This defines a new service called “Sample Service” and we are requesting that it is launched once the multi-user environment is available. The “ExecStart” parameter is used to specify the command we want to run. The “Type” is set to “idle” to ensure that the ExecStart command is run only when everything else has loaded. Note that the paths are absolute and define the complete location of Python as well as the location of our Python script.

In order to store the script’s text output in a log file you can change the ExecStart line to:

ExecStart=/usr/bin/python /home/pi/sample.py > /home/pi/sample.log 2>&1
The permission on the unit file needs to be set to 644 :

sudo chmod 644 /lib/systemd/system/sample.service
Step 2 – Configure systemd
Now the unit file has been defined we can tell systemd to start it during the boot sequence :

sudo systemctl daemon-reload
sudo systemctl enable sample.service
Reboot the Pi and your custom service should run:

sudo reboot
Configure systemd Run a Program On Your Raspberry Pi At Startup
But getting a error
Failed to enable unit: ..... 'Invalid argument'

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Thu Sep 27, 2018 11:12 am

referring to the systemd, I change the code from:

Code: Select all

ExecStart=/usr/bin/python /home/pi/sample.py
to:

Code: Select all

ExecStart=/usr/bin/python3 /home/pi/sample.py
and the error message disappear, after reboot still nothing happens.

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Thu Sep 27, 2018 11:19 am

If i try to access the program from LXterminal direct i get this:

'access denied'

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: run script automatic

Thu Sep 27, 2018 11:44 am

If your script uses pygame to create a display it probably can't be run until the graphical desktop is up and running.

The configuration file ~/.config/lxsession/LXDE-pi/autostart is probably the right place to implement this.

But of course you will need a script that can be accessed and run before trying to automate it...

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Thu Sep 27, 2018 11:55 am

OK, first I will get running, I changed chmod to 755 and getting this error now:

Code: Select all

line 3: Import: command not found
line 4: Import: command not found
line 5: Import: command not found

line 9 syntaks "win"  
do I need full address to the imports?

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: run script automatic

Thu Sep 27, 2018 11:58 am

kelderkold wrote:
Thu Sep 27, 2018 11:55 am
OK, first I will get running, I changed chmod to 755 and getting this error now:

Code: Select all

line 3: Import: command not found
line 4: Import: command not found
line 5: Import: command not found

line 9 syntaks "win"  
do I need full address to the imports?
If that's in a Python script then you need to use 'import' (all lowercase)

BTW: if you post code or (error) messages it's best to post *full* messages.

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Thu Sep 27, 2018 12:02 pm

sorry they are lowercase, the script work fine when i start it in 'thonny'

My errors are in my local languages so i try to translate to english

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: run script automatic

Thu Sep 27, 2018 12:04 pm

kelderkold wrote:
Thu Sep 27, 2018 12:02 pm
sorry they are lowercase, the script work fine when i start it in 'thonny'

My errors are in my local languages so i try to translate to english
So where are these 'Import' commands? Are they in a Python script or maybe a Bash script?
Could it be that you are trying to run a Python script as a shell script?
Please post the code *and* the exact command you use to start the script.

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Thu Sep 27, 2018 12:14 pm

I try to start it as:

Code: Select all

ExecStart=/usr/bin/python3 /home/pi/Documents/LapMasterLight.py
Here is the code, or that you need to see what happens:

Code: Select all

import RPi.GPIO as GPIO
import time
import pygame

pygame.init()

win = pygame.display.set_mode((480, 320), pygame.FULLSCREEN)
#win = pygame.display.set_mode((480, 320), 0, 16)
bg = pygame.image.load('counterbgr.png')

GPIO.setmode(GPIO.BOARD)
GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #L1 time reset
GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #L2 time reset
GPIO.setup(35, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Startrace
GPIO.setup(37, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #stop program
GPIO.setup(38, GPIO.IN)                             #L1 Count
GPIO.setup(40, GPIO.IN)
.
.
.
ALL THIS IN THE MIDDLE I REMOVED
.
.
.
font = pygame.font.SysFont('none', 100)        
run = True
while run:

    GPIO.add_event_detect(38, GPIO.RISING, callback=Lane1, bouncetime=300)
    GPIO.add_event_detect(40, GPIO.RISING, callback=Lane2, bouncetime=300)
    GPIO.add_event_detect(31, GPIO.RISING, callback=L1reset, bouncetime=300)
    GPIO.add_event_detect(33, GPIO.RISING, callback=L2reset, bouncetime=300)
    GPIO.add_event_detect(35, GPIO.RISING, callback=Startrace, bouncetime=300)
    GPIO.add_event_detect(37, GPIO.RISING, callback=Stopprg, bouncetime=300)
    

    redrawGameWindow()


GPIO.cleanup()
print("Done")
pygame.quit()


tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: run script automatic

Thu Sep 27, 2018 12:27 pm

I find the documentation for systemd a bit unclear. But I've cargoculted systemd unit files a few times in the past for graphical applications. Here is a sample for the Chromium browser, which you might modify for your script once the other lumps are smoothed out. I think I've guessed correctly because it appears to work:

Code: Select all

[Unit]
Description=Chromium Under a Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes

[Service]
Type=simple
Environment="DISPLAY=:0.0"
ExecStart=/usr/bin/chromium-browser
ExecStop=/usr/bin/pkill -u pi -o -x chromium-browser
User=pi

[Install]
WantedBy=multi-user.target
It has to point to an X11 display and :0.0 is the default. It needs to run as a non-root user, pi in this case. And it has to wait for the other parts of the system to come up before it can start.

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Sun Sep 30, 2018 3:36 pm

right now i can't check this, I need to get the program running first, and have no clue on how to do.

Why can't the imports be found? do I need full adress to the files and if yes where do I find them?

Code: Select all

import RPi.GPIO as GPIO
import time
import pygame

pygame.init()

win = pygame.display.set_mode((480, 320), pygame.FULLSCREEN)
#win = pygame.display.set_mode((480, 320), 0, 16)
bg = pygame.image.load('counterbgr.png')

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: run script automatic

Sun Sep 30, 2018 4:28 pm

Post command you use to start the script and full error message(s)

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: run script automatic

Sun Sep 30, 2018 4:36 pm

I'm unable to duplicate the problem on Raspbian GNU/Linux 9.4 (stretch) using your script from today's post -- unless I (mis-)run it as a shell script instead of a python script. One way around that, and the common way to set up a script, is to have the shebang on the first line to identify the interpreter. So insert the following as the very first line:

Code: Select all

#!/usr/bin/python3
And then make the script executable with chmod. Then you can run it just with the following line, assuming the path and name are correct:

Code: Select all

/home/pi/sample.py
Please try that and say how it works or doesn't.

If that does not work, then we'll need some more context. Which distro do you have and which version of python3-rpi.gpio is installed on it?

Code: Select all

lsb_release -rd
apt-cache policy python3-rpi.gpio | head -n 7

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Mon Oct 01, 2018 9:51 am

Shebang line does it :D Thanks, now the program starts up but giving me a error on line 207, in module:

GPIO.add_event_detect(38, GPIO.RISING, callback=Lane1, bouncetime=300)
conflicting edge detection already enabled for this GPIO channel

my code for running the program is:

Code: Select all

font = pygame.font.SysFont('none', 100)        
run = True
while run:

    GPIO.add_event_detect(38, GPIO.RISING, callback=Lane1, bouncetime=300)
    GPIO.add_event_detect(40, GPIO.RISING, callback=Lane2, bouncetime=300)
    GPIO.add_event_detect(31, GPIO.RISING, callback=L1reset, bouncetime=300)
    GPIO.add_event_detect(33, GPIO.RISING, callback=L2reset, bouncetime=300)
    GPIO.add_event_detect(35, GPIO.RISING, callback=Startrace, bouncetime=300)
    GPIO.add_event_detect(37, GPIO.RISING, callback=Stopprg, bouncetime=300)
    

    redrawGameWindow()


GPIO.cleanup()
print("Done")
pygame.quit()
as far as I can see searching this error is that I detect inside the loop

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: run script automatic

Mon Oct 01, 2018 9:59 am

kelderkold wrote:
Mon Oct 01, 2018 9:51 am
as far as I can see searching this error is that I detect inside the loop
The problem is not that you're not detecting inside a loop. You're adding the same add_detect_event again and again because they're in a loop.

Move those 6 lines outside the loop and the errors should disappear.

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Mon Oct 01, 2018 10:10 am

I try but can't find out how to fix it

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: run script automatic

Mon Oct 01, 2018 10:43 am

kelderkold wrote:
Mon Oct 01, 2018 10:10 am
I try but can't find out how to fix it
:?: It's as simple as I said.

Code: Select all

font = pygame.font.SysFont('none', 100)        
run = True

GPIO.add_event_detect(38, GPIO.RISING, callback=Lane1, bouncetime=300)
GPIO.add_event_detect(40, GPIO.RISING, callback=Lane2, bouncetime=300)
GPIO.add_event_detect(31, GPIO.RISING, callback=L1reset, bouncetime=300)
GPIO.add_event_detect(33, GPIO.RISING, callback=L2reset, bouncetime=300)
GPIO.add_event_detect(35, GPIO.RISING, callback=Startrace, bouncetime=300)
GPIO.add_event_detect(37, GPIO.RISING, callback=Stopprg, bouncetime=300)

while run:
    redrawGameWindow()

GPIO.cleanup()
print("Done")
pygame.quit()

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Mon Oct 01, 2018 12:37 pm

Yes that was simple, and now I can start the program fra lxterminal, but it aint counting correct :(

I did try your suggestion but it is not starting up when rebooting
by tpyo kingg » Thu Sep 27, 2018 2:27 pm

I find the documentation for systemd a bit unclear. But I've cargoculted systemd unit files a few times in the past for graphical applications. Here is a sample for the Chromium browser, which you might modify for your script once the other lumps are smoothed out. I think I've guessed correctly because it appears to work:
Code: Select all

[Unit]
Description=Chromium Under a Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes

[Service]
Type=simple
Environment="DISPLAY=:0.0"
ExecStart=/usr/bin/chromium-browser
ExecStop=/usr/bin/pkill -u pi -o -x chromium-browser
User=pi

[Install]
WantedBy=multi-user.target
It has to point to an X11 display and :0.0 is the default. It needs to run as a non-root user, pi in this case. And it has to wait for the other parts of the system to come up before it can start.
I made these changes:

Code: Select all

 
[Unit]
Description=My Sample Service
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes

[Service]
Type=simple
Environment="DISPLAY=:0.0"
ExecStart=/usr/bin/python3 /home/pi/Documents/LapMasterLight.py
ExecStop=/usr/bin/pkill -u pi -o -x python3
User=pi

[Install]
WantedBy=multi-user.target

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: run script automatic

Mon Oct 01, 2018 12:55 pm

I'd try these since you are launching the script with /home/pi/Documents/LapMasterLight.py

Code: Select all

ExecStart=/home/pi/Documents/LapMasterLight.py
ExecStop=/usr/bin/pkill -u pi -o -x LapMasterLight.py
Then where whatever.service is the .service file for your script in the /lib/systemd/system/ service directory.

Code: Select all

sudo systemctl start whatever.service
Then, if and only if that works, then

Code: Select all

sudo systemctl enable whatever.service

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Mon Oct 01, 2018 1:02 pm

I found this and now i starts up - Thanks for all the help so far :D :D :D



Code: Select all

Code: Select all

nano /home/pi/.config/lxsession/LXDE-pi/autostart
Your entry in autostart should look something like this added to the end of what's there already:

Code: Select all

Code: Select all

@lxterminal -e python /home/pi/Documents/LapMasterLight.py

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: run script automatic

Tue Oct 02, 2018 5:22 am

Excellent. Remember that works as long as you are running LXDE for your desktop environment. If you switch to another DE or run a bare window manager some day then there will be a different file to change.

Code: Select all

@lxterminal -e /home/pi/Documents/LapMasterLight.py

kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

Re: run script automatic

Tue Oct 02, 2018 7:33 am

Thx.
The pi will never be used for anything else than this timer program.

I still have some issues i need to find out, in the first 5 minutes the pi 2 or 3 times restart the screen, it become black then blue and then it is back in normal, and the counting is still working, after some time it just keep working without problems.


The pi and the screen is built in this garagehouse on the track.
Image

Return to “Python”