Learn more about how to use AWS IoT's Device Shadows and Rules Engine to build powerful IoT applications. With Device Shadows, you can build applications that interact with your devices by providing always available REST APIs. By taking advantage of AWS IoT's topic-based rules and built-in integrations, you can build IoT applications that gather, process, analyze, and act on data generated by connected devices at global scale, without having to manage any infrastructure.
DSPy a system for AI to Write Prompts and Do Fine Tuning
Programming the Physical World with Device Shadows and Rules Engine
1. AWS Cloud Kata for Start-Ups and Developers
Hong
Kong
Programming the Physical World
with Device Shadows and Rules
Engine
Dickson Yue
Solutions Architect, AWS
2. AWS Cloud Kata for Start-Ups and Developers
Outline
• Shadows
• Rules
• Sample code
• Demo
5. AWS Cloud Kata for Start-Ups and Developers
Interact with with unreliable
connections thing
Device Shadow
6. AWS Cloud Kata for Start-Ups and Developers
AWS IoT Device Shadows flow
Shadow
Thing
SDK
1. Device publishes current state
2. Persist JSON data store
3. App requests device’s current state
4. App requests change the state
5. Device Shadow syncs
updated state
6. Device publishes current state
7. Device Shadow confirms state change
report {light: off}
get {light : off}
desire {light : on}
delta {light : on}
report {light : on}
reported {turbine: off}
desired{turbine: on}
reported {turbine: on}
7. AWS Cloud Kata for Start-Ups and Developers
AWS IoT Device Shadow - Simple Yet
{
"state" : {
“desired" : {
"light": “on”
},
"reported" : {
"light" : “off”
},
"delta" : {
"light" : “on”
} },
"version" : 10
}
Device
Report its current state to one or multiple shadows
Retrieve its desired state from shadow
Mobile App
Set the desired state of a device
Get the last reported state of the device
Delete the shadow
Shadow
Shadow reports delta, desired and reported
states along with metadata and version
10. AWS Cloud Kata for Start-Ups and Developers
http://bit.ly/iot-plb
11. AWS Cloud Kata for Start-Ups and Developers
http://bit.ly/iot-plb2
12. AWS Cloud Kata for Start-Ups and Developers
AWS IoT
Device gateway
reported: {temp:27,
humidity: 80%}
Light
Amazon Alexa
Service
AWS Lambda
Sensor Tag
Light Bulb
Shadow
4. Update shadow
desired: {light: off}
Environment
Monitor
2. Update shadow
Switch
1. Sensors update
MQTT
MQTT over the WebSocket
HTTP
Alexa + Lambda
$$ charged by
Prices are based on the number of
messages published to AWS IoT
(Publishing Cost), and the number
of messages delivered by AWS IoT
to devices or applications (Delivery
Cost).
6. Update shadow
reported: {light: off}
14. AWS Cloud Kata for Start-Ups and Developers
Load cert and private key
certfile="../../certs/grove-cert.pem",
keyfile="../../certs/grove-privateKey.pem",
rootCA="../../certs/root.pem",
15. AWS Cloud Kata for Start-Ups and Developers
Connect through MQTT libary
import paho.mqtt.client as mqtt
def init():
global client
try:
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.clientid = clientid
client.tls_set( "../../certs/root.pem",
certfile="../../certs/grove-cert.pem",
keyfile="../../certs/grove-privateKey.pem",
tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None )
client.connect("data.iot.us-east-1.amazonaws.com", 8883, 10)
client.loop_forever()
except:
print "Mqtt Unexpected error:", sys.exc_info()[0]
16. AWS Cloud Kata for Start-Ups and Developers
Connect through Python SDK
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
def init():
global client
try:
client = AWSIoTMQTTClient(clientid)
client.configureEndpoint(host, 8883)
client.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
client.configureAutoReconnectBackoffTime(1, 32, 20)
client.configureDrainingFrequency(2) # Draining: 2 Hz
client.configureConnectDisconnectTimeout(10) # 10 sec
client.configureMQTTOperationTimeout(5) # 5 sec
client.connect()
thingLightbulb = client.createShadowHandlerWithName(thingLightbulbName, True)
thingLightbulb.shadowRegisterDeltaCallback(lightbulbShadowCallback_Delta)
except:
print "Unexpected error at init:", sys.exc_info()[0]
17. AWS Cloud Kata for Start-Ups and Developers
Read sensor data from Grove
try:
[ temp,hum ] = dht(dht_sensor_port,dht_sensor_type)
ppm = grovepi.analogRead(air_sensor)
aq = getairquality(ppm)
loudness = grovepi.analogRead(loudness_sensor)
light = grovepi.analogRead(light_sensor)
state = {
"temp" : temp,
"humidity" : hum,
"lightsensorvalue" : light,
"lightsensorresistance" : resistance,
"ppm" : ppm,
"airquality" : aq,
"loudness" : loudness,
"createdat" : nowstr
}
18. AWS Cloud Kata for Start-Ups and Developers
Update shadow through MQTT
state = {
"temp" : temp,
"humidity" : hum,
"lightsensorvalue" : light,
"lightsensorresistance" : resistance,
"ppm" : ppm,
"airquality" : aq,
"loudness" : loudness,
"createdat" : nowstr
}
msg = {
"state":{
"reported": state
}
}
topicshadow = "$aws/things/grove/shadow/update"
client.publish(topicshadow,json.dumps(msg),1)
19. AWS Cloud Kata for Start-Ups and DevelopersAWS Cloud Kata for Start-Ups and Developers
Connect through JavaScript
20. AWS Cloud Kata for Start-Ups and Developers
Connect through JavaScript
var region = c.AWS.region; //'ap-northeast-1'
var iotEndpoint = c.AWS.iotEndpoint;
var identityPoolId = c.AWS.identityPoolId;
AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: identityPoolId
});
AWS.config.credentials.get(function(){
var signedUrl = getSignedUrl();
initClient(signedUrl);
});
var requestUrl = 'wss://' + host + canonicalUri + '?' + canonicalQuerystring;
21. AWS Cloud Kata for Start-Ups and Developers
Connect through JavaScript
function initClient(requestUrl) {
var clientId = String(Math.random()).replace('.', '');
var client = new Paho.MQTT.Client(requestUrl, clientId);
mqttClient = client;
var connectOptions = {
onSuccess: function() {
client.subscribe(topiclightbulb);
client.subscribe(topicgrove);
},
useSSL: true, timeout: 3, mqttVersion: 4,
onFailure: function() { console.error('connect failed'); }
};
client.connect(connectOptions);
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
}
22. AWS Cloud Kata for Start-Ups and Developers
Update shadow through MQTT
function updateLightShadow(state) {
lightstate = state;
msg = "{"state": {"desired" : { "light":"" + lightstate + "" }}}";
topicshadow = topiclightbulbUpdate;
console.log(msg);
mqttClient.send(topicshadow, msg);
}
23. AWS Cloud Kata for Start-Ups and Developers
Button
Control
Voice
Control
24. AWS Cloud Kata for Start-Ups and Developers
Update shadow through Lambda
var AWS = require('aws-sdk');
var config = {iotendpoint: 'A1OMFFL4UASE1E.iot.us-east-1.amazonaws.com'};
var iotdata = new AWS.IotData({endpoint:config.iotendpoint});
var newstate = reported.light === "on"? "off" : "on";
var state = {
"state" : {
"desired" :{
"light" : newstate
}
}
}
var params = {
payload: JSON.stringify(state),
thingName: thingName
};
iotdata.updateThingShadow(params, function(err, data) {
callback(data);
});
25. AWS Cloud Kata for Start-Ups and Developers
Update shadow through Alexa
if ("AskLightControlIntent" === intentName) {
handleLightControlIntent(intent, session, callback);
}
function handleLightControlIntent(intent, session, callback) {
var switchSlot = intent.slots.Switch;
var sw = "";
if(switchSlot)
sw = switchSlot.value;
speechOutput = "The light is now " + sw +".";
var data = sw=="on" ? LightblubShadow.on : LightblubShadow.off;
var next = function(){
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
updateShadow("lightbulb",JSON.stringify(data),next);
26. AWS Cloud Kata for Start-Ups and Developers
Rules engine
27. AWS Cloud Kata for Start-Ups and Developers
Extracting the value from messages
• Filter messages with certain criteria
• Move messages to other topics
• Move messages to other systems
• Transform the payload of messages
• Predict messages based on trends
• React based on messages
28. AWS Cloud Kata for Start-Ups and Developers
But how?
instance
database
?
29. AWS Cloud Kata for Start-Ups and Developers
But how?
Highly available?
Scalable?
Easy to operate?
Easy to change?
Easy to implement?
instance
database
30. AWS Cloud Kata for Start-Ups and Developers
Rules Engine
31. AWS Cloud Kata for Start-Ups and Developers
AWS IoT rules engine
predict,
republish
Amazon Machine
Learning
index
archive,
analyze
Amazon
DynamoDB
AWS
Lambda
Amazon
Redshift
process
32. AWS Cloud Kata for Start-Ups and Developers
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’)
33. AWS Cloud Kata for Start-Ups and Developers
Rules Engine
• Familiar SQL syntax
• SELECT * FROM topic WHERE filter
• Functions
• String manipulation (regex support)
• Mathematical operations
• Context based helper functions
• Crypto support
• UUID, timestamp, rand, etc.
• Execute Simultaneous Actions
34. AWS Cloud Kata for Start-Ups and Developers
Quick example
SELECT reading, timestamp() as time
FROM example-topic/input
WHERE reading > 0
Republish to example-topic/output
35. AWS Cloud Kata for Start-Ups and Developers
Quick example
Publish a message
Receive the re-published,
transformed message
40. AWS Cloud Kata for Start-Ups and Developers
ES Index
PUT awsiot-grove{
"mappings": {
"grove": {
"properties": {
"temp": { "type": "double" },
"humidity": { "type": "double“ },
"lightsensorvalue": { "type": "double“ },
"lightsensorresistance": { "type": "double“ },
"createdat": { "type": "date","format" : "yyyy-MM-dd HH:mm:ss" },
"time": { "type": "date" },
"timestamp": { "type": "date" }
}
}
}
}
41. AWS Cloud Kata for Start-Ups and Developers
Get Started with AWS IoT Device SDK
C-SDK
(Ideal for embedded
OS)
JavaScript-SDK
(Ideal for Embedded
Linux Platforms)
Arduino Library
(Arduino Yun)
Mobile SDK
(Android and iOS)
Java-SDKPython
42. AWS Cloud Kata for Start-Ups and Developers
Summary
• Shadows
• Nodejs, Python, JavaScript code
• Rules
• Elasticsearch integration