Pirate Radio VLC play/pause cron command

Hello Pi Gurus, :wave:

I’ve set up the Pirate Radio kit with the VLC and added some online radio’s into it. All works fine and the VLC is on 24/7.

I’ve been trying to figure out, how to create a cron job to send play/pause to VLC. Haven’t found any answer on the internet or my skill is too low to recognize the solution. Is there a simple command or script I could use to automatically play/pause the VLC?

I could probably just kill the VLC and execute a new instance to start it anew. But I want to have also ability to play/pause anytime between.

Automated scenario should be:
weekdays start at 8am and stop at 10am, start at 4pm stop at 10pm
weekends start at 8am and stop at 10pm
have ability to play/pause the radio anytime between

Thanks a bunch.
Humbly yours, noob.

Does this help.

vlc.communicate("voldown") # Turn the volume down one step
vlc.communicate("volup") # Turn the volume up one step
vlc.communicate("next") #  Next track
vlc.communicate("prev") # Prev track
vlc.communicate("pause") # Pause/Play

From here, Use a media keyboard with your pHat Beat, can it be done?

1 Like

Also be aware, I’ve seen reports that if the pHat Beat is left in pause for a long time, the speakers get hot. Apparently there is a DC current going out to the speakers that causes the coils to heat up? I haven’t noticed / confirmed it myself. May also have been fixed? Not sure? Might want to check it for yourself and then judge accordingly. I turn mine off when not using it. I added my own easy to use mini arcade buttons for all the pHat Beat buttons. I also wired one across the Pi’s RUN terminals. I shut down but don’t unplug the power supply. When I want my tunes I just press the run button and the Pi boots up.

WOW! Got it working, thanks mate!

With my no knowledge of python, low level of bash scripting and ability to read I was able to edit your script to this:

> #!/usr/bin/env python
> 
> import os
> import socket
> import re
> import sys
> 
> VLC_HOST = "127.0.0.1"
> VLC_PORT = 9294
> 
> class VLC():
> 
>     def __init__(self, host="127.0.0.1", port=9294):
>         self.host = host
>         self.port = port
>         self.current_stream = None
>         self.current_state = None
>         self.connecting = False
>         self.socket = None
> 
>     def send(self, command):
>         if self.connecting:
>             return False
> 
>         print("Sending command: {}".format(command))
>         command_string = command + "\n"
> 
>         if sys.version_info[0] >= 3:
>             command_string = command_string.encode("utf-8")
> 
>         try:
>             self.socket.send(command_string)
> 
>         except socket.error:
>             print("Failed to send command to VLC")
>             if self.connect():
>                 self.send(command)
> 
>     def recv(self, length):
>         value = self.socket.recv(8192)
> 
>         if sys.version_info[0] >= 3:
>             value = value.decode("utf-8")
> 
>         return value
>  
>     def communicate(self, command, response_length=8192):
>         self.send(command)
>         return self.recv(response_length)
> 
>     def get_current_stream(self):
>         self.send("status")
>         status = self.recv(8192)
> 
>         result = re.search("input:\ (.*)\ ", status)
>         state = re.search("state\ (.*)\ ", status)
> 
>         if state is not None:
>             self.current_state = state.group(1)
> 
>         if result is not None:
>             self.current_stream = result.group(1)
>         else:
>             self.current_stream = None
> 
>         return self.current_stream
> 
>     def connect(self):
>         if self.connecting:
>             return
> 
>         self.connecting = True
> 
>         if self.socket is not None:
>             self.socket.close()
> 
>         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> 
>         for attempt in range(10):
>             try:
>                 print("Attempting to connect to VLC on {host}:{port}".format(ho$
>                 self.socket.connect((self.host, self.port))
>                 print("Connection successful!")
>                 self.connecting = False
>                 return True
> 
>             except socket.error:
>                 time.sleep(1)
> 
>         print("Connection failed!")
>         self.connecting = False
>         return False
> 
> vlc = VLC(host=VLC_HOST, port=VLC_PORT)
> 
> if vlc.connect():
> 
>     vlc.communicate("pause") # Pause/Play

running ./play.py resulted into play/pause of the radio. Now just adding it to crontab and it’s good to go. Thanks for your advice.

Must say it is very nice script testing with those conditions to test connection and so on… Big thumbs UP.

Hmm, I have it running the whole day idle and the speaker is cool. The only thing slightly warmer than body temperature is the PiZero.

Good stuff, very glad it worked out for you. I would class myself as above average python wise. I’m no expert that’s for sure, not a noob either lol. All self taught, learning as I go.

Also good to hear the speakers didn’t get hot. Thumbs up.