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 ToolUse a separate ESP32 subscriber to turn a simulated LED on and off in response to the companion motion publisher.
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.
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
- GPIO 2 drives the LED through a 220 ohm resistor, with its cathode connected to ground.
- Why it matters
- The subscriber turns received commands into a visible output, separate from the sensor.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- 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
- The callback accepts ON and OFF; the subscribed path ends in light/command and the reported path in light/state.
- Why it matters
- Commands and reported state have different meanings and therefore use separate topics.

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
- Wi-Fi connected and Subscribed to: home/ROOM_ID/light/command appear; the LED starts off.
- Why it matters
- A non-retained command reaches a currently connected subscriber; starting in order avoids missing the first command.

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
- Received command: ON and Light turned ON appear.
- Why it matters
- The callback’s output is evidence of delivery and actuation, beyond a successful broker connection.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- Inspect the subscriber’s LED while the motion event is active.
- You will see
- The green LED is illuminated.
- Why it matters
- Checking the output alongside the log distinguishes the message from the visible simulated action.

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
- Received command: OFF and Light turned OFF appear; the LED goes dark.
- Why it matters
- The subscriber acts on a delivered OFF command. Its retained state report is a snapshot, not a history of the motion event.

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