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
But getting a errorStep 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
Code: Select all
ExecStart=/usr/bin/python /home/pi/sample.pyCode: Select all
ExecStart=/usr/bin/python3 /home/pi/sample.pyCode: Select all
line 3: Import: command not found
line 4: Import: command not found
line 5: Import: command not found
line 9 syntaks "win" If that's in a Python script then you need to use 'import' (all lowercase)kelderkold wrote: ↑Thu Sep 27, 2018 11:55 amOK, first I will get running, I changed chmod to 755 and getting this error now:
do I need full address to the imports?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"
So where are these 'Import' commands? Are they in a Python script or maybe a Bash script?kelderkold wrote: ↑Thu Sep 27, 2018 12:02 pmsorry 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
Code: Select all
ExecStart=/usr/bin/python3 /home/pi/Documents/LapMasterLight.pyCode: 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()
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
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')
Code: Select all
#!/usr/bin/python3
Code: Select all
/home/pi/sample.py
Code: Select all
lsb_release -rd
apt-cache policy python3-rpi.gpio | head -n 7
conflicting edge detection already enabled for this GPIO channel
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()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.kelderkold wrote: ↑Mon Oct 01, 2018 9:51 amas far as I can see searching this error is that I detect inside the loop
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()I made these changes: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.
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
Code: Select all
ExecStart=/home/pi/Documents/LapMasterLight.py
ExecStop=/usr/bin/pkill -u pi -o -x LapMasterLight.py
Code: Select all
sudo systemctl start whatever.service
Code: Select all
sudo systemctl enable whatever.service
Code: Select all
nano /home/pi/.config/lxsession/LXDE-pi/autostartCode: Select all
@lxterminal -e python /home/pi/Documents/LapMasterLight.pyCode: Select all
@lxterminal -e /home/pi/Documents/LapMasterLight.py
