Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pythonと便利ガジェット、サービス、ツールを使ってセンシング〜見える化してみよう

Yuji Chino
September 16, 2019

 Pythonと便利ガジェット、サービス、ツールを使ってセンシング〜見える化してみよう

【PyCon JP 2019の発表資料です】
Pythonと便利ガジェット/サービス/ツールを使って、まかせるところはまかせて、「つくらない」「やらない」をテーマに、センシング〜Webで見える化までを目指します。

Yuji Chino

September 16, 2019
Tweet

More Decks by Yuji Chino

Other Decks in Programming

Transcript

  1. ͓લ୭Α ஌໺ ༤ೋ facebook : chinoppy twitter : @chinoppy0727 (ג)೔ຊγεςϜٕݚʢJSLʣ

    ॴଐ ௕໺ݝ௕໺ࢢʹ͋Γ·͢ ࠓ೥ͷPyCon JP 2019 ͰʮGold Sponsorʯ WebΤϯδχΞʢPython࢖ͬͯ·͢ʂʣ ۀ຿ͱϓϥΠϕʔτͰIoTͯ͠·͢ GEEKLAB.NAGANOͷ؅ཧਓ ౰͕ࣾӡӦ͢ΔίϛϡχςΟεϖʔε ௨শɿ͗ʔΒ΅ ฏ೔9:00ʙ18:00͜Ζ·Ͱ͸ແྉ։์͍ͯ͠·͢ ࡞ۀ৔ͱͯ͠΋ར༻Ͱ͖·͢ͷͰɺ͓ؾܰʹ͓ӽ͍ͩ͘͠͞
  2. ߏ੒ɿσόΠε Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ Իηϯα

    ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core
  3. ߏ੒ɿΫϥ΢υ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service
  4. ߏ੒ɿWeb AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0
  5. લ൒ σόΠε ηϯγϯάσʔλͷऔಘ ωοτϫʔΫ Ϋϥ΢υ΁ૹ৴ MQTT Raspberry Pi㱺AWS IoT Core

    Raspberry Pi㱺SORACOM㱺AWS IoT Core Ϋϥ΢υ SORACOM㱺AWS IoT Core AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ
  6. ߏ੒ Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ Իηϯα

    ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core
  7. લ൒ σόΠε ηϯγϯάσʔλͷऔಘ ωοτϫʔΫ Ϋϥ΢υ΁ૹ৴ MQTT Raspberry Pi㱺AWS IoT Core

    Raspberry Pi㱺SORACOM㱺AWS IoT Core Ϋϥ΢υ SORACOM㱺AWS IoT Core AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ
  8. ηϯαɺRaspberry Pi Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ

    Իηϯα ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core
  9. ηϯαʔ ૠ͚ͩ͢ͰηοτΞοϓ׬ྃʂ Interface Digital Grove Analog Grove UART Grove I2C

    Sensor Թ࣪౓ ໌Δ͞ ਫ෼ Ի LED Ϙλϯ LCD 100छྨҎ্͋Δ
  10. ϚΠίϯʗίϯϐϡʔλ Raspberry Pi γϯάϧϘʔυίϯϐϡʔλ ΋ͱ΋ͱڭҭΛ૝ఆ੍ͯ͠࡞ ࢖༻Ϟσϧɿ3 Model B 3͔ΒແઢLAN౥ࡌ 3͔ΒBluetoothඪ४౥ࡌ

    ͜Εʹͱ΋ͳ͍UART௨৴஫ҙ ࢀߟɿPi 3 serial Issues https://www.raspberrypi.org/forums/ viewtopic.php? f=63&t=151454#p993901 OS Raspbian https://www.raspberrypi.org/downloads/ raspbian/ Stretch Lite
  11. σδλϧ >>> import grovepi >>> >>> sensor = 4 #

    The Sensor goes on digital port 4. >>> blue = 0 # The Blue colored sensor. >>> white = 1 # The White colored sensor. >>> >>> grovepi.dht(sensor, white) [32.4, 44.0] # Թ౓, ࣪౓ >>> Թ࣪౓Λऔಘ http://wiki.seeedstudio.com/Grove-TemperatureAndHumidity_Sensor/ ʲblue : Temperature&Humidity Sensorʳ ϚΠφεͷԹ౓͕ଌΕͳ͍ ͍͚҆Ͳਫ਼౓͸ѱ͍ 820ԁ ʲwhite : Temperature&Humidity Sensor proʳ ϚΠφεͷԹ౓͕ଌΕΔ blueΑΓߴ͍͕ਫ਼౓͕Α͍ 2,095ԁ
  12. I2C $ $ cd [yourpath]/GrovePi/Software/Python/grove_i2c_digital_light_sensor $ pwd [yourpath]/GrovePi/Software/Python/grove_i2c_digital_light_sensor # αϯϓϧιʔε΋ॆ࣮

    $ python grove_i2c_digital_light_sensor.py Lux: 72 [Vis+IR=570, IR=276 @ Gain=1x, Timing=402.0ms] Lux: 72 [Vis+IR=570, IR=276 @ Gain=1x, Timing=402.0ms] ɾɾɾ ໌Δ͞Λऔಘ http://wiki.seeedstudio.com/Grove-Digital_Light_Sensor/ Digital Light Sensor
  13. Ξφϩά >>> import grovepi >>> >>> loudness_sensor = 0 >>>

    grovepi.analogRead(loudness_sensor) 124 >>> Իηϯα http://wiki.seeedstudio.com/Grove-Loudness_Sensor/ Loudness Sensor
  14. Χϝϥ >>> from picamera import PiCamera >>> >>> camera =

    PiCamera() >>> >>> camera.start_preview() <picamera.renderers.PiPreviewRenderer object at 0x769d16b0> >>> >>> # ࡱӨ >>> camera.capture('./sample.jpg') >>> >>> camera.stop_preview() >>> camera.close() ࡱӨ 3BTQCFSSZ1J$BNFSB.PEVMF7ΧϝϥϞδϡʔϧ
  15. ηϯα౳ͷσʔλऔಘ׬ྃʂ Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ Իηϯα

    ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core આ໌ࡁΈ
  16. લ൒ σόΠε ηϯγϯάσʔλͷऔಘ ωοτϫʔΫ Ϋϥ΢υ΁ૹ৴ MQTT Raspberry Pi㱺AWS IoT Core

    Raspberry Pi㱺SORACOM㱺AWS IoT Core Ϋϥ΢υ SORACOM㱺AWS IoT Core AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ
  17. ωοτϫʔΫɺΫϥ΢υ΁ૹ৴ Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ Իηϯα

    ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core આ໌ࡁΈ
  18. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi 㱺

    SORACOM 㱺 AWS IoT Core AWS IoT Core AWS IoT Core
  19. MQTT ϒϩʔΧʔ ʢதܧऀʣ MQTT ΫϥΠΞϯτA ΫϥΠΞϯτB ΞΫλʔ ύϒϦογϟʔʢൃߦऀʣ αϒεΫϥΠόʔʢߪಡऀʣ ϒϩʔΧʔʢதܧऀʣ

    ϝοηʔδͷ఻ૹ ϝοηʔδΛൃߦʢૹ৴ʣ͢ΔɿPublish ϝοηʔδͷߪಡʢड৴ʣΛొ࿥͢ΔɿSubscribe ϝοηʔδͷΩʔɿtopic
  20. MQTT ϒϩʔΧʔ ʢதܧऀʣ σʔλΛड͚औΓɺ ֘౰͢Δߪಡऀ΁ σʔλΛ఻ૹ MQTT ΫϥΠΞϯτA ΫϥΠΞϯτB ΫϥΠΞϯτ͸ʮϒϩʔΧʔʯΛ௨ͯ͠ɺϝοηʔδͷ΍ΓͱΓΛ͢Δ

    ϒϩʔΧʔ͸ɺΫϥΠΞϯτ͔Βૹ৴͞ΕͨσʔλΛ
 ͦͷtopicΛαϒεΫϥΠϒ͍ͯ͠ΔΫϥΠΞϯτ΁ૹΔ ʲPublishʳ topicɿenvironment/ ʲSubscribeʳ topicɿenvironment/ ΫϥΠΞϯτC ʲSubscribeʳ topicɿenvironment/ ΫϥΠΞϯτD
  21. MQTT ϒϩʔΧʔ ʢதܧऀʣ σʔλΛड͚औΓɺ ֘౰͢Δߪಡऀ΁ σʔλΛ఻ૹ MQTT ΫϥΠΞϯτA ΫϥΠΞϯτB ʲPublishʳ

    topicɿenvironment/ ʲSubscribeʳ topicɿenvironment/ ΫϥΠΞϯτC ʲSubscribeʳ topicɿenvironment/ ϒϩʔΧʔ͸͋Δఔ౓ɺPublish͞ΕͨϝοηʔδΛอ࣋Ͱ͖Δ ΫϥΠΞϯτD ʲSubscribeʳ topicɿenvironment/
  22. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi 㱺

    SORACOM 㱺 AWS IoT Core AWS IoT Core AWS IoT Core MQTTS MQTT
  23. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core ४උ AWS IoT

    ͷSDKͷΠϯετʔϧ https://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/iot-sdks.html#iot-python-sdk σόΠεূ໌ॻΛσόΠεଆʹ഑ஔ SDK σόΠεূ໌ॻ AWS IoT Core MQTTS
  24. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core αϯϓϧίʔυ͔Βൈਮ https://github.com/aws/aws-iot-device-sdk-python/blob/master/samples/basicPubSub/basicPubSub.py ɾɾɾʢলུʣ

    from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient ɾɾɾʢলུʣ # Read in command-line parameters parser = argparse.ArgumentParser() parser.add_argument("-e", "--endpoint", action="store", required=True, dest="host", help="Your AWS IoT custom endpoint") ɾɾɾʢলུʣ # Init AWSIoTMQTTClient myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId) myAWSIoTMQTTClient.configureEndpoint(host, port) myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath) ɾɾɾʢলུʣ # AWSIoTMQTTClient connection configuration myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20) myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing ɾɾɾʢ࣍ͷϖʔδ΁ʣ ᶃ ূ໌ॻ౳ͷಡΈࠐΈ ᶄΫϥΠΞϯτͷॳظԽ ᶅ઀ଓͷઃఆ౳
  25. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core αϯϓϧίʔυ͔Βൈਮ https://github.com/aws/aws-iot-device-sdk-python/blob/master/samples/basicPubSub/basicPubSub.py ɾɾɾʢଓ͖ʣ

    # Connect and subscribe to AWS IoT myAWSIoTMQTTClient.connect() ɾɾɾʢলུʣ # Publish to the same topic in a loop forever message = {} message['message'] = args.message messageJson = json.dumps(message) # Connect and subscribe to AWS IoT myAWSIoTMQTTClient.publish(topic, messageJson, 1) ɾɾɾʢলུʣ ᶆAWS IoT Core΁઀ଓ ᶇૹ৴σʔλ४උ ᶈpublishʂʂʂ
  26. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi㱺 SORACOM

    㱺 AWS IoT Core AWS IoT Core AWS IoT Core MQTTS MQTT આ໌ࡁΈ
  27. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 SORACOM ४උ PythonͷMQTTϥΠϒϥϦͷΠϯετʔϧ Paho-mqttΛ࢖༻ https://pypi.org/project/paho-mqtt/ SORACOM

    Air SIM ࠓճ͸ηϧϥʔΛ࢖༻ USBυϯάϧ ͜ͷதʹSIMΛ͍ΕͯRaspberry Piʹૠ͢ʂ ※υϯάϧͷઃఆʹؔͯ͠͸Githubࢀর MQTT
  28. Ϋϥ΢υ΁ૹ৴ import json import time import paho.mqtt.client as mqtt host

    = 'beam.soracom.io' port = 1883 topic = 'environment' # ॳظԽ client = mqtt.Client(protocol=mqtt.MQTTv311) client.connect(host, port=port, keepalive=60) time.sleep(5) # ૹ৴σʔλ json_dict = { 'temp': 34, 'humi': 45, } # ηϯγϯά৘ใΛૹ৴ result = client.publish(topic, json.dumps(json_dict), qos=1) client.disconnect() Raspberry Pi 㱺 SORACOM ᶃ ΫϥΠΞϯτॳظԽ ᶄSORACOM΁઀ଓ ᶅૹ৴σʔλ४උ ᶆpublishʂʂʂ
  29. Ϋϥ΢υ΁ૹ৴׬ྃʂ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi 㱺

    SORACOM 㱺 AWS IoT Core AWS IoT Core AWS IoT Core MQTTS MQTT આ໌ࡁΈ
  30. ηϯα͔ΒͷσʔλऔಘɺωοτϫʔΫ
 Ϋϥ΢υ΁ͷૹ৴׬ྃʂ Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ

    Իηϯα ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core આ໌ࡁΈ
  31. ߏ੒ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service
  32. લ൒ σόΠε ηϯγϯάσʔλͷऔಘ ωοτϫʔΫ Ϋϥ΢υ΁ૹ৴ MQTT Raspberry Pi㱺AWS IoT Core

    Raspberry Pi㱺SORACOM㱺AWS IoT Core Ϋϥ΢υ SORACOM㱺AWS IoT Core AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ
  33. SORACOM 㱺 AWS IoT Core΁ AWS IoT Core AWS Lambda

    Amazon DynamoDB AWS Lambda Amazon API Gateway Amazon Simple Storage Service
  34. SORACOMͷαʔϏεͰసૹ SORACOM 㱺 AWS IoT Core Beam ҉߸Խͯ͠ σʔλΛసૹͯ͘͠ΕΔ ূ໌ॻ͸SORACOM΁

    ొ࿥ͯ͠ઃఆ࣌ʹࢦఆ͢Δ AWS IoT Core Funnel σʔλΛಛఆͷΫϥ΢υαʔϏε΁௚઀సૹ͢ΔαʔϏε MQTT MQTTS
  35. SORACOM㱺AWS IoT Core΁ૹ৴׬ྃʂ AWS IoT Core AWS Lambda Amazon DynamoDB

    AWS Lambda Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ
  36. AWS IoT Core ͔ΒAmazon DynamoDBʹσʔλอଘ AWS IoT Core AWS Lambda

    Amazon DynamoDB AWS Lambda Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ
  37. AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ AWS IoT Core AWS Lambda Amazon

    DynamoDB AWS Lambda IoT rule ಛఆͷtopicΛड৴ͨ͠Βޙड़ ͷactionΛ࣮ߦ IoT action DynamoDBͷςʔϒϧ΁σʔλ Λొ࿥ LambdaΛ࣮ߦ ͳͲෳ਺ͷΞΫγϣϯΛα ϙʔτ͍ͯ͠Δ ࢀߟɿAWS IoT ϧʔϧΞΫγϣϯ https://docs.aws.amazon.com/ ja_jp/iot/latest/developerguide/ iot-rule-actions.html IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service
  38. AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ σόΠε͔ΒૹΒΕ͖ͯͨσʔλ { “imsi": XXXX, “timestamp": 1565276403000,

    “air_quality”: 83, “humi": 41.8, “loudness": 88, “lux": 35, “temp": 25.3, } IoT action IoT rule
  39. AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ΋׬ྃ AWS IoT Core AWS Lambda Amazon

    DynamoDB AWS Lambda Amazon API Gateway Amazon Simple Storage Service IoT action IoT rule આ໌ࡁΈ આ໌ࡁΈ
  40. ͍Ζ͍ΖͳαʔϏεΛ࢖͍·͢ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    AWS Identity and Access Management Permisions Role Amazon API Gateway Amazon Simple Storage Service
  41. serverless framework https://serverless.com/framework/ Node.jsΛར༻ͨ͠serverlessΛߏஙɺ؅ཧɺσϓϩΠ౳ΛͰ͖Δπʔ ϧ yamlʹߏங಺༰Λهࡌ͢Ε͹ίϚϯυҰൃͰσϓϩΠ AWSɺGCPɺAzureͳͲʹ΋ରԠ AWS Lambda PythonͷϥΠϒϥϦ΋ҰॹʹzipԽͯ͠σϓϩΠ

    docker-lambdaΠϝʔδΛ࢖ͬͯ
 ίϯύΠϧޙʹσϓϩΠ΋ͯ͘͠ΕΔ ߏ੒؅ཧπʔϧ ྨࣅαʔϏε AWS SAM https://aws.amazon.com/jp/serverless/sam/ serverless ༻ʹCloudFormationΛ֦ுͨ͠AWSαʔϏε AWSͷΈ࢖༻Ͱ͋Ε͹ɺͪ͜Β΋ݕ౼ͯ͠Έ͍ͯͩ͘͞
  42. ઃఆ৘ใͷอ࣋ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ આ໌ࡁΈ
  43. ઃఆ৘ใͷอ࣋ Device ShadowαʔϏε https://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/iot-device-shadows.html AWS IoT CoreͱσόΠεؒΛJSONͰ΍ΓͱΓʢGET/UPDATEʣ͕Ͱ͖Δ ͜ͷJSONʹσόΠεͷʮઃఆ৘ใʯ΍ʮঢ়ଶʯΛ΋͓ͬͯ͘ Device ShadowαʔϏε

    { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } }
  44. ઃఆ৘ใͷอ࣋ Device ShadowαʔϏε ηΫγϣϯ desiredɿΫϥ΢υ͔Βͷཁٻ reportedɿσόΠεͷঢ়ଶ deltaɿΫϥ΢υͱσόΠεͷࠩ෼ Device ShadowαʔϏε {

    # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } }
  45. σόΠεଆ cronͰຖਖ਼࣌ɺ5෼͝ͱىಈ ىಈ͝ͱDevice ShadowΛࢀর Device Shadow ૹ৴ִؒʢintervalʣ 5෼㱺10෼ִؒʹมߋ Device ShadowαʔϏε

    { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 # 10෼ִؒ } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ
  46. AWS IoT Core Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ Device Shadowͷऔಘ Ϋϥ΢υ σόΠε GETཁٻ Device

    ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } }
  47. AWS IoT Core Ϋϥ΢υ σόΠε JSONΛड͚औΔ GETཁٻ Device ShadowαʔϏε {

    # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ Device Shadowͷऔಘ
  48. AWS IoT Core Թ౓Λૹ৴ Ϋϥ΢υ σόΠε JSONΛड͚औΔ GETཁٻ Device ShadowαʔϏε

    { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } ݱࡏ࣌ࠁ 11 : 40 㱺Թ౓Λૹ৴ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ
  49. AWS IoT Core Device Shadow ߋ৽ Ϋϥ΢υ σόΠε JSONΛड͚औΔ GETཁٻ

    Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } ݱࡏ࣌ࠁ 11 : 40 㱺Թ౓Λૹ৴ UPDATEཁٻ ૹ৴σʔλ { "reported": { "sensor": { "interval": 10 } }, } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ
  50. AWS IoT Core Ϋϥ΢υ σόΠε JSONΛड͚औΔ GETཁٻ Device ShadowαʔϏε {

    # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 10 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ # ͕ͳ͘ͳΔʂ } Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } } ݱࡏ࣌ࠁ 11 : 40 㱺Թ౓Λૹ৴ UPDATEཁٻ ૹ৴σʔλ { "reported": { "sensor": { "interval": 10 } }, } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ Device Shadow ߋ৽
  51. AWS IoT Core Ϋϥ΢υ σόΠε GETཁٻ Device ShadowαʔϏε { #

    Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ # ͕ͳ͘ͳΔʂ } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ Device Shadow औಘ MQTT Ͱ΍ΓͱΓ͍ͯ͠Δ
  52. AWS IoT Core Ϋϥ΢υ σόΠε JSONΛड͚औΔ Device ShadowαʔϏε { #

    Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ # ͕ͳ͘ͳΔʂ } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ Device Shadow औಘ Subscribe GETཁٻʢPublishʣ MQTT Ͱ΍ΓͱΓ͍ͯ͠Δ
  53. ઃఆ৘ใͷอ࣋ subscribe import paho.mqtt.client as mqtt ɾɾɾ def on_connect(client, userdata,

    flags, respons_code): # ϒϩʔΧʔʹ઀ଓͰ͖ͨΒαϒεΫϥΠϒ client.subscribe(topic) def on_message(client, userdata, msg): # ϝοηʔδड৴ʢDevice Shadow͔Βͷσʔλड৴ʣ࣌ json_str = msg.payload.decode(encoding='utf-8') client.disconnect() client = mqtt.Client(protocol=mqtt.MQTTv311) # callbackΛొ࿥ client.on_connect = on_connect client.on_message = on_message # ઀ଓ client.connect(host, port=port, keepalive=60) # ଴ͪड͚ঢ়ଶʹ͢Δ client.loop_forever() Device Shadow ͔Βड৴ͨ͠σʔλ 㱺͜ΕΛ࢖͍͍ͨʂ on_messageΛcallback ొ࿥͢Δ ※ຊίʔυ͸paho-mqtt࢖༻࣌ͷྫ ※AWS IoT SDK࢖༻࣌͸SDKͷϝιουΛ࢖༻ͯ͠Device Shadowͱ΍ΓͱΓ͠·͢
  54. ઃఆ৘ใͷอ࣋ subscribe import paho.mqtt.client as mqtt ɾɾɾ @app.task def run_subscribe(host,

    port, topic): redis_client = RedisClient() def on_connect(client, userdata, flags, respons_code): client.subscribe(topic) def on_message(client, userdata, msg): json_str = msg.payload.decode(encoding='utf-8') redis_client.set('token', json_str) client.disconnect() redis_client.delete('token') client = mqtt.Client(protocol=mqtt.MQTTv311) client.on_connect = on_connect client.on_message = on_message client.connect(host, port=port, keepalive=60) # ଴ͪड͚ঢ়ଶʹ͢Δ client.loop_forever() Device Shadow ͔Βड৴ͨ͠σʔλ 㱺Redisʹset PythonͷඇಉظϥΠϒϥϦ CeleryΛ࢖༻ͯ͠λεΫԽ ※ຊίʔυ͸paho-mqtt࢖༻࣌ͷྫ ※AWS IoT SDK࢖༻࣌͸SDKͷϝιουΛ࢖༻ͯ͠Device Shadowͱ΍ΓͱΓ͠·͢
  55. ઃఆ৘ใͷอ࣋ subscribe + publishʢGETཁٻʣ import paho.mqtt.client as mqtt ɾɾɾ redis_client

    = RedisClient() # αϒεΫϥΠϒͷඇಉظλεΫ࣮ߦ result_sub = tasks_mqtt.run_subscribe.delay(ɾɾɾ) # publishʢGETཁٻʣ client = mqtt.Client(protocol=mqtt.MQTTv311) client.connect(host, port=port, keepalive=60) result = client.publish(topic, json.dumps(json_dict), qos=1) # αϒεΫϥΠϒͷඇಉظλεΫऴྃ଴ͪ while not result_sub.ready(): time.sleep(1) # Device ShadowΛऔಘ value = redis_client.get('token') payload_str = value.decode(encoding='utf-8') # Թ౓Λpublish # Device ShadowΛߋ৽ʢupdateͷτϐοΫʹpublishʣ Redis͔ΒDevice Shadow ͷσʔλΛऔಘ subscribeͷλεΫΛ࣮ߦ publishʂʂʂ ※ຊίʔυ͸paho-mqtt࢖༻࣌ͷྫ ※AWS IoT SDK࢖༻࣌͸SDKͷϝιουΛ࢖༻ͯ͠Device Shadowͱ΍ΓͱΓ͠·͢ αϒεΫϥΠϒͷ λεΫऴྃ଴ͪ
  56. ঢ়ଶͷมߋ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ આ໌ࡁΈ ৄࡉ͸
 Appendixࢀর
  57. ؂ࢹ AWS IoT Core AWS Lambda Amazon DynamoDB Amazon Simple

    Storage Service AWS Lambda Amazon API Gateway આ໌ࡁΈ આ໌ࡁΈ
  58. AWS IoT Core ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ Ϋϥ΢υ σόΠε ը૾Λૹ৴ Amazon Simple

    Storage Service TriggerͰ LambdaΛىಈ ࡱӨ ͲͷΑ͏ʹ Tweet͍ͯ͠Δ͔
  59. ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ url_media = "https://upload.twitter.com/1.1/media/upload.json" url_text = "https://api.twitter.com/1.1/statuses/update.json" CK =

    os.getenv('TWITTER_CONSUMER_KEY') ɾɾɾʢলུʣ twitter = OAuth1Session(CK, CS, AT, ATS) # S3΁อଘ͞Εͨը૾Λऔಘ bucket_name = event['Records'][0]['s3']['bucket']['name'] object_key = event['Records'][0]['s3']['object']['key'] response = s3.get_object(Bucket=bucket_name, Key=object_key) body = response['Body'].read() # ը૾Λpost files = {"media": body} req_media = twitter.post(url_media, files=files) media_id = json.loads(req_media.text)['media_id'] # tweet jst = timezone(timedelta(hours=+9), 'JST') now = datetime.now(jst) params = {"status": "{} XXXXXX ษڧձ։࠵த ษڧத #glnagano" .format(now.strftime(“%Y/%m/%d %H:%M")), "media_ids": [media_id]} req_text = twitter.post(url_text, params=params) ը૾Λऔಘ Twitter΁ը૾Λpost Twitter΁Tweet
  60. ௨஌ AWS IoT Core AWS Lambda Amazon DynamoDB Amazon Simple

    Storage Service AWS Lambda Amazon API Gateway આ໌ࡁΈ આ໌ࡁΈ આ໌ࡁΈ આ໌ࡁΈ LINE Notifyͷઆ໌͸
 Appendixࢀর
  61. import ssl import slackweb class Slack: slack_webhook_url = '[slackͰऔಘͨ͠webhookͷURL]' @staticmethod

    def notify(slack_url, message): ssl._create_default_https_context = ssl._create_unverified_context slack = slackweb.Slack(url=slack_url) slack.notify(text=message, username="notify", icon_emoji=":bell:") # slack΁௨஌ Slack.notify(Slack.slack_webhook_url, 'Թ౓͕30౓Ҏ্Ͱ͢ʂ') ௨஌ Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ Slack΁௨஌ʂ
  62. import requests class LineNotify: @classmethod def notify(cls, message): payload =

    {'message': message} res = cls._requests(payload) return res @classmethod def _requests(cls, payload): url = 'https://notify-api.line.me/api/notify' access_token = '[ΞΫηετʔΫϯ]' headers = {'authorization': 'Bearer {}'.format(access_token)} res = requests.post(url, headers=headers, params=payload) return res # LINE Notifyʹ௨஌ LineNotify.notify('Թ౓͕30౓Ҏ্Ͱ͢ʂ') ௨஌ Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ LINE Notifyʹ௨஌ʂ
  63. σʔλอଘɺ؂ࢹɺ௨஌͕Ͱ͖·ͨ͠ʂ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ
  64. Web

  65. ߏ੒ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0
  66. APIͰηϯαͷσʔλΛެ։ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 ΦʔιϥΠβʔɾ Auth0ͷৄࡉ͸
 Appendixࢀর
  67. ΞΫηε੍ݶ ࠓճ͸Amazon API Gateway + AWS LambdaͰσʔλΛެ։ Amazon API GatewayͷΞΫηε੍ݶ

    https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/apigateway- control-access-to-api.html ࠓճ͸ʮAPI Gateway Lambda ΦʔιϥΠβʔʯΛ࢖༻ https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/ apigateway-use-lambda-authorizer.html τʔΫϯͷੜ੒ɺೝূ౳͸Auth0Λ࢖༻
  68. APIͰηϯαͷσʔλΛެ։·Ͱ׬ྃʂ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 આ໌ࡁΈ
  69. Webϖʔδ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 આ໌ࡁΈ
  70. Char.js άϥϑදࣔ const chartDht = new Chart(ctxDht, { type: 'line',

    data: { labels: labels, datasets: [ { label: 'Թ౓', data: dataTemp, backgroundColor: 'rgba(0, 0, 0, 0)', borderColor: 'rgba(255, 99, 132, 1)', borderWidth: 1 }, { label: '࣪౓', data: dataHumi, ɾɾɾ }, { label: 'ۭؾͷ඼࣭', data: dataAirQuality, ɾɾɾ },] }, options: { ɾɾɾ } });
  71. ɾɾɾ columnDefs: [ // ૹ৴೔࣌ { "targets": 0, data: "send_time"},

    // Թ౓ { "targets": 1, data: "temp", "render": function (data, type, full, meta) { return (webCommons.dispRange(data)) ? data : "-"; } }, // ࣪౓ { "targets": 2, data: "humi", ɾɾɾ { className: "center", "targets": [ 0, 1, 2, 3, 4, 5, ] } ], ɾɾɾ ςʔϒϧදࣔ DataTables
  72. WebϖʔδͰݟ͑ΔԽ·Ͱ׬ྃʂ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 આ໌ࡁΈ આ໌ࡁΈ
  73. ॳظίετ ඼໊ ݸ਺ ߹ܭ ࢀߟݩ౳ 3BTQCFSSZ1J .PEFM#   ԁ

    ੫ࠐ ,4: (SPWF1J   ԁ ੫ࠐ "NB[PO Թ࣪౓ܭQSP   ԁ ੫ࠐ "NB[PO (64#υϯάϧ ",   ԁ ੫ൈ 403"$0. 403"$0.4*.  ԁ ੫ൈ ˞QMBO%ɺσʔλ௨৴ͷΈ ߹ܭ — ໿ ԁ ॳظඅ༻ʢૹྉผʣ ※ηϯα͸͢΂ͯܝࡌ͍ͯ͠·ͤΜ
  74. ϥϯχϯάίετ ༻్ ೔͝ͱ ݄͝ͱ උߟ ηϯγϯάσʔλͷૹ৴ ճʢ෼͝ͱʣ࣌✕✕୆ ܭɿճ ճ೔✕೔ ܭɿճ

    σόΠε㱺 "84*P5$PSF㱺 ֤αʔϏε σʔλόοΫΞοϓ ճ ճ %ZOBNP%#ͷ೔෼ͷσʔ λΛ4ʹόοΫΞοϓ ௨஌ ఆظ͕ճ ෆఆظ͕ճ͘Β͍ ܭɿճ ճ೔✕೔ ܭɿճ TMBDLɺ-*/&/PUJGZ΁ ௨஌ 8FCϖʔδͷࢀর ໿ճ ճ೔✕೔ ճ ※ෆఆظͰಈ͍͍ͯΔͱ͜Ζ΋͋ΔͨΊɺྉ࣮ۚ੷ͷ࢖༻ྔͱҰக͸͠·ͤΜ ࢖༻ස౓౳ ʲσʔλͷݟ͑ΔԽʗ௨஌ʳ
  75. ϥϯχϯάίετ ༻్ ೔͝ͱ ݄͝ͱ උߟ ࣸਅࡱӨͨ͠5XFFU ճ ෼͝ͱ ࣌✕ ܭɿճ

    ճ೔✕೔ ܭɿճ σόΠε㱺 "84*P5$PSF㱺 ֤αʔϏε ,ຕ✕ճ ෼͝ͱ ࣌✕ ܭɿ.# .#೔✕೔ ܭɿ.# ʏ %FWJDF4IBEPX ʢ(&561%"5&ʣ ճ ෼͝ͱ ࣌✕ ܭɿճ ճ೔✕೔ ܭɿ ճ σόΠε㱻"84*P5$PSF ※ෆఆظͰಈ͍͍ͯΔͱ͜Ζ΋͋ΔͨΊɺྉ࣮ۚ੷ͷ࢖༻ྔͱҰக͸͠·ͤΜ ※೔தͷΈՔಇʢ҉͘ͳΔͱࡱӨ͠ͳ͍ʣ ࢖༻ස౓౳ ʲ؂ࢹʳ
  76. ϥϯχϯάίετ αʔϏε ྉۚʢ੫ผʣ උߟ "JS4*. جຊྉۚ ຕɿԁ೔ ˞ ௨৴ྉ ԁ.#

    ˞ #FBN جຊྉۚ ແྉ ϦΫΤετྉ ԁϦΫΤετ ˞ ແྉར༻࿮͋Γ SORACOM ※ಛఆ஍Ҭ޲͚ IoT SIMɺplan-Dɺ࢖༻։࢝ޙɺσʔλ௨৴ͷΈɺs1.standard ɹhttps://soracom.jp/services/air/cellular/price_specific_area_sim/#plan-d 1SIM ໿300ԁ/݄ ✕ ࢖༻ຕ਺ ʲσʔλͷݟ͑ΔԽʗ௨஌ɺ؂ࢹʳ
  77. ϥϯχϯάίετ αʔϏε ࢖༻ྉ ࢖༻ྔ ྉۚମܥ౳ උߟ "84*P5$PSF   .FTTBHFT

    ϝοηʔδԯ݅·Ͱ 64%  ϝοηʔδສ݅͋ͨΓ ˞ ଞͷྉۚ͸ԁͷͨΊׂѪ %ZOBNP%#   3FBE 8SJUF (#.P DBQBDJUZVOJUɿͳͷͰແྉ࿮಺ (#Ҏ಺ͳͷͰແྉ࿮ ˞ -BNCEB   TFDPOET  3FRVFTUT ྆ํͱ΋ແྉ࿮಺ ˞ "NB[PO"1* (BUFXBZ  3FRVFTU ʲ"1*ίʔϧʳ ࠷ॳͷԯ ສ·Ͱ ສ͋ͨΓ64% ˞ "NB[PO4JNQMF 4UPSBHF4FSWJDF  3FRVFTUT 3FRVFTUT (#.P  165ଞ·Ͱɿ  (&5ଞ·Ͱɿ ࠷ॳͷ5#݄·Ͱɿ(# ˞ ߹ܭ  AWS ※౦ژϦʔδϣϯͷྉۚͰ͢ ※ྉۚମܥ͸লུͯ͠Δͱ͜Ζ΋͋Γ·͢ͷͰɺৄࡉ͸AWSͷϖʔδΛࢀর ʲσʔλͷݟ͑ΔԽʗ௨஌ʳ˞2019೥7݄࣮੷
  78. ϥϯχϯάίετ αʔϏε ྉۚ ࢖༻ྔ ྉۚମܥ౳ උߟ "84*P5$PSF  ໿0QFSBUJPOT %FWJDF4IBEPXBOE%FWJDF3FHJTUSZ

    0QFSBUJPOTɿສ͝ͱ ˞ ଞͷྉۚ͸ԁͷͨΊׂѪ -BNCEB    ˞ "NB[PO4JNQMF 4UPSBHF4FSWJDF    165ଞ·Ͱɿ  (&5ଞ·Ͱɿ ࠷ॳͷ5#݄·Ͱɿ(# ˞ ߹ܭ  AWS ※౦ژϦʔδϣϯͷྉۚͰ͢ ※ྉۚମܥ͸লུͯ͠Δͱ͜Ζ΋͋Γ·͢ͷͰɺৄࡉ͸AWSͷϖʔδΛࢀর ※LambdaɺS3ʹ͍ͭͯ͸ɺଞαʔϏεͰ΋࢖༻͍ͯ͠ΔͨΊਪఆ஋ʢσʔλͷݟ͑ΔԽʗ௨஌ΑΓ࢖༻ྉ͸গͳ͍ʣ ʲ؂ࢹʳ˞2019೥7݄࣮੷
  79. ϥϯχϯάίετ ϓϥϯ ྉۚ උߟ '3&&ϥΠηϯε ແྉ Ұ෦ɺػೳ੍ݶ͋Γ %&7&-01&3 ݄ʙ &YUFSOBM"DUJWF6TFSTʹΑΓมಈ

    %&7&-01&3130 ˈ݄ʙ &YUFSOBM"DUJWF6TFSTଞʹΑΓมಈ Auth0 ※DEVELOPER PROͷ্ʹʮENTERPRISEʯϥΠηϯε͕͋Γ·ׂ͕͢Ѫ FREEϥΠηϯεͳͷͰɺ0ԁ ʲσʔλͷݟ͑ΔԽʳ
  80. ϥϯχϯάίετ HEROKU FreeͳͷͰ0ԁ ࢖༻ऀ͸਺ेਓͰͦΕ΄ͲසൟʹΞΫηε͞ΕΔ༁Ͱ͸ͳ͍ͷͰFreeͰे෼ ʲdyno https://jp.heroku.com/pricing#dynosʳ ϓϥϯ ྉۚ උߟ 'SFF

    ແྉ Ұ෦ɺػೳ੍ݶ͋Γ )PCCZ ࠷େ݄EZOP ඵ୯Ґͷ՝ۚ 4UBOEBSE ࠷େ݄EZOP ඵ୯Ґͷ՝ۚ 1FSGPSNBODF ʲσʔλͷݟ͑ΔԽʳ
  81. ϥϯχϯάίετ HEROKU Hobby-FreeͳͷͰ0ԁ ηϯγϯάσʔλ͸AWSʹ͋ΔͷͰHobby - FreeͰे෼ ʲHeroku Postgres https://jp.heroku.com/pricing#databasesʳ ϓϥϯ

    ྉۚ උߟ )PCCZ'SFF ແྉ  ߦ·Ͱ )PCCZ#BTJD ࠷େ݄  ສߦ·Ͱ ඵ୯Ґͷ՝ۚ 4UBOEBSE ࠷େ ݄ ඵ୯Ґͷ՝ۚ 1SFNJVN ࠷େ ݄ ඵ୯Ґͷ՝ۚ ʲσʔλͷݟ͑ΔԽʳ
  82. ঢ়ଶͷมߋ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ આ໌ࡁΈ
  83. σόΠεଆ cronͰຖਖ਼࣌ɺ5෼͝ͱىಈ ىಈ͝ͱDevice ShadowΛࢀর Device Shadow ΧϝϥͷࡱӨ༗ແʢis_shootingʣ trueɿࡱӨ͢Δ falseɿࡱӨ͠ͳ͍ Device

    ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } "camera": { "is_shooting": true } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 10 } "camera": { "is_shooting": true } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ } ঢ়ଶͷมߋ
  84. AWS IoT Core ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ Ϋϥ΢υ σόΠε Device ShadowαʔϏε {

    # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 }, “camera": { “is_shooting": true } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 10 }, “camera": { “is_shooting": true } }, } ໌Δ͞Λૹ৴ ໌Δ͞ʹ0ʢਅͬ҉ʣ LambdaΛ࣮ߦ ਅͬ҉ʹͳͬͨͷͰShadowΛߋ৽
 is_shootingΛʮfalseʯ
  85. AWS IoT Core ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ Ϋϥ΢υ σόΠε Device ShadowαʔϏε {

    # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 }, “camera": { “is_shooting": false } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 10 }, “camera": { “is_shooting": true } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ “delta": { “camera": { “is_shooting": false } } } ໌Δ͞Λૹ৴ ໌Δ͞ʹ0ʢਅͬ҉ʣ LambdaΛ࣮ߦ ਅͬ҉ʹͳͬͨͷͰShadowΛߋ৽
 is_shootingΛʮfalseʯ
  86. AWS IoT Core ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ Ϋϥ΢υ σόΠε Device ShadowαʔϏε 


    Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 }, "camera": { "is_shooting": false } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 10 }, "camera": { "is_shooting": true } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "camera": { "is_shooting": false } } } ࠩ෼Λreportedͱͯ͠ JSONͰૹΔʢUPDATEʣ JSONΛड͚औΔ GETཁٻ is_shooting͕false ͳͷͰࡱӨ͠ͳ͍ ໌Δ͞Λૹ৴ ໌Δ͞ʹ0ʢਅͬ҉ʣ LambdaΛ࣮ߦ ਅͬ҉ʹͳͬͨͷͰShadowΛߋ৽
 is_shootingΛʮfalseʯ
  87. AWS IoT Core ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ Ϋϥ΢υ σόΠε Device ShadowαʔϏε 


    Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 }, "camera": { "is_shooting": false } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 10 }, "camera": { "is_shooting": false } } # Ϋϥ΢υͱσόΠεͷࠩ෼ } ࠩ෼Λreportedͱͯ͠ JSONͰૹΔʢUPDATEʣ JSONΛड͚औΔ GETཁٻ is_shooting͕false ͳͷͰࡱӨ͠ͳ͍ ໌Δ͞Λૹ৴ ໌Δ͞ʹ0ʢਅͬ҉ʣ LambdaΛ࣮ߦ ਅͬ҉ʹͳͬͨͷͰShadowΛߋ৽
 is_shootingΛʮfalseʯ
  88. AWS IoT Core ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ Ϋϥ΢υ σόΠε Device ShadowαʔϏε 


    Device ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 }, "camera": { "is_shooting": false } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 10 }, "camera": { "is_shooting": false } } # Ϋϥ΢υͱσόΠεͷࠩ෼ } ࠩ෼Λreportedͱͯ͠ JSONͰૹΔʢUPDATEʣ JSONΛड͚औΔ GETཁٻ is_shooting͕false ͳͷͰࡱӨ͠ͳ͍ ໌Δ͞Λૹ৴ ໌Δ͞ʹ0ʢਅͬ҉ʣ LambdaΛ࣮ߦ ਅͬ҉ʹͳͬͨͷͰShadowΛߋ৽
 is_shootingΛʮfalseʯ ͲͷΑ͏ʹ Device Shadow Λมߋ͍ͯ͠Δ͔
  89. ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ import boto3 # IoTDataPlaneΫϥΠΞϯτ iot_data = boto3.client('iot-data') #

    ࡱӨ͠ͳ͍ʹ͢Δ update_payload = b'{"state": {"desired": {"camera": {"is_shooting": false}}}}' # ShadowΛUPDATE͢Δ iot_data.update_thing_shadow( thingName=thing_name, payload=update_payload )
  90. ঢ়ଶͷมߋ ͪͳΈʹDevice Shadowͷऔಘ΋؆୯ import boto3 # IoTDataPlaneΫϥΠΞϯτ iot_data = boto3.client('iot-data')

    # shadow ͷऔಘ response = iot_data.get_thing_shadow( thingName=thing_name ) payload = response['payload'] payload_dict = json.loads(payload.read())
  91. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ʲϨεϙϯεʳDynamoDB΁ొ࿥ͨ͠σʔλ [ { “imsi": XXXX, “timestamp": 1565276403000, “air_quality”: 83, “humi": 41.8, “loudness": 88, “lux": 35, “temp": 25.3, }, ɾɾɾ ] ΞΫηε੍ݶͳ͠ͷ৔߹
  92. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ʲϨεϙϯεʳDynamoDB΁ొ࿥ͨ͠σʔλ [ { “imsi": XXXX, “timestamp": 1565276403000, “air_quality”: 83, “humi": 41.8, “loudness": 88, “lux": 35, “temp": 25.3, }, ɾɾɾ ] ΞΫηε੍ݶͳ͠ͷ৔߹ URLΛ஌͍ͬͯΔͱ୭Ͱ΋ΞΫηεͰ͖ ͯ͠·͏
  93. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ΞΫηε੍ݶ͋Γͷ৔߹
  94. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ
  95. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ ೝূOK
  96. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ ʲϨεϙϯεʳDynamoDB΁ొ࿥ͨ͠σʔλ [ { “imsi": XXXX, “timestamp": 1565276403000, “air_quality”: 83, “humi": 41.8, “loudness": 88, “lux": 35, “temp": 25.3, }, ɾɾɾ ] ೝূOK
  97. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ ʲϨεϙϯεʳDynamoDB΁ొ࿥ͨ͠σʔλ [ { “imsi": XXXX, “timestamp": 1565276403000, “air_quality”: 83, “humi": 41.8, “loudness": 88, “lux": 35, “temp": 25.3, }, ɾɾɾ ] ೝূOK
  98. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ ೝূNG
  99. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ ೝূNG ޙଓͷLambda͸࣮ߦ͞Εͣ ݖݶͳ͕͠ฦ٫͞ΕΔ
  100. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ ޙଓͷLambda͸࣮ߦ͞Εͣ ݖݶͳ͕͠ฦ٫͞ΕΔ τʔΫϯͷൃߦ͸ʁ
  101. ΞΫηε੍ݶ Auth0Λར༻ Amazon API Gateway ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ Auth0 ϦΫΤετલʹ

    τʔΫϯऔಘ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ϦΫΤετͷ τʔΫϯΛೝূ
  102. ΞΫηε੍ݶ Auth0Λར༻ Amazon API Gateway ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ Auth0 ϦΫΤετલʹ

    τʔΫϯऔಘ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ϦΫΤετͷ τʔΫϯΛೝূ
  103. from auth0.v3.authentication import GetToken ɾɾɾ # τʔΫϯऔಘʹඞཁͳ৘ใʢAuth0ͷϖʔδ͔Βऔಘʣ domain = settings.AUTH0_DOMAIN

    client_id = settings.AUTH0_CLIENT_ID client_secret = settings.AUTH0_CLIENT_SECRET api_audience = settings.API_AUDIENCE # τʔΫϯΛऔಘ get_token = GetToken(domain) token = get_token.client_credentials(client_id, client_secret, api_audience) ΞΫηε੍ݶ Auth0Λར༻
  104. ΞΫηε੍ݶ Auth0Λར༻ Amazon API Gateway ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ Auth0 ϦΫΤετલʹ

    τʔΫϯऔಘ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ϦΫΤετͷ τʔΫϯΛೝূ
  105. from jose import jwt ɾɾɾ try: payload = jwt.decode( token,

    rsa_key, algorithms=ALGORITHMS, audience=API_AUDIENCE, issuer="https://" + AUTH0_DOMAIN + "/" ) except jwt.ExpiredSignatureError: raise AuthError({"code": "token_expired", "description": "token is expired"}, 401) except jwt.JWTClaimsError: raise AuthError({"code": "invalid_claims", "description": "incorrect claims, please check the audience and issuer"}, 401) except Exception: raise AuthError({"code": "invalid_header", "description": "Unable to parse authentication token."}, 401) ΞΫηε੍ݶ Auth0Λར༻