arduino yun uhttpd flask setup arduino yun uhttpd flask setup flask flask

arduino yun uhttpd flask setup


I looked into this too and got a little further, I was able to setup a simple hello world but once I tried to do something non-trivial I ran into a big issue that uhttpd doesn't support URL rewriting/aliasing. This means your flask app can only be served at the URL of its .py file instead of at a root like http:// (arduino IP) /flaskapp/. None of the routes inside the app will be visible and makes the whole thing unusable.

However, instead of trying to force flask into uhttpd I had great success running the built in server that flask provides. Take a look at this guide I wrote up that uses flask to serve data from a Yun: https://learn.adafruit.com/smart-measuring-cup/overview

The thing to do is add a call to app.run when the script is run, for example make your flask app look like:

from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():    return "Hello Flask!"if __name__ == '__main__':    app.run(host='0.0.0.0', debug=True, threaded=True)

Then log in to the Yun and run the script using python. Flask's built in server should start serving the app on http:// (arduino IP) :5000/. Make sure to include the host='0.0.0.0' as it's required to listen on the Yun's external network interface. You probably also want debug=True so there are better error messages (and live reloading of the server when the code changes), and I found threaded=True helps because the default server only handles one connection at a time. The Yun is a relatively slow processor so don't expect to service a lot of concurrent requests, however it's quite capable for providing a simple REST API or web application for a few users.

If you want this server to always run on bootup, edit the /etc/rc.local file to include a call to python and your script.