I've bought my first Raspberry Pi 3B+ last week and started working on my project but I seem to be stuck at this point. I've looked through a hundred different posts but none of the solutions seem to work for me.
Brief info on what I'm trying to accomplish: A python script the listens for button presses needs to be started at boot (regardless of if I'm booting in headless mode or full desktop) and needs to be able to control MPD playback (python-mpd2)
I've tried: - adding 'python /home/pi/Documents/Jukebox/Listener.py' to /etc/rc.local while enabling rc.local and making it executable - creating a service 'listener.service' in the /etc/systemd/system/ folder with the following code:
[Unit]
Description=start listening to button presses
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/Documents/Jukebox/Listener.py
[Install]
WantedBy=multi-user.target
My python script is very basic and just assign the GPIO inputs, listens to changes and executes a MPD command:
#!/usr/bin/python
import RPi.GPIO as GPIO
import mpd
import time
from toggleplayback import toggleplaypauze
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,pull_up_down = GPIO.PUD_DOWN)
GPIO.add_event_detect(17, GPIO.RISING, bouncetime = 200)
GPIO.setup(18,GPIO.IN,pull_up_down = GPIO.PUD_DOWN)
GPIO.add_event_detect(18, GPIO.RISING, bouncetime = 200)
GPIO.setup(20,GPIO.IN,pull_up_down = GPIO.PUD_DOWN)
GPIO.add_event_detect(20, GPIO.RISING, bouncetime = 200)
client = mpd.MPDClient()
client.connect("localhost",6600)
def toggleplayback(self):
status=client.status()
print(status['state'])
if (status['state'] == "pause"):
client.pause(0)
elif (status['state'] == "stop"):
client.play(1)
else :
client.pause(1)
def nextsong(self):
client.next()
def previoussong(self):
client.previous()
GPIO.add_event_callback(17,toggleplayback)
GPIO.add_event_callback(18,nextsong)
GPIO.add_event_callback(20,previoussong)
while True:
time.sleep(0.01)
When I test the service by enabling it and starting it, it works perfectly. However, when I restart the service is not started by default and if I check the status of the service I get the following error:
pi@raspberrypi:~ $ systemctl status listener.service
listener.service - start listening to button presses
Loaded: loaded (/etc/systemd/system/listener.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2019-01-13 16:04:44 CET; 42s ago
Process: 398 ExecStart=/home/pi/Documents/Jukebox/Listener.py (code=exited, status=1/FAILURE)
Main PID: 398 (code=exited, status=1/FAILURE)
jan 13 16:04:44 raspberrypi Listener.py[398]: File "/home/pi/Documents/Jukebox/Listener.py", line 14, in <module>
jan 13 16:04:44 raspberrypi Listener.py[398]: client.connect("localhost",6600)
jan 13 16:04:44 raspberrypi Listener.py[398]: File "/home/pi/.local/lib/python2.7/site-packages/mpd/base.py", line 641, in connect
jan 13 16:04:44 raspberrypi Listener.py[398]: self._sock = self._connect_tcp(host, port)
jan 13 16:04:44 raspberrypi Listener.py[398]: File "/home/pi/.local/lib/python2.7/site-packages/mpd/base.py", line 604, in _connect_tcp
jan 13 16:04:44 raspberrypi Listener.py[398]: raise err
jan 13 16:04:44 raspberrypi Listener.py[398]: socket.error: [Errno 111] Connection refused
jan 13 16:04:44 raspberrypi systemd[1]: listener.service: Main process exited, code=exited, status=1/FAILURE
jan 13 16:04:44 raspberrypi systemd[1]: listener.service: Unit entered failed state.
jan 13 16:04:44 raspberrypi systemd[1]: listener.service: Failed with result 'exit-code'.
I'm now officially out of ideas on what to do. If anyone can give me a new idea or an explanation about why what I'm trying to do is wrong and how I should be doing this, please advise!
Thanks
sudo netstat -tlnpu | grep 6600
– Dougie Jan 13 '19 at 20:19Wants=xxx.service
dependency so that your script starts after the mpd.service is running. – Dougie Jan 14 '19 at 0:53