Bird
Raised Fist0
IOT Protocolsdevops~6 mins

Device shadow (digital twin) in IOT Protocols - Full Explanation

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
Introduction
Imagine trying to control or check the status of a device that is far away or temporarily offline. Without a way to keep track of its state, managing such devices becomes confusing and unreliable. Device shadows solve this problem by acting as a virtual copy of the device's current state.
Explanation
Virtual Representation
A device shadow is a virtual version of a physical device stored in the cloud or a server. It keeps track of the device's last known state, even when the device is offline or unreachable. This allows applications to interact with the device's state without needing the device to be connected at that moment.
Device shadows provide a virtual copy of a device's state that is always accessible.
Desired and Reported States
The device shadow stores two main pieces of information: the desired state and the reported state. The desired state is what the user or application wants the device to be. The reported state is what the device actually is. The system compares these to know if the device needs to change.
Device shadows track both what is wanted and what the device currently reports.
Synchronization Process
When the device connects, it checks the shadow for any differences between desired and reported states. It then updates itself to match the desired state and reports back its new state. This keeps the shadow and device in sync, even if the device was offline for a while.
Device shadows help synchronize device state changes smoothly after disconnections.
Use in IoT Systems
Device shadows are widely used in Internet of Things (IoT) systems to manage many devices remotely. They simplify communication by letting applications read and write device states without direct device interaction. This improves reliability and user experience.
Device shadows enable reliable remote management of IoT devices.
Real World Analogy

Imagine you have a smart home light bulb that you want to turn on while you are away. The light bulb might be offline when you send the command. A device shadow acts like a note on your fridge that says 'Turn the light on.' When the bulb comes back online, it reads the note and turns on accordingly.

Virtual Representation → The note on the fridge representing the light bulb's state
Desired and Reported States → The note saying 'Turn the light on' (desired) and the bulb actually being on or off (reported)
Synchronization Process → The bulb reading the note and changing its state when it returns home
Use in IoT Systems → Managing many smart devices in a home using notes to keep track of their states
Diagram
Diagram
┌───────────────┐       ┌───────────────┐
│   Application │──────▶│ Device Shadow │
└───────────────┘       └───────────────┘
                              ▲   │
                              │   │
                       Desired│   │Reported
                              │   ▼
                      ┌─────────────────┐
                      │  Physical Device │
                      └─────────────────┘
Diagram showing the application interacting with the device shadow, which synchronizes with the physical device's desired and reported states.
Key Facts
Device ShadowA virtual copy of a physical device's state stored remotely.
Desired StateThe state that an application wants the device to have.
Reported StateThe actual state the device reports back.
SynchronizationThe process of updating the device to match the desired state and reporting back.
Offline Device HandlingDevice shadows allow state changes to be queued when devices are offline.
Common Confusions
Device shadow is the device itself.
Device shadow is the device itself. The device shadow is only a virtual copy stored remotely; the physical device is separate and may be offline.
Desired state always matches reported state instantly.
Desired state always matches reported state instantly. There can be delays; the device updates reported state only when it connects and processes changes.
Summary
Device shadows act as virtual copies of physical devices to track their state remotely.
They store desired and reported states to manage device behavior and synchronization.
This system allows reliable control and monitoring of devices even when they are offline.

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