Skip to content

Quickstart — REST Example

Connect an existing REST backend to ODevice.

Flow

Your REST API → Python poll/loop → device.update() → runtime → UI

Code

python
import asyncio
import httpx
from odevice import App, Device, Number

app = App("REST Monitor")

device = Device(
    id="machine-01",
    name="Machine Status",
    properties={
        "rpm": Number(unit="RPM", view="gauge"),
    },
)
app.add(device)

async def poll_backend():
    async with httpx.AsyncClient() as client:
        while True:
            resp = await client.get("http://your-backend/api/status")
            data = resp.json()
            device.update(rpm=data["rpm"])
            await asyncio.sleep(1)

async def main():
    asyncio.create_task(poll_backend())
    app.run()

asyncio.run(main())

Writing back (commands)

python
@device.on_write("power")
async def set_power(value: bool):
    async with httpx.AsyncClient() as client:
        await client.post(
            "http://your-backend/api/control",
            json={"power": value},
        )
    return value

The UI switch triggers this handler, which POSTs to your backend.

Built for self-hosted IoT runtimes.