Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
4
Indexable
Never
from flask import Flask, render_template
import paho.mqtt.client as mqtt
import ssl
import json


app = Flask(__name__)

client_username = "Foxtrot"
client_password = "kEkTyCr1mI6A2s3O"
client_url = "myggen.mooo.com"
client_port = 8883


@app.route("/", methods=["GET", "POST"])
def index():
    return render_template(
        "index.html",
    )


def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected with result code " + str(rc))
        client.subscribe("Foxtrot/#")
    else:
        print("Connection Failed!")


def on_message(client, userdata, msg):
    topic = msg.topic
    payload = msg.payload.decode()
    payload = json.loads(payload)
    print(f"Received MQTT on {topic}: {payload}")


client = mqtt.Client()
client.username_pw_set(client_username, client_password)
client.tls_set(tls_version=ssl.PROTOCOL_TLSv1_2)
client.on_connect = on_connect
client.on_message = on_message

try:
    client.connect(client_url, client_port, keepalive=60)
except Exception as e:
    print("Error connecting to MQTT broker:", str(e))
client.loop_start()