*** This is a python program ,for how to read data from cloud. same data need to be update continuously in html page with in 1 second time interval. if any one know please help me how to push data in html continuously***
from flask import Flask, request, jsonify, render_template
from flask_mqtt import Mqtt
app = Flask(__name__)
app.config['MQTT_BROKER_URL'] = 'broker.emqx.io'
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = 'AMIRUL****' # Set this item when you need to verify username and password
app.config['MQTT_PASSWORD'] = 'AMI123@****' # Set this item when you need to verify username and password
app.config['MQTT_KEEPALIVE'] = 50 # Set KeepAlive time in seconds
app.config['MQTT_TLS_ENABLED'] = False # If your broker supports TLS, set it True
topic = 'python2/mqtt'
mqtt_client = Mqtt(app)
@app.route('/')
def home():
return render_template('index.html')
@mqtt_client.on_connect()
def handle_connect(client, userdata, flags, rc):
if rc == 0:
print('Connected successfully')
mqtt_client.subscribe(topic) # subscribe topic
else:
print('Bad connection. Code:', rc)
@mqtt_client.on_message()
def handle_mqtt_message(client, userdata, message):
data = dict(
topic=message.topic,
payload=message.payload.decode()
)
data1=message.payload.decode()
print('{payload}'.format(**data))
@app.context_processor
def ret():
return {'value':data1}
@app.route('/publish', methods=['POST'])
def publish_message():
request_data = request.get_json()
publish_result = mqtt_client.publish(request_data['topic'], request_data['msg'])
return jsonify({'code': publish_result[0]})
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000)