Skip to content

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., your practice guide

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 Tool

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

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

723 bytes · Circuit connections

Download

sketch.ino

2,155 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
    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: The PIR OUT connects to GPIO 13. The ring marks the PIR.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 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: The Wi-Fi and broker settings and ROOM_ID define this exercise. The ring marks those settings.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 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: Wi-Fi connected and MQTT Connected appear. The ring marks the MQTT connection.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 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: Motion detected! appears. The ring marks the motion event.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 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: Motion cleared and Auto turning off light appear once. The ring marks 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.

  1. What is the main design value of MQTT publish-subscribe in an IoT deployment?

    Return to the chapter’s knowledge check
  2. A 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

Caution

Use only simulated LEDs and non-sensitive test messages on test.mosquitto.org:1883. Set the same unique ROOM_ID (up to 24 characters) in both sketches before starting; client IDs have different role prefixes. The public broker has no topic privacy or delivery guarantee. Start the subscriber first, keep both simulator tabs open, and stop both after the exercise. Commands are not retained; a disconnected actuator can miss OFF and remain lit. Never connect this exercise to a real relay or mains load.

Return to MQTT Publish-Subscribe Basics · Browse Labs