(MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

38
© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Jinesh Varia | @jinman October 8, 2015 MBL312 AWS IoT Deep Dive Programming the Physical World with Shadows and Rules

Transcript of (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Page 1: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Jinesh Varia | @jinman

October 8, 2015

MBL312

AWS IoT Deep Dive Programming the Physical World with

Shadows and Rules

Page 2: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT

“Securely connect one or one billion devices to AWS,

so they can interact with applications and other devices”

Page 3: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT

DEVICE SDKSet of client libraries to

connect, authenticate and

exchange messages

DEVICE GATEWAYCommunicate with devices via

MQTT and HTTP

AUTHENTICATION

AUTHORIZATIONSecure with mutual

authentication and encryption

RULES ENGINETransform messages

based on rules and

route to AWS Services

AWS Services

- - - - -

3P Services

DEVICE SHADOWPersistent thing state

during intermittent

connections

APPLICATIONS

AWS IoT API

DEVICE REGISTRYIdentity and Management of

your things

Page 4: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

In this session, we are going to dive deep

RULES ENGINETransform messages

based on rules and

route to AWS Services

DEVICE SHADOWPersistent thing state

during intermittent

connections

Page 5: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

An Example: Connected Vacuum Cleaner

Sweeps (duh!)

Reports its state via events:

• power, battery

• status (DOCKED,

RUNNING),

• bin.lastEmptied,

• bin.used,

• firmware version,

• filter.lastChanged,

• filter.usageMinutes,

• next run time

Mobile Phone can

• Initiate “Sweep”

• Receives Push

Notifications

• Show Current State

• Track History of Cleans

• Track Path and Uncleaned

Areas

• Initiate Firmware Updates

Wi-Fi Connected

Vacuum Cleaner

Mobile App to

Control the Device

Page 6: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Rules Engine Basics

SELECT * FROM ‘things/thing-2/color’

WHERE color = ‘red’

Rule

Name

Description

SQL Statement

Array of Actions AWS Services, Native

Page 7: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Simple & Familiar Syntax

- SQL Statement to define topic filter

- Optional WHERE clause

- Advanced JSON support

Functions improve signal : noise

- String manipulation (regex support)

- Mathematical operations

- Context based helper functions

- Crypto support

- UUID, Timestamp, rand, etc.

AWS IoT Rules Engine Basics

SELECT * FROM ‘things/thing-2/color’

WHERE color = ‘red’

Page 8: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Rules Engine’s Flexibility

SELECT *, clientId() as MQTTClientId

FROM 'one/rule'

WHERE

startsWith(topic(2), ’Vac123') AND

(state = ‘SWEEP' OR bin.size < 30)",

"actions":

[{

"republish": {

"topic":

"controllers/${substring(topic(3),

3, 5)}",

}]

Page 9: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM TOPIC WHERE FILTER

Page 10: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Rules Engine – Format

{

"sql":"SELECT 'IDLE' AS status FROM 'vacuum/+/events' WHEREevent = 'COMPLETE'",

"actions": [

{

"dynamoDB": {

"tableName":"vaccum-status",

"hashKeyField":"vacuum_id",

"hashKeyValue":"${topic(2)}",

"payloadField":"statusDocument",

"roleArn":"arn:aws:iam::77777:role/rules_action_ddb"

}

}

]

}

Page 11: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM TOPIC WHERE FILTER

• Like scanning a database table

• Default source is an MQTT topic

EXAMPLES:

• FROM mqtt(‘my/topic’)

• FROM mqtt(‘my/wildcard/+/topic’)

• FROM (‘my/topic’)

Page 12: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM TOPIC WHERE FILTER

• Properties from the JSON Object in the payload

• “.” Operator

• “..” Operator

• “*” Operator

• Apply functions to attribute value

Page 13: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM…

• SELECT *

• SELECT deviceid, temp

• SELECT coords.latitude

• SELECT a.another_level.b

• Returns {“b” : 3}

• SELECT a..b

• Returns {“b” : 3}

{

“deviceid” : “iot123”,

“temp” : 54,

“humidity” : 32,

“coords” : {

“latitude” : 47.615694,

“longitude” : -122.3359976

},

“a” : {

“another_level” : {

{“b” : 3},

{“b” : 5}

}}

}

SAMPLE PAYLOAD

Page 14: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM…

• SELECT deviceid AS client

• SELECT md5(deviceid) AS hashed_id

Substitution Templates

• ${expression}

• ${topic() - md5(deviceid)}

• ${deviceid - temp}

{

“deviceid” : “iot123”,

“temp” : 54,

“humidity” : 32,

“coords” : {

“latitude” : 47.615694,

“longitude” : -122.3359976

},

“a” : {

“another_level” : {

{“b” : 3},

{“b” : 5}

}}

}

SAMPLE PAYLOAD

Page 15: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM TOPIC WHERE FILTER

Token Meaning Example

= Equal, comparison color = 'red'

<> Not Equal, comparison color <> 'red'

AND Logical AND color = 'red' AND siren = 'on'

OR Logical OR color = 'red' OR siren = 'on'

() Parenthesis, grouping color = 'red' AND (siren = 'on' OR isTest)

+ Addition, arithmetic 5 + 3

- Substitution, arithmetic 5 - 4

/ Division, arithmetic 8 / 2

Page 16: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM TOPIC WHERE FILTER

Token Meaning Example

< Less than, comparison color = 'red'

<= Less than or equal color <> 'red'

> Greater than, comparison color = 'red' AND siren = 'on'

>= Greater than or equal color = 'red' OR siren = 'on'

CASE …

WHEN …

THEN …

ELSE …

END

Case statement CASE location

WHEN 'home’

THEN 'off'

WHEN 'work’

THEN 'on' ELSE 'silent' END

Page 17: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT – SQL Reference

SELECT DATA FROM TOPIC WHERE FILTER

• Properties from the JSON Object in the payload

• “.” Operator

• “..” Operator

• “*” Operator

• Apply functions to attribute value

Page 18: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Rules Engine Actions

RULES ENGINETransform messages

based on rules and

route to AWS Services

AWS Services

- - - - -

3P Services

AWS Services

- - - - -

3P Services

Page 19: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

1. AWS Services(Direct Integration)

Rules Engine

Actions

AWS IoT Rules Engine

AWS

Lambda

Amazon

SNS

Amazon

SQS

Amazon

S3

Amazon

Kinesis

Amazon

DynamoDB Amazon RDS

Amazon

Redshift

Amazon Glacier

Amazon

EC2

3. External Endpoints(via Lambda and SNS)

Rules Engine connects AWS

IoT to External Endpoints and

AWS Services.

2. Rest of AWS(via Amazon Kinesis, AWS

Lambda, Amazon S3, and

more)

Page 20: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Rules Engine

Rules Engine evaluates inbound

messages published into AWS

IoT, transforms and delivers to the

appropriate endpoint based on

business rules.

External endpoints can be

reached via Lambda and Amazon

Simple Notification Service

(Amazon SNS).

Invoke a Lambda function

Put object in an S3 bucket

Insert, Update, Read from a

DynamoDB table

Publish to an SNS Topic

or Endpoint

Publish to an Amazon Kinesis

stream

Actions

Publish to Amazon Kinesis

Firehose

Republish to AWS IoT

Page 21: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT to AWS Lambda to and External Endpoint

Lambda Function

Rules Engine

PolicyPrivate Key

& Certificate

Thing/Device

RuleSDK

AWS IoT AWS Services

Execution

RolePolicy

External Endpoint

Permission

Select * from ‘iotbutton/+’

Action

Page 22: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT to Amazon DynamoDB to Dashboard

DynamoDB Table

Rules Engine

PolicyPrivate Key

& Certificate

Thing/Device

RuleSDK

AWS IoT AWS Services

Policy

Dashboard

IAM

Role

Select * from ‘iotbutton/+’

ActionDynamoDB S3 Website

Page 23: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Rules Engine for Machine Learning

Anomaly Detection

Amazon Machine Learning can feed predictive evaluation criteria to the Rules Engine

Continuous Improvement Around Predication

Continuously look for outliers and re-calibrate the Amazon Machine Learning models

Send to S3

Amazon

Machine

Learning

Re-Train

S3

Page 24: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Rules Engine & Stream Data

N:1 Inbound Streams of Sensor Data (Signal to Noise Reduction)

Rules Engine filters, transforms sensor data then sends aggregate to Amazon Kinesis

Amazon Kinesis Streams to Enterprise Applications

Simultaneously stream processed data to databases, applications, other AWS

Services

Ordered Stream

Amazon

Kinesis

Page 25: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

In this session, we are going to dive deep

RULES ENGINETransform messages

based on rules and

route to AWS Services

DEVICE SHADOWPersistent thing state

during intermittent

connections

Page 26: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Device Shadow

Shadow

Virtual representation

of your device in the

cloud

• Device State

• desired

• reported

• Device metadata• Sensors

• Version

• clientToken

• timestamp

Page 27: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Shadow Flow

Shadow

Device SDK

1. Device Publishes Current State

2. Persist JSON Data Store

3. App requests device’s current state

4. App requests change the state5. Device Shadow sync’s

updated state

6. Device Publishes Current State7. Device Shadow confirms state change

AWS IoT

Page 28: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT Device Shadow Topics (MQTT)

Thing SDK (C-SDK, JS-SDK)

makes it easy for you build shadow

functionality into your device so it

can automatically synchronize the

state with the device.

AWS IoT Thing Shadow

UPDATE: $aws/things/{thingName}/shadow/update

DELTA: $aws/things/{thingName}/shadow/update/delta

GET: $aws/things/{thingName}/shadow/get

DELETE: $aws/things/{thingName}/shadow/delete

Sensor Reported Desired Delta

LED1 RED YELLOW

LED1 =

Yellow

TEMP = 60F

ACCEL X=1,Y=5,Z=4 X=1,Y=5,Z=4

TEMP 83F 60F

Page 29: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Thing Shadow – Document Format

{

“state”: {

“desired” : {

“color”: “RED”,

“sequence” :

[ “RED”,

“GREEN”, “BLUE”]

},

“reported” : {

“color” : “GREEN”

}

}

“metadata” : {

“desired” : {

“color” : {

“timestamp” : 12345

},

“sequence” : {

“timestamp” : 12345

}

},

},

“version” : 10,

“clientToken” : “Unique-client-token”,

“timestamp” : 123456

}

Page 30: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Demo: Avnet Broadcom WICED

Page 31: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules
Page 32: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules
Page 33: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Automatically clean when lights go off (night time)

desired: {

“status”: “RUNNING”

}

shadow/update update/accepted

Republish vacuum/shadow/update

vacuum/update/delta

reported: {

“AmbientLightValue”: 55

}

Rules Engine

Shadow

Shadow

One sensor automatically triggering the other device!

Select * from $/update/accepted where

AmbientLightValue <100

Page 34: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Automatically clean when lights go off (night time)

desired: {

“status”: “RUNNING”

}

shadow/update update/accepted

Republish vacuum/shadow/update

vacuum/update/delta

reported: {

“AmbientLightValue”: 55

}

Rules Engine

Shadow

Shadow

One sensor automatically triggering the other device!

Select * from $/update/accepted where

AmbientLightValue <100

Page 35: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Turn Off Automatic Cleaning At Night or Trigger

Manually using the Mobile App

Android App

UpdateThingShadow

Desired: {

“status”: “RUNNING”

}

$aws/things/light/update

$a/vacuum/update/delta

Shadow

Shadow

Page 36: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

AWS IoT

DEVICE SDKSet of client libraries to

connect, authenticate and

exchange messages

DEVICE GATEWAYCommunicate with devices via

MQTT and HTTP

AUTHENTICATION

AUTHORIZATIONSecure with mutual

authentication and encryption

RULES ENGINETransform messages

based on rules and

route to AWS Services

AWS Services

- - - - -

3P Services

DEVICE SHADOWPersistent thing state

during intermittent

connections

APPLICATIONS

AWS IoT API

DEVICE REGISTRYIdentity and Management of

your things

Page 37: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Thank you!

Page 38: (MBL312) NEW! AWS IoT: Programming a Physical World w/ Shadows & Rules

Remember to complete

your evaluations!