Skip to content

Quickstart — MQTT Example ​

Connect an existing MQTT data source to ODevice.

Principle ​

The framework does not define an MQTT topic standard. You bridge your MQTT client to device.update() yourself. This keeps your existing broker, topics, and auth untouched.

Flow ​

MQTT broker → your callback → device.update() → runtime → WebSocket → UI

Code ​

python
import json
import paho.mqtt.client as mqtt
from odevice import App, Device, Number

app = App("Temperature Monitor")

sensor = Device(
    id="temp-01",
    name="Room Temperature",
    properties={
        "temperature": Number(unit="°C", min=-10, max=50, view="gauge"),
    },
)
app.add(sensor)

def on_message(client, userdata, msg):
    data = json.loads(msg.payload)
    sensor.update(temperature=data["temperature"])

client = mqtt.Client()
client.on_message = on_message
client.connect("your-broker", 1883, 60)
client.subscribe("your/topic")
client.loop_start()

app.run()

Try it ​

bash
pip install paho-mqtt
python main.py

Publish {"temperature": 23.5} to your topic and watch the gauge move.

Built for self-hosted IoT runtimes.