You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

42 lines
899 B

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()