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 ToolCompare a DHT22 reading with its local digital-twin mirror, history, prediction, and temperature-triggered LED command.
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.
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
- 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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