[Dprglist] Running a Flask web server on a Pi as a robot remote control

Murray Altheim murray18 at altheim.com
Wed Mar 24 04:56:20 PST 2021


Hi,

As promised, I did a bit of cleanup and re-posted the Python flask web
server I mentioned in today's VTC. This uses the flask web server, a
flask-restful library for RESTful web services, and a few of my 'ros'
classes to publish a web page from your Raspberry Pi that can activate
various things on your robot (well, or anything else that can be tied
to python code). The current controller has buttons like "Turn ahead
starboard", "Brake", and "standby", as well as two drag bars to set
port and starboard motor velocity. I leave it to the reader to modify
things to suit your own purposes.

This is all a bit rough, I've not played with it in over a year,
but it works.

To use this you'll need Python 3. You'd download the code via:

   % git clone https://github.com/ifurusato/ros.git

Then install colorama (which I use for fancy colored console output)
and the two flask-related libraries:

   % sudo pip3 install colorama
   % sudo pip3 install flask
   % sudo pip3 install flask-restful

Then navigate into the ros directory. You'll find:

   flask_test.py      the executable that runs the flask server
   flask_wrapper.py   actually does all the work
   templates/         contains the index.html home page
   style/             contains the stylesheet and associated files
   script/            contains the javascript support files

  To start the server execute the test file:

   % cd ros
   % flask_test.py

You can find out what the IP address of your Pi is via:

   % ifconfig

Under wlan0 (or similar) you'll see your IP address following "inet";
generally on a local network it'll be something like 192.168.1.77.
You can then go to a web browser (on your home network) and browse to

   http://192.168.1.77:8085/

and if the flask web server is running you should see the Controller
web page. Clicking on a button will fire an event that shows up on
your Pi console as something like:

   2021-03-24T17:27:44.479.773283Z	: flask.wrapper     : INFO  : RESPOND to event BRAKE.
   2021-03-24T17:27:44.481.144428Z	: flask.wrapper     : INFO  : added message id b8bdd168-5056-4d61-9caf-33aeb7c6fadb with event: brake
   RESPONSE mimetype: application/json; json: [{'id': 'b8bdd168-5056-4d61-9caf-33aeb7c6fadb'}, {'event': 'BRAKE'}, {'priority': 4}]

You may find that you need to install another library or two. I haven't
tested this on a brand new Pi. If you have any trouble don't hesitate to
reply to this message on the DPRG list (as others will undoubtedly run
into the same issues).

BTW, the bulk of the code in the above-described project is logging,
message and event handling, etc.  The core of the flask server is just
these few lines:

    app = Flask(__name__)
    api = Api(app)

    @app.route('/')
    def home():
       return render_template('index.html')  # render a template

That's about all you really need to get started with flask and a single
home page. I'm including the rest because it'd take more time to tease
out the basics and you may get ideas from what I've done with the
messages, events, and how to set up the basic structures for templates,
stylesheets and scripts.

Enjoy!

Murray

...........................................................................
Murray Altheim <murray18 at altheim dot com>                       = =  ===
http://www.altheim.com/murray/                                     ===  ===
                                                                    = =  ===
     In the evening
     The rice leaves in the garden
     Rustle in the autumn wind
     That blows through my reed hut.
            -- Minamoto no Tsunenobu



More information about the DPRGlist mailing list