ESP32sensoroutboxWi-Fi[0][1][2][3][4]broker :18831esp_mqtt_client_start(client);2float t = read_temp();3snprintf(buf, 8, "%.1f", t);4esp_mqtt_client_publish(client,5 TOPIC, buf, 0, 1, 0);6vTaskDelay(pdMS_TO_TICKS(10000));dashboardwait 10 s21.5CONNECTCONNACK 0PUBLISH "21.5""21.5"PUBACK 1PUBLISH "21.5"DUP "21.5"PUBACK 1
Back: the ESP32 node (sensor, text buffer, outbox), the Wi-Fi router and the brokerFront: the firmware loop in C and the subscribing dashboardEnvelopes: MQTT packets (CONNECT, CONNACK, PUBLISH, PUBACK); dashed line: the open TCP connectionAn ESP32-DevKitC sensor node that has already joined Wi-Fi opens one MQTT connection to a broker (CONNECT, CONNACK). Every 10 s it reads 21.5, formats the text "21.5" and publishes a 27-byte packet at QoS 1 on home/kitchen/temp; the broker forwards it to the dashboard and answers with a PUBACK. Until that PUBACK arrives the client keeps a copy in its outbox, and resends it with the DUP flag after a reconnect.

Connect

  1. Our ESP32-DevKitC has already joined Wi-Fi. Now it should report the kitchen temperature. It will not send to the dashboard directly: it sends to a broker, a server in the middle, using MQTT, and the broker passes it on.

Publish one reading

Stay connected

What happens

;; step 1/10 · node
board     ESP32-DevKitC (ESP-IDF, esp-mqtt client, C)
Wi-Fi     already joined: an IP address on the home network
broker    mqtt://192.168.1.20  (TCP port 1883)
topic     home/kitchen/temp
dashboard subscribes to that topic at the broker

The card shows the work of the loop. The full program also sets up the client (esp_mqtt_client_init with the broker URI mqtt://192.168.1.20), registers an event handler that sets up = true on MQTT_EVENT_CONNECTED, and only publishes while up is true. The packet sizes follow MQTT 3.1.1, the esp-mqtt default (MQTT 5 can be switched on in menuconfig and adds a properties field). The packet id is chosen by the client; 1 is shown as an example.