Skip to main content
IoT8 min read

MQTT + UDP: Bringing AI Down to Edge Devices

Why Assistant Core's ESP32 devices use MQTT for control and UDP for audio instead of one WebSocket, and how the gateway bridges them into the runtime. The AES-128-GCM packet format, 60 ms Opus frames, and the security model with device secrets, TLS and signed firmware.

MQTTUDPESP32Edge AISecurity
AC

Assistant Core Team

Hardware & Firmware

MQTT Gateway — Edge AI IoT Architecture
MQTT Gateway — Edge AI IoT Architecture

When we put the AI assistant on ESP32-S3 boards, the hard part was not the model but the transport. This post describes how a device talks to the Assistant Core cloud runtime: MQTT for control, UDP for audio, a gateway that bridges into the runtime, the encrypted packet format, and the security model around it. If you want the big picture of what stays on the device and what the cloud does, read ESP32 voice assistant with a cloud runtime first.

The problem with a single WebSocket

WebSocket is the natural choice when the runtime already has a voice endpoint over WebSocket for browsers. On an embedded board it shows three problems:

  • Audio queues behind a lost packet. WebSocket runs on TCP. One lost packet makes every packet after it wait for the retransmission, newer audio frames included. For speech, a late frame is worse than a missing one, because the Opus decoder can conceal a gap but not a delay.
  • Memory. A TLS session plus WebSocket buffers take RAM that the wake word, the decoder and the display also need.
  • Control and media on one stream. Status messages need order and reliability; audio needs to arrive on time. One connection cannot be tuned for both.

WebRTC separates signaling (reliable) from media (UDP, SRTP) for exactly these reasons. Our design does the same with lighter tools.

The design: MQTT for control, UDP for audio

ChannelRuns onCarries
MQTT over TLS, port 8883TCPSession hello and goodbye, listening and speaking state, display content, device tool calls, errors
UDP, port 8884UDPOpus frames in both directions, each packet encrypted and authenticated

In the middle sits the gateway, a Node.js service doing three jobs at once: an MQTT server for devices, a UDP server for audio, and a WebSocket client into the runtime's voice endpoint. To the runtime, a device looks exactly like a browser: the same hello message, the same binary Opus frames, the same pipeline of VAD, speech recognition, model, speech synthesis, or the Realtime mode. Every upgrade shipped for the web is available on devices automatically.

ESP32 ──MQTT/TLS──▶ ┌─────────┐ ──WebSocket──▶ runtime (/voice/ws)
      ◀──UDP/GCM──  │ gateway │ ◀──WebSocket──
                    └─────────┘ ◀──HTTP 1884── runtime (online status, MCP commands)

One session from start to finish

  1. OTA check-in. Every time it gets Wi-Fi, the device calls the OTA endpoint with a Device-Secret header. The response carries the server time, an activation code if the device is not bound yet, the MQTT connection details, and a firmware update when one exists. See how devices connect.
  2. MQTT connect. The client ID has the form GID@@@mac@@@uuid, the username is base64-encoded JSON, and the password is an HMAC-SHA256 signature of those two fields under the gateway's key. The gateway compares the signature in constant time and closes the connection on a mismatch. Credentials expire after 30 days.
  3. hello. The device publishes a version 3 hello with its audio parameters and the UDP encryption it supports, currently aes-128-gcm.
  4. Bridge. The gateway opens a WebSocket to the runtime, forwards the hello, receives a session_id, and replies to the device on the topic devices/p2p/<mac> with the UDP address and port, a 16-byte key and a header template.
  5. Audio. Opus frames from the microphone travel over UDP to the gateway and on as binary WebSocket frames to the runtime; the spoken reply takes the reverse path. JSON messages (listening state, display text, tools) keep flowing over MQTT.
  6. goodbye. The session ends and the gateway logs the call duration.

All MQTT traffic runs at QoS 0, and the gateway closes the connection if a device publishes at a higher level. MQTT already runs on TCP, so ordering and delivery within a connection come for free; QoS 1 and 2 add state the board has to keep for very little in return. Session state lives in the hello and goodbye pair, not in the protocol layer. The MQTT specification is the reference if you want to check.

