Skip to content

Instantly share code, notes, and snippets.

@dnas2
Last active April 8, 2018 08:26
Show Gist options
  • Save dnas2/d3627d29d273e577319231518d8981d5 to your computer and use it in GitHub Desktop.
Save dnas2/d3627d29d273e577319231518d8981d5 to your computer and use it in GitHub Desktop.
Play a file when radio carrier signal drops
#!/usr/bin/python
# Run this with RTL-SDR connected to Raspberry Pi and Pipe rtl_power to it as follows:
# rtl_power -f 433.175M:433.225M:0.005M -p 42 -g 20 -i 3 | python gb3py_mon.py
# Set the -p (Parts per million calibration) and -g (Gain) as required for your SDR. You may also need to change the threshold for detecting a carrier at line 27
import sys
import datetime
import os
do_run = True
is_qrv = False
is_qso = False
periods_qrv = 0
play_file_next = False
idents_since_file = 0
while do_run:
i = ''
line = ''
while i != '\n':
# Read a line from rtl_power
i = sys.stdin.read(1)
line += i
if "," in line:
currentDT = datetime.datetime.now()
vals = line.split(",")
# 433.200MHz is the 14th element in the array.
# A strength over -25dB is considered a carrier from the repeater
if float(vals[14]) > -25:
is_qrv = True
periods_qrv += 1
play_file_next = False # Ensures we don't double with a QSO
print (currentDT.strftime("%H:%M:%S")) + " QRV for " + str(periods_qrv) + " periods"
# rtl_power sends a line every 5 secs, so we consider a QSO in progress after 100 contiguous secs with a carrier
if periods_qrv > 19:
if is_qso == False:
print (currentDT.strftime("%H:%M:%S")) + " Strength: " + vals[14]
print (currentDT.strftime("%H:%M:%S")) + " *** NOW IN QSO ***"
is_qso = True
idents_since_file = 0
else:
# No carrier present
if is_qrv == True: # Carrier was there on the last sample
# The period with a carrier lasted less than 20 seconds, so we assume it was a CW ident
if periods_qrv <4:
idents_since_file += 1
print "Beacon number " + str(idents_since_file)
if idents_since_file == 4:
# If this is the 4th CW ident since we last played the wav file, set the wav file to play
play_file_next = True
idents_since_file = 0
if is_qso == True:
# There was a carrier last sample and it was part of a QSO (>100 secs of carrier)
periods_qrv = 0 # Reset the CW idents counter
play_file_next = True # Play the file now the QSO is over
if play_file_next == True:
print (currentDT.strftime("%H:%M:%S")) + " *** QSO ENDED, playing WAV ***"
# System call to play /home/pi/test.wav on the soundcard with ID 0 (you may need 1,0 for some USB soundcards)
os.system("mplayer -ao alsa:device=hw=0,0 /home/pi/test.wav")
play_file_next = False
is_qrv = False
is_qso = False
periods_qrv = 0
sys.stdout.flush() # Ensures any prints happen immediately
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment