15. Part - 2/2 - Learn AWS IOT Core with Node Red 🚥🏘🛎⚓⚙⛓

While I worked at AWS during the publishing of this post / video, the views expressed here are my own and may not reflect those of my employer. Only publicly available material is used to put together the content of this article and video. The website and the Youtube videos are NOT monetized.

You can directly scroll down for the Youtube Video, just make sure you have checked out the part - 1.

In this post we continue our quest for learning of AWS IOT core. We are focusing on device shadows and how they help accomplish our use case of immobilising our stolen car (please check out the part 1 in case you are not getting that reference).

Again as with part 1, since the video is extremely detailed one, the blog post is just to cover the agenda, sample code/ node-red flow and important links.

Part 1 Agenda (completed)

  1. What is IOT and MQTT ?
  2. AWS IOT walkthrough and creating a thing.
  3. What is Node Red? Installing Node Red.
  4. Using Node Red to pubish and subscribe messages
  5. AWS IOT Rules Demo

Part 2 Agenda

  1. Device Shadows and IOT Core Shadow Service
  2. Demo - Using AWS Device Shadows

DEMO | AWS IOT Core With Node Red | Part 2

Please watch in full screen or on youtube directly



Download Node Red Flow used in this Video

aws-iot-core-node-red-part-2-flow.json

Code Snippets (if you download the flow above they are already included)

//JSON to reset device shadow
{
    "state": {
        "reported": {
            "fuel_level": 100,
            "tire_pressure": "normal",
            "stolen": false
        },
        "desired": {
            "fuel_level": 100,
            "tire_pressure": "normal",
            "stolen": false
        }
    }
}
// javascript function code for topic /delta
new_msg = {"payload":{"state":{}}} 
orig_payload = msg.payload
if (orig_payload.state.stolen == true){
        new_msg.payload.state.reported = {
            "stolen":orig_payload.state.stolen,
            "immobilize_on_next_startup":true
        }
   
   return new_msg 
}else{
    new_msg.payload = null
    return new_msg
}
 

// javascript function code for topic /get/accepted (offline device coming online after a while)
new_msg = {"payload":{"state":{}}} 
orig_payload = msg.payload
if (orig_payload.state.desired.stolen == true){
        new_msg.payload.state.reported = {
            "stolen":orig_payload.state.desired.stolen,
            "immobilize_on_next_startup":true
        }
   
   return new_msg 
}else{
    new_msg.payload = null
    return new_msg
}

Reference / Further reading

  1. Using shadows in devices
  2. Device Shadow MQTT topics
  3. Classic Device Shadows / Reserved Topics

Thank you for reading through, Please share if it’s useful to someone.

-Nikhil

comments powered by Disqus