The UDP packet: 16-byte header and AES-128-GCM

Every audio packet has the same frame:

header (16 bytes)            ‖ ciphertext (Opus) ‖ GCM tag (16 bytes)
type=1 | flags | payload_len(2) | connection_id(4) | timestamp(4) | sequence(4)

The key is handed over in the hello reply on MQTT, which means over TLS, the same way WebRTC hands SRTP keys over the signaling channel. The 12-byte nonce is built from the direction (one byte, 0x01 server to device, 0x02 the other way) and the packet sequence number, so it never repeats within a direction. The header is passed as additional authenticated data (AAD), so nobody can change the connection_id or the sequence without breaking the tag. Receiver state advances only after the tag verifies, and a packet that jumps more than 1,000 sequence numbers ahead is dropped.

// GCM nonce: direction | 3 zero bytes | sequence (u32 BE) | 4 zero bytes
function udpGcmNonce(direction, sequence) {
  const nonce = Buffer.alloc(12)
  nonce.writeUInt8(direction, 0)
  nonce.writeUInt32BE(sequence, 4)
  return nonce
}

Encryption alone is not enough. A mode like AES-CTR keeps the content private but proves nothing about where a packet came from: a forged packet could redirect the downlink or poison the sequence counter. GCM, the standard AEAD mode from NIST SP 800-38D, closes both holes for 16 bytes per packet.

Audio: Opus, 60 ms frames

Speech travels as mono Opus in 60 ms frames, with the server side at 24 kHz. A 60 ms frame sends a third of the packets that 20 ms would, cutting header overhead per second of audio and the number of times the board has to wake up, while the extra 40 ms of latency stays below what people notice in conversation. There is no retransmission: Opus conceals a lost frame by interpolation (RFC 6716), and a late frame is dropped. Voice activity detection runs on the server, so the device can be interrupted; whether barge-in works in practice depends on echo cancellation on the board, see AEC for AI voice assistants.

Device tools: MCP over MQTT

Firmware declares hardware functions as tools using the MCP protocol: volume, lights, sensors, motors. When a device connects, the gateway asks for tools/list and keeps the list in memory. The runtime has an internal API on the gateway (port 1884) to check whether a device is online and to forward tools/call; the model calls these tools mid-conversation like any other tool. Details in device tools.

The security model

  • One secret per device. Issued once at activation; the server stores only its SHA-256 and issues connection details only when the secret verifies. A bound device without its secret gets nothing, not even an activation code.
  • MQTT over TLS only on port 8883; the gateway refuses to start without certificates.
  • Signed, expiring credentials. The HMAC signature is mandatory, compared in constant time, and expires after 30 days.
  • Every audio packet authenticated with AES-128-GCM, the header as AAD and a replay window.
  • Signed firmware. A detached RSA-SHA256 signature produced offline; the server verifies it on upload, the device verifies the digest and signature before installing, and non-HTTPS URLs are refused. New builds reach devices through OTA.

Measurements on ESP32-S3

Measured internally on an ESP32-S3 development board with the same voice pipeline, comparing the two transports:

MetricWebSocketMQTT + UDP
Voice round-trip latency380 ms165 ms
RAM for the transport layer45 KB12 KB
Reconnect time4–8 s1–2 s
Battery lifebaseline+40%

When we still use WebSocket

Browsers and the web app talk directly to the runtime's WebSocket endpoint, and so does the HarmonyOS watch. The simple rule: if the device is a browser, WebSocket; if it is RAM-constrained hardware that needs voice, MQTT plus UDP. Either way, the runtime side is one voice protocol, and its two modes, Standard and Realtime, are compared in speech-to-speech vs ASR → LLM → TTS.

To try it on real hardware, pick a supported board and flash the firmware from your browser.

Ready to try Assistant Core?

Build an assistant from your documents and put it on Zalo, Messenger, and your website in minutes. Start free.

Create a free assistant