Publish motion and a timed light command
Publish PIR motion and verify one ON/OFF command pair reaches the companion light controller.

Broker Bex: I want you to trace the motion event through its topic to the light’s response.
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 and paste this lab’s diagram.json and sketch.ino. Add PubSubClient using Library Manager; inspect the PIR wiring.
- You will see
- Input: inspect the publisher diagram in the running Wokwi editor. The PIR module is visible at upper right beside the ESP32. Its VCC, GND, and OUT leads reach VIN, ground, and GPIO 13. Serial Monitor shows ESP32 boot text and Wi-Fi retry dots.
- Why it matters
- The publisher models the sensing side of the paired exercise. GPIO 13 carries the PIR's simulated digital motion signal. The boot text confirms this circuit is in the real simulator. No motion event or broker connection is claimed from this frame.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- In the Wokwi simulation panel, in both this sketch and /labs/home-light-subscribe.html, choose the same unique ROOM_ID. Keep Wokwi-GUEST, an empty Wi-Fi password, and test.mosquitto.org; start the companion subscriber first.
- You will see
- Input: inspect the publisher sketch in the running Wokwi editor. It sets Wokwi-GUEST, an empty password, and test.mosquitto.org. ROOM_ID reads recap1-20261009-a, matching the subscriber. The source formats home/%s/motion and home/%s/light/command.
- Why it matters
- The shared room string scopes messages to the paired controllers. Motion and light commands have separate topic buffers. The source uses its own publisher client name in reconnect(). The Serial Monitor is 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 simulation, open Serial Monitor, and wait for the MQTT connection. Keep the subscriber running in its other tab.
- You will see
- Input: start the publisher after the paired subscriber is prepared. Observed: "Wi-Fi connected". Observed: "Connecting to MQTT... Connected". Expected: the browser Wokwi guest network and broker session connect.
- Why it matters
- The run used the same unique ROOM_ID in both browser tabs. The connection lines precede any PIR event. They prove only simulator connectivity for this session. Broker reachability is not a guarantee for later users.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- In the publisher Simulation panel, click the PIR and Simulate motion once; read both controllers’ Serial Monitor panels.
- You will see
- Input: click the PIR and choose Simulate motion in Wokwi. Observed in publisher: "Motion detected!". Observed in subscriber: "Received command: ON" and "Light turned ON". Expected: one motion pulse leads to an ON command.
- Why it matters
- The two real Serial Monitors show the message path. The subscriber acknowledged the ON command in this run. The PIR event is simulated, not a physical movement. The unique room string confines this paired exercise.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- In the publisher Serial Monitor, wait for Motion cleared and the 30-second auto-off; check the paired subscriber Serial Monitor for OFF.
- You will see
- Input: leave the PIR idle until its pulse clears and the 30-second auto-off fires. Observed: "Motion cleared". Observed: "Auto turning off light". Observed in subscriber: "Received command: OFF" and "Light turned OFF".
- Why it matters
- The auto-off followed the detected motion in the live run. The paired subscriber received the OFF command. The screenshot marks the same publisher auto-off line. The 60-second run cap precluded an extra 30 idle seconds after auto-off.

Step 5 · 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