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

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
    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: 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 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: 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
    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: 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
    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: 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
    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: 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