Skip to content

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

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 Tool

Use a separate ESP32 subscriber to turn a simulated LED on and off in response to the companion motion publisher.

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

883 bytes · Circuit connections

Download

sketch.ino

1,992 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, 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: GPIO 2 connects through 220 ohms to the LED. The ring marks the LED and resistor.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 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: The callback drives the LED for ON and OFF. The ring marks the command handling.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 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: The subscriber names its light/command topic. The ring marks the subscription.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 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: Received command: ON and Light turned ON appear. The ring marks the ON response.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 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: The green LED is on. The ring marks the illuminated LED.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 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: Received command: OFF and Light turned OFF appear. The ring marks the OFF response.
    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.

  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