Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Device Shadow (Digital Twin) Simulation
📖 Scenario: You are managing smart home devices remotely. Each device has a device shadow, a digital copy of its state stored in the cloud. This shadow helps you track and control the device even when it is offline.In this project, you will simulate a simple device shadow using a dictionary to represent the device's reported and desired states.
🎯 Goal: Build a small program that creates a device shadow dictionary, sets a desired state, updates the reported state, and finally prints the current shadow state.
📋 What You'll Learn
Create a dictionary called device_shadow with keys reported and desired, both starting as empty dictionaries.
Add a desired state for temperature set to 22 degrees.
Update the reported state with temperature set to 20 degrees.
Print the complete device_shadow dictionary to show current states.
💡 Why This Matters
🌍 Real World
Device shadows are used in IoT to keep a cloud copy of a device's state. This helps users control devices remotely and handle offline scenarios.
💼 Career
Understanding device shadows is important for IoT developers and DevOps engineers managing connected devices and cloud services.
Progress0 / 4 steps
1
Create the initial device shadow dictionary
Create a dictionary called device_shadow with two keys: reported and desired. Both should be empty dictionaries.
IOT Protocols
Hint
Think of device_shadow as a box with two smaller boxes inside: one for what the device says (reported) and one for what you want it to do (desired).
2
Set the desired temperature state
Add a key temperature with value 22 inside the desired dictionary of device_shadow.
IOT Protocols
Hint
Use device_shadow["desired"]["temperature"] = 22 to set the desired temperature.
3
Update the reported temperature state
Add a key temperature with value 20 inside the reported dictionary of device_shadow.
IOT Protocols
Hint
Use device_shadow["reported"]["temperature"] = 20 to update the reported temperature.
4
Print the device shadow
Print the device_shadow dictionary to display the current reported and desired states.
IOT Protocols
Hint
Use print(device_shadow) to show the current device shadow.
Practice
(1/5)
1. What is the main purpose of a device shadow in IoT?
easy
A. To keep a virtual copy of a device's state for synchronization
B. To physically store device hardware components
C. To replace the device's firmware remotely
D. To encrypt device communication data
Solution
Step 1: Understand device shadow concept
A device shadow is a virtual representation of a device's state, not a physical component or firmware.
Step 2: Identify its main use
It stores desired and reported states to keep device and cloud synchronized, especially when devices are offline.
Final Answer:
To keep a virtual copy of a device's state for synchronization -> Option A
Quick Check:
Device shadow = virtual state copy [OK]
Hint: Device shadow = virtual device state copy [OK]
Common Mistakes:
Confusing device shadow with physical device hardware
Thinking device shadow replaces device firmware
Assuming device shadow encrypts data
2. Which JSON field in a device shadow document holds the desired state of the device?
easy
A. "reported"
B. "desired"
C. "metadata"
D. "version"
Solution
Step 1: Recall device shadow JSON structure
The device shadow JSON has fields like "desired" and "reported" to represent states.
Step 2: Identify desired state field
The "desired" field contains the state the cloud wants the device to have.
Final Answer:
"desired" -> Option B
Quick Check:
Desired state = "desired" field [OK]
Hint: "desired" field stores target device state [OK]
What will the device shadow report after the device updates its state to match the desired power?
medium
A. {"state": {"desired": {"power": "off"}, "reported": {"power": "off"}}}
B. {"state": {"desired": {"power": "on"}, "reported": {"power": "off"}}}
C. {"state": {"desired": {"power": "on"}, "reported": {"power": "on"}}}
D. {"state": {"desired": {}, "reported": {"power": "on"}}}
Solution
Step 1: Understand initial shadow states
The desired state requests power "on"; the reported state shows power "off" initially.
Step 2: Update reported state after device changes
When the device matches the desired state, the reported state updates to "power": "on".
Final Answer:
{"state": {"desired": {"power": "on"}, "reported": {"power": "on"}}} -> Option C
Quick Check:
Reported state matches desired after update [OK]
Hint: Reported state updates to match desired after device sync [OK]
Common Mistakes:
Assuming desired state changes after device update
Leaving reported state unchanged
Removing desired state incorrectly
4. You receive this device shadow update error:
{"error": "Invalid JSON format"}
Which of these shadow update requests is causing the error?
medium
A. {"state": {"desired": {"temperature": 22}}}
B. {"state": {"desired": {"temperature": 22}, "reported": {"temperature": 22}}}
C. {"version": 1, "state": {"desired": {"temperature": 22}, "reported": {"temperature": 22}}}
D. {"state": {"desired": {"temperature": 22}, "reported": {"temperature": 22}"}}
Solution
Step 1: Check JSON syntax of each option
{"state": {"desired": {"temperature": 22}, "reported": {"temperature": 22}"}} has an extra double quote after the reported object closing brace, breaking JSON format.
Step 2: Identify invalid JSON causing error
Invalid JSON causes the "Invalid JSON format" error in device shadow update.
Final Answer:
{"state": {"desired": {"temperature": 22}, "reported": {"temperature": 22}"}} -> Option D
Quick Check:
Invalid JSON syntax = error [OK]
Hint: Look for extra or missing quotes/braces in JSON [OK]
Common Mistakes:
Overlooking extra quotes or commas
Confusing valid JSON with similar invalid syntax
Assuming error is from device, not JSON format
5. You want to ensure a device shadow only updates the reported state if the device is online. Which approach best achieves this?
hard
A. Use a device shadow update callback to confirm device state before updating reported state
B. Delete the device shadow when device is offline
C. Update reported state immediately on cloud request without device confirmation
D. Set desired state to null when device is offline
Solution
Step 1: Understand device shadow update flow
Reported state should reflect actual device state, so cloud must confirm device is online and updated.
Step 2: Choose method to confirm device state
Using a callback or acknowledgment from the device before updating reported state ensures accuracy.
Final Answer:
Use a device shadow update callback to confirm device state before updating reported state -> Option A
Quick Check:
Confirm device state before reported update [OK]
Hint: Confirm device state via callback before updating reported [OK]
Common Mistakes:
Updating reported state without device confirmation
Deleting shadow causing loss of state info
Setting desired state to null instead of managing reported