Skip to content

Mirror sensor state and trigger a twin command

Compare a DHT22 reading with its local digital-twin mirror, history, prediction, and temperature-triggered LED command.

Blueprint Bina: I want you to compare the observed state, the model’s prediction, and the command that actually changes the LED., your practice guide

Blueprint Bina: I want you to compare the observed state, the model’s prediction, and the command that actually changes the LED.
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

Compare a DHT22 reading with its local digital-twin mirror, history, prediction, and temperature-triggered LED command.

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

1,885 bytes · Circuit connections

Download

sketch.ino

13,043 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
    Paste diagram.json and sketch.ino into Wokwi ESP32 and install DHT sensor library using Library Manager. Inspect the circuit.
    You will see
    DHT22 data is on GPIO 4, the red LED uses GPIO 2 through 220 Ω, and the GPIO 15 button has a 10 kΩ pull-down.
    Why it matters
    The host distinguishes telemetry, properties, and commands. These connections define which physical input and output the local model represents.
    Step 1: The DHT22, LED, and pulled-down sync button connect to the ESP32. The ring marks the sensor and control wiring.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 Step 2

    Do
    Start the simulation at 115200 baud and wait for the first periodic synchronization.
    You will see
    [PHYSICAL] Temp: 24.0C, Humidity: 40.0%, LED: OFF is followed by [TWIN] State synchronized successfully.
    Why it matters
    The SYNC_INTERVAL is 2000 ms. The host chooses cadence from decision needs rather than assuming the fastest possible update is always best.
    Step 2: The physical device reports 24.0C, 40.0%, LED OFF and the twin confirms synchronization. The ring marks the physical-to-twin update.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 Step 3

    Do
    Compare Physical Device State with Digital Twin Mirror in the status output.
    You will see
    Both temperatures read 24.0 C and both humidity values read 40.0%; the mirror marks its sensor values as synced.
    Why it matters
    A mirror should identify which observed values it represents. Here both objects live on one ESP32, so agreement does not prove a remote service is current.
    Step 3: The physical and mirrored values agree at 24.0 C and 40.0%. The ring marks both state blocks.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 Step 4

    Do
    Keep the DHT22 at 24 °C and 40% RH for at least ten periodic cycles.
    You will see
    The history buffer reaches 10/10 entries; the stable trend is +0.00 C/cycle and the three-cycle prediction is 24.0 C.
    Why it matters
    A bounded history supports a simple prediction while limiting memory. This extrapolation is a teaching model, not a validated model of physical equipment.
    Step 4: The history is 10/10, the trend is +0.00 C/cycle, and the forecast is 24.0 C. The ring marks the history and analytics output.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 Step 5

    Do
    Set the DHT22 temperature to 35 °C and wait for a valid updated sample.
    You will see
    The mirror reports 35.0 C, Status becomes WARNING: High Temp, and [COMMAND] Twin activating LED due to high temperature appears as the LED turns on.
    Why it matters
    This is a model-to-device command as well as device-to-model telemetry. The host requires the command’s physical result to be verified.
    Step 5: At 35.0 C, the twin activates the LED and reports WARNING: High Temp. The ring marks the command and resulting twin state.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 Step 6

    Do
    Set temperature back to 24 °C, leave humidity at 40%, and wait until ten new stable samples replace the history.
    You will see
    Temperature, average, and prediction return to 24.0 C and Status returns to NORMAL, but the LED remains ON.
    Why it matters
    The supplied policy only activates the LED on high temperature; it has no automatic clearing command. A current sensor value alone does not explain a latched actuator state.
    Step 6: After ten stable replacement samples, temperature, average, and forecast are 24.0 C and status is NORMAL, while LED State remains ON. The ring marks the restored readings and persistent LED state.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 Step 7

    Do
    Inspect executeTwinCommands and compare its activation condition with the displayed LED state.
    You will see
    The condition is temperature > tempAlertHigh, with tempAlertHigh set to 30.0; there is no matching digitalWrite LOW in that function.
    Why it matters
    Observed state and commanded state have different owners. The host’s conflict rules require an explicit policy for a command that remains effective after its trigger ends.
    Step 7: The threshold is 30.0 C and executeTwinCommands only writes the LED HIGH, with no matching LOW branch. The ring marks the threshold and complete activation function; intervening source is collapsed.
    Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  8. 8 Step 8

    Do
    Stop and restart with the sensor at 24 °C and 40% RH.
    You will see
    The LED starts OFF, history restarts at 1/10 entries, and the first periodic mirror again reports 24.0 C and 40.0%.
    Why it matters
    Reset clears this local model’s memory. The host’s production freshness and quality contract would also need to distinguish resets, failed samples, and stale observations.
    Step 8: Restart restores LED OFF, a 1/10 history, and the 24.0 C, 40.0% baseline. The ring marks the first restarted cycle.
    Step 8 · 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. A team wants every sensor to stream to the digital twin once per second because faster sounds more accurate. What is the better design rule?

    Return to the chapter’s knowledge check
  2. A pump twin shows outlet pressure as normal, but that value was last observed 38 minutes ago. The twin is used to recommend whether an operator should restart the pump after a trip. What should the system do?

    Return to the chapter’s knowledge check

Caution

The twin and device are local objects on one ESP32; no cloud, real network delay, or remote command acknowledgement is tested. Sensor failures retain the last values while the source still updates sync metadata, so isOnline is not a freshness guarantee. The history trend is a simple difference calculation, not fitted regression. GPIO 15 edge detection is seeded at startup to avoid treating the initial pin level as a manual press. Keep both resistors; the LED command is latched until reset or a manual toggle.

Return to Digital Twin Sync and Modeling · Browse Labs