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 ToolPublish PIR motion and verify one ON/OFF command pair reaches the companion light controller.
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 and paste this lab’s diagram.json and sketch.ino. Add PubSubClient using Library Manager; inspect the PIR wiring.
- You will see
- PIR VCC connects to VIN, GND to ground, and OUT to GPIO 13.
- Why it matters
- The publisher owns sensing. It sends a topic and payload without addressing the actuator directly.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- 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
- The sketches use separate motion, light/command and light/state topics under home/ROOM_ID.
- Why it matters
- A shared topic contract connects independent controllers; distinct client IDs prevent session replacement.

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
- Wi-Fi connected and Connecting to MQTT... Connected appear.
- Why it matters
- A connection establishes the broker path; the next steps test whether the intended consumer receives a command.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- Click the PIR, then Simulate motion once. Read both controllers’ Serial Monitors.
- You will see
- This node prints Motion detected!; the companion prints Light turned ON.
- Why it matters
- The PIR event causes a retained motion state and a non-retained ON command; the subscriber supplies actuation evidence.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- Wait for Motion cleared, then for the 30-second timer measured from Motion detected. Leave the PIR idle for another 30 simulated seconds.
- You will see
- Motion cleared and Auto turning off light appear; the companion prints Light turned OFF. Auto turning off light appears once for this event.
- Why it matters
- The timer belongs to the publisher. A pending flag prevents unsigned-time arithmetic from repeatedly publishing OFF; elapsed simulation time may differ from wall time.

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