Update: Working on SDR RX

- Switched to using GQRX
- Created a handler to connect over Telnet to local GQRS

TODO
- Need to find a way to close GQRX without closing the application; Can't start the app with the radio listening, so it must be opened manually
This commit is contained in:
Logan Cusano
2022-01-03 22:54:18 -05:00
parent 1c08bc6919
commit eac3fc4e39
3 changed files with 67 additions and 81 deletions

View File

@@ -152,3 +152,14 @@ def get_user_mention_channel_id():
print("Length error in ID, please try again")
channel_id = None
continue
def check_negative(s):
try:
f = float(s)
if (f < 0):
return True
# Otherwise return false
return False
except ValueError:
return False

99
bot.py
View File

@@ -6,6 +6,8 @@ import sound
import configparser
from discord.ext import commands
from subprocess import Popen, PIPE
from gqrxHandler import GQRXHandler
from BotResources import check_negative
# Init class for bot
@@ -29,21 +31,14 @@ class Bot(commands.Bot):
# Init radio parameters
self.profile_name = None
self.freq = "104.7M"
self.freq_re = re.compile('(\d+\.?\d*M)')
self.freq = "104700000"
self.mode = "wbfm"
self.gain = 40
self.squelch = 0
self.sample_rate = "200k"
self.play_sample_rate = '32k'
self.sample_rate_re = re.compile('(\d+\.?\d*k)')
self.sdr_filters = ["dc"]
# Init SDR Variables
self.sdr_process = None
self.system_os_type = None
self.sdr_output_process = None
self.sdr_started = False
self.GQRXHandler = GQRXHandler()
# Set linux or windows
self.check_os_type()
@@ -114,24 +109,18 @@ class Bot(commands.Bot):
"is required\nExample command: '@ chfreq wbfm 104.7M\n"
"Example command: '@ chfreq fm 154.785M",
brief="Changes radio frequency")
async def chfreq(ctx, mode: str, freq: str, sdr_filter: str = None, member: discord.Member = None):
async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None):
# Possible band-types that can be used
possible_modes = ['wbfm', 'fm']
possible_filters = ['dc', 'deemp']
member = member or ctx.author.display_name
# Check to make sure the frequency input matches the syntax needed
if self.freq_re.search(freq):
if len(freq) >= 6:
self.freq = freq
# Check to make sure the selected mode is valid
if mode in possible_modes:
self.mode = mode
if sdr_filter in possible_filters:
self.sdr_filters = [sdr_filter]
else:
self.sdr_filters = ['deemp']
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the mode to {str(self.mode).upper()} and frequency to"
f" {self.freq}")
@@ -150,8 +139,14 @@ class Bot(commands.Bot):
@self.command(name='chsquelch', help="Use this command to change the squelch for the frequency"
"the bot is listening to",
brief="Changes radio squelch")
async def chsquelch(ctx, squelch: int, member: discord.Member = None):
async def chsquelch(ctx, squelch: float, member: discord.Member = None):
member = member or ctx.author.display_name
print(f"Squelch = {squelch}")
if not check_negative(squelch):
squelch = float(-abs(squelch))
print(f"Squelch = {squelch}")
self.squelch = squelch
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the squelch to {self.squelch}")
@@ -159,48 +154,6 @@ class Bot(commands.Bot):
if self.sdr_started:
self.start_sdr()
@self.command(name='chgain', help="Use this command to change the radio gain for the bot",
brief="Changes radio gain")
async def chgain(ctx, gain: int, member: discord.Member = None):
member = member or ctx.author.display_name
self.gain = gain
await ctx.send(f"Ok {str(member).capitalize()}, I have changed the gain to {self.gain}")
# If the SDR is started, restart it with the updates
if self.sdr_started:
self.start_sdr()
@self.command(name='chbw', help="Use this command to change the center frequency bandwidth for the bot."
"Note: 'k' is required\nExample Command: '@ chbw <xxx.xxk'",
brief="Changes radio bandwidth")
async def chbw(ctx, sample_rate: str, member: discord.Member = None):
member = member or ctx.author.display_name
if self.sample_rate_re.search(sample_rate):
# Check to see if sampling rate of player needs to be changed
if '.' in sample_rate:
if len(sample_rate.split('.')[0]) <= 2:
self.sample_rate = sample_rate
self.play_sample_rate = self.sample_rate
else:
self.sample_rate = sample_rate
self.play_sample_rate = "32k"
else:
if len(sample_rate.split('k')[0]) <= 2:
self.sample_rate = sample_rate
self.play_sample_rate = self.sample_rate
else:
self.sample_rate = sample_rate
self.play_sample_rate = "32k"
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the sample rate to {self.sample_rate}")
# If the SDR is started, restart it with the updates
if self.sdr_started:
self.start_sdr()
else:
await ctx.send(f"{str(member).capitalize()}, {sample_rate} is not valid. please refer to the help page '@ help chbw'")
# Hidden admin commands
@self.command(name='saveprofile', hidden=True)
async def _saveprofile(ctx, profile_name: str, member: discord.Member = None):
@@ -295,14 +248,10 @@ class Bot(commands.Bot):
# Start the radio
print(f"Starting freq: {self.freq}")
# Start the SDR receiver and pipe the output
self.sdr_process = Popen(["rtl_fm", "-M", str(self.mode), "-f", str(self.freq), "-g", str(self.gain),
"-l", str(self.squelch), "-s", str(self.sample_rate), "-E",
str(self.sdr_filters[0])], stdout=PIPE)
# Use the piped output of the SDR receiver to generate audio
self.sdr_output_process = Popen(["play", "-t", "raw", "-r", str(self.play_sample_rate), "-es", "-b",
"16", "-c", "1", "-V1", "-"],
stdin=self.sdr_process.stdout)
# Set the settings in GQRX
self.GQRXHandler.set_all_settings(str(self.mode), str(self.squelch), str(self.freq))
# Set the started variable for later checks
self.sdr_started = True
@@ -310,13 +259,7 @@ class Bot(commands.Bot):
def stop_sdr(self):
if self.sdr_started:
# Wait for the running processes to close
while self.sdr_process.poll() is None and self.sdr_output_process.poll() is None:
self.sdr_process.terminate()
self.sdr_process.kill()
time.sleep(1)
self.sdr_output_process.terminate()
self.sdr_output_process.kill()
time.sleep(1)
# Need a way to 'close' GQRX
self.sdr_started = False
@@ -347,10 +290,7 @@ class Bot(commands.Bot):
config[str(profile_name)]['Frequency'] = self.freq
config[str(profile_name)]['Mode'] = self.mode
config[str(profile_name)]['Gain'] = str(self.gain)
config[str(profile_name)]['Squelch'] = str(self.squelch)
config[str(profile_name)]['Sample Rate'] = self.sample_rate
config[str(profile_name)]['Player Sample Rate'] = self.play_sample_rate
with open('./profiles.ini', 'w+') as config_file:
config.write(config_file)
@@ -368,10 +308,7 @@ class Bot(commands.Bot):
self.profile_name = profile_name
self.freq = config[str(profile_name)]['Frequency']
self.mode = config[str(profile_name)]['Mode']
self.gain = int(config[str(profile_name)]['Gain'])
self.squelch = int(config[str(profile_name)]['Squelch'])
self.sample_rate = config[str(profile_name)]['Sample Rate']
self.play_sample_rate = config[str(profile_name)]['Player Sample Rate']
if self.sdr_started:
self.start_sdr()

38
gqrxHandler.py Normal file
View File

@@ -0,0 +1,38 @@
from telnetlib import Telnet
class GQRXHandler():
def __init__(self, hostname: str = "localhost", port: int = 7356):
self.hostname = hostname
self.port = port
self.telnet_connection = None
def create_telnet_connection(self):
tel_conn = Telnet(self.hostname, self.port)
tel_conn.read_until("Escape character is '^]'.")
return tel_conn
def change_freq(self, freq):
tel_conn = self.create_telnet_connection()
tel_conn.write(f"F {int(freq)}")
tel_conn.read_all()
tel_conn.close()
def change_squelch(self, squelch):
tel_conn = self.create_telnet_connection()
tel_conn.write(f"L SQL {float(squelch)}")
tel_conn.read_all()
tel_conn.close()
def change_mode(self, mode):
tel_conn = self.create_telnet_connection()
tel_conn.write(f"M {str(mode)}")
tel_conn.read_all()
tel_conn.close()
def set_all_settings(self, mode, squelch, freq):
tel_conn = self.create_telnet_connection()
tel_conn.write(f"F {int(freq)}")
tel_conn.write(f"M {str(mode)}")
tel_conn.write(f"L SQL {float(squelch)}")