I built a box for my friend's bar that has a Raspberry Pi inside and with the push of one button it streams audio to a website using Darkice and Icecast, while simultaneously lighting an 'On-Air' sign. I thought this was something people were already doing but after extensive searching I found nothing and decided to figure it out for myself and make this Instructable.
I used a Raspberry Pi 3+ B but as far as I can tell it would work on any Pi (perhaps the GPIO pin numbers would vary, I cannot attest to that though). I used the NOOBS package for the OS and bought a super simple soundcard for the input. I also added a safe shut-down button and activity LED to the box to protect the Pi.
You will need:
NOOBS package on an SD card
On/Off switch (I used a big red emergency stop button like this, but it can be any kind of 'click' switch)
An 'On-Air' sign, I made my own with an LED array
Soldering iron and small connector-board (I just cut a strip off one of these that I had around)
Soundcard (I used a super-simple one like this)
Step 1: Set-up Your Server
For the actual radio station we're using I didn't do this part as I didn't have access to the server and my friend did it, but there is a basic set-up guide on the Icecast website and I managed to get it working from the Pi in the beginning just to see how it goes. Alternatively you can get an account at internet-radio.com and it works with Darkice.
Step 2: Set-up the Pi and Install Darkice
Obtain an SD Card with the NOOBS package. Once you have opened Raspbian and installed all of the updates open a Terminal window and type:
sudo apt-get update
sudo apt-get install darkice
Darkice is part of Debian so it will install automatically, it takes a few moments.
Once it is finished at the prompt type:
It will give an error message and not run, but that's ok. In the message it will say something like:
Using config file: /etc/darkice.cfg
You need to edit that file, so at the prompt type:
sudo nano /etc/darkice.cfg
Edit the file that opens using the information on this wiki.Radioreference page, when you are finished press CTRL-X to close, Y to save and you should return to the Terminal prompt. It's always good to reboot after installing a new programme so type:
and when the Pi has restarted re-open a Terminal window. Now when you type 'Darkice' it should run without any error messages. If you go to https://YOURSERVERADDRESS:8000/ (8000 or whichever port your icecast server is assigned to) you should get a statistics page saying you're connected. If not, tweak the config file (the audio input line gave me some problems but I tried all the options and eventually found my soundcard). Once Darkice is running properly move on to the next step.
Step 3: Stream Button and On-Air Sign
I did a bunch of different tutorials for this as there was no specific one for assigning an action to a button in Python. The biggest problem I faced was getting Darkice to run in the background (and thus not freeze the Python script when it started running) but I asked a coder friend to come and help and he figured out a way to do it writing sheets, something I don't get. Apparently it could also be possible using an '&' somewhere in the code but I'm no coder and what I have works so I chose not to mess with it. Once I had everything working I started whittling down all of the cables from the breadboard until I was left with the following set-up:
GPIO16 to an external 3-point connector.
GPIO7 to the positive terminal of the LED array for the ON-Air sign.
GPIO25 to the click-switch.
The other side of the click-switch attaches to the 3-point connector.
The negative terminal of the LED array also attaches to the 3-point connector.
(The other components on the diagram are explained in the next step).
The Python script that we ended up with is as follows (please note this is an Instructable for the whole project, I don't really know enough to explain the code. I pieced it together from various other tutorials for different functions):
import time<br>import os import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) ledPin = 12 buttonPin = 16 GPIO.setup(ledPin, GPIO.OUT) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
toggle = False
def start_darkice(): print('calling subprocess') #result = subprocess.call('sh screen -S darkice -d -m darkice') os.system('screen -S darkice -d -m darkice') print('resuming') def stop_darkice(): os.system('screen -X -S darkice quit')
while True: buttonState = GPIO.input(buttonPin) if buttonState == True: if not toggle: toggle = True print('turning on darkice') start_darkice() #print ('BUTTON STATE TRUE') GPIO.output(ledPin, GPIO.HIGH)
else: if toggle: toggle = False print('stopping darkice') stop_darkice() #print ('BUTTON STATE FALSE') GPIO.output(ledPin, GPIO.LOW) time.sleep(1)
The py file is attached to this Instructible, you can just download it and follow Step 5 if you like.
Step 4: Safe Shutdown Button and Activity LED
Just for safety and to prolong the life of the Pi I added a safe shutdown button and an external activity LED so people know when it's safe to unplug the box. I easily found tutorials for both of these, but I did notice that the Raspberry Pi 3 B+ has an ACT LED assigned GPIO pin while earlier models don't and that could be problematic if you have an older Pi.
Attach the positive terminal of the LED to GPIO13 and the negative to GPIO26. For the ACT LED to work you need to edit the config.txt file in the BOOT directory. You can find this in the Raspbian browser or with:
sudo nano /usr/boot/config.txt
When you are editing the file simply add the following lines:
# Use external LED as SD ACT LED <br>dtoverlay=pi3-act-led,gpio=19
Now the external LED wil flicker with SD card activity.
Attach the push switch terminals to GPIO3 and GPIO8. I found the following code from a tutorial and it works fine:
import time<br>from gpiozero import Button from subprocess import check_call from signal import pause
def shutdown(): check_call(['sudo', 'poweroff'])
shutdown_btn = Button(2, hold_time=1) shutdown_btn.when_held = shutdown
This py file is also attached for you to download if you want to. My button has to be held for 1 second, if you want to change that change the 'hold-time=' value.
Step 5: Make the Python Scripts Run From Boot
Again, there are a lot of tutorials on this subject, I used this one from Dexter Industries and followed step one by editing the rc.local file.
sudo nano /etc/rc.local
In here you need to add the complete file-path to the end of the document, complete with an '&' at the end of each line. You should add one line for each of the files:
sudo python /home/pi/YOURFILENAME,py &
sudo python /home/pi/YOURFILENAME2.py &
just before the line
The '&' at the end of each line is important to ensure the programme loops.
Step 6: Build the Box
This is entirely up to you. It really only needs to be the size of the Pi, but I put a compressor in my set-up to keep the radio stream at a fairly consistent level so the box is a bit larger. Mine has a hole on the front for the On-Air sign and one for the Big Red Button, plus I made a little instruction card for the DJs and inlaid it below the button. On the side of the box there's a hole for the push button, the ACT LED and the audio-input, plus inlaid instructions for the bar staff on how to safely shut-down the box.
Step 7: Installation
Fix all the separate components into your box, bearing in mind it's always good to allow some air circulation around the Pi to prevent overheating. Attach the audio cables from your mixer, hit the button and enjoy broadcasting!