December 6, 2023, 15:42
amixer cset numid=3 1The last number is the audio output with 1 being the 3.5 jack, 2 being HDMI and 0 being auto. try that if not
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