Page 1 of 1

Python-vlc, RasberryPi and looping script MP3 only plays one time

Posted: 10 Feb 2024 00:40
by musiceverywhere
The following is for a Gate Annunciator. IR Beam creates a contact closure, and MP3 file plays. However it only plays the first contact closure. After that the code runs a it should but no sound.... I'm lost any help is greatly appriciated!

Code: Select all

#!/usr/bin/env python3 import time import RPi.GPIO as GPIO import vlc import logging import signal import sys # Set up logging logging.basicConfig(filename='gate_announcer.log', level=logging.INFO) # Set up GPIO and the sensor GPIO.setmode(GPIO.BCM) sensor_pin = 14 # replace with your sensor's pin GPIO.setup(sensor_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set up LEDs led_pin_waiting = 22 # replace with your LED's pin led_pin_playing = 23 # replace with your LED's pin led_pin_sleeping = 24 # replace with your LED's pin GPIO.setup(led_pin_waiting, GPIO.OUT) GPIO.setup(led_pin_playing, GPIO.OUT) GPIO.setup(led_pin_sleeping, GPIO.OUT) # Set up VLC and load the mp3 AnnounceNorth = vlc.MediaPlayer ("NorthGateAnnounce.mp3") AnnounceSouth = vlc.MediaPlayer ("SouthGateAnnounce.mp3") Sleeping = vlc.MediaPlayer ("Sleeping_3_Hrs.mp3") # Variables to keep track of state last_announcement_time = 0 announcement_cooldown = 5 # 15 seconds contact_closures = 0 max_closures = 5 # replace with your desired number closure_window = 600 # replace with your desired number of seconds sleep_time = 3600 # 1 hour current_time = 0 #TRACKING Varable start_time = time.time() # Debouncing variables debounce_time = 0.05 # 50 milliseconds, adjust as needed last_input_state = GPIO.HIGH input_state = GPIO.HIGH def signal_handler(sig, frame): logging.info('Script terminated by user') GPIO.cleanup() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print ("Kevins Gate Controller Version 1") print ("Loading after reboot") print ("Waiting for Object...") while True: try: last_input_state = input_state input_state = GPIO.input(sensor_pin) if input_state != last_input_state: time.sleep(debounce_time) if input_state == GPIO.input(sensor_pin): if input_state == GPIO.HIGH: print ("Gate Triggered") current_time = time.time() print ("Trigger Allowed? " , current_time - last_announcement_time > announcement_cooldown) if current_time - last_announcement_time > announcement_cooldown: GPIO.output(led_pin_playing, GPIO.HIGH) print ("Announcing Gate Object") print ("Time since last Announcement was " , time.time() - last_announcement_time) AnnounceNorth.play() time.sleep(AnnounceNorth.get_length() / 1000) # wait for the mp3 to finish playing print ("Finished Gate Announcement") GPIO.output(led_pin_playing, GPIO.LOW) last_announcement_time = current_time contact_closures += 1 print ("Announcements within window " , contact_closures) if contact_closures >= max_closures and current_time - start_time < closure_window: print ("Sleeping...") print ("Playing Sleep Announcement") Sleeping.play() time.sleep(Sleeping.get_length() / 1000) # wait for the mp3 to finish playing print ("Sleep Announcement Concluded") GPIO.output(led_pin_sleeping, GPIO.HIGH) print ("Sleeping for 3 Hours...") time.sleep(sleep_time) print ("Sleep Time Satisfied") GPIO.output(led_pin_sleeping, GPIO.LOW) start_time = time.time() contact_closures = 0 else: GPIO.output(led_pin_waiting, GPIO.HIGH) time.sleep(0.5) GPIO.output(led_pin_waiting, GPIO.LOW) time.sleep(0.5) if current_time - start_time > closure_window: closure_window == 0 time.sleep(1) # check the sensor once every second except Exception as e: logging.error('An error occurred: %s', e)