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

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
    Power: DHT22 VCC connects to ESP32 3.3 V; GND returns to ground (diagram.json lines 75, 81). DHT22 data connects to ESP32 GPIO4 (diagram.json line 87). LED anode connects to ESP32 GPIO2; its cathode returns through ledR to ground (diagram.json lines 93, 99, 105). The LED’s series resistor ledR is 220 Ω (diagram.json line 34). Sync button joins ESP32 GPIO15 to 3.3 V when pressed (diagram.json lines 111, 117). A 10 kΩ syncR resistor pulls the sync input to ground (diagram.json lines 53, 123, 129).
    Why it matters
    GPIO4 supplies the physical temperature and humidity readings, while GPIO2 exposes the LED state. The GPIO15 button and its 10 kΩ pull-down provide a definite sync input for later mirror updates.
    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
    In the Simulation toolbar, start the run and read the first Serial Monitor sync cycle.
    You will see
    Input: DHT22 at 24 C and 40% RH. Observed: "[PHYSICAL] Temp: 24.0C, Humidity: 40.0%, LED: OFF". Observed: "[TWIN] State synchronized successfully". Expected: the physical reading enters the twin mirror.
    Why it matters
    The first sensor values are 24.0 C and 40.0%. The physical LED is OFF. The synchronization line follows the physical read. This is a simulated mirror update.
    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
    In the Serial Monitor panel, compare Physical Device State and Digital Twin Mirror.
    You will see
    Input: keep the DHT22 at 24 C and 40% RH. Observed physical: "Temperature: 24.0 C" and "Humidity: 40.0 %". Observed mirror: "Temperature: 24.0 C (synced)" and "Humidity: 40.0 % (synced)". Expected: both views show the same sensor pair.
    Why it matters
    The physical temperature is 24.0 C. The mirror temperature is also 24.0 C. Both humidity values are 40.0%. The mirror marks both values as synced.
    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
    In the Serial Monitor panel, follow the stable DHT22 history until the buffer is full.
    You will see
    Input: hold DHT22 at 24 C and 40% RH. Observed: "[HISTORY] Recorded state 12 (buffer: 10/10 entries)". Observed: "[ANALYTICS] Avg Temp: 24.0C, Trend: +0.00C/cycle". Observed: "[PREDICTION] In 3 cycles, predicted temp: 24.0C".
    Why it matters
    The ten-entry history is full. The average stays at the input temperature. The computed trend is zero per cycle. The forecast stays at 24.0 C while input is stable.
    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
    In the DHT22 control panel, set temperature to 35 C; read the next Serial Monitor metadata.
    You will see
    Input: DHT22 temperature 35 C, humidity 40%. Observed: "[COMMAND] Twin activating LED due to high temperature". Observed: "Predicted: 46.0 C (in 3 cycles)" and "Sync Count: 14". Observed: "Sync Latency: 41.00 ms" and "Status: WARNING: High Temp".
    Why it matters
    The high temperature crossed the model alert threshold. The twin commanded the LED ON. The prediction reflects the sudden rising trend. The warning and sync metadata match the real capture.
    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
    In the DHT22 control panel, restore 24 C and wait for ten new stable history samples.
    You will see
    Input: DHT22 restored to 24 C and 40% RH. Observed: "Predicted: 24.0 C (in 3 cycles)" and "Sync Count: 26". Observed: "Sync Latency: 30.00 ms" and "Status: NORMAL". Observed: "LED State: ON" remains after the warning clears.
    Why it matters
    The average and forecast return to baseline. The warning state clears to NORMAL. The LED remains ON in the mirror and physical state. The output shows no automatic deactivate command.
    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
    In Wokwi's sketch editor, 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
    In the Simulation toolbar, stop and start again at 24 C and 40% RH; read the first Serial Monitor metadata.
    You will see
    Input: fresh browser simulation at 24 C and 40% RH. Observed: "[PHYSICAL] Temp: 24.0C, Humidity: 40.0%, LED: OFF". Observed: "Sync Count: 1" and "Sync Latency: 30.00 ms". Observed: "Status: NORMAL" on the first mirror cycle.
    Why it matters
    The new run resets the cycle counter. The LED starts OFF. The sensor pair is back at baseline. The first mirror status is NORMAL.
    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