unable to play musicscript in systemd service

December 6, 2023, 15:42

yoni2003

If i run python3 motionsensor.py it works but if i run it through a systemd service it gives this error: Dec 06 16:36:05 raspberrypi python3[2244]: pygame 2.1.2 (SDL 2.26.5, Python 3.11.2) Dec 06 16:36:05 raspberrypi python3[2244]: Hello from the pygame community. https://www.pygame.org/contribute.html Dec 06 16:36:05 raspberrypi python3[2244]: Traceback (most recent call last): Dec 06 16:36:05 raspberrypi python3[2244]: File "/home/yoni/scripts/motionsensor.py", line 22, in <module> Dec 06 16:36:05 raspberrypi python3[2244]: pygame.mixer.init() Dec 06 16:36:05 raspberrypi python3[2244]: pygame.error: ALSA: Couldn't open audio device: Unknown error 524 this is my systemdfile: [Unit] Description=Motion Detector Service After=network.target apache2.service [Service] ExecStart=/usr/bin/python3 /home/yoni/scripts/motionsensor.py WorkingDirectory=/home/yoni/scripts StandardOutput=inherit StandardError=inherit Restart=always User=yoni SendSIGHUP = no [Install] WantedBy=multi-user.target thank you in advance!

yoni2003

this wil not help in my case

chopps568

what does line 22 say on your python code?

yoni2003

<@120274865973493761> pygame.mixer.init()

yoni2003

but if i run this command: python3 motionsensor.py everything works but if i use the service for automatisation it could't use the audio port on the pi for some reason.

chopps568

amixer cset numid=3 1
The last number is the audio output with 1 being the 3.5 jack, 2 being HDMI and 0 being auto. try that if not

yoni2003

<@120274865973493761> does it also work with pygame?

yoni2003

py
import subprocess
import time
import os
import random
import pygame
import requests

PIR_PIN = 21  # Change this to the actual GPIO pin number you are using for motionsensor

emotion_server_ip = ""

# Set the local folder containing MP3 files
base_folder = "/home/yoni"
emotion_folder = os.path.join(base_folder, "emotions", "happy")

# Initialize pygame for audio playback
pygame.init()
pygame.mixer.init()  

try:
    while True:
        # Run the gpio read command
        command = f'gpio read {PIR_PIN}'
        motion_detected = int(subprocess.check_output(command, shell=True))

        if motion_detected:
            print("Motion detected!")

            # Fetch emotion from the server (for now, it's always "happy")
            emotion = fetch_emotion_from_server()

            if emotion:
                print(f"Fetched emotion: {emotion}")

                emotion_folder_path = os.path.join(base_folder, "emotions", emotion)
                mp3_files = [f for f in os.listdir(emotion_folder_path) if f.endswith('.mp3')]

                if mp3_files:
                    # Select a random MP3 file
                    selected_file = random.choice(mp3_files)

                    # Construct the full path to the selected file
                    file_path = os.path.join(emotion_folder_path, selected_file)

                    # Play the selected MP3 file
                    pygame.mixer.music.load(file_path)
                    pygame.mixer.music.play()

                    while pygame.mixer.music.get_busy():
                        time.sleep(1)

                    print(f"Played: {selected_file}")
                else:
                    print(f"No MP3 files in the {emotion} folder.")
            else:
                print("Emotion not available.")

        else:
            print("No motion detected!")

        time.sleep(1)

except KeyboardInterrupt:
    pygame.quit()
    pass

yoni2003

solved

oops.se

And the solution is the steps above?