Flask Server Test on Raspberry Pi with FaBo9Axis_MPU9250

Environment

  • Raspberry Pi 3B+
  • Stratux AHRS Sensor (MPU9250 + BMP280)
  • Raspbian GNU/Linux 9.4
  • Python 3
  • Flask 1.0.2

Setup Flask

$ pip3 install Flask

Code

$ vim mpu_flask.py
#!/usr/bin/python3

from flask import Flask
import FaBo9Axis_MPU9250
import json

mpu9250 = FaBo9Axis_MPU9250.MPU9250()

app = Flask(__name__)

@app.route('/')
def hello():
    name = "Top Page"
    return name

@app.route('/mpu')
def mpu():
    temp = mpu9250.readTemperature()
    accel = mpu9250.readAccel()
    gyro = mpu9250.readGyro()
    mag = mpu9250.readMagnet()
    dic =  {
            "temp": str(temp),
            "accel": {
                        "x" : str(accel['x']), 
                        "y" : str(accel['y']),
                        "z" : str(accel['z'])
                    } ,                
            "gyro" : {
                        "x" : str(gyro['x']), 
                        "y" : str(gyro['y']),
                        "z" : str(gyro['z'])
                    } ,
            "mag" :{
                        "x" : str(mag['x']), 
                        "y" : str(mag['y']),
                        "z" : str(mag['z'])
                    }
            }
    json_str = json.dumps(dic)
    return json_str

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=5005)

Result

$ sudo python3 mpu_flask.py
 * Serving Flask app "mpu_flask" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5005/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 337-794-775