MQTT publish and subscribe
Report SHT3X readings to a broker and react to commands coming back on a wildcard subscription — the session keeps itself alive.
Click through the files — it's the whole project
MQTT publish and subscribe — Visual Studio Code
Select a file from the explorer
esp32s3_devkitc · MQTT Mqtt · Sensor · wildcards
What's going on
connect()returns immediately — the session completes on a background poll thread that owns the keepalives and acks, which is why the loop checkslinked()before publishing instead of waiting on connect.- The wildcard
cmd/#subscription catches every sub-topic undercmd/; theonMessagehandler gets(topic, payload)and decides what each one means. - Publishes are QoS 1 — the client retries until the broker acks. Payloads are strings;
String(value)is all the formatting this example needs. - Like all networking, this needs an ESP32 target. MQTT over TLS is the same constructor
with an
mqtts://URI.
Flash and try it
terminal
npx @typecad/hal create my-project --target esp32s3_devkitc
cd my-project
npx @typecad/hal build --compile --upload --port COM4Point it at a broker you can reach — Mosquitto on a LAN machine works, and mosquitto_sub -t 'sensors/#' shows the readings arriving. Publish to cmd/report and the board answers on the serial monitor.
Where to go next
- IoT Networking — the full Mqtt API and TLS
- Networking — the WiFi layer underneath
- Sensors — the SHT3X feeding the readings