42 lines
899 B
Python
42 lines
899 B
Python
from flask import Flask, render_template, request
|
|
from flask_mqtt import Mqtt
|
|
|
|
app = Flask(__name__)
|
|
app.config['MQTT_BROKER_URL'] = 'localhost'
|
|
app.config['MQTT_KEEPALIVE'] = 5
|
|
app.config['MQTT_TLS_ENABLED'] = False
|
|
mqtt = Mqtt(app)
|
|
|
|
DATA = []
|
|
|
|
@mqtt.on_connect()
|
|
def connect(client, userdata, flags, rc):
|
|
mqtt.subscribe('main')
|
|
|
|
@mqtt.on_message()
|
|
def message(client, userdata, message):
|
|
data = dict(
|
|
topic=message.topic,
|
|
payload=message.payload
|
|
)
|
|
DATA.append(data)
|
|
|
|
@app.route('/get')
|
|
def get():
|
|
if not DATA:
|
|
return ''
|
|
return '<ul>' + ''.join(
|
|
[f"<li>{data['topic']}: {data['payload']}" for data in DATA]
|
|
) + '</ul>'
|
|
|
|
@app.route('/send', methods=["POST"])
|
|
def send():
|
|
message = request.form.get('msg')
|
|
mqtt.publish('main', message)
|
|
return 'OK'
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return render_template("index.html")
|
|
|
|
app.run() |