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

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
    Input: inspect the subscriber diagram in the running Wokwi editor. The ESP32 GPIO 2 lead runs to the green LED at upper right. The LED return includes a 220 ohm resistor and ground connection. Serial Monitor shows the ESP32 boot followed by Wi-Fi retry dots.
    Why it matters
    The modeled subscriber has a visible output for later commands. The resistor sits in series with the LED path. The boot text confirms this is the live simulator window. The retry dots at this moment do not prove broker connection.
    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
    In Wokwi's sketch editor, 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
    Input: inspect the subscriber sketch in the running Wokwi editor. ROOM_ID is recap1-20261009-a, shared with the paired publisher run. The callback writes GPIO 2 HIGH for ON and LOW for OFF. Separate light_command_topic and light_state_topic buffers are visible.
    Why it matters
    The callback turns a received payload into a modeled LED state. The topic buffers separate incoming commands from reported state. Both paired controllers used the same unique room identifier. The Serial Monitor is still retrying Wi-Fi in this capture.
    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
    Input: start the subscriber before the paired Wokwi publisher. Serial Monitor: Wi-Fi connected. The connection line says Connecting to MQTT... Connected. Subscription: home/recap1-20261009-a/light/command; the LED is off.
    Why it matters
    The new room id is shared with the publisher in this run. The subscriber reports broker connection before any motion command. The LED begins dark in the real Wokwi circuit. A non-retained ON command requires an active subscriber to observe it.
    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
    Input: click the publisher PIR and Simulate motion. Publisher Serial Monitor: Motion detected! Subscriber Serial Monitor: Received command: ON. The next subscriber line says Light turned ON.
    Why it matters
    The paired tabs used the same room id. The subscriber callback received the ON command. The LED lights in the same real browser run. The serial result goes beyond an initial 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
    On the Wokwi circuit canvas, inspect the subscriber’s LED while the motion event is active.
    You will see
    Input: inspect the subscriber Wokwi circuit after motion starts. The green LED at the top right glows. Serial Monitor still shows Received command: ON. The next line confirms Light turned ON.
    Why it matters
    The lit LED is a visible simulated output. The ON line shows the associated command reached the callback. The two observations agree within the same browser capture. This checks the modeled actuator, not a physical relay.
    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
    Input: wait for motion to clear and the publisher timer to expire. Publisher Serial Monitor: Auto turning off light. Subscriber Serial Monitor: Received command: OFF. The next line says Light turned OFF and the green LED is dark.
    Why it matters
    The OFF command is delivered to the connected subscriber. The callback changes the modeled LED back to dark. The paired run shows both ON and OFF transitions. A disconnected subscriber could miss a non-retained OFF command.
    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