$30 off During Our Annual Pro Sale. View Details »

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

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

Kentaro Kuribayashi

November 26, 2020
Tweet

More Decks by Kentaro Kuribayashi

Other Decks in Technology

Transcript

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

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

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

    - πʔϧνΣΠϯ: ϑΝʔϜ΢ΣΞΛϏϧυͨ͠ΓσϓϩΠͨ͠Γ͢ΔͨΊͷCLI πʔϧΛఏڙ - ͦͷଞ: ϑΝʔϜ΢ΣΞΛOTA͢ΔͨΊͷNervesHubͱ͍͏ج൫Λఏڙʢݸਓత ʹ͸͜͜΁ͷڵຯ͔ΒNervesʹϋϚΓ࢝Ίͨʣ Nervesͱ͸ ग़ॴ: Nerves PlatformɺGetting Started — nerves v1.7.0 ΑΓචऀ͕·ͱΊͨ 5
  4. - લϖʔδͷίʔυͰ͸ɺϕλʹϧʔϓΛճͯ͠νΧνΧ͍ͤͯ͞Δ - LνΧΛ࣮૷͢Δ෦෼Λଞͷػೳͱͭͳ͍͛ͨͱࢥͬͨΒͲ͏͢Δʁ - Web API͔ΒLνΧΛݺͼग़͍ͨ͠ʢࠓճ΍ͬͨ͜ͱͱͯ͠࿩͢಺༰ʣ - ෳ਺σόΠεʹεέʔϧΞ΢τ͍ͨ͠ʢࠓޙͷల๬ͱͯ͠࿩͢಺༰ʣ -

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

    ఏڙ͢Δϓϩηεɻ಺෦Ͱ ϧʔϓॲཧ΋୲͍ͬͯΔɻ ҎԼͷϝοηʔδʹԠ౴͢ Δɻ :turn_on :turn_off :start_blinking :stop_blinking LEDͷૢ࡞Λఏڙ͢Δϓϩη εɻҎԼͷϝοηʔδʹԠ౴ ͢Δɻ :turn_on :turn_off 8 Message
  6. - ͲͪΒ΋GenServerΛ༻͍࣮ͯ૷͞Εͨϓϩηε - Workerϓϩηε - ֎෦ϓϩηεʢREPL΍Web APIͳͲʣ͔Βͷɺϋʔυ΢ΣΞʢ͜͜Ͱ͸LEDʣͷૢ࡞Λ ґཔ͢ΔϦΫΤετΛड͚෇͚Δ - LEDΛνΧνΧͤ͞ΔͨΊͷϧʔϓॲཧ΋୲͏ʢվળͷ༨஍͋Γ※ʣ

    - Ledϓϩηε - Workerϓϩηε͔ΒΈΔͱαʔόʔϓϩηεʹ͋ͨΔ - ϋʔυ΢ΣΞ΁ͷૢ࡞Λ୲͏ɻ۩ମతʹ͸ɺRaspberry PiͷGPIOܦ༝ͰLEDΛૢ࡞͢Δ WorkerͱLed ※ ʮ͸͡ΊͯNerves(6) GPIOೖྗͷΤοδΛ࢖ͬͯΠϕϯτυϦϒϯͳϘλνΧΛ͢Δʯࢀরɻ 10
  7. - ࣍͸ɺWebʢAPIʣܦ༝ͰLνΧ=ʮ΢ΣϒνΧʯʢ※ʣ͍ͨ͠ - Web APIͷ࣮૷͸ɺElixirͷσϑΝΫτελϯμʔυͳWAFͰ͋Δ PhoenixΛ༻͍Δ - ࣮૷ʹࡍͯ͠͸ɺطʹݟͨLνΧΛߏ੒͢Δϓϩηε܈΁ͷϝοηʔδ ύογϯά͚ͩͰ࣮ݱ͢Δ -

    ͭ·ΓɺWeb APIଆͰϋʔυ΢ΣΞͷૢ࡞Λ୲͏࣮૷ʹґଘ͠ͳ͍Α͏ ʹ͢Δ Web͔ΒLνΧʢ΢ΣϒνΧʣ͍ͨ͠ ※ ʮElixir_Nervesೖ໳ʂݎ࿚ͳIoT EdgeσόΠεϓϩάϥϛϯάΛ͓खܰʹʙNervesͰͭ͘Δ΢ΣϒνΧʙʯ͕ൃ঵ʁ 13
  8. - ϑΝʔϜ΢ΣΞͷىಈ࣌ʹࣗಈͰLνΧ΋࢝·ΔΑ͏ʹ͠ͳ͔ͬͨͷ͸ɺ։ൃ࣌ʹ໨͕ νΧνΧ௧͘ͳΔ͔Β - WorkerɺLed౳ͷϓϩηε͸ΞϓϦέʔγϣϯىಈ࣌ʹࣗಈతʹىಈ - ৄ͘͠͸ίʔυΛࢀরͷ͜ͱ - ࠓճ͸1୆ͷRaspberry Pi্ͰɺWeb

    APIΛؚΉશͯͷϓϩηεΛ࣮ߦ͕ͨ͠ɺෳ਺୆ʹ ΋༰қʹεέʔϧΞ΢τͰ͖Δʢ※ʣ - ܭࢉϦιʔεΛେྔʹফඅͨ͠ΓɺI/Oͷ਺͕ଟ͔ͬͨΓ͢ΔΑ͏ͳσόΠεΛ࡞Δ৔߹ʹศ ར - ͞ΒʹϝοηʔδΛPub/SubͰ΍ΓͱΓ͢ΔͱΑΓૄ݁߹ʹͳ͍͍ͬͯײ͡ ิ଍ͱٞ࿦ ※ ۩ମతʹ͸ʮ͸͡ΊͯNerves(8) ୯ҰϗετͰಈ͘γεςϜΛෳ਺ϗετʹ෼ࢄ͢ΔʯΛࢀরͷ͜ͱ 17