Pro Yearly is on sale from $80 to $50! »

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

7c79a6c043825af5c538fb996430607c?s=47 Yuji Chino
September 16, 2019

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

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

7c79a6c043825af5c538fb996430607c?s=128

Yuji Chino

September 16, 2019
Tweet

Transcript

  1. PythonͱศརΨδΣοτɺαʔϏεɺπʔϧΛ ࢖ͬͯηϯγϯάʙݟ͑ΔԽͯ͠ΈΑ͏ 1Z$PO+1 ஌໺༤ೋʢ!DIJOPQQZʣ

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

    ॴଐ ௕໺ݝ௕໺ࢢʹ͋Γ·͢ ࠓ೥ͷPyCon JP 2019 ͰʮGold Sponsorʯ WebΤϯδχΞʢPython࢖ͬͯ·͢ʂʣ ۀ຿ͱϓϥΠϕʔτͰIoTͯ͠·͢ GEEKLAB.NAGANOͷ؅ཧਓ ౰͕ࣾӡӦ͢ΔίϛϡχςΟεϖʔε ௨শɿ͗ʔΒ΅ ฏ೔9:00ʙ18:00͜Ζ·Ͱ͸ແྉ։์͍ͯ͠·͢ ࡞ۀ৔ͱͯ͠΋ར༻Ͱ͖·͢ͷͰɺ͓ؾܰʹ͓ӽ͍ͩ͘͠͞
  3. ͓લ୭Α ͗ʔΒ΅ͪΌΜ ͗ʔΒ΅ʹՃΘͬͨ৽͍͠஥ؒ ه೦඼ʹεςοΧʔ͸͍ͬͯ·͢ ࡞ɿࣾ಺ͷPython࢖͍ͷֆࢣ͞Μ

  4. εϥΠυɺίʔυ౳ʹ͍ͭͯ ͜ͷεϥΠυ͸ެ։͠·͢ʂ εϥΠυதͷίʔυ͸࠷খݶͷهࡌͱͳͬͯ·͢ GithubͰެ։தͰ͢ʢҰ෦ඇެ։ίʔυ͋Γʣ ηϯγϯάʢRaspberry PiΛલఏʣ https://github.com/peacemaker07/iot_making_for_raspberry_pi Ϋϥ΢υʢserverless frameworkʣ https://github.com/peacemaker07/iot_making_for_sls

    WebʢDjangoʣ https://github.com/peacemaker07/iot_making_for_django ※ࠓޙ΋ਵ࣌ɺߋ৽༧ఆͰ͢
  5. ͓ئ͍ ·ͩ·ͩվળ఺΍ศརͳαʔϏεɺπʔϧ͸͍Ζ͍Ζ͋Γ·͢ ΑΓΑ͍πʔϧɺαʔϏεɺϥΠϒϥϦ͋ΔΑʂ ͦ͜ɺ͜͏ͨ͠ํ͕Α͍ͷͰ͸ʁ ͦ͜͸஌Βͳ͔ͬͨʂ ͳͲڭ͑ͯ΋Β͑Δͱ͏Ε͍͠Ͱ͢

  6. ηϯγϯάʙݟ͑ΔԽ ͦ΋ͦ΋ηϯγϯάͱ͸ ηϯαʔΛར༻ͯ͠෺ཧྔ΍ԻɾޫɾѹྗɾԹ౓ͳͲΛ ܭଌɾ൑ผ͢Δ͜ͱ ※ʮίτόϯΫ-ηϯγϯάͱ͸ʯ͔ΒҾ༻

  7. ηϯαʔ౳ ηϯγϯάʙݟ͑ΔԽ ܭଌɿσʔλͷݟ͑ΔԽ

  8. ηϯαʔ౳ ηϯγϯάʙݟ͑ΔԽ ؂ࢹɿผͷ৔ॴͷݟ͑ΔԽ

  9. ηϯγϯάʙݟ͑ΔԽ ௨஌ɿঢ়گͷݟ͑ΔԽ ηϯαʔ౳

  10. ηϯγϯάʙݟ͑ΔԽ ௨஌ɿঢ়گͷݟ͑ΔԽ ܭଌɿσʔλͷݟ͑ΔԽ ؂ࢹɿผͷ৔ॴͷݟ͑ΔԽ

  11. ن໛ খن໛ ݸਓ·ͨ͸਺ਓͰ։ൃɺӡ༻͍͚ͯ͠ΔϨϕϧ ར༻ऀ΋਺ेਓ͘Β͍Λ૝ఆ

  12. ηϯαʔ౳ ͍Ζ͍Ζͳٕज़ɾ஌͕ࣝඞཁ Ϋϥ΢υ Web ωοτϫʔΫ σόΠε

  13. ຊ೔ͷΰʔϧʂ Python σόΠεɺΫϥ΢υɺWeb͢΂ͯ࢖͑Δʂ ࠓޙɺػցֶशͳͲಋೖ࣌΋Python࢖͑Δʂ ศརͰ৴པͰ͖ΔΨδΣοτɺαʔϏεɺϥΠϒϥϦΛ࢖͏ ʮͭ͘Βͳ͍ʯʮ΍Βͳ͍ʯͰ΋Α͍αʔϏε͕ߏஙͰ͖Δ ͦͷ෼ɺࣗ෼͕ͭ͘Γ͍ͨ΋ͷʹ஫ྗͰ͖Δ εϐʔσΟʹΑ͍αʔϏε͕ϦϦʔεͰ͖Δ

  14. ͖͔͚ͬ ೶Ոͷํ ʢݩϋʔυ԰͞Μʣ ϋ΢ε಺ͷ؀ڥσʔλऩूΛ͍ͨ͠ʂ Ͱ΋ɺΫϥ΢υʗWebΘ͔Βͳ͍ʢ>_<ʣ ௨஌ WebͰݟ͑ΔԽ ૣ͘࢖ͬͯ΋Β͏͜ͱ͕Ͱ͖ͨ ࠓ΋҆ఆՔಇதͰ͢

  15. ͖͔͚ͬ ΧʔυΛ͔ͯ͟͠ ϫϯλονͰग़ୀۈͰ͖Δʂ ग़ୀۈ੾Γସ͑

  16. ͖͔͚ͬ Πϕϯτ࣌ʹ։࠵෩ܠΛࣗಈTweet ௨஌

  17. ݟ͑ΔԽɹָ͍͠ ࠓ·Ͱؾ͕෇͔ͳ͔ͬͨ͜ͱʹؾ͕͚ͭͨΓ σʔλΛ஝ੵ͓͚ͯ͠͹ɺ͋ͱͰ෼ੳ
 㱺͔ͦ͜Β·ͨ৽͍͜͠ͱ͕ݟ͑ͨΓ

  18. ΞδΣϯμ σόΠε Ϋϥ΢υ Web ࠷ޙʹ ίετ

  19. Ί͟͢΋ͷ

  20. Ί͟͢΋ͷ σʔλͷݟ͑ΔԽ

  21. Ί͟͢΋ͷ ผͷ৔ॴͷݟ͑ΔԽ

  22. Ί͟͢΋ͷ ঢ়گͷݟ͑ΔԽ

  23. ߏ੒

  24. ߏ੒ɿσόΠε Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ Իηϯα

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

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

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

    Raspberry Pi㱺SORACOM㱺AWS IoT Core Ϋϥ΢υ SORACOM㱺AWS IoT Core AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ
  28. σόΠε

  29. ߏ੒ Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ Իηϯα

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

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

    Իηϯα ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core
  32. ηϯαʔ Grove System seeed studio͕ࣾ։ൃ https://www.seeedstudio.com ϕʔεϢχοτͱඪ४Խ͞ΕͨίωΫλΛඋ͍͑ͯΔ ૠ͚ͩ͢Ͱѻ͑ΔηϯαʔͳͲͷϞδϡʔϧ͕ଟ਺ PythonϥΠϒϥϦ͕͋Δʂ https://github.com/DexterInd/GrovePi/tree/master/Software/Python

    ࢀߟ : http://wiki.seeedstudio.com/Grove_System/ ͸Μ͚ͩͮɺ഑ઢͷඞཁ͕ͳ͘PythonϥΠϒϥϦ΋͋Δ
  33. ηϯαʔ ϕʔεϢχοτɿGrove Pi ඪ४Խ͞ΕͨίωΫλͱηϯα

  34. ηϯαʔ ૠ͚ͩ͢ͰηοτΞοϓ׬ྃʂ Interface Digital Grove Analog Grove UART Grove I2C

    Sensor Թ࣪౓ ໌Δ͞ ਫ෼ Ի LED Ϙλϯ LCD 100छྨҎ্͋Δ
  35. ϚΠίϯʗίϯϐϡʔλ 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
  36. σδλϧɿԹ࣪౓ܭ I2Cɿ໌Δ͞ ΞφϩάɿԻͷେ͖͞ ΧϝϥɿࡱӨ ϥΠϒϥϦΛ࢖ͬͯσʔλΛऔಘ

  37. σδλϧ >>> 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ԁ
  38. 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
  39. Ξφϩά >>> import grovepi >>> >>> loudness_sensor = 0 >>>

    grovepi.analogRead(loudness_sensor) 124 >>> Իηϯα http://wiki.seeedstudio.com/Grove-Loudness_Sensor/ Loudness Sensor
  40. Χϝϥ >>> 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ΧϝϥϞδϡʔϧ
  41. ηϯα౳ͷσʔλऔಘ׬ྃʂ Raspberry Pi 3 Model B Grove System Թ࣪౓ܭ Իηϯα

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

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

    ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core આ໌ࡁΈ
  44. SORACOM IoT ϓϥοτϑΥʔϜ MVNO ͱͯ͠σʔλ௨৴ SIM Λఏڙ ϞόΠϧ௨৴ͱΫϥ΢υΛҰ؏ͯ͠ఏڙ ࢀߟɿ https://soracom.jp/overview/

    ωοτϫʔΫ ωοτϫʔΫɺηΩϡϦςΟΛ͓·͔ͤʂ
  45. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi 㱺

    SORACOM 㱺 AWS IoT Core AWS IoT Core AWS IoT Core
  46. MQTT Message Queuing Telemetry Transportͷུ Pub/Subܕσʔλ഑৴Ϟσϧ ඇྗͳσόΠε΍ωοτϫʔΫ͕ෆ҆ఆͳ৔ॴͰ΋ಈ࡞͠΍͢ ͍Α͏ʹϝοηʔδిจ͕ܰྔʹઃܭ͞Ε͍ͯΔ ࢀߟɿ΢ΟΩϖσΟΞʮMQ Telemetry

    Transportʯ ɹɹɿhttps://tech-blog.optim.co.jp/entry/2019/08/20/163000
  47. MQTT HTTP ΫϥΠΞϯτA ϦΫΤετ αʔό Ϩεϙϯε ΫϥΠΞϯτ͕αʔόʹϦΫΤετ αʔό͕ͦΕʹ͋ͬͨϨεϙϯεΛฦ٫͢Δ

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

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

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

    topicɿenvironment/ ʲSubscribeʳ topicɿenvironment/ ΫϥΠΞϯτC ʲSubscribeʳ topicɿenvironment/ ϒϩʔΧʔ͸͋Δఔ౓ɺPublish͞ΕͨϝοηʔδΛอ࣋Ͱ͖Δ ΫϥΠΞϯτD ʲSubscribeʳ topicɿenvironment/
  51. MQTT ࠓޙɺهࡌͷʮMQTTʯ͸ํࣜͷҙຯͰ͸ͳ͘ ϓϩτίϧΛද͠·͢ MQTTʹϙʔτ 1883ʢΠϝʔδͱͯ͠͸HTTP:80ʣ MQTTSʹϙʔτ 8883ʢΠϝʔδͱͯ͠͸HTTPS:443ʣ AWS IoT Core͕ϒϩʔΧʔͷ໾ׂΛͯ͘͠Ε·͢ʂ

    AWS IoT Core
  52. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi 㱺

    SORACOM 㱺 AWS IoT Core AWS IoT Core AWS IoT Core MQTTS MQTT
  53. Ϋϥ΢υ΁ૹ৴ 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
  54. Ϋϥ΢υ΁ૹ৴ 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 ɾɾɾʢ࣍ͷϖʔδ΁ʣ ᶃ ূ໌ॻ౳ͷಡΈࠐΈ ᶄΫϥΠΞϯτͷॳظԽ ᶅ઀ଓͷઃఆ౳
  55. Ϋϥ΢υ΁ૹ৴ 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ʂʂʂ
  56. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi㱺 SORACOM

    㱺 AWS IoT Core AWS IoT Core AWS IoT Core MQTTS MQTT આ໌ࡁΈ
  57. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 SORACOM 㱺 AWS IoT Core σόΠεଆͰઆ໌

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

    Air SIM ࠓճ͸ηϧϥʔΛ࢖༻ USBυϯάϧ ͜ͷதʹSIMΛ͍ΕͯRaspberry Piʹૠ͢ʂ ※υϯάϧͷઃఆʹؔͯ͠͸Githubࢀর MQTT
  59. Ϋϥ΢υ΁ૹ৴ Raspberry Pi 㱺 SORACOM σόΠεূ໌ॻ SDK ௚઀ɺAWS΁ૹ৴͢Δͱ͖ͱͷҧ͍ σόΠεূ໌ॻΛஔ͘ඞཁ͕ͳ͍ SDKෆཁ

    Ϋϥ΢υʹґଘ͠ͳ͍ίʔυ͕ॻ͚Δ MQTT
  60. Ϋϥ΢υ΁ૹ৴ 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ʂʂʂ
  61. Ϋϥ΢υ΁ૹ৴׬ྃʂ Raspberry Pi 㱺 AWS IoT Core Raspberry Pi 㱺

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

    Իηϯα ϕʔεϢχοτɿGrove Pi ωοτϫʔΫ ໌Δ͞ ਫ෼ηϯα Χϝϥ AWS IoT Core આ໌ࡁΈ
  63. σόΠε·ͱΊ ϋʔυۤखɾϓϩτλΠϐϯά Grove System, Sense HAT Wifi؀ڥ͕ͳ͍ SORACOM࢖ͬͯΈ͍ͯͩ͘͞ ※SORACOMͷγεςϜ͸AWS಺Ͱ͕͢ศ্ٓɺ෼͚ͯॻ͍ͯ͋Γ·͢ Sense

    HAT
  64. Ϋϥ΢υ

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

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

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

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

    ొ࿥ͯ͠ઃఆ࣌ʹࢦఆ͢Δ AWS IoT Core Funnel σʔλΛಛఆͷΫϥ΢υαʔϏε΁௚઀సૹ͢ΔαʔϏε MQTT MQTTS
  69. SORACOMͷαʔϏεͰసૹ SORACOM 㱺 AWS IoT Core SORACOMͷίϯιʔϧ͔Β సૹઌ͸ม͑ΒΕΔʂ

  70. SORACOMͷαʔϏεͰసૹ SORACOM 㱺 AWS IoT Core SORACOMͷίϯιʔϧ͔Β సૹઌ͸ม͑ΒΕΔʂ

  71. SORACOM㱺AWS IoT Core΁ૹ৴׬ྃʂ AWS IoT Core AWS Lambda Amazon DynamoDB

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

    Amazon DynamoDB AWS Lambda Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ
  73. 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
  74. 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
  75. 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 આ໌ࡁΈ આ໌ࡁΈ
  76. σʔλΛΫϥ΢υ·ͰอଘͰ͖·ͨ͠ ٳܜɾɾɾ

  77. ௥Ճ৘ใɿSigfoxʢγάϑΥοΫεʣ ෹ࠃSigfox͕ࣾఏڙ͢ΔLPWAωοτϫʔΫ LPWAʹLow Power Wide Area ௿Ձ֨ɾ௿ফඅిྗɾ௕ڑ཭఻ૹ͕ಛ௃ ηϯαʔ౳ ੍ݶ 1ճͷ࠷େૹ৴ྔ͸12όΠτ

    1೔Ͱૹ৴Ͱ͖Δճ਺ʢ্Γ140ճ/೔˞ɺԼΓ4ճ/೔ʣ ※໿10෼ʹ1ճϖʔε
  78. ௥Ճ৘ใɿSigfoxʢγάϑΥοΫεʣ Sens’it https://soracom.jp/products/sigfox/sensit/ Sigfoxʹ઀ଓՄೳͳηϯα಺ଂσόΠε Թ࣪౓ɺর౓ηϯαʔɺৼಈݕ஌ͳͲ಺ଂ όοςϦʔ಺ଂ ௨৴ྉ͕ʮ1,440ԁʗ೥ʯ ߪೖͯ͠ɺಈ࡞ϞʔυΛબ୒͢Ε͹উखʹσʔλૹ৴͕࢝·Δ ϦΞϧλΠϜੑ͕ͳ͍ɺిݯ͕ͳ͍৔ॴͰܭଌ͍ͨ͠ ϋ΢ε಺Ͱ࢖༻தɿόοςϦʔ͸͍͍ͩͨ2िؒ͘Β͍΋͍ͬͯΔ

  79. ޙ൒ Ϋϥ΢υͷଓ͖ Web ίετ

  80. ޙ൒ Ϋϥ΢υ ߏ੒؅ཧπʔϧ ઃఆ৘ใͷอ࣋ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ ௨஌

    Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ
  81. ޙ൒ Ϋϥ΢υ ߏ੒؅ཧπʔϧ ઃఆ৘ใͷอ࣋ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ ௨஌

    Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ
  82. ͍Ζ͍ΖͳαʔϏεΛ࢖͍·͢ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    AWS Identity and Access Management Permisions Role Amazon API Gateway Amazon Simple Storage Service
  83. 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ͷΈ࢖༻Ͱ͋Ε͹ɺͪ͜Β΋ݕ౼ͯ͠Έ͍ͯͩ͘͞
  84. ޙ൒ Ϋϥ΢υ ߏ੒؅ཧπʔϧ ઃఆ৘ใͷอ࣋ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ ௨஌

    Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ
  85. ઃఆ৘ใͷอ࣋ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ આ໌ࡁΈ
  86. ઃఆ৘ใͷอ࣋ 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 } } }
  87. ઃఆ৘ใͷอ࣋ Device ShadowαʔϏε ηΫγϣϯ desiredɿΫϥ΢υ͔Βͷཁٻ reportedɿσόΠεͷঢ়ଶ deltaɿΫϥ΢υͱσόΠεͷࠩ෼ Device ShadowαʔϏε {

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

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

  90. AWS IoT Core Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ Device Shadowͷऔಘ Ϋϥ΢υ σόΠε GETཁٻ Device

    ShadowαʔϏε { # Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ "delta": { "sensor": { "interval": 10 } } }
  91. 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ͷऔಘ
  92. 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 㱺Թ౓Λૹ৴ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ
  93. 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 } }, } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ
  94. 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 ߋ৽
  95. AWS IoT Core Ϋϥ΢υ σόΠε GETཁٻ Device ShadowαʔϏε { #

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

    Ϋϥ΢υ͔Βͷཁٻ "desired": { "sensor": { "interval": 10 } }, # σόΠεͷঢ়ଶ "reported": { "sensor": { "interval": 5 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ # ͕ͳ͘ͳΔʂ } Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ Device Shadow औಘ Subscribe GETཁٻʢPublishʣ MQTT Ͱ΍ΓͱΓ͍ͯ͠Δ
  97. ઃఆ৘ใͷอ࣋ 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ͱ΍ΓͱΓ͠·͢
  98. ઃఆ৘ใͷอ࣋ 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ͱ΍ΓͱΓ͠·͢
  99. ઃఆ৘ใͷอ࣋ 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ͱ΍ΓͱΓ͠·͢ αϒεΫϥΠϒͷ λεΫऴྃ଴ͪ
  100. ઃఆ৘ใͷอ࣋·ͱΊ σόΠεଆʹઃఆ஋Λอ࣋͠ͳ͍ ·ͨ͸ɺΫϥ΢υ͔Βड͚औͬͨ΋ͷΛอ࣋͢Δ͚ͩ Ϋϥ΢υଆΛࢀর͢Ε͹σόΠεͷઃఆ͕Θ͔Δ Webϖʔδ͔ΒΫϥ΢υܦ༝Ͱઃఆ஋ͷมߋ͕Ͱ͖Δ

  101. ޙ൒ Ϋϥ΢υ ߏ੒؅ཧπʔϧ ઃఆ৘ใͷอ࣋ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ ௨஌

    Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ
  102. ঢ়ଶͷมߋ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ આ໌ࡁΈ ৄࡉ͸
 Appendixࢀর
  103. ঢ়ଶͷมߋ ঢ়ଶͷมߋͷ൑ఆ΋Ϋϥ΢υଆͰߦ͍ࢦࣔ σόΠεଆ͸ͦͷࢦࣔͰಈ͚ͩ͘ σόΠεଆ͸ηϯγϯάσʔλΛૹΔ͚ͩ σόΠεଆͷίʔυ͕୯७ʹͳΔ

  104. ޙ൒ Ϋϥ΢υ ߏ੒؅ཧπʔϧ ઃఆ৘ใͷอ࣋ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ ௨஌

    Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ
  105. ؂ࢹ AWS IoT Core AWS Lambda Amazon DynamoDB Amazon Simple

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

    Storage Service TriggerͰ LambdaΛىಈ ࡱӨ
  107. AWS IoT Core ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ Ϋϥ΢υ σόΠε ը૾Λૹ৴ Amazon Simple

    Storage Service TriggerͰ LambdaΛىಈ ࡱӨ ͲͷΑ͏ʹ Tweet͍ͯ͠Δ͔
  108. ؂ࢹ ࣸਅΛࡱͬͯ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
  109. ؂ࢹ·ͱΊ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ Twitter΁ͷૹ৴͸ɺʮγʔΫϨοτΩʔʯʮτʔΫϯʯͳͲ͕ඞཁ Ϋϥ΢υଆͰTweet͢ΔͷͰΫϥ΢υଆͰΩʔ͕؅ཧͰ͖Δ ஫ҙ AWS IoT CoreͰ΍ΓͱΓͰ͖Δσʔλྔ͸128KB·Ͱ https://docs.aws.amazon.com/ja_jp/general/latest/gr/aws_service_limits.html#iot-protocol-limits ࡱӨ࣌ʹը૾ղ૾౓Λམͱ͢

    Ұ࣌తͳτʔΫϯΛAWS IoTܦ༝Ͱऔಘ͠ɺ
 σόΠε͔Β௚઀S3΁ΞοϓϩʔυͳͲ ϥʔδσʔλΞοϓϩʔυύλʔϯ https://www.slideshare.net/AmazonWebServicesJapan/aws-black-belt-online-seminar-2017-iot#37
  110. ޙ൒ Ϋϥ΢υ ߏ੒؅ཧπʔϧ ઃఆ৘ใͷอ࣋ Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ ؂ࢹ ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ ௨஌

    Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ
  111. ௨஌ AWS IoT Core AWS Lambda Amazon DynamoDB Amazon Simple

    Storage Service AWS Lambda Amazon API Gateway આ໌ࡁΈ આ໌ࡁΈ આ໌ࡁΈ આ໌ࡁΈ LINE Notifyͷઆ໌͸
 Appendixࢀর
  112. AWS IoT Core ௨஌ Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ Ϋϥ΢υ σόΠε Թ౓Λૹ৴ LambdaΛ࣮ߦ ᮢ஋ΛνΣοΫͯ͠

    ֤αʔϏε΁௨஌
  113. AWS IoT Core ௨஌ Թ౓͕ᮢ஋Ҏ্ʹͳͬͨΒ௨஌͢Δ Ϋϥ΢υ σόΠε Թ౓Λૹ৴ LambdaΛ࣮ߦ ᮢ஋ΛνΣοΫͯ͠

    ֤αʔϏε΁௨஌ ͲͷΑ͏ʹ ૹ৴͍ͯ͠Δ͔
  114. 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΁௨஌ʂ
  115. 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ʹ௨஌ʂ
  116. ௨஌·ͱΊ ؆୯ʹଞαʔϏε΁௨஌Ͱ͖ΔϥΠϒϥϦɺαʔϏε͕͋Δʂ LINE NotifyΛ͔͖͔͚ͭͬͨͬ ೶Ոͷํ͔Βɺϋ΢εͷԹ౓͕ҰఆҎ্orҎԼʹͳͬͨΒ௨஌ͯ͠΄͍͠ slack஌Βͳ͍ LINE͸࢖༻͍ͯ͠Δͻͱ͕ଟ͍ʂ

  117. σʔλอଘɺ؂ࢹɺ௨஌͕Ͱ͖·ͨ͠ʂ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ
  118. Ϋϥ΢υ·ͱΊ ߏ੒؅ཧπʔϧΛ࢖༻͢Δ͜ͱʹΑΓ Ϋϥ΢υͷߏ੒ΛϑΝΠϧͰ؅ཧʹόʔδϣϯ؅ཧ΋Ͱ͖Δ ίϚϯυҰൃͰߏங

  119. Ϋϥ΢υ·ͱΊ ઃఆ৘ใͷอ࣋ɺঢ়ଶͷมߋɺ؂ࢹɺ௨஌ ͳΔ΂͘Ϋϥ΢υଆͰߦ͏ σόΠε͸ͦͷࢦࣔʹै͏͚ͩʗσʔλΛૹΔ͚ͩ Ϋϥ΢υ͔Β࿈ܞͰ͖ΔαʔϏεɺΨδΣοτ͕૿͑ͨʂʂʂ Ϋϥ΢υଆͰ৘ใΛอ͓࣋ͯ͘͜͠ͱʹΑΓ࿈ܞָ͕ twilioɺkintoneɺεϚʔτϗʔϜΩοτɺεϚʔτϐʔΧʔͳͲ Ϋϥ΢υଆͰʮ΍Δʯ͜ͱΛҙࣝ͢Δͱ͍Ζ͍Ζͱ޾ͤʹͳΔʂ

  120. Web

  121. ޙ൒ Web Amazon API GatewayͰσʔλΛެ։ ΞΫηε੍ݶ WebϖʔδͰάϥϑɺදΛදࣔ

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

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0
  123. ޙ൒ Web Amazon API GatewayͰσʔλΛެ։ ΞΫηε੍ݶ WebϖʔδͰάϥϑɺදΛදࣔ

  124. APIͰηϯαͷσʔλΛެ։ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 ΦʔιϥΠβʔɾ Auth0ͷৄࡉ͸
 Appendixࢀর
  125. ΞΫηε੍ݶ ࠓճ͸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Λ࢖༻
  126. ࣗલͰϓϥοτϑΥʔϜΛߏங͢Δඞཁ͕ͳ͍ ΞΫηε੍ݶ Auth0 https://auth0.com/jp/ WebɺϞόΠϧɺ͓ΑͼϨΨγʔΞϓϦέʔγϣϯ޲͚ͷ
 ϢχόʔαϧೝূɾೝՄϓϥοτϑΥʔϜΛఏڙ

  127. ΞΫηε੍ݶ Auth0Λར༻ Amazon API Gateway Auth0 ϦΫΤετલʹ τʔΫϯऔಘ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ

    ϦΫΤετͷ τʔΫϯΛೝূ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ
  128. APIͰηϯαͷσʔλΛެ։·Ͱ׬ྃʂ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 આ໌ࡁΈ
  129. ޙ൒ Web Amazon API GatewayͰσʔλΛެ։ ΞΫηε੍ݶ WebϖʔδͰάϥϑɺදΛදࣔ

  130. Webϖʔδ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 આ໌ࡁΈ
  131. Django https://docs.djangoproject.com/ja/2.2/ ͓ͳ͡ΈͷPythonͷWebϑϨʔϜϫʔΫ ࠓճ͸ϩάΠϯػೳͱWebϖʔδͷදࣔʹ࢖༻ django rest framework΋࢖༻ Ϋϥ΢υ͔Βͷσʔλऔಘ WebϑϨʔϜϫʔΫ

  132. Char.js https://www.chartjs.org/ JavascriptͷϥΠϒϥϦ ؆୯ʹάϥϑ͕͔͚Δʂ άϥϑͷछྨ΋͍Ζ͍ΖͱରԠ͍ͯ͠Δʂ άϥϑදࣔ

  133. 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: { ɾɾɾ } });
  134. Char.js άϥϑදࣔ

  135. Char.js άϥϑදࣔ ΫϦοΫ͢Δͱɺ දࣔ͢Δର৅Λ͔͑ΒΕΔʂ

  136. DataTables https://datatables.net/ jQueryͷϓϥάΠϯ ؆୯ʹද͕͔͚Δʂ ϖʔδωʔγϣϯɺݕࡧʹ΋ରԠʂ ςʔϒϧදࣔ

  137. ɾɾɾ 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
  138. ςʔϒϧදࣔ DataTables 1ϖʔδͷදࣔߦ σʔλͷݕࡧػೳ ϖʔδωʔγϣϯ

  139. HEROKU https://jp.heroku.com/ Paas PythonϥΠϒϥϦͷdjango-herokuΛ࢖ͬͯ؆୯ʹઃఆͰ͖Δʂ $ git push heroku master Ͱ؆୯σϓϩΠ

    αΠτ͸σϑΥϧτͰHTTPS αʔό
  140. WebϖʔδͰݟ͑ΔԽ·Ͱ׬ྃʂ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    IoT action IoT rule Amazon API Gateway Amazon Simple Storage Service Ϋϥ΢υଆͰઆ໌ࡁΈ Auth0 આ໌ࡁΈ આ໌ࡁΈ
  141. Web·ͱΊ ηΩϡϦςΟɺೝূ Djangoɺdjango rest frameworkɺAuth0ʹ͓೚ͤ ؆୯ʹϏδϡΞϧΛΧοίΑ͘Ͱ͖Δʂ Chart.jsɺDataTablesͰΠέͯΔϖʔδ࡞੒ αʔόߏஙɺ؅ཧ͔Βͷղ์ HEROKUΛ࢖ͬͯ؆୯ެ։ʂ

  142. ίετ

  143. ίετ ॳظίετ ϥϯχϯάίετ ※2019/09/02ݱࡏͰ͢

  144. ॳظίετ ඼໊ ݸ਺ ߹ܭ ࢀߟݩ౳ 3BTQCFSSZ1J .PEFM#   ԁ

    ੫ࠐ ,4: (SPWF1J   ԁ ੫ࠐ "NB[PO Թ࣪౓ܭQSP   ԁ ੫ࠐ "NB[PO (64#υϯάϧ ",   ԁ ੫ൈ 403"$0. 403"$0.4*.  ԁ ੫ൈ ˞QMBO%ɺσʔλ௨৴ͷΈ ߹ܭ — ໿ ԁ ॳظඅ༻ʢૹྉผʣ ※ηϯα͸͢΂ͯܝࡌ͍ͯ͠·ͤΜ
  145. ϥϯχϯάίετ ࢖༻ස౓౳ σʔλͷݟ͑ΔԽʗ௨஌ ؂ࢹ ྉۚ SORACOM AWS Auth0 HEROKU

  146. ϥϯχϯάίετ ࢖༻ස౓౳ σʔλͷݟ͑ΔԽʗ௨஌ ؂ࢹ ྉۚ SORACOM AWS Auth0 HEROKU

  147. ϥϯχϯάίετ ༻్ ೔͝ͱ ݄͝ͱ උߟ ηϯγϯάσʔλͷૹ৴ ճʢ෼͝ͱʣ࣌✕✕୆ ܭɿճ ճ೔✕೔ ܭɿճ

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

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

  150. ϥϯχϯάίετ αʔϏε ྉۚʢ੫ผʣ උߟ "JS4*. جຊྉۚ ຕɿԁ೔ ˞ ௨৴ྉ ԁ.#

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

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

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

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

    ྉۚ උߟ )PCCZ'SFF ແྉ  ߦ·Ͱ )PCCZ#BTJD ࠷େ݄  ສߦ·Ͱ ඵ୯Ґͷ՝ۚ 4UBOEBSE ࠷େ ݄ ඵ୯Ґͷ՝ۚ 1SFNJVN ࠷େ ݄ ඵ୯Ґͷ՝ۚ ʲσʔλͷݟ͑ΔԽʳ
  156. ίετ·ͱΊ ॳظඅ༻͕ͪΐͬͱߴΊɾɾɾ ϥϯχϯάίετ͸΄΅ɺSIMྉ͘Β͍ʂ খن໛Ͱ͋Ε͹ɺݸਓͰ΋ӡ༻Ͱ͖ΔϨϕϧ

  157. ·ͱΊ Python͸σόΠεʗΫϥ΢υʗWebͰ࢖͑Δ ࠓޙɺ౷ܭ΍ػցֶशͳͲͰ΋Python࢖͑Δʂ ·ͣ͸ϦϦʔεͯ͠࢖ͬͯ΋Β͏͜ͱ͕େࣄ ݟ͑ΔԽ͢Δ͚ͩͰ΋͘͢͝ت͹ΕΔ ೶Ոͷํɺ੡଄ۀͷํ ঢ়گ͚ͩΘ͔Ε͹͍͍ʗ஌Βͤͯ͘ΕΕ͹Α͍ ౎౓౎౓ɺݱ஍ʹ͍ͬͯ֬ೝ͍ͯͨ͜͠ͱ͔Βղ์͞ΕΔʂ Ϟϊ͕Πϯλʔωοτʹܨ͕͍ͬͯΔ͜ͱ͕౰ͨΓલ ݸਓͰ΋͓खܰʹγεςϜ͕ߏஙͰ͖ΔΑ͏ʹͳͬͨʂ

    ࠓޙ΋Α͍αʔϏ౳͸ͲΜͲΜͰͯ͘Δ औࣺબ୒͸͋Δ͚ͲɺΑ͍΋ͷ͸ͲΜͲΜऔΓೖΕΔ
  158. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ Enjoy IoT Life

  159. Appendix

  160. σόΠεؔ࿈Ͱࠓޙ΍Γ͍ͨ͜ͱ σόΠε΁ͷσϓϩΠ balenaCloud https://www.balena.io/ ίϯςφΠϝʔδΛσόΠεʹ഑෍ͨ͠ΓͰ͖Δʂ

  161. ௨஌ LINE Notify https://notify-bot.line.me/ja/ WebαʔϏε͔Βͷ௨஌ΛLINEͰड৴Ͱ͖Δ άϧʔϓͰ΋௨஌Λड৴͢Δ͜ͱ͕Մೳ ΞΫηετʔΫϯΛऔಘͯ͠
 ࢦఆͷURL΁POST͢Δ͚ͩʂ ར༻ྉۚ͸ແྉ 1࣌ؒʹ௨஌1000ճ·ͰՄ

  162. Appendixɿঢ়ଶͷมߋ

  163. ঢ়ଶͷมߋ AWS IoT Core AWS Lambda Amazon DynamoDB AWS Lambda

    Amazon API Gateway Amazon Simple Storage Service આ໌ࡁΈ આ໌ࡁΈ
  164. σόΠεଆ 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 } }, # Ϋϥ΢υͱσόΠεͷࠩ෼ } ঢ়ଶͷมߋ
  165. 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ʯ
  166. 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ʯ
  167. 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ʯ
  168. 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ʯ
  169. 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 Λมߋ͍ͯ͠Δ͔
  170. ঢ়ଶͷมߋ ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ 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 )
  171. ঢ়ଶͷมߋ ͪͳΈʹ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())
  172. AppendixɿΞΫηε੍ݶ

  173. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/ apigateway-use-lambda-authorizer.html AWS Lambda Amazon

    API Gateway Amazon DynamoDB ΞΫηε੍ݶͳ͠ͷ৔߹
  174. ΞΫηε੍ݶ 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, }, ɾɾɾ ] ΞΫηε੍ݶͳ͠ͷ৔߹
  175. ΞΫηε੍ݶ 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Λ஌͍ͬͯΔͱ୭Ͱ΋ΞΫηεͰ͖ ͯ͠·͏
  176. ΞΫηε੍ݶ API Gateway Lambda ΦʔιϥΠβʔͱ͸ AWS Lambda Amazon API Gateway

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

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

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

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

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

    Amazon DynamoDB ΞΫηε੍ݶ͋Γͷ৔߹ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ ޙଓͷLambda͸࣮ߦ͞Εͣ ݖݶͳ͕͠ฦ٫͞ΕΔ τʔΫϯͷൃߦ͸ʁ
  184. ࣗલͰϓϥοτϑΥʔϜΛߏங͢Δඞཁ͕ͳ͍ ΞΫηε੍ݶ Auth0 WebɺϞόΠϧɺ͓ΑͼϨΨγʔΞϓϦέʔγϣϯ޲͚ͷ
 ϢχόʔαϧೝূɾೝՄϓϥοτϑΥʔϜΛఏڙ

  185. ΞΫηε੍ݶ Auth0Λར༻ Amazon API Gateway ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ Auth0 ϦΫΤετલʹ

    τʔΫϯऔಘ
  186. ΞΫηε੍ݶ Auth0Λར༻ Amazon API Gateway ೝূɺೝՄΛߦ͏Lambda Lambda ΦʔιϥΠβʔ Auth0 ϦΫΤετલʹ

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

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

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

    τʔΫϯऔಘ ϦΫΤετ࣌ͷϔομʹ τʔΫϯΛ΋ͨͤΔʂ ϦΫΤετͷ τʔΫϯΛೝূ
  191. 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Λར༻