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.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolOpen 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.
sketch.ino
- Use the launch button above to open the ESP32 editor in Wokwi.
- Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
- Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.
Steps
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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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.
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 checkA 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