Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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)