Python Forum
How to control flowgraph variables from a Webserver?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to control flowgraph variables from a Webserver?
#1
I have a simple "dial tone" GRC application that I am running through a simple python webserver, when I run this application and call the freq method I have to set the variable value before running the script like this:

tb.set_samp_rate(32e3)
So, what I am wondering about is if I can have some sort of dynamic button or slider on my webserver page that I can use to adjust the parameter while the program is running. Meaning, to control the variables in realtime of a GNU Radio Flowgraph (like Sampling Rate, Center Frequency, Demodulator type etc.) from a Webpage.

Is that possible?

Here is my webserver script:

import SimpleHTTPServer
import SocketServer
from dial_tone_modified import dial_tone_modified

tb = dial_tone_modified()

tb.set_samp_rate(32e3)

tb.start()

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print("Server at PORT ",PORT)
httpd.serve_forever()

tb.stop()
tb.wait()
And here is my GRC python code:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Dial Tone Modified
# Generated: Mon Jul 15 11:15:28 2019
##################################################


from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser



class dial_tone_modified(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self, "Dial Tone Modified")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate_0 = samp_rate_0 = 32000
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.blocks_add_xx = blocks.add_vff(1)
        self.audio_sink = audio.sink(32000, '', True)
        self.analog_sig_source_x_1 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 440, .4, 0)
        self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 350, .4, 0)
        self.analog_noise_source_x_0 = analog.noise_source_f(analog.GR_GAUSSIAN, 0.005, -42)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx, 2))
        self.connect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))
        self.connect((self.analog_sig_source_x_1, 0), (self.blocks_add_xx, 1))
        self.connect((self.blocks_add_xx, 0), (self.audio_sink, 0))

    def get_samp_rate_0(self):
        return self.samp_rate_0

    def set_samp_rate_0(self, samp_rate_0):
        self.samp_rate_0 = samp_rate_0

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.analog_sig_source_x_1.set_sampling_freq(self.samp_rate)
        self.analog_sig_source_x_0.set_sampling_freq(self.samp_rate)


def main(top_block_cls=dial_tone_modified, options=None):
    
    tb = dial_tone_modified()
    tb.start()
    try:
        raw_input('Press Enter to quit: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()
 

if __name__ == '__main__':
    main()
Reply
#2
Hi,

I was hopeful to get some answers for my posting that I placed two days ago but got nothing! Is this website still active?

Here is my posting:

https://python-forum.io/Thread-How-to-co...-Webserver


Thanks,
Hadad
Reply
#3
(Jul-18-2019, 04:46 PM)Hadad Wrote: Is that possible?

Of course it is and sounds no different from any other kind of web app really. The form on the page needs to be submitted to the server, which will read the data from the request and do whatever you need to with it. Have you looked at the documentation for SimpleHTTPRequestHandler to see how you get access to the form data in a request? I can't say I know; I've only used Flask for web apps in Python.
Reply
#4
Quote:Of course it is and sounds no different from any other kind of web app really. The form on the page needs to be submitted to the server, which will read the data from the request and do whatever you need to with it. Have you looked at the documentation for SimpleHTTPRequestHandler to see how you get access to the form data in a request? I can't say I know; I've only used Flask for web apps in Python.


Would you please elaborate more. I need an example or links of where you have seen that done if possible. I am a total newbie when its related to python.
Reply
#5
What have you tried looking up yourself? At the very least, if don't understand HTTP POST requests and HTML forms, you should go look those up. Weirdly, SimpleHTTPRequestHandler doesn't seem to support POST requests (at least, it doesn't have a do_POST method according to the docs. I suppose it really is meant to be quite simple.

I'd take a look at Flask, which is quite a small web framework. The Quickstart section is pretty good and there's a whole page titled "Accessing Request Data" that tells you, e.g. how to get access to the form data.
Reply
#6
Quote:I'd take a look at Flask, which is quite a small web framework. The Quickstart section is pretty good and there's a whole page titled "Accessing Request Data" that tells you, e.g. how to get access to the form data.

Thanks a lot ndc85430, I will check the recommended resources and go from there.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020