Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】

2,595 views

Published on

2016/03/13に行われたJAWS DAYS2016の発表資料です。

Published in: Software
  • Be the first to comment

AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】

  1. 1. AWS IoTで 家庭内IoTやってみた 土持 昌志
  2. 2. 自己紹介 • 土持昌志 • @pampitter • 株式会社鈴木商店 • JAWS-UG大阪 • 好きなAWSサービス:Lambda、S3
  3. 3. AWS IoTをやってみたきっかけ
  4. 4. AWS IoTをやってみたきっかけ • 2015年10月に子供が生まれました • 2015年10月のre:InventでAWS IoT発表 • 風邪・インフルエンザ対策に湿度データを収 集してみよう
  5. 5. AWS IoTの概要
  6. 6. Device Gateway Device Gateway (Broker) Publisher Subscriber Publisher Publisher Subscriber Subscriber MQTT HTTP WebSocket MQTT HTTP WebSocket
  7. 7. Rule Engine Action SELECT humidity FROM '#' WHERE humidity <= 40 Rule Query Device Gateway {"temp": 20.0} {"humidity": 40.0} {"temp": 22.0} {"humidity": 70.0} {"temp": 21.0} {"humidity": 20.0} {"humidity": 40.0} {"humidity": 20.0}
  8. 8. Demo 1 湿度センサー MQTT Rule EngineTopic
  9. 9. センサーデータを収集 # -*- coding: utf-8 -*- from __future__ import division from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals from sense_hat import SenseHat import datetime import json sense = SenseHat() hum = sense.get_humidity() now = datetime.datetime.now() hum_json = { 'timestamp': now.strftime("%Y-%m-%d %H:%M:%S"), 'humidity': hum } print(json.dumps(hum_json))
  10. 10. var execSync = require('child_process').execSync; module.exports = { sensor: function() { var data = "" + execSync('python ' + __dirname + '/sensor.py'); return JSON.parse(data); } } NodeからPythonへのブリッジ
  11. 11. var awsIot = require('aws-iot-device-sdk'); var bridge = require('./bridge.js'); var device = awsIot.device({ keyPath: './certs/private.pem.key', certPath: './certs/certificate.pem.crt', caPath: './certs/root-CA.crt', clientId: 'pi2_01', region: 'ap-northeast-1' }); device .on('connect', function() { console.log('connect'); setInterval(function() { var humidity = bridge.sensor(); console.log(humidity) device.publish('pi2_01', JSON.stringify(humidity)); }, 1000); }); AWS IoTにパブリッシュ
  12. 12. Demo 1
  13. 13. Device Shadow Device Device Shadow App
  14. 14. • desired:管理アプリなどから指定されたあるべき状態 • reported:デバイスが報告した現在の状態 • delta:desiredとreportedの差分 Device Shadow { "desired": { "color": "white" }, "reported": { "color": "white" } } { "desired": { "color": "red" }, "reported": { "color": "red" } } { "desired": { "color": "red" }, "reported": { "color": "white" }, "delta": { "color": "red" } }
  15. 15. Demo 2 LED Device Shadow Console
  16. 16. LEDを点灯 # -*- coding: utf-8 -*- from __future__ import division from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals from sense_hat import SenseHat import sys sense = SenseHat() argv = sys.argv colors = { "red": (255, 0, 0), "yellow": (255, 215, 0), "green": (50, 205, 50), "blue": (0, 0, 255), "light_blue": (91, 192, 222), "white": (255, 255, 255), "black": (0, 0, 0) } if colors.has_key(argv[1]): color = colors[argv[1]] else: color = colors['black'] pixels = [] for pix in range(0, 64) : pixels.append(color) sense.set_pixels(pixels)
  17. 17. var execSync = require('child_process').execSync; module.exports = { sensor: function() { var data = "" + execSync('python ' + __dirname + '/sensor.py'); return JSON.parse(data); }, led: function(color) { execSync('python ' + __dirname + '/led.py ' + color); return; } } NodeからPythonへのブリッジ
  18. 18. var awsIot = require('aws-iot-device-sdk'); var bridge = require('./bridge.js'); var thingShadows = awsIot.thingShadow({ keyPath: './certs/private.pem.key', certPath: './certs/certificate.pem.crt', caPath: './certs/root-CA.crt', clientId: 'pi2_01', region: 'ap-northeast-1' }); var clientTokenGet; var clientTokenUpdate; thingShadows.on('connect', function() { thingShadows.register('pi2_01'); setTimeout(function() { clientTokenGet = thingShadows.get('pi2_01'); }, 2000); }); Device ShadowからLEDを操作 thingShadows.on('status', function(thingName, stat, clientToken, stateObject) { console.log('received ' + stat + ' on ' + thingName + ': ' + JSON.stringify(stateObject)); if('delta' in stateObject.state && 'color' in stateObject.state.delta){ var delta_state = stateObject.state.delta.color; bridge.led(delta_state); console.log('received delta ' + ' on ' + thingName + ': ' + JSON.stringify(stateObject)); clientTokenUpdate = thingShadows.update('pi2_01', { "state": { "reported": { "color": delta_state } } }); } }); thingShadows.on('delta', function(thingName, stateObject) { var state = stateObject.state.color; bridge.led(state); console.log('received delta ' + ' on ' + thingName + ': ' + JSON.stringify(stateObject)); clientTokenUpdate = thingShadows.update('pi2_01', { "state": { "reported": { "color": state } } }); }); thingShadows.on('timeout', function(thingName, clientToken) { console.log('received timeout ' + ' on ' + operation + ': ' + clientToken); });
  19. 19. Demo 2
  20. 20. Demo 3 湿度センサー Rule EngineTopic LED Device Shadow
  21. 21. var awsIot = require('aws-iot-device-sdk'); var bridge = require('./bridge.js'); var thingShadows = awsIot.thingShadow({ keyPath: './certs/private.pem.key', certPath: './certs/certificate.pem.crt', caPath: './certs/root-CA.crt', clientId: 'pi2_01', region: 'ap-northeast-1' }); var clientTokenGet; var clientTokenUpdate; thingShadows.on('connect', function() { thingShadows.register( 'pi2_01' ); setTimeout( function() { clientTokenGet = thingShadows.get('pi2_01'); }, 2000 ); setInterval(function() { var humidity = bridge.sensor(); console.log(humidity) thingShadows.publish('pi2_01', JSON.stringify(humidity)); }, 1000); }); センサーデータを収集/ Device ShadowからLEDを操作 thingShadows.on('status', function(thingName, stat, clientToken, stateObject) { console.log('received ' + stat + ' on ' + thingName + ': ' + JSON.stringify(stateObject)); if('delta' in stateObject.state && 'color' in stateObject.state.delta){ var delta_state = stateObject.state.delta.color; bridge.led(delta_state); console.log('received delta ' + ' on ' + thingName + ': ' + JSON.stringify(stateObject)); clientTokenUpdate = thingShadows.update('pi2_01', { "state": { "reported": { "color": delta_state } } }); } }); thingShadows.on('delta', function(thingName, stateObject) { var state = stateObject.state.color; bridge.led(state); console.log('received delta ' + ' on ' + thingName + ': ' + JSON.stringify(stateObject)); clientTokenUpdate = thingShadows.update('pi2_01', { "state": { "reported": { "color": state } } }); }); thingShadows.on('timeout', function(thingName, clientToken) { console.log('received timeout ' + ' on ' + operation + ': ' + clientToken); });
  22. 22. センサーデータを収集/ Device ShadowからLEDを操作 // データの送受信のみの場合はdevice.publish device.publish('pi2_01', JSON.stringify(humidity)); // DeviceShadow使用時はthingShadows.publish thingShadows.publish('pi2_01', JSON.stringify(humidity));
  23. 23. # -*- coding: utf-8 -*- from __future__ import print_function from __future__ import unicode_literals from __future__ import absolute_import from __future__ import division import json import boto3 print('Loading function') iot = boto3.client('iot-data') def lambda_handler(event, context): print("Received event: " + json.dumps(event)) humidity = event['humidity'] if humidity < 40: color = 'blue' elif humidity >= 40 and humidity <= 70: color = 'green' else : color = 'red' DeviceShadowを操作する Lambda Function try: shadow_stream = response = iot.get_thing_shadow( thingName='pi2_01' ) shadow_string = shadow_stream['payload'].read().decode('utf-8') shadow = json.loads(shadow_string) desired_color = shadow['state']['desired']['color'] if color != desired_color: payload = { "state": { "desired": { "color": color, } } } iot.update_thing_shadow( thingName='pi2_01', payload=json.dumps(payload) ) print('Update DeviceShadow delta: {}'.format(color)) else: print('DeviceShadow has been updated') return except Exception as e: print(e) print('Error') raise e
  24. 24. Demo 3
  25. 25. 気をつけたいところ • DynamoDBにデータが入らない!? • AWS SDKにIoT関連のものが2つある
  26. 26. DynamoDBにデータが入らない!?
  27. 27. DynamoDBにデータが入らない!?
  28. 28. • Python • Node.js • Java AWS SDKにIoT関連のものが2つある
  29. 29. まとめ • AWS IoTで簡単にセンサーデータをアップで きるようになった。 • Device Shadowでリモートからのデバイス制 御がやりやすくなった。 • この冬は風邪を引かなかった!
  30. 30. まとめ • 家庭の課題解決からIoTを始めよう!

×