Skip to content

Instantly share code, notes, and snippets.

@Gadgetoid
Last active September 4, 2019 17:23
Show Gist options
  • Save Gadgetoid/47d8f873fde39fcbe83e6dab0e855f72 to your computer and use it in GitHub Desktop.
Save Gadgetoid/47d8f873fde39fcbe83e6dab0e855f72 to your computer and use it in GitHub Desktop.
VLC PHATBEAT Pirate Radio

Starting from Jessie or Jessie Lite

curl get.pimoroni.com/phatbeat | bash

Then install VLC:

sudo apt-get install vlc-nox

Create a new dir vlc in /home/pi and copy all of the below files into it.

Then run crontab -e and add:

@reboot /home/pi/vlc/start.sh
http://relay4.slayradio.org:8200/
http://allstream.rainwave.cc:8000/all.mp3
http://tx.sharp-stream.com/icecast.php?i=planetrock.mp3
http://s1.viastreaming.net:8000
#!/bin/bash
cd /home/pi/vlc
python vlc.py
#!/usr/bin/env python
import atexit
import re
import socket
import subprocess
import time
import signal
from sys import exit, version_info
import phatbeat
class VLC():
def __init__(self, host="127.0.0.1", port=9294):
self.host = host
self.port = port
self.pid = None
self.current_stream = None
self.current_state = None
self.running = False
def kill(self):
if self.pid is not None and self.running:
subprocess.call(['/bin/kill','-9', str(self.pid)])
print('Killing VLC process with PID: ' + str(self.pid))
def send(self, command):
print("Sending command: " + command)
command += "\n"
if version_info[0] >= 3:
command = command.encode("utf-8")
try:
self.socket.send(command)
except socket.error:
print("Failed to send command to VLC")
def recv(self, length):
value = self.socket.recv(8192)
if 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):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for attempt in range(10):
try:
print("Attempting to connect to VLC")
self.socket.connect((self.host, self.port))
print("Connection successful!")
return True
except socket.error:
time.sleep(1)
def start(self):
if self.pid is None:
try:
return_value = subprocess.check_output(['pidof', 'vlc'])
if version_info[0] >= 3:
self.pid = int(return_value.decode('utf-8').split(' ')[0])
else:
self.pid = int(return_value.decode('utf-8').split(' ')[0])
print('Found VLC with PID: ' + str(self.pid))
if self.connect():
return True
except subprocess.CalledProcessError:
pass
try:
return_value = subprocess.check_output(['./vlc.sh'])
pids = return_value.decode('utf-8').split('\n')[0]
self.pid = int(pids.split(' ')[0])
self.started_vlc_instance = True
print('VLC started with PID: ' + str(self.pid))
except subprocess.CalledProcessError:
print('You must have VLC installed to use Dot3k Radio')
print('Try: sudo apt-get install vlc')
return False
if not self.connect():
print("Unable to connect to VLC")
return False
return True
if __name__ == "__main__":
vlc = VLC()
if vlc.start():
@phatbeat.on(phatbeat.BTN_VOLDN)
def pb_volume_down(pin):
print(vlc.communicate("voldown"))
@phatbeat.on(phatbeat.BTN_VOLUP)
def pb_volume_up(pin):
print(vlc.communicate("volup"))
@phatbeat.on(phatbeat.BTN_PLAYPAUSE)
def pb_play_pause(pin):
print(vlc.communicate("pause"))
@phatbeat.on(phatbeat.BTN_FASTFWD)
def pb_fast_forward(pin):
print(vlc.communicate("next"))
@phatbeat.on(phatbeat.BTN_REWIND)
def pb_rewind(pin):
print(vlc.communicate("prev"))
signal.pause()
else:
exit()
#!/usr/bin/env bash
killall vlc > /dev/null 2>&1
/usr/bin/cvlc --quiet --intf rc --rc-fake-tty --rc-host 0.0.0.0:9294 --extraintf http --http-password vlcremote playlist.m3u > /dev/null & 2>&1
sleep 0.5
/bin/pidof vlc
@pierreyvesbaloche
Copy link

Hi Philip,
Thank you for the great instructions on own to setup the software part for the Pirate Radio Kit.
I've forked your GIST in order to add the TURN OFF feature for the phatbeat.
Feel free to merge ;)
Best Regards,
Pierre-yves

@PaulComis
Copy link

Thanks...

one of the missing pieces since vlc-nox seems to be retired!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment