Subscribe to commands and report the light state
Use a separate ESP32 subscriber to turn a simulated LED on and off in response to the companion motion publisher.

Broker Bex: I want you to compare the received command with the LED and the state it reports.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolOpen 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.
sketch.ino
- Use the launch button above to open the ESP32 editor in Wokwi.
- Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
- Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.
Steps
Step 1
- Do
- Open Wokwi, paste this lab’s diagram.json and sketch.ino, and add PubSubClient in Library Manager. Inspect the GPIO 2 LED path.
- You will see
- Input: inspect the subscriber diagram in the running Wokwi editor. The ESP32 GPIO 2 lead runs to the green LED at upper right. The LED return includes a 220 ohm resistor and ground connection. Serial Monitor shows the ESP32 boot followed by Wi-Fi retry dots.
- Why it matters
- The modeled subscriber has a visible output for later commands. The resistor sits in series with the LED path. The boot text confirms this is the live simulator window. The retry dots at this moment do not prove broker connection.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- In Wokwi's sketch editor, choose the same unique ROOM_ID in this sketch and /labs/home-motion-publish.html; inspect the callback and topic strings before starting.
- You will see
- Input: inspect the subscriber sketch in the running Wokwi editor. ROOM_ID is recap1-20261009-a, shared with the paired publisher run. The callback writes GPIO 2 HIGH for ON and LOW for OFF. Separate light_command_topic and light_state_topic buffers are visible.
- Why it matters
- The callback turns a received payload into a modeled LED state. The topic buffers separate incoming commands from reported state. Both paired controllers used the same unique room identifier. The Serial Monitor is still retrying Wi-Fi in this capture.

Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 3
- Do
- Start this subscriber first and open Serial Monitor. Then start the companion motion publisher in a second tab with the matching ROOM_ID.
- You will see
- Input: start the subscriber before the paired Wokwi publisher. Serial Monitor: Wi-Fi connected. The connection line says Connecting to MQTT... Connected. Subscription: home/recap1-20261009-a/light/command; the LED is off.
- Why it matters
- The new room id is shared with the publisher in this run. The subscriber reports broker connection before any motion command. The LED begins dark in the real Wokwi circuit. A non-retained ON command requires an active subscriber to observe it.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- In the publisher tab, click the PIR and Simulate motion. Return to this subscriber’s Serial Monitor.
- You will see
- Input: click the publisher PIR and Simulate motion. Publisher Serial Monitor: Motion detected! Subscriber Serial Monitor: Received command: ON. The next subscriber line says Light turned ON.
- Why it matters
- The paired tabs used the same room id. The subscriber callback received the ON command. The LED lights in the same real browser run. The serial result goes beyond an initial broker connection.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- On the Wokwi circuit canvas, inspect the subscriber’s LED while the motion event is active.
- You will see
- Input: inspect the subscriber Wokwi circuit after motion starts. The green LED at the top right glows. Serial Monitor still shows Received command: ON. The next line confirms Light turned ON.
- Why it matters
- The lit LED is a visible simulated output. The ON line shows the associated command reached the callback. The two observations agree within the same browser capture. This checks the modeled actuator, not a physical relay.

Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 6
- Do
- Wait for the publisher’s motion to clear and its 30-second timer to expire. Read this subscriber’s Serial Monitor.
- You will see
- Input: wait for motion to clear and the publisher timer to expire. Publisher Serial Monitor: Auto turning off light. Subscriber Serial Monitor: Received command: OFF. The next line says Light turned OFF and the green LED is dark.
- Why it matters
- The OFF command is delivered to the connected subscriber. The callback changes the modeled LED back to dark. The paired run shows both ON and OFF transitions. A disconnected subscriber could miss a non-retained OFF command.

Step 6 · 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.
What is the main design value of MQTT publish-subscribe in an IoT deployment?
Return to the chapter’s knowledge checkA dashboard needs all temperature readings from greenhouse 2, but not other sites or other signal types. Which subscription is the best fit?
Return to the chapter’s knowledge check