Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
以Device Shadows與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
7. Device Shadow confirms state change
1. Device publishes current state
report {light: off}
3. App requests device’s current state
get {light : off}
4. App requests change the state
desire {light : on}5. Device Shadow syncs
updated state
delta {light : on}
6. Device publishes current state
report {light : on}
2. Persist JSON data store
reported {light : off}
desired{light : on}
reported {light : on}
App
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
Grove
Light Bulb
Shadow
4. Update shadow
desired: {light: off}
Environment
Monitor
3. Switch off the light
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}
15. AWS Cloud Kata for Start-Ups and Developers
Load cert and private key
Generate certivate with AWS IoT
certfile="../../certs/grove-cert.pem",
keyfile="../../certs/grove-privateKey.pem",
rootCA="../../certs/root.pem”
Copy the cert through SSH
JITR - Just In Time Certificate Registration
1. You will register and activate the CA certificate.
2. Use this certificate to create and sign the certificates you will use per device.
3. Let the device present the certificate to AWS and then activate it, i.e. through Lambda
4. Finally, use an AWS Lambda function to notify you when a new certificate is present to AWS.
It can also activate the certificate and handle any registration or initialization.
16. AWS Cloud Kata for Start-Ups and Developers
Connect through MQTT library
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]
Do not duplicate your clientid
MQTT Event handler
Cert, Private key, Root CA
17. 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]
Auto reconnection with
progressive backoff
Offline publish queue
18. 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
}
Extract sensor value
Prepare your message
19. 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)
Update shadow through
publish a MQTT message
20. AWS Cloud Kata for Start-Ups and DevelopersAWS Cloud Kata for Start-Ups and Developers
Connect through JavaScript
21. AWS Cloud Kata for Start-Ups and Developers
Connect through JavaScript
AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials(
{ IdentityPoolId: identityPoolId }
);
credentials.get(
function(err) {
if(err) {var requestUrl = SigV4Utils.getSignedUrl(
'wss', c.AWS.iotEndpoint, '/mqtt', 'iotdata', c.AWS.iotRegion,
credentials.accessKeyId,
credentials.secretAccessKey,
credentials.sessionToken);
initClient(requestUrl);
});
Get credential through
Cognito
With temp access token
Get signed url
22. 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;
}
Signed url
MQTT Event handler
23. 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);
}
24. AWS Cloud Kata for Start-Ups and Developers
Button
Control
Click
+
Publish a msg to a topic
+
Rules
+
Trigger Lambda
26. 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);
});
Update shadow through
publish a AWS SDK
Flip the switch
Prepare your state
27. AWS Cloud Kata for Start-Ups and Developers
Voice
Control
Alexa
+
Skill
+
Lambda
28. AWS Cloud Kata for Start-Ups and DevelopersAWS Cloud Kata for Start-Ups and Developers
29. AWS Cloud Kata for Start-Ups and DevelopersAWS Cloud Kata for Start-Ups and Developers
30. AWS Cloud Kata for Start-Ups and DevelopersAWS Cloud Kata for Start-Ups and Developers
Bind a Lambda function with your skill
31. AWS Cloud Kata for Start-Ups and Developers
Update shadow through Lambda
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);
Handle the corresponding
intent
32. AWS Cloud Kata for Start-Ups and Developers
Rules engine
33. 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
34. AWS Cloud Kata for Start-Ups and Developers
But how?
instance
database
?
35. AWS Cloud Kata for Start-Ups and Developers
But how?
Highly available?
Scalable?
Easy to operate?
Easy to change?
Easy to implement?
instance
database
36. AWS Cloud Kata for Start-Ups and Developers
Rules Engine
37. 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
38. 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’)
39. 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
42. 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" }
}
}
}
}
43. 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
44. AWS Cloud Kata for Start-Ups and Developers
Key take away
• Program with Shadows
• Nodejs, Python, JavaScript
• MQTT library vs AWS SDK
• Cert vs Cognito
• Message routing Rules Engine configuration
• Elasticsearch integration
45. AWS Cloud Kata for Start-Ups and Developers
Hong
Kong
Thank you