python 2.7 - Implementing MQTT in flask -


i want ask question regarding on how implementing mqtt in flask. wrote code when go specific page, receive message, storing inside database , output message in table in specific page.

below snippet of code.

'views.py'

from flask import render_template, request, url_for, redirect, flash flask_wtf import form flask_login import login_user, logout_user, login_required import paho.mqtt.client mqtt app import app, db models import user, data  ...other @app.route...  @app.route('/table') @login_required def table_data():     def on_connect(client, userdata, flags, rc):         flash("connected")          client.subscribe("abc123")      def on_message(client, userdata, msg, message):         message = data(temperature=msg.temperature, ph=msg.ph, time=msg.time)          db.session.add(message)         db.session.commit()  client = mqtt.client(client_id = "my_visualise", clean_session = true) client.username_pw_set("mosquitto", "mosquitto") client.on_connect = on_connect client.on_message = on_message  return render_template('table_data.html') 

'models.py'

from app import app, db  ...user table...  # data table class data(db.model):     __tablename__= 'data_reading'     id = db.column(db.integer, primary_key=true)     temperature = db.column(db.integer, index=true)     ph = db.column(db.integer, index=true)     time = db.column(db.datetime, index=true)      def __init__(self, temperature, ph, time):         self.temperature = temperature         self.ph = ph         self.time = time   db.create_all() 

'table_data.html'

... <div class="jumbotron">     <div class="container-fluid">         <h2>data</h2>         <p>this table includes data temperature, ph value , time</p>         <div class="table-responsive">             <table class="table", border=2>                         {% value in message.iteritems() %}                     <thead>                         <tr>                             <th>id</th>                             <th>temperature</th>                             <th>ph value</th>                             <th>timestamp</th>                         </tr>                     </thead>                     <tbody>                         <tr>                             <td> {{value}} </td>                         </tr>                     </tbody>                     {% endfor %}                 </table>             </div>         </div> 

i run code locally on computer. no error @ first once go page '/table', see following error.

'error'

undefinederror: 'message' undefined 

i believe must problem way wrote script in 'views.py' since did not find example or tutorial can understand enough implement mqtt in flask. decided try implement on own.

i ask opinion this.

thanks in advance.

i don't know enough flask give working example, need move mqtt code separate thread can run continuously. onmessage function can still store arriving messages in database.

then can render results directly database.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -