Field Notes: Implementing a Digital Shadow of a Connected Vehicle with AWS IoT
AWS Architecture BlogField Notes: Implementing a Digital Shadow of a Connected Vehicle with AWS IoTby Amit Kumar | on06 OCT 2020| inArchitecture,Automotive,Internet of Things,Technical How-to|Permalink|ShareInnovations in connected vehicle technology are expected to improve the quality and speed of vehicle communications and create a safer driving experience. As connected vehicles are becoming part of the mainstream, OEMs (Original Equipment Manufacturers) are broadening the capabilities of their products and dramatically improving the in-vehicle experience for customers.An important feature in a connected vehicle is its ability to execute a remote command and synchronize the state of the vehicle between a web/mobile app in real time.This blog demonstrates how to:secure two-way communication between a device (vehicle telematics control unit) and the AWS Cloud usingAWS IoTexecute command at vehicleexecute a remote commandand test with a vehicle virtual modelYou can watch a quick animation of a remote command execution in the following GIF:Solution OverviewIn a traditional connected vehicle approach, there are many processes running on multiple servers. These processes are subscribing to one another, coordinating with each other, and polling for an update. This makes scalability and availability a challenge. We useAWS IoT CoreandAWS IoT Device Shadow serviceas primary components for this solution.This solution has three building blocks:a vehicle TCU (telematics control unit),the AWS Cloud (with connection via AWS IoT Core) anda virtual Model (e.g.; web/mobile app to send/receive commands to TCU). These three building blocks together reflect the current state of a vehicle.The previous diagram shows a message flowing in the following example:A user of a connected vehicle wants to open their door using a web/mobile app. The app updates the device shadow with (desired {““door””: ““open””}). The app will always request the vehicle to execute the command; therefore, it will always update the device shadow with the desired state.Vehicle TCU registered the callback function shadowRegisterDeltaCallback(). Listen on delta topics for the device shadow by subscribing to delta topics. Whenever there is a difference between the desired and reported state, the registered callback is called and the delta payload will be available in the callback. Update performed in #1 will received in delta callback.Now, the vehicle needs to act on the desired state. In this case, ‘act on’ is the door status change. After performing the required action for the door change, the vehicle TCU will update the device shadow with the reported state (reported : { “door”: “open”} )Now, the vehicle is closing the door. The vehicle will always perform the action; therefore, it will always update device shadow with reports state (reported: {“door” : “close”})The Web/Mobile app subscribed to topic $aws/things/tcu/shadow/update/accepted”. Therefore, as soon as the vehicle TCU updates the shadow, the Web/Mobile app received the update and synchronized the UI state.You can also build an Amazon Alexa skill to control your vehicle (“Alexa, raise my window”). After identifying the utterance, Alexa can invoke the Lambda function to update the device shadow and perform the requested action.Note:For the Web/Mobile app developments for production, it is recommended to useAWS AppSyncandAWS AmplifySDK for building a flexible and decoupled application from the API. Refer to thiscode samplefor more detail.ImplementationFirst, you need to set up the code. Refer to the directions in thiscode sample.Create deviceInAWS IoT Core, name a device ‘TCU’ (created by connected-vehicle-app-cdk-stack). Create a new certificate (download files) and attach the policy generated by cdk.Next, deploy the certificate key and pem file on your device so it can connect with the AWS Cloud using the X.509 certificate. For more detail, refer to the directions in thecode sample.Execute Command at VehicleAWS IoT Device Shadowis an important feature of AWS IoT core for remote command execution because it allows you to decouple the vehicle and the app which controls and commands the vehicle. A device’s shadow is a JSON document that is used to store and retrieve current state information for a device. Primarily we use state.desired and state.reported. properties of adevice’s shadow document.The device shadow (Device SKD and APIs)enables applicationsto interact with devices even when they are offline and allow:Cloud representation of device stateQuery last known state for offline devicesReal-time state changesTrack last known device stateControl devices via change of stateAutomatic synchronization once devices connect to the cloudAPIs for applications to discover and interact with devicesThe rich features of a device shadow allows the app to interact with the vehicle TCU even when there is no connectivity. Once connectivity is established, the device gateway pushes the changes to device and vice versa.We need to deploy a program (tcuShadowWrite.py) on the vehicle TCU device to update the device shadow and send the update to the AWS Cloud. This program is available in thiscode repository.Let’s assume that after reaching their home, the vehicle’s user closes the door, switches off the headlights, and rolls up the windows. The same state of the vehicle should be reflected on their web/mobile app in real time. The vehicle TCU has to update the “reported” state in the device shadow JSON document.AWSIoTMQTTShadowClientlibrary has a method calledshadowUpdatethat needs to be called from the vehicle TCU to update the device shadow. Essentially, it is publishing the shadow reported state on topic $aws/things/<thingName>/shadow/update.If you runtcuShadowWrite.pyscript, you should be able to see the output as described in the following image.Open theAWS IoT Core console.SelectManage->Things-> Selecttcu, and then chooseShadow. You should be able to see the shadow message sent from the device described in the following image.Execute Remote CommandWe need to deploy a program (tcuShadowRead.py) on the vehicle TCU to receive updates from the AWS Cloud. It is available in thiscode sample.Let’s assume the vehicle owner uses the mobile app to open the door, switch on the headlight and roll down the windows. The vehicle TCU should receive this command and instruct the Electronic Control Unit (ECU) to execute the command. The web/mobile app will update the “desire” state in the device shadow JSON document.In tcuShadowRead.py, AWSIoTMQTTShadowClient has a method shadowRegisterDeltaCallback. It listens on delta topics for this device shadow by subscribing to delta topics. Whenever there is a difference between the desired and reported state, the registered callback is called and the delta payload will be available in the callback.The callback function has a code to handle the state change request. In an actual implementation, a function like door_handle() would be calling the ECU to execute the door open command.If you make changes in Device Shadow on AWS IoT for the tcu device, you should receive the output in the following image:Test with a Virtual Vehicle ModelTo help you test this solution, you can deploy the virtual vehicle model shown in the following image. Detailed steps for the deployment of the virtual vehicle is available in thiscode sample.Any changes in the model state should be reflected on the virtual demo vehicle and vice versa.Here, we use open-source Paho-mqtt library.  and Developers can use this to write JavaScript applications that access AWS IoT using MQTT or MQTT over the WebSocket protocol without using AWS IoT SDK. This implementation is made simpler by usingAWS IoT Device SDK for JavaScript v2 Readme.Review the JavaScript file namedwebSocketApp.js:onMessageArrived() function will be invoked whenever the device will change the shadow state.handle<object>Commandfunctions (such ashandleDoorCommand) will be called with the current state.  Call this function if the device has received any status change.We have another JavaScript filedemo-car.jsin the demo-car folder. This includes the functions that our simulated vehicle will use in order to change the device shadow.Let’s review the following code:We have 3 handle command function defined (e.g., handleDoorCommand) to take the user’s input and access AWS IoT Core services.connectDeviceis an actual function to invokeupdateThingsShadowfunction to send the desired stateaccessIoTDeviceusesAmazon Cognito Identityto get the authenticated identities to access AWS IoT Core securely without exposing the access key or secret key.Now, keepdemo.htmlside by side to your code and run thetcuShadowRead.pyscript. Any change made at the virtual model will reflect at the command output. Similarly, any change made bytcuShadowWrite.pywill reflect the state update on the virtual model.ConclusionIn this blog, we showed how to implement a digital shadow of a connected vehicle using AWS IoT. This solution removes complexity from running multiple processes in parallel and ensures a successful outcome. AWS IoT Core enables scalable, secure, low-latency, low-overhead, bi-directional communication between connected devices, tolerate and recover from slow/brittle connection, the AWS Cloud and customer-facing applications.The Device Shadow in AWS IoT Core enables the AWS Cloud and applications to easily and accurately receive data from connected vehicles and send commands to the vehicles. The Device Shadow’s uniform and always-available interface simplifies the implementation of time-sensitive use cases. These include, remote command execution and two-way state synchronization between a device and app where the cloud is acting as a broker. This solution enables you to shift operational responsibilities of a connected vehicle infrastructure to the AWS Cloud while paying only for what you use, with no minimum fees or mandatory service usage.For more information about how AWS can help you build connected vehicle solutions, refer to theAWS Connected Vehicle solution page.You can read more on this topic in my next blog:Field Notes: Powering the Connected Vehicle with Amazon Alexa.Also, check out the Automotive issue of theAWS Architecture Monthly Magazine.Field Notes provides hands-on technical guidance from AWS Solutions Architects, consultants, and technical account managers, based on their experiences in the field solving real-world business problems for customers.TAGS:Field NotesAmit KumarAmit Kumar is a Global Solutions Architect at Amazon Web Services. He works with Automotive customers supporting them on their cloud journey. Amit primarily helps OEMs (Original Equipment Manufacturer) to build next generation connected vehicle platforms.
Innovations in connected vehicle technology are expected to improve the quality and speed of vehicle communications and create a safer driving experience. As connected vehicles are becoming part of the mainstream, OEMs (Original Equipment Manufacturers) are broadening the capabilities of their products and dramatically improving the in-vehicle experience for customers. An important feature in a […]
Amit Kumar
Architecture Automotive Internet of Things Technical How-to