All of these steps have been performed on a fresh installation of Raspbian Stretch.
Prepare the system
Code: Select all
# get the latest packages and apply any updates
sudo apt-get update && sudo apt-get upgrade
# install required packages
sudo apt install rtl-sdr librtlsdr-dev -y
# get the latest udev rules for common dongles
sudo wget -O /etc/udev/rules.d/20-rtlsdr.rules https://raw.githubusercontent.com/osmocom/rtl-sdr/master/rtl-sdr.rules
# reboot to apply udev rules
sudo reboot
Here we create a system user "rtltcp" with no login privileges or shell to help protect against shell escapes if opening this service to the internet
Code: Select all
sudo adduser --system rtltcp
sudo addgroup rtltcp
sudo usermod -aG dialout,rtltcp -a rtltcp
We'll define a serial number for each device to match the TCP port. I've applied a physical label to each dongle for easy identification, so I can plug in the desired antenna to the correct device and then connect to the TCP port written on the label.
Code: Select all
# First, run rtl_eeprom to display installed devices
rtl_eeprom
# Then assign the desired SN to each device. We want the SN to line up w/ the TCP port, so pick a valid value.
# Here I'm setting up two dongles to present on ports 1230 and 1231. Change these lines to match your requirements!
rtl_eeprom -d0 -s 1230
rtl_eeprom -d1 -s 1231
rtl_tcp identifies devices by device number, which is non-deterministic. The script below will accept a serial number as an argument, and return the device ID assigned to the provided serial number.
Code: Select all
sudo mkdir /opt/rtltcp
sudo tee /opt/rtltcp/rtlsn2dev.sh << 'EOF'
#!/bin/bash
# usage: ./rtlsn2dev.sh [serial number]
# returns: device ID
if [ $1 ]
then
rtl_num_devices=$(rtl_eeprom 2>&1 >/dev/null | grep "Found [0-9][0-9]*" | sed -E 's/.*([0-9]+).*/\1/')
if [ $rtl_num_devices ]
then
for i in $(seq 1 $rtl_num_devices);
do
rtl_device=$((i-1))
rtl_serial=$(rtl_eeprom -d$rtl_device 2>&1 >/dev/null | grep "Serial number\:" | sed -E 's/Serial number:[[:blank:]]+//')
if [ "$1" == "$rtl_serial" ]
then
echo $rtl_device
fi
done
fi
fi
EOF
sudo chmod +x /opt/rtltcp/rtlsn2dev.sh
sudo chown -R rtltcp:rtltcp /opt/rtltcp
Now we'll make a systemd service template which we'll then use for each instance we plan to run.
Code: Select all
sudo tee /etc/systemd/system/[email protected] << 'EOF'
[Unit]
Description=rtl_tcp radio streaming service
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
User=%p
Restart=always
WorkingDirectory=/opt/rtltcp
ExecStart=/bin/sh -c "/usr/bin/rtl_tcp -d $(/opt/rtltcp/rtlsn2dev.sh %i) -a 0.0.0.0 -p %i"
EOF
# Now reload systemd, create a couple instances of this service, and start them
sudo systemctl --system daemon-reload
sudo systemctl enable [email protected]
sudo systemctl enable [email protected]
sudo systemctl start [email protected]
sudo systemctl start [email protected]