From 10cfa222bad520c64b41c27c08163791cd917e4f Mon Sep 17 00:00:00 2001 From: Rafaelca70x <75044825+Rafaelca70x@users.noreply.github.com> Date: Tue, 8 Jun 2021 10:42:15 -0300 Subject: [PATCH] Create app.py API Flask for sub and pub. 1.0 --- app.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..faec886 --- /dev/null +++ b/app.py @@ -0,0 +1,88 @@ +import logging +import json +from flask import Flask, render_template +from flask_mqtt import Mqtt +from flask_socketio import SocketIO +from flask_bootstrap import Bootstrap + +# import libraries + + +app = Flask(__name__) +port = 1883 +host = 'localhost' +# create an instance of Flask and set the parameters of host + + +app.config['SECRET'] = 'LASSE' +app.config['TEMPLATES_AUTO_RELOAD'] = True +app.config['MQTT_BROKER_URL'] = host +app.config['MQTT_BROKER_PORT'] = port +app.config['MQTT_CLIENT_ID'] = 'flask_mqtt' +app.config['MQTT_CLEAN_SESSION'] = True +app.config['MQTT_USERNAME'] = '' +app.config['MQTT_PASSWORD'] = '' +app.config['MQTT_KEEPALIVE'] = 5 +app.config['MQTT_TLS_ENABLED'] = False +app.config['MQTT_LAST_WILL_TOPIC'] = 'home/lastwill' +app.config['MQTT_LAST_WILL_MESSAGE'] = 'bye' +app.config['MQTT_LAST_WILL_QOS'] = 2 + +# set others parameters of app.config + + +# Parameters for SSL enabled +# app.config['MQTT_BROKER_PORT'] = 8883 +# app.config['MQTT_TLS_ENABLED'] = True +# app.config['MQTT_TLS_INSECURE'] = True +# app.config['MQTT_TLS_CA_CERTS'] = 'ca.crt' + + +mqtt = Mqtt(app) +socketio = SocketIO(app) +bootstrap = Bootstrap(app) + + +@app.route('/') # rout of homepage, return index.html +def index(): + return render_template('index.html') + +# when using SocketIO, messages are received by both parties as events. + + +@socketio.on('publish') # receive the message typed by the user and publish with json in topic, send message and qos. +def handle_publish(json_str): + data = json.loads(json_str) + mqtt.publish(data['topic'], data['message'], data['qos']) + + +@socketio.on('subscribe') # subscribe to a certain topic +def handle_subscribe(json_str): + data = json.loads(json_str) + mqtt.subscribe(data['topic'], data['qos']) + + +@socketio.on('unsubscribe_all') # unsubscribe from a single topic +def handle_unsubscribe_all(): + mqtt.unsubscribe_all() + + +@mqtt.on_message() # Decorator to handle all messages that have been subscribed and that are not handled via the +# on_message decorator. +def handle_mqtt_message(client, userdata, message): + data = dict( + topic=message.topic, + payload=message.payload.decode(), + qos=message.qos, + ) + socketio.emit('mqtt_message', data=data) + + +@mqtt.on_log() # enable log +def handle_logging(client, userdata, level, buf): + print(level, buf) + pass + + +if __name__ == '__main__': + socketio.run(app, host='0.0.0.0', port=5000, use_reloader=False, debug=True)