Thursday, June 13, 2013

Using Python to Update Icecast Scanner Audio Feeds with a Raspberry Pi

The python scripts below will add the ability to update Icecast feed metadata via the Raspberry Pi.  They are compatible with Uniden Scanners including:

BCT8
BCT15
BCT15X
BCD996T
BCD996XT
BC346T
BC346XT
BCD396T
BCD396XT

Functionality could be added for other Uniden scanners such as the 780XLT, 785D, 796D, and 898T, but as of now I don't have access to any of these models for testing.

To implement the script, download the script by right clicking on the appropriate link below and click "Save As".  You can do this from the Raspberry Pi or from another computer and use any number of tools such as ssh and scp to copy the file to your desired location on your Raspberry Pi.

Uniden MetaPy.py Script - For all models except the BCT8
Uniden MetaPy8.py Script - For the BCT8 model

Or you can use the wget command from your Raspberry Pi's console to download and save the file:

wget -O ~/metaPy.py http://www.k7bbr.net/files/metaPy.py

or for the BCT8:

wget -O ~/metaPy8.py http://www.k7bbr.net/files/metaPy8.py

(The -O option allows you to give the file location and filename for the downloaded file - in this case it's saved in your home directory under the name metaPy.py or metaPy8.py.)

Once downloaded, use a text editor such as nano or vi to change the configuration section at the top of the script to match your settings:
'''-----------------USER CONFIGURATION-----------------'''
port = "/dev/ttyUSB0" #enter scanner USB/serial port in quotes here
baudrate = 115200 #enter scanner baudrate here
icecastUser = "username" #enter icecast username in quotes here (for RR feed use "source")
icecastPass = "hackme" #enter icecast password in quotes here
icecastServerAddress = "192.168.1.100:8000" #enter icecast server IP Address (and port if necessary) here
icecastMountpoint = "mymountpoint" #enter icecast mountpoint in quotes here - don't add leading '/'
'''-----------------END USER CONFIGURATION---------------'''
'''----------UNNECESSARY TO MODIFY SCRIPT BELOW----------'''

To connect your scanner you'll need a USB to serial adapter. If it's the first one connected, it should show up as /dev/ttyUSB0. You can check and see by going to /dev and list the files and look for ttyUSB0 or something similar.  Also make sure that the baudrate in the script is the same as the baudrate set in your scanner.

The script uses two Python modules requests and serial that may or may not be installed on your Raspberry Pi. You can install these packages by using apt-get:

sudo apt-get install python-serial, python-requests

To run the script, change directories to the location the script is saved. Then type python metaPy.py to begin the script. If it is running, you will see it print out talkgroup information, time information and an update status:

Davis County Sim 11232 Syracuse PD 2 C
Thu Jun 13 21:07:03 2013
Icecast Update OK
Davis County Sim 10688 Davis Ops 2
Thu Jun 13 21:07:21 2013
Icecast Update OK


The script can be placed in the background by typing ctrl-z and then bg. It can be brought back to the foreground by typing fg.

You can also start the script detached from the console by adding an & to the end of the command: python metaPy.py &  .

To stop the script, simply type ctrl-c, or find the process and kill it.

By using Darkice to feed the audio and this metaPy.py script to feed the alpha tags, you now have a 5W streaming media box to stream both scanner audio and text alpha tags.