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

    View full-size slide

  2. ͓લ୭Α
    ஌໺ ༤ೋ
    facebook : chinoppy
    twitter : @chinoppy0727
    (ג)೔ຊγεςϜٕݚʢJSLʣ ॴଐ
    ௕໺ݝ௕໺ࢢʹ͋Γ·͢
    ࠓ೥ͷPyCon JP 2019 ͰʮGold Sponsorʯ
    WebΤϯδχΞʢPython࢖ͬͯ·͢ʂʣ
    ۀ຿ͱϓϥΠϕʔτͰIoTͯ͠·͢
    GEEKLAB.NAGANOͷ؅ཧਓ
    ౰͕ࣾӡӦ͢ΔίϛϡχςΟεϖʔε
    ௨শɿ͗ʔΒ΅
    ฏ೔9:00ʙ18:00͜Ζ·Ͱ͸ແྉ։์͍ͯ͠·͢
    ࡞ۀ৔ͱͯ͠΋ར༻Ͱ͖·͢ͷͰɺ͓ؾܰʹ͓ӽ͍ͩ͘͠͞

    View full-size slide

  3. ͓લ୭Α
    ͗ʔΒ΅ͪΌΜ
    ͗ʔΒ΅ʹՃΘͬͨ৽͍͠஥ؒ
    ه೦඼ʹεςοΧʔ͸͍ͬͯ·͢
    ࡞ɿࣾ಺ͷPython࢖͍ͷֆࢣ͞Μ

    View full-size slide

  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
    ※ࠓޙ΋ਵ࣌ɺߋ৽༧ఆͰ͢

    View full-size slide

  5. ͓ئ͍
    ·ͩ·ͩվળ఺΍ศརͳαʔϏεɺπʔϧ͸͍Ζ͍Ζ͋Γ·͢
    ΑΓΑ͍πʔϧɺαʔϏεɺϥΠϒϥϦ͋ΔΑʂ
    ͦ͜ɺ͜͏ͨ͠ํ͕Α͍ͷͰ͸ʁ
    ͦ͜͸஌Βͳ͔ͬͨʂ
    ͳͲڭ͑ͯ΋Β͑Δͱ͏Ε͍͠Ͱ͢

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    㱺͔ͦ͜Β·ͨ৽͍͜͠ͱ͕ݟ͑ͨΓ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. ߏ੒ɿσόΠε
    Raspberry Pi 3 Model B
    Grove System
    Թ࣪౓ܭ
    Իηϯα
    ϕʔεϢχοτɿGrove Pi
    ωοτϫʔΫ
    ໌Δ͞
    ਫ෼ηϯα
    Χϝϥ
    AWS IoT Core

    View full-size slide

  23. ߏ੒ɿΫϥ΢υ
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service

    View full-size slide

  24. ߏ੒ɿWeb
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda
    IoT
    action
    IoT
    rule
    Amazon API Gateway
    Amazon Simple Storage
    Service
    Ϋϥ΢υଆͰઆ໌ࡁΈ
    Auth0

    View full-size slide

  25. લ൒
    σόΠε
    ηϯγϯάσʔλͷऔಘ
    ωοτϫʔΫ
    Ϋϥ΢υ΁ૹ৴
    MQTT
    Raspberry Pi㱺AWS IoT Core
    Raspberry Pi㱺SORACOM㱺AWS IoT Core
    Ϋϥ΢υ
    SORACOM㱺AWS IoT Core
    AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ

    View full-size slide

  26. ߏ੒
    Raspberry Pi 3 Model B
    Grove System
    Թ࣪౓ܭ
    Իηϯα
    ϕʔεϢχοτɿGrove Pi
    ωοτϫʔΫ
    ໌Δ͞
    ਫ෼ηϯα
    Χϝϥ
    AWS IoT Core

    View full-size slide

  27. લ൒
    σόΠε
    ηϯγϯάσʔλͷऔಘ
    ωοτϫʔΫ
    Ϋϥ΢υ΁ૹ৴
    MQTT
    Raspberry Pi㱺AWS IoT Core
    Raspberry Pi㱺SORACOM㱺AWS IoT Core
    Ϋϥ΢υ
    SORACOM㱺AWS IoT Core
    AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ

    View full-size slide

  28. ηϯαɺRaspberry Pi
    Raspberry Pi 3 Model B
    Grove System
    Թ࣪౓ܭ
    Իηϯα
    ϕʔεϢχοτɿGrove Pi
    ωοτϫʔΫ
    ໌Δ͞
    ਫ෼ηϯα
    Χϝϥ
    AWS IoT Core

    View full-size slide

  29. ηϯαʔ
    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ϥΠϒϥϦ΋͋Δ

    View full-size slide

  30. ηϯαʔ
    ϕʔεϢχοτɿGrove Pi
    ඪ४Խ͞ΕͨίωΫλͱηϯα

    View full-size slide

  31. ηϯαʔ
    ૠ͚ͩ͢ͰηοτΞοϓ׬ྃʂ
    Interface
    Digital
    Grove Analog
    Grove UART
    Grove I2C
    Sensor
    Թ࣪౓
    ໌Δ͞
    ਫ෼
    Ի
    LED
    Ϙλϯ
    LCD
    100छྨҎ্͋Δ

    View full-size slide

  32. ϚΠίϯʗίϯϐϡʔλ
    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

    View full-size slide

  33. σδλϧɿԹ࣪౓ܭ
    I2Cɿ໌Δ͞
    ΞφϩάɿԻͷେ͖͞
    ΧϝϥɿࡱӨ
    ϥΠϒϥϦΛ࢖ͬͯσʔλΛऔಘ

    View full-size slide

  34. σδλϧ
    >>> 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ԁ

    View full-size slide

  35. 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

    View full-size slide

  36. Ξφϩά
    >>> import grovepi
    >>>
    >>> loudness_sensor = 0
    >>> grovepi.analogRead(loudness_sensor)
    124
    >>>
    Իηϯα http://wiki.seeedstudio.com/Grove-Loudness_Sensor/
    Loudness Sensor

    View full-size slide

  37. Χϝϥ
    >>> from picamera import PiCamera
    >>>
    >>> camera = PiCamera()
    >>>
    >>> camera.start_preview()

    >>>
    >>> # ࡱӨ
    >>> camera.capture('./sample.jpg')
    >>>
    >>> camera.stop_preview()
    >>> camera.close()
    ࡱӨ
    3BTQCFSSZ1J$BNFSB.PEVMF7ΧϝϥϞδϡʔϧ

    View full-size slide

  38. ηϯα౳ͷσʔλऔಘ׬ྃʂ
    Raspberry Pi 3 Model B
    Grove System
    Թ࣪౓ܭ
    Իηϯα
    ϕʔεϢχοτɿGrove Pi
    ωοτϫʔΫ
    ໌Δ͞
    ਫ෼ηϯα
    Χϝϥ
    AWS IoT Core
    આ໌ࡁΈ

    View full-size slide

  39. લ൒
    σόΠε
    ηϯγϯάσʔλͷऔಘ
    ωοτϫʔΫ
    Ϋϥ΢υ΁ૹ৴
    MQTT
    Raspberry Pi㱺AWS IoT Core
    Raspberry Pi㱺SORACOM㱺AWS IoT Core
    Ϋϥ΢υ
    SORACOM㱺AWS IoT Core
    AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ

    View full-size slide

  40. ωοτϫʔΫɺΫϥ΢υ΁ૹ৴
    Raspberry Pi 3 Model B
    Grove System
    Թ࣪౓ܭ
    Իηϯα
    ϕʔεϢχοτɿGrove Pi
    ωοτϫʔΫ
    ໌Δ͞
    ਫ෼ηϯα
    Χϝϥ
    AWS IoT Core
    આ໌ࡁΈ

    View full-size slide

  41. SORACOM
    IoT ϓϥοτϑΥʔϜ
    MVNO ͱͯ͠σʔλ௨৴ SIM Λఏڙ
    ϞόΠϧ௨৴ͱΫϥ΢υΛҰ؏ͯ͠ఏڙ
    ࢀߟɿ https://soracom.jp/overview/
    ωοτϫʔΫ
    ωοτϫʔΫɺηΩϡϦςΟΛ͓·͔ͤʂ

    View full-size slide

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

    View full-size slide

  43. MQTT
    Message Queuing Telemetry Transportͷུ
    Pub/Subܕσʔλ഑৴Ϟσϧ
    ඇྗͳσόΠε΍ωοτϫʔΫ͕ෆ҆ఆͳ৔ॴͰ΋ಈ࡞͠΍͢
    ͍Α͏ʹϝοηʔδిจ͕ܰྔʹઃܭ͞Ε͍ͯΔ
    ࢀߟɿ΢ΟΩϖσΟΞʮMQ Telemetry Transportʯ
    ɹɹɿhttps://tech-blog.optim.co.jp/entry/2019/08/20/163000

    View full-size slide

  44. MQTT
    HTTP
    ΫϥΠΞϯτA
    ϦΫΤετ
    αʔό
    Ϩεϙϯε
    ΫϥΠΞϯτ͕αʔόʹϦΫΤετ
    αʔό͕ͦΕʹ͋ͬͨϨεϙϯεΛฦ٫͢Δ

    View full-size slide

  45. MQTT
    ϒϩʔΧʔ
    ʢதܧऀʣ
    MQTT
    ΫϥΠΞϯτA ΫϥΠΞϯτB
    ΞΫλʔ
    ύϒϦογϟʔʢൃߦऀʣ
    αϒεΫϥΠόʔʢߪಡऀʣ
    ϒϩʔΧʔʢதܧऀʣ
    ϝοηʔδͷ఻ૹ
    ϝοηʔδΛൃߦʢૹ৴ʣ͢ΔɿPublish
    ϝοηʔδͷߪಡʢड৴ʣΛొ࿥͢ΔɿSubscribe
    ϝοηʔδͷΩʔɿtopic

    View full-size slide

  46. MQTT
    ϒϩʔΧʔ
    ʢதܧऀʣ
    σʔλΛड͚औΓɺ
    ֘౰͢Δߪಡऀ΁
    σʔλΛ఻ૹ
    MQTT
    ΫϥΠΞϯτA
    ΫϥΠΞϯτB
    ΫϥΠΞϯτ͸ʮϒϩʔΧʔʯΛ௨ͯ͠ɺϝοηʔδͷ΍ΓͱΓΛ͢Δ
    ϒϩʔΧʔ͸ɺΫϥΠΞϯτ͔Βૹ৴͞ΕͨσʔλΛ

    ͦͷtopicΛαϒεΫϥΠϒ͍ͯ͠ΔΫϥΠΞϯτ΁ૹΔ
    ʲPublishʳ
    topicɿenvironment/
    ʲSubscribeʳ
    topicɿenvironment/
    ΫϥΠΞϯτC
    ʲSubscribeʳ
    topicɿenvironment/
    ΫϥΠΞϯτD

    View full-size slide

  47. MQTT
    ϒϩʔΧʔ
    ʢதܧऀʣ
    σʔλΛड͚औΓɺ
    ֘౰͢Δߪಡऀ΁
    σʔλΛ఻ૹ
    MQTT
    ΫϥΠΞϯτA
    ΫϥΠΞϯτB
    ʲPublishʳ
    topicɿenvironment/
    ʲSubscribeʳ
    topicɿenvironment/
    ΫϥΠΞϯτC
    ʲSubscribeʳ
    topicɿenvironment/
    ϒϩʔΧʔ͸͋Δఔ౓ɺPublish͞ΕͨϝοηʔδΛอ࣋Ͱ͖Δ
    ΫϥΠΞϯτD
    ʲSubscribeʳ
    topicɿenvironment/

    View full-size slide

  48. MQTT
    ࠓޙɺهࡌͷʮMQTTʯ͸ํࣜͷҙຯͰ͸ͳ͘
    ϓϩτίϧΛද͠·͢
    MQTTʹϙʔτ 1883ʢΠϝʔδͱͯ͠͸HTTP:80ʣ
    MQTTSʹϙʔτ 8883ʢΠϝʔδͱͯ͠͸HTTPS:443ʣ
    AWS IoT Core͕ϒϩʔΧʔͷ໾ׂΛͯ͘͠Ε·͢ʂ
    AWS IoT Core

    View full-size slide

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

    View full-size slide

  50. Ϋϥ΢υ΁ૹ৴
    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

    View full-size slide

  51. Ϋϥ΢υ΁ૹ৴
    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
    ɾɾɾʢ࣍ͷϖʔδ΁ʣ
    ᶃ ূ໌ॻ౳ͷಡΈࠐΈ
    ᶄΫϥΠΞϯτͷॳظԽ
    ᶅ઀ଓͷઃఆ౳

    View full-size slide

  52. Ϋϥ΢υ΁ૹ৴
    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ʂʂʂ

    View full-size slide

  53. Ϋϥ΢υ΁ૹ৴
    Raspberry Pi 㱺 AWS IoT Core
    Raspberry Pi㱺 SORACOM 㱺 AWS IoT Core
    AWS IoT Core
    AWS IoT Core
    MQTTS
    MQTT
    આ໌ࡁΈ

    View full-size slide

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

    View full-size slide

  55. Ϋϥ΢υ΁ૹ৴
    Raspberry Pi 㱺 SORACOM
    ४උ
    PythonͷMQTTϥΠϒϥϦͷΠϯετʔϧ
    Paho-mqttΛ࢖༻
    https://pypi.org/project/paho-mqtt/
    SORACOM Air SIM
    ࠓճ͸ηϧϥʔΛ࢖༻
    USBυϯάϧ
    ͜ͷதʹSIMΛ͍ΕͯRaspberry Piʹૠ͢ʂ
    ※υϯάϧͷઃఆʹؔͯ͠͸Githubࢀর
    MQTT

    View full-size slide

  56. Ϋϥ΢υ΁ૹ৴
    Raspberry Pi 㱺 SORACOM
    σόΠεূ໌ॻ SDK
    ௚઀ɺAWS΁ૹ৴͢Δͱ͖ͱͷҧ͍
    σόΠεূ໌ॻΛஔ͘ඞཁ͕ͳ͍
    SDKෆཁ
    Ϋϥ΢υʹґଘ͠ͳ͍ίʔυ͕ॻ͚Δ
    MQTT

    View full-size slide

  57. Ϋϥ΢υ΁ૹ৴
    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ʂʂʂ

    View full-size slide

  58. Ϋϥ΢υ΁ૹ৴׬ྃʂ
    Raspberry Pi 㱺 AWS IoT Core
    Raspberry Pi 㱺 SORACOM 㱺 AWS IoT Core
    AWS IoT Core
    AWS IoT Core
    MQTTS
    MQTT
    આ໌ࡁΈ

    View full-size slide

  59. ηϯα͔ΒͷσʔλऔಘɺωοτϫʔΫ

    Ϋϥ΢υ΁ͷૹ৴׬ྃʂ
    Raspberry Pi 3 Model B
    Grove System
    Թ࣪౓ܭ
    Իηϯα
    ϕʔεϢχοτɿGrove Pi
    ωοτϫʔΫ
    ໌Δ͞
    ਫ෼ηϯα
    Χϝϥ
    AWS IoT Core
    આ໌ࡁΈ

    View full-size slide

  60. σόΠε·ͱΊ
    ϋʔυۤखɾϓϩτλΠϐϯά
    Grove System, Sense HAT
    Wifi؀ڥ͕ͳ͍
    SORACOM࢖ͬͯΈ͍ͯͩ͘͞
    ※SORACOMͷγεςϜ͸AWS಺Ͱ͕͢ศ্ٓɺ෼͚ͯॻ͍ͯ͋Γ·͢
    Sense HAT

    View full-size slide

  61. ߏ੒
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service

    View full-size slide

  62. લ൒
    σόΠε
    ηϯγϯάσʔλͷऔಘ
    ωοτϫʔΫ
    Ϋϥ΢υ΁ૹ৴
    MQTT
    Raspberry Pi㱺AWS IoT Core
    Raspberry Pi㱺SORACOM㱺AWS IoT Core
    Ϋϥ΢υ
    SORACOM㱺AWS IoT Core
    AWS IoT Core͔ΒAmazon DynamoDBʹσʔλอଘ

    View full-size slide

  63. SORACOM 㱺 AWS IoT Core΁
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service

    View full-size slide


  64. SORACOMͷαʔϏεͰసૹ
    SORACOM 㱺 AWS IoT Core
    Beam
    ҉߸Խͯ͠
    σʔλΛసૹͯ͘͠ΕΔ
    ূ໌ॻ͸SORACOM΁
    ొ࿥ͯ͠ઃఆ࣌ʹࢦఆ͢Δ
    AWS IoT Core
    Funnel
    σʔλΛಛఆͷΫϥ΢υαʔϏε΁௚઀సૹ͢ΔαʔϏε
    MQTT MQTTS

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  67. SORACOM㱺AWS IoT Core΁ૹ৴׬ྃʂ
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service
    આ໌ࡁΈ

    View full-size slide

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

    View full-size slide

  69. 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

    View full-size slide

  70. 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

    View full-size slide

  71. 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
    આ໌ࡁΈ
    આ໌ࡁΈ

    View full-size slide

  72. σʔλΛΫϥ΢υ·ͰอଘͰ͖·ͨ͠
    ٳܜɾɾɾ

    View full-size slide

  73. ௥Ճ৘ใɿSigfoxʢγάϑΥοΫεʣ
    ෹ࠃSigfox͕ࣾఏڙ͢ΔLPWAωοτϫʔΫ
    LPWAʹLow Power Wide Area
    ௿Ձ֨ɾ௿ফඅిྗɾ௕ڑ཭఻ૹ͕ಛ௃
    ηϯαʔ౳
    ੍ݶ
    1ճͷ࠷େૹ৴ྔ͸12όΠτ
    1೔Ͱૹ৴Ͱ͖Δճ਺ʢ্Γ140ճ/೔˞ɺԼΓ4ճ/೔ʣ
    ※໿10෼ʹ1ճϖʔε

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  78. ͍Ζ͍ΖͳαʔϏεΛ࢖͍·͢
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda
    AWS Identity and Access
    Management
    Permisions
    Role
    Amazon API Gateway
    Amazon Simple Storage
    Service

    View full-size slide

  79. 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ͷΈ࢖༻Ͱ͋Ε͹ɺͪ͜Β΋ݕ౼ͯ͠Έ͍ͯͩ͘͞

    View full-size slide

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

    View full-size slide

  81. ઃఆ৘ใͷอ࣋
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service
    આ໌ࡁΈ
    આ໌ࡁΈ

    View full-size slide

  82. ઃఆ৘ใͷอ࣋
    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
    }
    }
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  85. Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ

    View full-size slide

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

    View full-size slide

  87. 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ͷऔಘ

    View full-size slide

  88. 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
    㱺Թ౓Λૹ৴
    Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ

    View full-size slide

  89. 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
    }
    },
    }
    Թ౓Λຖਖ਼࣌ɺΫϥ΢υ΁ૹΔ

    View full-size slide

  90. 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 ߋ৽

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  93. ઃఆ৘ใͷอ࣋
    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ͱ΍ΓͱΓ͠·͢

    View full-size slide

  94. ઃఆ৘ใͷอ࣋
    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ͱ΍ΓͱΓ͠·͢

    View full-size slide

  95. ઃఆ৘ใͷอ࣋
    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ͱ΍ΓͱΓ͠·͢
    αϒεΫϥΠϒͷ
    λεΫऴྃ଴ͪ

    View full-size slide

  96. ઃఆ৘ใͷอ࣋·ͱΊ
    σόΠεଆʹઃఆ஋Λอ࣋͠ͳ͍
    ·ͨ͸ɺΫϥ΢υ͔Βड͚औͬͨ΋ͷΛอ࣋͢Δ͚ͩ
    Ϋϥ΢υଆΛࢀর͢Ε͹σόΠεͷઃఆ͕Θ͔Δ
    Webϖʔδ͔ΒΫϥ΢υܦ༝Ͱઃఆ஋ͷมߋ͕Ͱ͖Δ

    View full-size slide

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

    View full-size slide

  98. ঢ়ଶͷมߋ
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service
    આ໌ࡁΈ
    આ໌ࡁΈ
    ৄࡉ͸

    Appendixࢀর

    View full-size slide

  99. ঢ়ଶͷมߋ
    ঢ়ଶͷมߋͷ൑ఆ΋Ϋϥ΢υଆͰߦ͍ࢦࣔ
    σόΠεଆ͸ͦͷࢦࣔͰಈ͚ͩ͘
    σόΠεଆ͸ηϯγϯάσʔλΛૹΔ͚ͩ
    σόΠεଆͷίʔυ͕୯७ʹͳΔ

    View full-size slide

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

    View full-size slide

  101. ؂ࢹ
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    Amazon Simple Storage
    Service
    AWS Lambda Amazon API Gateway
    આ໌ࡁΈ
    આ໌ࡁΈ

    View full-size slide

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

    View full-size slide

  103. AWS IoT Core
    ؂ࢹ
    ࣸਅΛࡱͬͯTwitter΁౤ߘ͢Δ
    Ϋϥ΢υ
    σόΠε
    ը૾Λૹ৴
    Amazon Simple Storage
    Service
    TriggerͰ
    LambdaΛىಈ
    ࡱӨ
    ͲͷΑ͏ʹ
    Tweet͍ͯ͠Δ͔

    View full-size slide

  104. ؂ࢹ
    ࣸਅΛࡱͬͯ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

    View full-size slide

  105. ؂ࢹ·ͱΊ
    ࣸਅΛࡱͬͯ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

    View full-size slide

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

    View full-size slide

  107. ௨஌
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    Amazon Simple Storage
    Service
    AWS Lambda Amazon API Gateway
    આ໌ࡁΈ
    આ໌ࡁΈ
    આ໌ࡁΈ
    આ໌ࡁΈ
    LINE Notifyͷઆ໌͸

    Appendixࢀর

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  110. 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΁௨஌ʂ

    View full-size slide

  111. 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ʹ௨஌ʂ

    View full-size slide

  112. ௨஌·ͱΊ
    ؆୯ʹଞαʔϏε΁௨஌Ͱ͖ΔϥΠϒϥϦɺαʔϏε͕͋Δʂ
    LINE NotifyΛ͔͖͔͚ͭͬͨͬ
    ೶Ոͷํ͔Βɺϋ΢εͷԹ౓͕ҰఆҎ্orҎԼʹͳͬͨΒ௨஌ͯ͠΄͍͠
    slack஌Βͳ͍
    LINE͸࢖༻͍ͯ͠Δͻͱ͕ଟ͍ʂ

    View full-size slide

  113. σʔλอଘɺ؂ࢹɺ௨஌͕Ͱ͖·ͨ͠ʂ
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service
    આ໌ࡁΈ

    View full-size slide

  114. Ϋϥ΢υ·ͱΊ
    ߏ੒؅ཧπʔϧΛ࢖༻͢Δ͜ͱʹΑΓ
    Ϋϥ΢υͷߏ੒ΛϑΝΠϧͰ؅ཧʹόʔδϣϯ؅ཧ΋Ͱ͖Δ
    ίϚϯυҰൃͰߏங

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  117. ߏ੒
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda
    IoT
    action
    IoT
    rule
    Amazon API Gateway
    Amazon Simple Storage
    Service
    Ϋϥ΢υଆͰઆ໌ࡁΈ
    Auth0

    View full-size slide

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

    View full-size slide

  119. APIͰηϯαͷσʔλΛެ։
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda
    IoT
    action
    IoT
    rule
    Amazon API Gateway
    Amazon Simple Storage
    Service
    Ϋϥ΢υଆͰઆ໌ࡁΈ
    Auth0
    ΦʔιϥΠβʔɾ
    Auth0ͷৄࡉ͸

    Appendixࢀর

    View full-size slide

  120. ΞΫηε੍ݶ
    ࠓճ͸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Λ࢖༻

    View full-size slide

  121. ࣗલͰϓϥοτϑΥʔϜΛߏங͢Δඞཁ͕ͳ͍
    ΞΫηε੍ݶ
    Auth0
    https://auth0.com/jp/
    WebɺϞόΠϧɺ͓ΑͼϨΨγʔΞϓϦέʔγϣϯ޲͚ͷ

    ϢχόʔαϧೝূɾೝՄϓϥοτϑΥʔϜΛఏڙ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  126. Django
    https://docs.djangoproject.com/ja/2.2/
    ͓ͳ͡ΈͷPythonͷWebϑϨʔϜϫʔΫ
    ࠓճ͸ϩάΠϯػೳͱWebϖʔδͷදࣔʹ࢖༻
    django rest framework΋࢖༻
    Ϋϥ΢υ͔Βͷσʔλऔಘ
    WebϑϨʔϜϫʔΫ

    View full-size slide

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

    View full-size slide

  128. 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: {
    ɾɾɾ
    }
    });

    View full-size slide

  129. Char.js
    άϥϑදࣔ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  132. ɾɾɾ
    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

    View full-size slide

  133. ςʔϒϧදࣔ
    DataTables
    1ϖʔδͷදࣔߦ
    σʔλͷݕࡧػೳ
    ϖʔδωʔγϣϯ

    View full-size slide

  134. HEROKU
    https://jp.heroku.com/
    Paas
    PythonϥΠϒϥϦͷdjango-herokuΛ࢖ͬͯ؆୯ʹઃఆͰ͖Δʂ
    $ git push heroku master Ͱ؆୯σϓϩΠ
    αΠτ͸σϑΥϧτͰHTTPS
    αʔό

    View full-size slide

  135. WebϖʔδͰݟ͑ΔԽ·Ͱ׬ྃʂ
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda
    IoT
    action
    IoT
    rule
    Amazon API Gateway
    Amazon Simple Storage
    Service
    Ϋϥ΢υଆͰઆ໌ࡁΈ
    Auth0
    આ໌ࡁΈ
    આ໌ࡁΈ

    View full-size slide

  136. Web·ͱΊ
    ηΩϡϦςΟɺೝূ
    Djangoɺdjango rest frameworkɺAuth0ʹ͓೚ͤ
    ؆୯ʹϏδϡΞϧΛΧοίΑ͘Ͱ͖Δʂ
    Chart.jsɺDataTablesͰΠέͯΔϖʔδ࡞੒
    αʔόߏஙɺ؅ཧ͔Βͷղ์
    HEROKUΛ࢖ͬͯ؆୯ެ։ʂ

    View full-size slide

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

    View full-size slide

  138. ॳظίετ
    ඼໊ ݸ਺ ߹ܭ ࢀߟݩ౳
    3BTQCFSSZ1J
    .PEFM#
    ԁ ੫ࠐ
    ,4:
    (SPWF1J ԁ ੫ࠐ
    "NB[PO
    Թ࣪౓ܭQSP ԁ ੫ࠐ
    "NB[PO
    (64#υϯάϧ
    ",
    ԁ ੫ൈ
    403"$0.
    403"$0.4*. ԁ ੫ൈ
    ˞QMBO%ɺσʔλ௨৴ͷΈ
    ߹ܭ — ໿ ԁ
    ॳظඅ༻ʢૹྉผʣ
    ※ηϯα͸͢΂ͯܝࡌ͍ͯ͠·ͤΜ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  145. ϥϯχϯάίετ
    αʔϏε ࢖༻ྉ ࢖༻ྔ ྉۚମܥ౳ උߟ
    "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݄࣮੷

    View full-size slide

  146. ϥϯχϯάίετ
    αʔϏε ྉۚ ࢖༻ྔ ྉۚମܥ౳ උߟ
    "84*P5$PSF ໿0QFSBUJPOT
    %FWJDF4IBEPXBOE%FWJDF3FHJTUSZ
    0QFSBUJPOTɿສ͝ͱ
    ˞
    ଞͷྉۚ͸ԁͷͨΊׂѪ
    -BNCEB ˞
    "NB[PO4JNQMF
    4UPSBHF4FSWJDF

    165ଞ·Ͱɿ
    (&5ଞ·Ͱɿ
    ࠷ॳͷ5#݄·Ͱɿ(#
    ˞
    ߹ܭ
    AWS
    ※౦ژϦʔδϣϯͷྉۚͰ͢
    ※ྉۚମܥ͸লུͯ͠Δͱ͜Ζ΋͋Γ·͢ͷͰɺৄࡉ͸AWSͷϖʔδΛࢀর
    ※LambdaɺS3ʹ͍ͭͯ͸ɺଞαʔϏεͰ΋࢖༻͍ͯ͠ΔͨΊਪఆ஋ʢσʔλͷݟ͑ΔԽʗ௨஌ΑΓ࢖༻ྉ͸গͳ͍ʣ
    ʲ؂ࢹʳ˞2019೥7݄࣮੷

    View full-size slide

  147. ϥϯχϯάίετ
    ϓϥϯ ྉۚ උߟ
    '3&&ϥΠηϯε ແྉ Ұ෦ɺػೳ੍ݶ͋Γ
    %&7&-01&3 ݄ʙ &YUFSOBM"DUJWF6TFSTʹΑΓมಈ
    %&7&-01&3130 ˈ݄ʙ &YUFSOBM"DUJWF6TFSTଞʹΑΓมಈ
    Auth0
    ※DEVELOPER PROͷ্ʹʮENTERPRISEʯϥΠηϯε͕͋Γ·ׂ͕͢Ѫ
    FREEϥΠηϯεͳͷͰɺ0ԁ
    ʲσʔλͷݟ͑ΔԽʳ

    View full-size slide

  148. ϥϯχϯάίετ
    HEROKU
    FreeͳͷͰ0ԁ
    ࢖༻ऀ͸਺ेਓͰͦΕ΄ͲසൟʹΞΫηε͞ΕΔ༁Ͱ͸ͳ͍ͷͰFreeͰे෼
    ʲdyno https://jp.heroku.com/pricing#dynosʳ
    ϓϥϯ ྉۚ උߟ
    'SFF ແྉ Ұ෦ɺػೳ੍ݶ͋Γ
    )PCCZ ࠷େ݄EZOP ඵ୯Ґͷ՝ۚ
    4UBOEBSE
    ࠷େ݄EZOP ඵ୯Ґͷ՝ۚ
    1FSGPSNBODF
    ʲσʔλͷݟ͑ΔԽʳ

    View full-size slide

  149. ϥϯχϯάίετ
    HEROKU
    Hobby-FreeͳͷͰ0ԁ
    ηϯγϯάσʔλ͸AWSʹ͋ΔͷͰHobby - FreeͰे෼
    ʲHeroku Postgres https://jp.heroku.com/pricing#databasesʳ
    ϓϥϯ ྉۚ උߟ
    )PCCZ'SFF ແྉ ߦ·Ͱ
    )PCCZ#BTJD ࠷େ݄
    ສߦ·Ͱ
    ඵ୯Ґͷ՝ۚ
    4UBOEBSE ࠷େ ݄ ඵ୯Ґͷ՝ۚ
    1SFNJVN ࠷େ ݄ ඵ୯Ґͷ՝ۚ
    ʲσʔλͷݟ͑ΔԽʳ

    View full-size slide

  150. ίετ·ͱΊ
    ॳظඅ༻͕ͪΐͬͱߴΊɾɾɾ
    ϥϯχϯάίετ͸΄΅ɺSIMྉ͘Β͍ʂ
    খن໛Ͱ͋Ε͹ɺݸਓͰ΋ӡ༻Ͱ͖ΔϨϕϧ

    View full-size slide

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

    View full-size slide

  152. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ
    Enjoy IoT Life

    View full-size slide

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

    View full-size slide

  154. ௨஌
    LINE Notify
    https://notify-bot.line.me/ja/
    WebαʔϏε͔Βͷ௨஌ΛLINEͰड৴Ͱ͖Δ
    άϧʔϓͰ΋௨஌Λड৴͢Δ͜ͱ͕Մೳ
    ΞΫηετʔΫϯΛऔಘͯ͠

    ࢦఆͷURL΁POST͢Δ͚ͩʂ
    ར༻ྉۚ͸ແྉ
    1࣌ؒʹ௨஌1000ճ·ͰՄ

    View full-size slide

  155. Appendixɿঢ়ଶͷมߋ

    View full-size slide

  156. ঢ়ଶͷมߋ
    AWS IoT Core AWS Lambda
    Amazon DynamoDB
    AWS Lambda Amazon API Gateway
    Amazon Simple Storage
    Service
    આ໌ࡁΈ
    આ໌ࡁΈ

    View full-size slide

  157. σόΠεଆ
    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
    }
    },
    # Ϋϥ΢υͱσόΠεͷࠩ෼
    }
    ঢ়ଶͷมߋ

    View full-size slide

  158. 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ʯ

    View full-size slide

  159. 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ʯ

    View full-size slide

  160. 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ʯ

    View full-size slide

  161. 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ʯ

    View full-size slide

  162. 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
    Λมߋ͍ͯ͠Δ͔

    View full-size slide

  163. ঢ়ଶͷมߋ
    ҉͘ͳͬͨΒՄࢹޫΧϝϥͷࡱӨΛ΍ΊΔ
    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
    )

    View full-size slide

  164. ঢ়ଶͷมߋ
    ͪͳΈʹ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())

    View full-size slide

  165. AppendixɿΞΫηε੍ݶ

    View full-size slide

  166. ΞΫηε੍ݶ
    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
    ΞΫηε੍ݶͳ͠ͷ৔߹

    View full-size slide

  167. ΞΫηε੍ݶ
    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,
    },
    ɾɾɾ
    ]
    ΞΫηε੍ݶͳ͠ͷ৔߹

    View full-size slide

  168. ΞΫηε੍ݶ
    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Λ஌͍ͬͯΔͱ୭Ͱ΋ΞΫηεͰ͖
    ͯ͠·͏

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  172. ΞΫηε੍ݶ
    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

    View full-size slide

  173. ΞΫηε੍ݶ
    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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  177. ࣗલͰϓϥοτϑΥʔϜΛߏங͢Δඞཁ͕ͳ͍
    ΞΫηε੍ݶ
    Auth0
    WebɺϞόΠϧɺ͓ΑͼϨΨγʔΞϓϦέʔγϣϯ޲͚ͷ

    ϢχόʔαϧೝূɾೝՄϓϥοτϑΥʔϜΛఏڙ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  182. 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Λར༻

    View full-size slide

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

    View full-size slide

  184. 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Λར༻

    View full-size slide