Bird
Raised Fist0
IOT Protocolsdevops~20 mins

Device shadow (digital twin) in IOT Protocols - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Device Shadow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Device Shadow States

Which of the following best describes the desired state in a device shadow?

AThe state the device should be in, as set by the user or application.
BThe current physical state of the device as detected by sensors.
CThe error state when the device fails to connect to the cloud.
DThe last reported state sent by the device to the cloud.
Attempts:
2 left
💡 Hint

Think about what the user or application wants the device to do.

💻 Command Output
intermediate
2:00remaining
Interpreting Device Shadow Update Response

What is the output of this AWS CLI command updating a device shadow?

aws iot-data update-thing-shadow --thing-name MyDevice --payload '{"state":{"desired":{"power":"on"}}}' MyDeviceShadow.json
AAn error: Invalid payload file path or unrecognized argument
BCommand executed successfully with no output
C{"state":{"desired":{"power":"on"}},"metadata":{"desired":{"power":{"timestamp":1680000000}}},"version":5}
D{"state":{"reported":{"power":"on"}},"version":5}
Attempts:
2 left
💡 Hint

Check the command syntax and required parameters for updating a device shadow.

🔀 Workflow
advanced
2:30remaining
Correct Sequence to Sync Device Shadow

Arrange the steps in the correct order to synchronize a device's physical state with its device shadow.

A1,3,2,4
B1,2,3,4
C2,1,3,4
D1,2,4,3
Attempts:
2 left
💡 Hint

Think about the logical flow from retrieving data to updating device and reporting back.

Troubleshoot
advanced
1:30remaining
Troubleshooting Shadow Update Failures

A device shadow update fails with a ConflictException. What is the most likely cause?

AThe device shadow service is disabled in the cloud account.
BThe device is offline and cannot reach the cloud service.
CThe device shadow JSON payload is missing the <code>state</code> key.
DThe device shadow document version is outdated compared to the cloud version.
Attempts:
2 left
💡 Hint

Think about version control and concurrency in device shadows.

Best Practice
expert
2:00remaining
Best Practice for Device Shadow State Management

Which approach is best to minimize conflicts and ensure smooth device shadow synchronization in a fleet of IoT devices?

ADevices update their shadow state immediately after every small sensor reading change.
BDevices never report state; only the cloud updates the shadow to avoid conflicts.
CDevices batch state changes and update the shadow periodically with the latest state.
DDevices delete and recreate their shadow document on every update to reset state.
Attempts:
2 left
💡 Hint

Consider network usage and conflict frequency when updating shadows.

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

  1. Step 1: Understand device shadow concept

    A device shadow is a virtual representation of a device's state, not a physical component or firmware.
  2. Step 2: Identify its main use

    It stores desired and reported states to keep device and cloud synchronized, especially when devices are offline.
  3. Final Answer:

    To keep a virtual copy of a device's state for synchronization -> Option A
  4. 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

  1. Step 1: Recall device shadow JSON structure

    The device shadow JSON has fields like "desired" and "reported" to represent states.
  2. Step 2: Identify desired state field

    The "desired" field contains the state the cloud wants the device to have.
  3. Final Answer:

    "desired" -> Option B
  4. Quick Check:

    Desired state = "desired" field [OK]
Hint: "desired" field stores target device state [OK]
Common Mistakes:
  • Mixing up "reported" and "desired" fields
  • Confusing "metadata" with state data
  • Thinking "version" holds state info
3. Given this device shadow snippet:
{"state": {"desired": {"power": "on"}, "reported": {"power": "off"}}}

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

  1. Step 1: Understand initial shadow states

    The desired state requests power "on"; the reported state shows power "off" initially.
  2. Step 2: Update reported state after device changes

    When the device matches the desired state, the reported state updates to "power": "on".
  3. Final Answer:

    {"state": {"desired": {"power": "on"}, "reported": {"power": "on"}}} -> Option C
  4. 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

  1. 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.
  2. Step 2: Identify invalid JSON causing error

    Invalid JSON causes the "Invalid JSON format" error in device shadow update.
  3. Final Answer:

    {"state": {"desired": {"temperature": 22}, "reported": {"temperature": 22}"}} -> Option D
  4. 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

  1. Step 1: Understand device shadow update flow

    Reported state should reflect actual device state, so cloud must confirm device is online and updated.
  2. Step 2: Choose method to confirm device state

    Using a callback or acknowledgment from the device before updating reported state ensures accuracy.
  3. Final Answer:

    Use a device shadow update callback to confirm device state before updating reported state -> Option A
  4. 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