Skip to content

Publish temperature on a unique MQTT topic

Publish changing DHT22 readings every five seconds under the ESP32’s randomly generated MQTT topic root.

Broker Bex: I want you to follow one reading from its sensor value to its own topic and distinguish a publish result from subscriber delivery., your practice guide

Broker Bex: I want you to follow one reading from its sensor value to its own topic and distinguish a publish result from subscriber delivery.
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

Tier 1 · Web · No account

Version tested: Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-09. Date: 2026-09-09.

Open the ESP32 editor, paste diagram.json, then paste sketch.ino.

Open Wokwi to paste in the files (new tab)

Get the files

Use both prepared files. This is a paste-in setup; saving a project requires a Wokwi account.

diagram.json

1,065 bytes · Circuit connections

Download

sketch.ino

4,028 bytes · ESP32 program

Download

  1. Use the launch button above to open the ESP32 editor in Wokwi.
  2. Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
  3. Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.

Steps

Screens captured against Wokwi ESP32 Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-09 on 2026-09-09; the tool may have moved on — the text steps are the contract.

  1. 1 Step 1

    Do
    Paste the diagram and sketch into Wokwi ESP32. Install PubSubClient and DHT sensor library in Library Manager. Inspect the sensor wiring.
    You will see
    Power: DHT22 VCC connects to ESP32 3.3 V (diagram.json line 47). Ground: DHT22 GND connects to ESP32 ground (diagram.json line 53). Data: DHT22 SDA connects to ESP32 GPIO4 (diagram.json line 59). A 10 kΩ pull-up joins DHT22 data to 3.3 V (diagram.json lines 25, 65, 71).
    Why it matters
    The later publisher reads DHT22 data on GPIO4. Its 10 kΩ pull-up returns the data line high between transactions, while the 3.3 V and ground paths power the sensor.
    Step 1: DHT22 data uses GPIO 4 and a 10 kilohm pull-up. The ring marks the sensor and resistor.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 Step 2

    Do
    On Wokwi's simulation toolbar, start the simulation at 115200 baud and wait for Wi-Fi and broker connection.
    You will see
    Input: start the supplied ESP32 publisher in Wokwi. Observed: "Unique MQTT ClientID: iotlab-8A7A44F9903E". Observed: "Your Lab 1 topic root: iotclass/lab1/8A7A44F9903E". Observed: "Connecting to MQTT broker... Connected".
    Why it matters
    The same boot suffix appears in the client and topic root. The broker connection completed in this run. This suffix belongs only to this boot. A later restart generated a different suffix.
    Step 2: the real Wokwi monitor shows ClientID iotlab-8A7A44F9903E, matching topic root, and Connected; the ring marks these lines.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 Step 3

    Do
    In the Serial Monitor panel, keep consecutive publisher groups after the broker connects.
    You will see
    Input: DHT22 left at 24 C and 40% RH. Observed: "Temperature: 24.00C OK". Observed: "Humidity: 40.00% OK". Observed: "---" separates the next publication group.
    Why it matters
    The run publishes both sensor quantities. The suffix OK is the publish result printed by this sketch. The separator makes successive groups visible. The monitor does not prove that a subscriber received them.
    Step 3: Two consecutive groups show Temperature: 24.00C OK and Humidity: 40.00% OK. The ring marks the sensor publication groups.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 Step 4

    Do
    In the DHT22 control panel, set temperature to 30 C and leave humidity at 40%.
    You will see
    Input: DHT22 temperature 30 C, humidity 40%. Observed: "Temperature: 30.00C OK". Observed: "Humidity: 40.00% OK". Expected: temperature changes and humidity stays at 40%.
    Why it matters
    The 30 C line follows the changed control. The humidity line stays at its baseline value. The two readings are published separately. The real capture shows this same 30 C and 40% pair.
    Step 4: Temperature: 30.00C OK appears while humidity remains 40.00%. The ring marks the updated publication.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 Step 5

    Do
    In the DHT22 control panel, set humidity to 60% and leave temperature at 30 C.
    You will see
    Input: DHT22 humidity 60%, temperature 30 C. Observed: "Temperature: 30.00C OK". Observed: "Humidity: 60.00% OK". Expected: the humidity line changes while temperature stays at 30 C.
    Why it matters
    The 60% line follows the changed control. The temperature line stays at 30 C. The real capture shows the same final pair. The run shows publication success, not a subscriber receipt.
    Step 5: The next group shows Temperature: 30.00C OK and Humidity: 60.00% OK. The ring marks the changed humidity and unchanged temperature.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 Step 6

    Do
    Compare make_lab_identity and the publish calls in the sketch with the topic root printed in Serial Monitor.
    You will see
    The root has /temperature, /humidity, /status, and /command suffixes. Sensor publishes pass false for retained state, while the online status uses true.
    Why it matters
    Topic naming and retained state are distinct choices. This starter publishes sensor values at QoS 0; the Boolean argument is the retain flag, not a QoS setting.
    Step 6: Source excerpts show the four topic suffixes, retained online status true, and sensor publish flags false; intervening sections are collapsed. The ring marks those source lines.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 Step 7

    Do
    On the Wokwi circuit canvas, restore the DHT22 to 24 °C and 40% RH, confirm the readings, then stop and restart once.
    You will see
    Input: restore 24 C and 40% RH, then stop and restart. Observed: "Unique MQTT ClientID: iotlab-B5477AE40986". Observed: "Temperature: 24.00C OK" and "Humidity: 40.00% OK". Observed: "Connecting to MQTT broker... Connected".
    Why it matters
    The restored sensor readings appeared after restart. The new suffix differs from the first boot's 8A7A44F9903E. A client using the old topic root would miss this new root. The monitor shows publish success, not subscriber receipt.
    Step 7: after restart, the real Wokwi monitor shows new ClientID B5477AE40986 and restored 24 C, 40% readings; the ring marks both.
    Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)

Chapter checks

These questions refer to the chapter’s examples. Use the return links to review their answers.

  1. In an MQTT-based temperature monitoring system, an ESP32 sensor node reads temperature data and sends it to a broker. A Python dashboard running on a laptop displays the readings. Which statement correctly identifies each component's role?

    Return to the chapter’s knowledge check
  2. This chapter's 'Common Mistake' warning concerns the unauthenticated, plaintext port 1883 listener at test.mosquitto.org. Which of the following is one of its four production dangers?

    Return to the chapter’s knowledge check

Caution

Use synthetic sensor values only. test.mosquitto.org:1883 is a public plaintext test broker and can be unavailable; never enter personal data or production credentials. The Wi-Fi placeholders are configured for Wokwi-GUEST with an empty password. This source publishes at QoS 0, uses blocking reconnect loops, and retains online status without a Last Will, so that retained status can become stale after disconnection. Each restart changes the topic root.

Return to MQTT Publisher-Subscriber Setup · Browse Labs