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

Nervesで拡張性の高いIoTデバイスを作る / Making Extensible IoT Device with Nerves

Nervesで拡張性の高いIoTデバイスを作る / Making Extensible IoT Device with Nerves

23f4d5d797a91b6d17d627b90b5a42d9?s=128

Kentaro Kuribayashi

November 26, 2020
Tweet

Transcript

  1. NervesͰ֦ுੑͷߴ͍IoTσόΠεΛ࡞Δ ܀ྛ ݈ଠ࿠ʢGMOϖύϘגࣜձࣾɺ๺཮ઌ୺Պֶٕज़େֶӃେֶʣ ϥΠτχϯάτʔΫ@K-Ruby #25ʢ2020೥11݄26೔ʣ 1

  2. ܀ྛ ݈ଠ࿠ a.k.a. ͋ΜͪΆ https://kentarokuribayashi.com GMOϖύϘגࣜձࣾऔక໾CTO Ұൠࣾஂ๏ਓ೔ຊCTOڠձཧࣄ ๺཮ઌ୺Պֶٕज़େֶӃେֶʢJAISTʣത࢜ લظ՝ఔࡏֶதͷࣾձਓֶੜͰ΋͋Δ IoTηΩϡϦςΟؔ࿈Λݚڀ͢Δͭ΋Γ

    ࣗݾ঺հ 2
  3. - NervesΛ༻͍ͯRaspberry PiͰLEDΛνΧνΧͤ͞ΔʢLνΧʣ - ElixirͷGenServerͱ͍͏࢓૊ΈΛ࢖֦ͬͯுՄೳͳઃܭΛ͢Δ - PhoenixΛ༻͍ͯWeb APIܦ༝ͰLEDΛνΧνΧͤ͞Δʢ΢ΣϒνΧʣ - ΢ΣϒνΧΛෳ਺σόΠεؒͷ௨৴ʹΑ࣮ͬͯݱ͢Δల๬Λड़΂Δ

    ίʔυͷৄࡉΑΓ΋ʮElixirͱNervesͩͱ͜Μͳ͜ͱ͕؆୯ʹͰ͖͍ͯ͢͝ʂʯ ͱ͍͏࿩Λ͠·͢ʢίʔυ͸GitHubʹΞοϓͯ͠ΔͷͰɺৄࡉ͸ͦͪΒΛࢀর ͍ͯͩ͘͠͞ʣɻ ͜ͷLTͰ࿩͢͜ͱ 3
  4. - ϓϥοτϑΥʔϜ - ϓϩηεͱݺ͹ΕΔܰྔεϨουʹΑΓɺεέʔϥϏϦςΟ͕ߴ͍ - ϓϩηεΛ؂ࢹ͠ো֐࣌ʹࣗಈ෮چͰ͖ΔͨΊɺ଱ো֐ੑ͕ߴ͍ - ݴޠ - ؔ਺ܕݴޠͰ͋Γɺ؆ܿͰϝϯςφϯεੑͷߴ͍ίʔυΛॻ͚Δ

    - ڧྗͳύλʔϯϚονϯάػೳɺϚΫϩʹΑΔݴޠͷ֦ுੑΛ༗͢Δ - πʔϧ - ϏϧυπʔϧͷMix΍ύοέʔδϚωʔδϟʔͷHex౳ͷπʔϧ͕ॆ࣮ - Erlang VM্Ͱಈͨ͘ΊɺErlangͷར఺ɾࢿ࢈Λ׆༻Ͱ͖Δ Elixirͷಛ௃ ग़ॴ: The Elixir programming language ΑΓචऀ͕·ͱΊͨ 4
  5. - ElixirͰIoTσόΠεΛ։ൃ͢ΔͨΊͷศརͳ͘͠ΈҰࣜΛఏڙ - ϓϥοτϑΥʔϜ: BEAMʢErlang VMʣΛϒʔτ͢ΔϛχϚϧͳLinuxϑΝʔϜ ΢ΣΞΛఏڙ - ϑϨʔϜϫʔΫ: ElixirͰIoTσόΠεΛૉૣ͘։ൃ͢ΔͨΊͷϥΠϒϥϦ܈Λఏڙ

    - πʔϧνΣΠϯ: ϑΝʔϜ΢ΣΞΛϏϧυͨ͠ΓσϓϩΠͨ͠Γ͢ΔͨΊͷCLI πʔϧΛఏڙ - ͦͷଞ: ϑΝʔϜ΢ΣΞΛOTA͢ΔͨΊͷNervesHubͱ͍͏ج൫Λఏڙʢݸਓత ʹ͸͜͜΁ͷڵຯ͔ΒNervesʹϋϚΓ࢝Ίͨʣ Nervesͱ͸ ग़ॴ: Nerves PlatformɺGetting Started — nerves v1.7.0 ΑΓචऀ͕·ͱΊͨ 5
  6. ී௨ͷLνΧ ී௨ͷLνΧ 6

  7. - લϖʔδͷίʔυͰ͸ɺϕλʹϧʔϓΛճͯ͠νΧνΧ͍ͤͯ͞Δ - LνΧΛ࣮૷͢Δ෦෼Λଞͷػೳͱͭͳ͍͛ͨͱࢥͬͨΒͲ͏͢Δʁ - Web API͔ΒLνΧΛݺͼग़͍ͨ͠ʢࠓճ΍ͬͨ͜ͱͱͯ͠࿩͢಺༰ʣ - ෳ਺σόΠεʹεέʔϧΞ΢τ͍ͨ͠ʢࠓޙͷల๬ͱͯ͠࿩͢಺༰ʣ -

    ElixirʹΑΔϓϩάϥϛϯάͰ͸ɺଟ਺ͷϓϩηεؒͷϝοηʔδϯά ύογϯάʹΑͬͯઃܭ͢Δ - ϋʔυ΢ΣΞͷૢ࡞ͱɺૢ࡞ґཔϦΫΤετͷॲཧͱΛΘ͚ͯઃܭ͢Δ LνΧΛ࠶ઃܭ͢Δ 7
  8. LνΧͷॲཧΛߏ੒͢Δϓϩηε܈ Supervisor Worker Led supervise WorkerͱLedϓϩηεΛ supervise͠ɺϓϩηε͕མ ͪͨΒ্ཱͪ͛௚͢ɻ LEDΛΦϯɾΦϑͨ͠Γɺ ఺໓ͤͨ͞Γ͢ΔೖΓޱΛ

    ఏڙ͢Δϓϩηεɻ಺෦Ͱ ϧʔϓॲཧ΋୲͍ͬͯΔɻ ҎԼͷϝοηʔδʹԠ౴͢ Δɻ :turn_on :turn_off :start_blinking :stop_blinking LEDͷૢ࡞Λఏڙ͢Δϓϩη εɻҎԼͷϝοηʔδʹԠ౴ ͢Δɻ :turn_on :turn_off 8 Message
  9. - 1. ΫϥΠΞϯτϓϩηε͔ΒϝοηʔδΛड͚औΓɺ2. ͳΜΒ͔ͷॲཧΛ͠ ͯɺ3. ݁ՌΛฦ͢ͱ͍͏ʮαʔόʔϓϩηεͷ͋Δ͋ΔύλʔϯʯΛఆٛ - αʔόʔϓϩηε͸εςʔτϑϧͰ͋Δʢ಺෦ঢ়ଶΛ͍࣋ͬͯΔʣ - ۩ମతʹ͸ɺJavaͰ͍͏InterfaceͷΑ͏ͳ࢓૊ΈͰ͋ΔBehaviourʹΑ࣮ͬͯݱ

    ͍ͯ͠Δ - ΫϥΠΞϯτ͔Βͷಉظɾඇಉظݺͼग़͠ͷͲͪΒʹ΋ରԠ͍ͯ͠Δ - Supervisorͷ؂ࢹԼʹஔ͘ͱɺαʔόʔϓϩηε͕ࢮΜͰ΋ࣗಈతʹ࠶ىಈ͞Ε ΔΑ͏ʹͰ͖Δ GenServer 9
  10. - ͲͪΒ΋GenServerΛ༻͍࣮ͯ૷͞Εͨϓϩηε - Workerϓϩηε - ֎෦ϓϩηεʢREPL΍Web APIͳͲʣ͔Βͷɺϋʔυ΢ΣΞʢ͜͜Ͱ͸LEDʣͷૢ࡞Λ ґཔ͢ΔϦΫΤετΛड͚෇͚Δ - LEDΛνΧνΧͤ͞ΔͨΊͷϧʔϓॲཧ΋୲͏ʢվળͷ༨஍͋Γ※ʣ

    - Ledϓϩηε - Workerϓϩηε͔ΒΈΔͱαʔόʔϓϩηεʹ͋ͨΔ - ϋʔυ΢ΣΞ΁ͷૢ࡞Λ୲͏ɻ۩ମతʹ͸ɺRaspberry PiͷGPIOܦ༝ͰLEDΛૢ࡞͢Δ WorkerͱLed ※ ʮ͸͡ΊͯNerves(6) GPIOೖྗͷΤοδΛ࢖ͬͯΠϕϯτυϦϒϯͳϘλνΧΛ͢Δʯࢀরɻ 10
  11. ElixirͷREPLʢIExʣ͔ΒLνΧ͢Δ start_blinkingͰLνΧ։ ࢝ stop_blinkingͰLνΧऴྃ ʢ໨͕௧͘ͳΔͷͰ……ʣ νΧνΧ͍ͯ͠Δʂ 11

  12. ίʔυ͸ kentaro/nerves_webchika Ͱʂ 12

  13. - ࣍͸ɺWebʢAPIʣܦ༝ͰLνΧ=ʮ΢ΣϒνΧʯʢ※ʣ͍ͨ͠ - Web APIͷ࣮૷͸ɺElixirͷσϑΝΫτελϯμʔυͳWAFͰ͋Δ PhoenixΛ༻͍Δ - ࣮૷ʹࡍͯ͠͸ɺطʹݟͨLνΧΛߏ੒͢Δϓϩηε܈΁ͷϝοηʔδ ύογϯά͚ͩͰ࣮ݱ͢Δ -

    ͭ·ΓɺWeb APIଆͰϋʔυ΢ΣΞͷૢ࡞Λ୲͏࣮૷ʹґଘ͠ͳ͍Α͏ ʹ͢Δ Web͔ΒLνΧʢ΢ΣϒνΧʣ͍ͨ͠ ※ ʮElixir_Nervesೖ໳ʂݎ࿚ͳIoT EdgeσόΠεϓϩάϥϛϯάΛ͓खܰʹʙNervesͰͭ͘Δ΢ΣϒνΧʙʯ͕ൃ঵ʁ 13
  14. ΢ΣϒνΧͷॲཧΛߏ੒͢Δϓϩηε܈ Supervisor Worker Led supervise HTTP௨৴ ϝοηʔδૹ৴ 14 Ϣʔβ͔ΒͷWebܦ༝Ͱ ͷૢ࡞ϦΫΤετΛड͚

    ෇͚ΔAPIΛఏڙ Web API
  15. Web APIͷίʔυ͸ʢ࣮࣭ʣ͜Ε͚ͩ nerves.local/led/... - start_blinking - stop_blinking - turn_on -

    turn_off WorkerʹϝοηʔδΛૹΔ 15
  16. ίʔυ͸ kentaro/nerves_webchika Ͱʂ 16

  17. - ϑΝʔϜ΢ΣΞͷىಈ࣌ʹࣗಈͰLνΧ΋࢝·ΔΑ͏ʹ͠ͳ͔ͬͨͷ͸ɺ։ൃ࣌ʹ໨͕ νΧνΧ௧͘ͳΔ͔Β - WorkerɺLed౳ͷϓϩηε͸ΞϓϦέʔγϣϯىಈ࣌ʹࣗಈతʹىಈ - ৄ͘͠͸ίʔυΛࢀরͷ͜ͱ - ࠓճ͸1୆ͷRaspberry Pi্ͰɺWeb

    APIΛؚΉશͯͷϓϩηεΛ࣮ߦ͕ͨ͠ɺෳ਺୆ʹ ΋༰қʹεέʔϧΞ΢τͰ͖Δʢ※ʣ - ܭࢉϦιʔεΛେྔʹফඅͨ͠ΓɺI/Oͷ਺͕ଟ͔ͬͨΓ͢ΔΑ͏ͳσόΠεΛ࡞Δ৔߹ʹศ ར - ͞ΒʹϝοηʔδΛPub/SubͰ΍ΓͱΓ͢ΔͱΑΓૄ݁߹ʹͳ͍͍ͬͯײ͡ ิ଍ͱٞ࿦ ※ ۩ମతʹ͸ʮ͸͡ΊͯNerves(8) ୯ҰϗετͰಈ͘γεςϜΛෳ਺ϗετʹ෼ࢄ͢ΔʯΛࢀরͷ͜ͱ 17
  18. ෳ਺୆ͷ෺ཧϊʔυʹ֦ு͞Εͨϓϩηε܈ Supervisor Worker Led supervise Ϣʔβ͔ΒͷWebܦ༝Ͱ ͷૢ࡞ϦΫΤετΛड͚ ෇͚ΔAPIΛఏڙ Web API

    HTTP௨৴ ϝοηʔδૹ৴ 18 ϊʔυ 1 ϊʔυ 2
  19. - NervesΛ༻͍ͯRaspberry PiͰLEDΛνΧνΧͤͨ͞ʢLνΧʣ - ElixirͷGenServerͱ͍͏࢓૊ΈΛ࢖֦ͬͯுՄೳͳઃܭΛͨ͠ - PhoenixΛ༻͍ͯWeb APIܦ༝ͰLEDΛνΧνΧͤͨ͞ʢ΢ΣϒνΧʣ - ΢ΣϒνΧΛෳ਺σόΠεؒͷ௨৴ʹΑ࣮ͬͯݱ͢Δల๬Λड़΂ͨ

    ElixirͱNervesͰIoTσόΠεΛ࡞Δͷ͸ϚδͰେ͖ͳՄೳੑΛײ͡Δɻ ·ͱΊ 19