Post on 16-Apr-2017
AWS Pop-up Loft LondonGetting Started with AWS IoT, Devices & SDKs
Danilo Poccia, Technical Evangelist @danilop danilop
AWS IoT
Authentication & Authorization
AWS Auth+
HTTPS
MQTT+
Mutual Auth TLS
WebSockets wss://…
Registry
Hardware RevisionFirmware VersionSerial Numbers
Device TypeDevice Group
Device DescriptionLink to ProductDocumentation
. . .
Device Gateway
Millions of devices and apps can connect over
MQTT,WebSockets,and HTTP 1.1
PowerfulPub/Sub Broker with Long-livedbi-directional
messages
Rules EngineSELECT * FROM ‘things/thing-2/color’ WHERE color = ‘red’
Simple & Familiar Syntax
SQL Statement to define topic filter
with JSON supportFunctions improve
signal : noise
Device Shadow
Intermitted Connections
PersistentDevice State
(JSON)Desired Vs Reported
=> Delta
RESTful API
Shadow
AWS IoT Device SDK
C-SDK
JS-SDK
Arduino Yún
Mobile SDK Android
iOS
MQTT
MQTT TopicDevice A“Sensor”
Device B“Control Center”
(1) Subscribe
(3) Message
(2) Publish
MQTT Topic = topic level / topic level / … ↓
“myapp/customer123/house4/kitchen/temperature”
Wildcards
‘+’ single level ‘#’ multi level
“myapp/customer123/house4/+/temperature”“myapp/customer123/+/kitchen/temperature”
“myapp/customer123/house4/#”“myapp/customer123/#”
$topics are not subscribed by “#’”
Device Shadows
Device Shadows RESTful API
https://endpoint/things/thingName/shadow
endpoint = identifier.iot.region.amazonaws.com
HTTP GET → GetThingShadow
HTTP POST → UpdateThingShadow
HTTP DELETE → DeleteThingShadow
MQTT topics The Thing Shadows service sends messages to this topic when
$aws/things/myLightBulb/shadow/update/accepted an update is successfully made to a thing shadow
$aws/things/myLightBulb/shadow/update/rejected an update to a thing shadow is rejected
$aws/things/myLightBulb/shadow/update/delta a difference is detected between the reported and desired sections of a thing shadow
$aws/things/myLightBulb/shadow/get/accepted a request for a thing shadow is made successfully
$aws/things/myLightBulb/shadow/get/rejected a request for a thing shadow is rejected
Lightbulb Thing ShadowsService
Application
Lightbulb Thing ShadowsService
$aws/things/myLightbulb/shadow/update
{ "state": { "reported": { "color": "red" } }}
Application
Lightbulb Thing ShadowsService
$aws/things/myLightBulb/shadow/update/accepted
{ "state":{ "reported":{ "color":"red" } }, "metadata":{ "reported":{ "color":{ "timestamp":1447437304 } } }, "version":1, "timestamp":1447437304 }
Application
Lightbulb Thing ShadowsService
$aws/things/myLightBulb/shadow/get
“”(empty message)
Could be a RESTful call to GetThingShadow
Application
Lightbulb Thing ShadowsService
$aws/things/myLightBulb/shadow/get/accepted
{ "state":{ "reported":{ "color":"red" } }, "metadata":{ "reported":{ "color":{ "timestamp":1447437304 } } }, "version":1, "timestamp":1447437398 }
Application
Lightbulb Thing ShadowsService
$aws/things/myLightBulb/shadow/update
{ "state": { "desired": { "color": "green" } }}
Could be a RESTful call to UpdateThingShadow
Application
Lightbulb Thing ShadowsService
$aws/things/myLightBulb/shadow/update/accepted
{ "state":{ "desired":{ "color":"green" } }, "metadata":{ "desired":{ "color":{ "timestamp":1447437647 } } }, "version":2, "timestamp":1447437647 }
Application
Lightbulb Thing ShadowsService
$aws/things/myLightBulb/shadow/update/delta
{ "version":2, "timestamp":1447437647, "state":{ "color":"green" }, "metadata":{ "color":{ "timestamp":1447437647 } }}
Application
Lightbulb Thing ShadowsService
$aws/things/myLightbulb/shadow/update
{ "state":{ "reported":{ "color":"green" }, "desired":null} }}
Application
Lightbulb Thing ShadowsService
$aws/things/myLightBulb/shadow/update/accepted
{ "state":{ "reported":{ "color":"green" } }, "metadata":{ "reported":{ "color":{ "timestamp":1447437894 } } }, "version":3, “timestamp":1447437894}
Application
{ "state" : { "desired" : { "color" : "RED", "sequence" : [ "RED", "GREEN", "BLUE" ] }, "reported" : { "color" : "GREEN" } }, "metadata" : { "desired" : { "color" : { "timestamp" : 12345 }, "sequence" : { "timestamp" : 12345 } }, "reported" : { "color" : { "timestamp" : 12345 } } }, "version" : 10, "clientToken" : "UniqueClientToken", "timestamp": 123456789}
Sample Thing Shadow
Document
Optimistic Locking
Initial State
Update
Result
{ "state" : { "desired" : { "colors" : ["RED", "GREEN", "BLUE" ] } }, "version" : 10}
Optimistic Locking
Initial State
Update
Result
{ "state" : { "desired" : { "colors" : ["RED", "GREEN", "BLUE" ] } }, "version" : 10}
{ "state": { "desired": { "colors": [ "BLUE" ] } }, "version": 9}
Optimistic Locking
Initial State
Update
Result
{ "state" : { "desired" : { "colors" : ["RED", "GREEN", "BLUE" ] } }, "version" : 10}
{ "state": { "desired": { "colors": [ "BLUE" ] } }, "version": 9}
409 Conflict
Optimistic Locking
Initial State
Update
Result
{ "state" : { "desired" : { "colors" : ["RED", "GREEN", "BLUE" ] } }, "version" : 10}
{ "state": { "desired": { "colors": [ "BLUE" ] } }, "version": 10}
Optimistic Locking
Initial State
Update
Result
{ "state" : { "desired" : { "colors" : ["RED", "GREEN", "BLUE" ] } }, "version" : 10}
{ "state": { "desired": { "colors": [ "BLUE" ] } }, "version": 10}
{ "state": { "desired": { "colors": [ "BLUE" ] } }, "version": 11}
Optimistic Locking
Initial State
Update
Result
MQTT Connection
Create and maintain a mutually authenticatedTLS connection over which it runs MQTT
Subscribing to MQTT topics + callback function
Thing Shadow
Retrieve, update and delete Thing Shadows Abstracts the necessary MQTT topic subscriptions by automatically subscribing to and unsubscribing
from the reserved topics as neededInbound state change requests are automatically signalled
via a configurable callback
Device SDKs
Connecting to the AWS IoT MQTT platform
rc = aws_iot_mqtt_connect( &connectParams );
Subscribe to a topic
MQTTSubscribeParams subParams = MQTTSubscribeParamsDefault; subParams.mHandler = MQTTcallbackHandler;subParams.qos = QOS_0;subParams.pTopic = "sdkTest/sub";rc = aws_iot_mqtt_subscribe( &subParams );
Update Thing Shadow from a device
rc = aws_iot_shadow_update(&mqttClient, AWS_IOT_MY_THING_NAME, pJsonDocumentBuffer, ShadowUpdateStatusCallback, pCallbackContext, TIMEOUT_4SEC, persistenSubscription);
C SDK
var awsIot = require(‘aws-iot-device-sdk');
var device = awsIot.device({ keyPath: <YourPrivateKeyPath>, certPath: <YourCertificatePath>, caPath: <YourRootCACertificatePath>, clientId: <YourUniqueClientIdentifier>, region: <YourAWSRegion> });
device .on('connect', function() { console.log('connect'); device.subscribe('topic_1'); device.publish('topic_2', JSON.stringify({ test_data: 1})); });
device .on('message', function(topic, payload) { console.log('message', topic, payload.toString()); });
JS SDK
Device Class
var thingShadows = awsIot.thingShadow({ keyPath: <YourPrivateKeyPath>, certPath: <YourCertificatePath>, caPath: <YourRootCACertificatePath>, clientId: <YourUniqueClientIdentifier>, region: <YourAWSRegion>});
thingShadows.on('connect', function() { thingShadows.register( 'RGBLedLamp' ); setTimeout( function() { var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}}; clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState ); if (clientTokenUpdate === null) { console.log('update shadow failed, operation still in progress'); } }, 5000 ); });
JS SDK
Thing Shadow
Class
thingShadows.on('status', function(thingName, stat, clientToken, stateObject) { console.log('received '+stat+' on '+thingName+': '+ JSON.stringify(stateObject)); });
thingShadows.on('delta', function(thingName, stateObject) { console.log('received delta on '+thingName+': '+ JSON.stringify(stateObject)); });
JS SDK
Thing Shadow
Class
<demo>...
</demo>
AWS IoT
Focus on Your Idea
Danilo Poccia @danilop danilop
Thank You