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

kintoneで実践するIoTハンズオン -90分で挑戦!kintone & AWS IoT連携-

kintoneで実践するIoTハンズオン -90分で挑戦!kintone & AWS IoT連携-

kintoneで実践するIoTハンズオン
-90分で挑戦!kintone & AWS IoT連携-
Developers Summit 2016 【19-F-3】サイボウズ特別トラック

Ryu Yamashita

February 17, 2016
Tweet

More Decks by Ryu Yamashita

Other Decks in Programming

Transcript

  1. ࣗݾ঺հʮࢁԼɹཽʯ   *05ͱάϧʔϓ΢ΣΞʢσξΤʣͷ࿈ܞΛࢼΈͯLJOUPOFɹʹग़ձ͏  LJOUPOFΤόϯδΣϦετʹ  LJOUPOF޷͖͕ߴͯ͡ɺք۾ॳͷLJOUPOFస৬  LJOUPOF$BGÉӡӦࣄ຿ہ։ઃɺࣄ຿ہϝϯόʔʹ

     LJOUPOF$BGÉ෱Ԭ7PMΛओ࠵ ݱࡏʢLJOUPOFྺ೥ͪΐͬͱɺ"84ྺ਺ϲ݄ʣ ଟ෼ɺl೔ຊͰ།ҰLJOUPOFͰ͝൧৯΂ͯΔਓz גࣜձࣾδϣΠκʔɹࢁԼཽʢʣ෱Ԭݝେໂాࢢग़਎ ʮϒϧʔ΢Σʔ ϒʯ ͱ͍͏ڞ௨఺
  2. LJOUPOFͱ"84  w ΞϓϦέʔγϣϯدΓ1BB4 4BB4  w ΫϥΠΞϯταΠυϑϩϯτΤϯυ ʢ6*69ʣ w

    ߦಈతղऍͷڞ༗ͰΞΫγϣϯΛى͜͠ɺ ίϛϡχέʔγϣϯͰۀ຿Λճ͢ w ΠϯϑϥدΓ1BBT *BB4ɺ#BB4  w αʔόʔαΠυόοΫΤϯυɹɹɹ ίϚϯυ  w ࢥߟతσʔλΛࡹ͖ࣄ৅Λղऍ͢Δ "1*Ͱ ݁߹ɾิ׬ ྑ࣭ͷΠϯϓοτ ڧྗͳσʔλॲཧ
  3. -BNCEB Πϕϯτιʔε LJOUPOFΤίγεςϜʢԦಓͬΆ͍"84ͱLJOUPOFͷ࿈ܞύλʔϯʣ "NB[PO%ZOBNP%# 4USFBN "NB[PO4/4 "NB[PO4 "NB[PO,JOFTJT "84 -BNCEB

    "NB[PO&$ 3&45"1* LJOUPOFPODZCP[VDPN ɾϨίʔυૢ࡞ ɾϫʔΫϑϩʔૢ࡞ ɾεϖʔεૢ࡞ ɾΞϓϦૢ࡞ 3&45"1* LJOUPOFQSPYZ  "NB[PO"1*(BUFXBZܦ༝΋΍ͬͺΓΠΠʂ 3 &45" 1* ϓϦϛςΟϒͳ࿈ܞ ɾϗεςΟϯά ɾόονॲཧ "NB[PO "1*(BUFXBZ "84*P5 
  4. αʔόϨεʢ-BNCEBʣʹΑΔॲཧܥͷྫ   σʔλ τϦΨʔ σʔλͷൃੜ͕ τϦΨʔͱͯ͠౉Δ ɾϩʔυόϥϯε ɾϨϓϦέʔγϣϯ ɾΦʔτεέʔϧ

    ॲཧʢίʔυʣͷ ࣮ߦ w Πϯϑϥߏஙෆཁ w Πϯϑϥͷӡ༻؅ཧෆཁ w ΩϟύγςΟ w εέʔϧ w σϓϩΠ w ো֐଱ੑ w ϞχλϦχά w ϩΪϯά w ηΩϡϦςΟύονద༻ αʔόϨεʢ-BNCEBʣͷԸܙ
  5. -BNCEB Πϕϯτιʔε LJOUPOFΤίγεςϜʢԦಓͬΆ͍"84ͱLJOUPOFͷ࿈ܞύλʔϯʣ  "NB[PO%ZOBNP%# 4USFBN "NB[PO4/4 "NB[PO4 "NB[PO,JOFTJT "84

    -BNCEB "NB[PO&$ 3&45"1* LJOUPOFPODZCP[VDPN ɾϨίʔυૢ࡞ ɾϫʔΫϑϩʔૢ࡞ ɾεϖʔεૢ࡞ ɾΞϓϦૢ࡞ 3&45"1* LJOUPOFQSPYZ  "NB[PO"1*(BUFXBZܦ༝΋΍ͬͺΓΠΠʂ 3 &45" 1* ϓϦϛςΟϒͳ࿈ܞ ɾϗεςΟϯά ɾόονॲཧ "NB[PO "1*(BUFXBZ "84*P5
  6. "84֤αʔϏεΛ༻͍ͨLJOUPOF΁ͷΠϯϓοτ  ɾ೶ۀʢะ৔ʣ ɾݟकΓ ɾݐઃɾ޻ࣄݱ৔ ɾ޻৔ઃඋ ɾΤωϧΪʔઃඋ ݱ৔ ηϯαʔ ɾԹ౓

    ɾ࣪౓ ɾর౓ ɾՃ଎౓ ɾ૽Ի ηϯαʔ Ϋϥ΢υ LJOUPOF ɾ௨஌ ɾϫʔΫϑϩʔ ɾεϨου ɾΧελϚΠζϏϡʔ ࣄ৅ɾσʔλ͕ൃੜ͢Δ σʔλΛࡹ͖ɺ ࣄ৅Λղऍ͢Δ ղऍͷڞ༗ͰΞΫγϣϯΛ ى͜͠ɺίϛϡχέʔγϣ ϯͰۀ຿Λճ͢ *P5
  7. ϧʔϧ ʢᮢ஋৚݅ʣ "84֤αʔϏεΛ༻͍ͨLJOUPOF΁ͷΠϯϓοτ  X X .255 3&45 "1* 3VMF

    "DUJPO LJOUPOF"84ʹΑΔ*P5ߏ੒ྫ ηϯαʔ τϐοΫ 3VMF "DUJPO શσʔλ ஝ੵ ۀ຿ΞΫγϣϯͷͨΊʹ ଈ࣌࿈ܞ͕ඞཁͳσʔλ ௨஌ɾϫʔΫϑϩʔ ՄࢹԽ ඞཁͳσʔλΛ ඞཁͳ෼͚ͩऔಘ MQTT ϧʔϧ ʢશσʔλʣ ೝূ 3 &4 5  " 1 *
  8. "84֤αʔϏεΛ༻͍ͨLJOUPOF΁ͷΠϯϓοτ  LJOUPOFDPOUSPMMFS 3&45"1* "84-BNCEB "84*P5 ίϯτϩʔϥʔ *P5࿈ܞ ɾϨίʔυૢ࡞ ɾϫʔΫϑϩʔૢ࡞

    ɾεϖʔεૢ࡞ ɾΞϓϦૢ࡞ ᶃϘλϯΛԡ͢ ᶄ*P5৘ใΛड͚औͬͯɺ ࣍ͷαʔϏεʹ౉͢ ᶅड͚औͬͨ৘ใΛτϦΨʔ ʹLJOUPOFʹΞΫηε
  9. LJOUPOF΁ͷ"1*ΞΫηεͷϥοϐϯά  8FCIPPL &WFOU 3&45 "1* ᶃ(JUͷϓογϡɺ ՝୊௥Ճ౳ͷൃੜ ᶄ"1*ͰύϥϝʔλΛ ౉͢

    ᶅ&WFOUͰύϥϝʔλΛड͚औΓɺ "1*ͰLJOUPOF΁ 8FCIPPLʢ௨஌౳ʣΛ"1*(BUFXBZΛܦ༝ͯ͠-BNCEBͰࡹ͍ͨ಺༰Λ LJOUPOFʹ࿈ܞ 8FCIPPL͔ΒͷLJOUPOFΞΫηε
  10. LJOUPOF͔Β"84֤αʔϏε΁ͷΞΫηε  LJOUPOFͷσʔλΛݩʹͨ͠.BDIJOF-FBSOJOH "84 -BNCEB LJOUPOF PODZCP[VDPN ᶃLJOUPOFσʔλͰ ༧ଌϞσϧ࡞੒ "NB[PO

    "1*(BUFXBZ "NB[PO .BDIJOF-FBSOJOH ᶄLJOUPOFʹσʔλొ࿥ ᶅ"1*ʢΫΤϦʣͰ ύϥϝʔλΛ౉͢ ᶆ&WFOUͰύϥϝʔλΛड͚औΓɺ "1*ͰϦΞϧλΠϜ༧ଌ΁ 3&45"1* &WFOU 3&45"1* ᶇ༧ଌ࣮ߦޙ ༧ଌ஋Λฦ͢ ˞ॳಈͷϞσϧ࡞੒͸௚઀࣮ࢪ ɹʢ-BNCEBະܦ༝ʣ ᶈ༧ଌ஋Ληοτ LJOUPOFQSPYZ LJOUPOFͷσʔλ؅ཧʹ༧ଌػೳΛ௥Ճʂ
  11. LJOUPOF͔Β"84֤αʔϏε΁ͷΞΫηε  3&45"1* &WFOU 3&45"1* %FWJDF 4IBEPX 40"1"1* ᶃεΠον0/0'' ᶄ"1*(BUFXBZͱ-BNCEBΛհͯ͠ɺ

    %FWJDF4IBEPXͷ3&45"1*Λίʔϧ ᶅ%FWJDF4IBEPXͰ 3BTQCFSSZ1JΛ੍ޚ ᶆ3BTQCFSSZ1J͔Β-"/಺ͷ 8FNPʹΞΫηε ᶇՈిͷిݯΛ0/0''
  12. 4DIFEVMF&WFOUʹΑΔLJOUPOFόονॲཧ  ᶃ-BNCEBΛఆظىಈ εέδϡʔϧΠϕϯτʢఆظ࣮ߦʣͰɺαʔϏεؒ࿈ܞ &WFOU 3&45 "1* ࿈ܞ αʔϏε 3&45

    "1* ᶄDPOOQBTTͷσʔλΛऔಘ ᶅDPOOQBTTͷσʔλΛ LJOUPOF΁ ᶆLJOUPOFʹೖͬͨσʔλΛ L7JFXFSͰެ։
  13. ϋϯζΦϯϞνϕʔγϣϯ  LJOUPOFº*P5ͱ͸ʁ σʔλϕʔε ϓϩηε ؅ཧ ίϛϡχέʔ γϣϯ ͨͩͷʮσʔλϕʔεʯͰ͸ͳͯ͘ɺLJOUPOFͳΒͰ͸ͷ ʮϓϩηε؅ཧʯ΍ʮίϛϡχέʔγϣϯʯͷػೳΛ׆͔͢ʂ

    ͜͜Ͱେ੾ͳͷ͸ ʮΦϑΟεͷσʔλʯ͚ͩͰͳ͘ ʮݱ৔ͷσʔλʯ΋औΓࠐΉ ΦϑΟεͰਓ͕ೖྗ ݱ৔Ͱηϯαʔ͕ૹ৴ LJOUPOFºηϯαʔΫϥ΢υ࿈ܞࣄྫ σʔλૹ৴ ηϯαʔΫϥ΢υ LJOUPOF Ϩίʔυొ࿥ ɹ௨஌ ɹϫʔΫϑϩʔ ΧελϚΠζϏϡʔ ɹάϥϑΧελϚΠζ NFHBLJLVDPOOFDU ʢ*P5ΞϓϦʣ ˞LJOUPOF࿈ܞػೳ༗ Թ౓ɾ࣪౓ɾর౓౳ͷσʔλΛ஝ੵ ੜ࢈༧ଌͷਫ਼౓޲্ ্ͷϊ΢ϋ΢࠶ར༻ͰɺϑϥϯνϟΠζԽʹ൐͏ੜ࢈؅ཧͷޮ཰Խ /,ΞάϦ༷ʢ౦ژɾ࿨Վࢁʣ LJOUPOFͰ΍͍ͬͯΔ͜ͱ Թ౓ηϯαʔ ɾ௿Լ͢Δब࿑ਓޱ ɾ೶ۀܦӦɾٕज़ͷܧঝ໰୊ νʔϜϫʔΫϓϥοτϑΥʔϜͷ Πϯϓοτͱͯ͠ͷ*P5 Ҿ༻ʮLJOUPOFº*P5࿈ܞʹΑΔݱ৔ۀ຿ͷվળʯ IUUQXXXTMJEFTIBSFOFUKPZ[PKQLJOUPOFJPU
  14. 3VMF "DUJPO શσʔλ ஝ੵ ՄࢹԽ 3 &4 5  "

    1 * ඞཁͳσʔλΛ ඞཁͳ෼͚ͩऔಘ ϧʔϧ ʢશσʔλʣ ϧʔϧ ʢᮢ஋৚݅ʣ ϋϯζΦϯߏ੒ʢଓ͖ʣ  X X .255 3&45 "1* ηϯαʔ τϐοΫ 3VMF "DUJPO ۀ຿ΞΫγϣϯͷͨΊʹ ଈ࣌࿈ܞ͕ඞཁͳσʔλ ௨஌ɾϫʔΫϑϩʔ MQTT ೝূ LJOUPOF"84ʹΑΔ*P5ߏ੒ྫ
  15. ϋϯζΦϯߏ੒ʢଓ͖ʣ ᶃ#-&$FOUSBM͔Β#-&1FSJQIFSBMͷ ηϯαʔσʔλΛ"84*P5%FWJDF(8΁ .2551VCMJTI "84 -BNCEB LJOUPOFPODZCP[VDPN .255 "84*P5 &WFOU

    3&45 "1* *P5%FWJDF #-& 1FSJQIFSBM #-& $FOUSBM ᶄ"84*P5ϧʔϧ ΤϯδϯͰΠϕϯτυϥΠϒ ᶅ-BNCEB͔Β LJOUPOF΁ͷϨίʔυૢ࡞ "84*P5-BNCEB͔ΒLJOUPOF΁ͷॊೈͳσʔλऔΓࠐΈΛମݧ ˞ࠓճ#-&$FOUSBM͸3BTQCFSSZ1JͰ୅༻ 
  16. w LJOUPOF w LJOUPOF3&45"1*Λ/PEFKTͰίʔϧ͢Δ w Ϩίʔυૢ࡞ w ϫʔΫϑϩʔૢ࡞ʢԠ༻ฤʣ w "84

    w "84-BNCEBͷઃఆํ๏ w "84*P5ͷઃఆํ๏ w σόΠεɺϧʔϧɺϙϦγʔɺূ໌ॻ w *P5%FWJDF w "84*P54%,ʹΑΔ.2551VCMJTIͷهड़ํ๏ ࠓճͷϋϯζΦϯʹඞཁͱ͞ΕΔεΩϧηοτʢ໨ඪɾ੒Ռ෺ʣ 
  17. ϋϯζΦϯͷखॱ ᶃ#-&$FOUSBM͔Β#-&1FSJQIFSBMͷ ηϯαʔσʔλΛ"84*P5%FWJDF(8΁ .2551VCMJTI "84 -BNCEB *P5%FWJDFˡ"84*P5ˡ-BNCEBˡLJOUPOF .255 "84*P5 &WFOU

    3&45 "1* *P5%FWJDF #-& 1FSJQIFSBM #-& $FOUSBM ᶄ"84*P5ϧʔϧ ΤϯδϯͰΠϕϯτυϥΠϒ ᶅ-BNCEB͔Β LJOUPOF΁ͷϨίʔυૢ࡞ σʔλ΍ϓϩηεͷऴண఺ଆ͔Βઃఆ͢ΔͱɺεϜʔζʹਐΈ·͢ʂ 
  18. LJOUPOF3&45"1*ͷ֓ཁ  w LJOUPOFΞϓϦͷϨίʔυ΍εϖʔεͷૢ࡞ LJOUPOF3&45"1*ͷ༻్ ϓϩτίϧ w )5514ϓϩτίϧ ϑΥʔϚοτ w

    +40/ จࣈίʔυ w 65' ೝূೝূ w Ϣʔβೝূʢύεϫʔυɺ"1*τʔΫϯʣ w #BTJDೝূ ͦͷଞ w ಉ࣌ॲཧϨίʔυ਺݅ w "1*ಉ࣌ΞΫηε݅ ˞(&5SFDPSETͷΈ݅
  19. ϦΫΤετྫʢ(&5SFDPSETʣ  QWRtaW5pc3RyYXRvcjpwYXNzd29y (&5ϝιουͰ͸ $POUFOU5ZQF͸ෆཁ #BTJDೝূΛ࢖༻͍ͯ͠ͳ͍৔߹ʹ΋ ۭཝͰೖΕ͓ͯ͘෼ʹ͸໰୊ͳ͍༷ࢠ https://{subdomain}.cybozu.com/k/v1/records.json?app={app id} ɹʲೝূํ๏ͷهड़ʳ

    9$ZCP[V"VUIPSJ[BUJPO\#"4&FODPEFEJEQBTTXPSE^ "VUIPSJ[BUJPO#BTJD\#"4&FODPEFEJEQBTTXPSE^ ˞ྫͷ\CBTFFODPEFEJEQBTTXPSE^͸JEΛ"ENJOJTUSBUPSɺ QBTTXPSEΛQBTTXPSEͱͨ͠৔߹ͷ஋ͱ͍ͯ͠·͢ )551ΫϥΠΞϯτઃఆը໘ ˞Ҏ߱ɺ(PPHMF$ISPNFͷΞυΦϯʮ1045."/ʯΛར༻͍ͯ͠·͢
  20. ϦΫΤετྫʢ(&5SFDPSETɿଓ͖ʣ  Ϩεϙϯε "{field code}": { "type" : "{field type}",

    "value" : "{value}" } ֤ϑΟʔϧυ͸͜ͷΑ͏ʹهड़͞Ε·͢ ɾෳ਺஋ΛऔΓ͏Δ\WBMVF^͸഑ྻʹͳΓ·͢ ɾ1045165ͷࡍ͸UZQF͕ෆཁͰ͢ ϑΟʔϧυͷهड़
  21. "84*P5ͷ֓ཁ  Ҿ༻ʮ"84*P5ͷ࢓૊Έʯ IUUQTBXTBNB[PODPNKQJPUIPXJUXPSLT w %&7*$&("5&8": w ϝοηʔδϒϩʔΧʔɺ.255)551 w "65)&/5*$"5*0/"65)03*;"5*0/

    w 5-4ɺ*".ɺ4JH7ɺ$PHOJUP w "84*P5%&7*$&4%, w &NCFEEFE$ɺ+BWB4DSJQUɺ"SEVJOP:ÙO w 36-&4&/(*/& w 42-ϥΠΫͳهड़ɺ"84αʔϏε΁ͷΞΫγϣϯ w %&7*$&ɹ4)"%084 w ΦϑϥΠϯ࣌ͷϝοηʔδड৴ w 3&(*453: w ϝλσʔλ؅ཧ ࢀߟʮ&WFSZUIJOH:PV8BOUUP,OPX"CPVU"84*P5ʯ IUUQXXXTMJEFTIBSFOFU"NB[PO8FC4FSWJDFTNCMOFXFWFSZUIJOHZPVXBOUUPLOPXBCPVUBXTJPU
  22. "84*P5Λத৺ʹϋϯζΦϯߏ੒ΛݟΔ  X X .255 3&45 "1* ᶃূ໌ॻͰඥ͍ͭͨϞϊ͔Β ϝοηʔδʢσʔλʣΛ1VCMJTI &WFOU

    ᶄड͚औͬͨϝοηʔδΛ 5PQJD౳ͰϑΟϧλ ᶅड͚औͬͨϝοηʔδΛ -BNCEBͰॲཧͯ͠LJOUPOF΁࿈ܞ MQTT { "temp": "14", "humidity": "80", "time": "2016-02-19T12:23:56+09:00", "device": "Lambda test" } ϝοηʔδ LJOUPOFͷ Ϩίʔυ
  23. ϋϯζΦϯͷઃఆ֓ཁ  X X .255 3&45 "1* &WFOU ʲLJOUPOFʳ ᶃΞϓϦ࡞੒

    ᶄ"1*τʔΫϯͷऔಘ MQTT ʲ"84-BNCEBʳ ᶃFWFOUͰૹΒΕ͖ͯͨϝοηʔδΛ ॲཧͯ͠ɺLJOUPOFʹ࿈ܞ͢Δ-BNCEB ؔ਺ͷ࡞੒ ʲ"84*P5ʳ ᶃϞϊͷ࡞੒ ᶄϧʔϧͷ࡞੒ ᶄϙϦγʔͷ࡞੒ ᶆূ໌ॻͷੜ੒ ᶇূ໌ॻͱϞϊͷඥ෇͚ ᶈূ໌ॻͱϙϦγʔͷඥ෇͚ ʲ*P5%FWJDFʳ ᶃ#-&ʹΑΔηϯαʔ஋औಘ ᶄ"84*P5%FWJDF4%,ʹΑΔ ηϯαʔ஋ͷ.2551VCMJTIͷઃఆ
  24. LJOUPOFΞϓϦͷઃఆʢΠϯγσϯτ؅ཧΞϓϦͷ֓ཁʣ  ϑΥʔϜ߲໨ ϑΟʔϧυ ͷछྨ ϑΟʔϧυ ίʔυ ଌఆ೔࣌ ೔࣌ UJNF

    ηϯαʔ໊ จࣈྻʢߦʣ EFWJDF Թ౓ ਺஋ UFNQ ࣪౓ ਺஋ IVNJEJUZ *P5%FWJDF͔Β"84*P5-BNCEBΛܦ༝ͯ͠ొ࿥͞ΕΔηϯαʔσʔλ༻ͷϑΥʔϜΛ४උ
  25. LJOUPOFΞϓϦͷઃఆʢΠϯγσϯτ؅ཧΞϓϦͷ֓ཁɾଓ͖ʣ  ϑΥʔϜ߲໨ ϑΟʔϧυ ͷछྨ ϑΟʔϧυ ίʔυ λΠτϧ จࣈྻʢߦʣ λΠτϧ

    ༏ઌ౓ υϩοϓμ΢ϯ ༏ઌ౓ ಺༰ จࣈྻʢෳ਺ ߦʣ ಺༰ ରԠظݶ ೔෇ ରԠظݶ ରԠऀ Ϣʔβʔબ୒ ରԠظݶ ηϯαʔσʔλΛτϦΨʔʹൃੜ͢Δۀ຿ʹؔ࿈͢ΔϑΥʔϜΛ४උ
  26. -BNCEBɾؔ਺ͷઃఆʢଓ͖ʣ  ᶆʮ/BNFʯʹࠓճͷ-BNCEBؔ਺ͷ໊લ ʢྫLJOUPOFJPUʣΛهೖ ᶇʮ3VOUJNFʯ͸ʮ/PEFKTʯ Λબ୒͓ͯ͘͠ ᶉʮ"11@*%ʯͷ஋ΛϝϞ ͓͍ͯͨ͠LJOUPOFΞϓϦ ͷ*%Ͱࠩ͠ସ͑Δ ᶈαϯϓϧͷʮOPEFKT

    LJOUPOF@JPUKTʯΛష෇͚ ˞೔ຊޠ͸ష෇͚࣌ʹจࣈԽ͚͠ɺ ෆ۩߹ͷݪҼʹͳΔ͜ͱ͕͋Δ ˞શબ୒ͯ͠షΓ෇͚Δͱطଘͷ ιʔεΛ࢒ͯ͠௥Ճ͞ΕΔ͜ͱ͕ ͋Δ ᶊʮ"1*@50,&/ʯͷ஋Λ ϝϞ͓͍ͯͨ͠LJOUPOFΞ ϓϦͷ"1*5PLFOͰࠩ͠ ସ͑Δ
  27. // Libraries var https = require('https'); // HTTPS request //

    constant parameters const KINTONE_HOST = 'kintone-iot.cybozu.com'; const APP_ID = 3; const API_TOKEN = 'Tm8C8ziqgf7fUx2XdvAAIu1PJINczwPHVntFj9ON'; ΠϕϯτίʔυʮLJOUPOF@JPUKTʯͷ෦෼ղઆ  )5514ϦΫΤετΛߦ͏ඪ४Ϟδϡʔϧ /PEFKTͷϞδϡʔϧʢߦ໨ʙʣ ઃఆ஋ʢఆ਺ʣʢߦ໨ʙʣ ΞϓϦຖͷݻ༗ͷ৘ใʢࠓճͷฤूର৅ʣ
  28. // get options to access to kintone REST API var

    getOptions = function(path, method) { return { hostname: KINTONE_HOST, port: 443, path: path, method: method, secureProtocol: 'SSLv3_method', headers: { 'X-Cybozu-API-Token': API_TOKEN } }; }; ΠϕϯτίʔυʮLJOUPOF@JPUKTʯͷ෦෼ղઆʢଓ͖ʣ  ϔομ৘ใ͸ೝূ৘ใͷΈηοτ͓ͯ͘͠ ʢ63-౉͠ͷ(&5SFDPSET౳Ҏ֎͸$POUFOU 5ZQFBQQMJDBUJPOKTPOΛผ్෇Ճʣ )5514ϦΫΤετ࣌ʹඞཁͳ৘ใΛऔಘ͢Δؔ਺ʢߦ໨ʙʣ
  29. // regist record to kintone var postRecord = function(event, callback)

    { console.log('start postRecord'); // set request body var params = { "app": APP_ID, "record": { "time": { "value": event.time || event.timestamp || "" }, "device": { "value": event.device || "devsumi_device" }, "temp": { "value": event.temp || "" }, "humidity": { "value": event.humidity || "" } } }; var json = JSON.stringify(params); // set request headers var options = getOptions('/k/v1/record.json', 'POST'); options.headers['Content-Type'] = 'application/json'; // access to kintone REST API var req = https.request(options, function(res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function(chunk) { console.log('BODY: ' + chunk); if (res.statusCode === 200) { callback(null, JSON.parse(chunk)); } }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); callback(e.message); }); req.write(json); req.end(); }; ΠϕϯτίʔυʮLJOUPOF@JPUKTʯͷ෦෼ղઆʢଓ͖ʣ  FWFOUΛॲཧͯ͠Ϩίʔυొ࿥Λߦ͏ؔ਺ʢߦʙʣ FWFOU͔Βొ࿥͢ΔSFDPSEΛ࡞੒ ʮ$POUFOU5ZQFBQQMJDBUJPOKTPOʯΛ ϦΫΤετϔομʹ෇Ճ 1045SFDPSEΛίʔϧ
  30. ͜͜·ͰͷઃఆʹΑΔಈ͖ ᶃ#-&$FOUSBM͔Β#-&1FSJQIFSBMͷ ηϯαʔσʔλΛ"84*P5%FWJDF(8΁ .2551VCMJTI "84 -BNCEB .255 "84*P5 3&45 "1*

    *P5%FWJDF #-& 1FSJQIFSBM #-& $FOUSBM ᶄ"84*P5ϧʔϧ ΤϯδϯͰΠϕϯτυϥΠϒ ᶅ-BNCEB͔Β LJOUPOF΁ͷϨίʔυૢ࡞ &WFOU ʢઃఆࡁΈͷൣғʣ ઌ΄Ͳͷςετ 
  31. ͜͜·ͰͷઃఆʹΑΔಈ͖ "84 -BNCEB "84*P5 3&45 "1* *P5%FWJDF #-& 1FSJQIFSBM #-&

    $FOUSBM ᶄ"84*P5ϧʔϧ ΤϯδϯͰΠϕϯτυϥΠϒ ᶅ-BNCEB͔Β LJOUPOF΁ͷϨίʔυૢ࡞ &WFOU ʢઃఆࡁΈͷൣғʣ .2551VCMJTIςετ ᶃ#-&$FOUSBM͔Β#-&1FSJQIFSBMͷ ηϯαʔσʔλΛ"84*P5%FWJDF(8΁ .2551VCMJTI .255 
  32. .2551VCMJTIͷ֬ೝʢΤϛϡϨʔλͷ४උɾଓ͖ʣ   ᵚ"84*P51VCMJTIFSEBSXJOY .BD༻  ᴺᵎ"84*P51VCMJTIFSBQQ ΤϛϡϨʔλΞϓϦ  ᵚ"84*P51VCMJTIFSXJOJB

    8JOEPXTCJU༻  ᴺᵎ"84*P51VCMJTIFSFYF ΤϛϡϨʔλΞϓϦ  ᵎ"84*P51VCMJTIFSXJOY 8JOEXPTCJU༻  ᵎ"84*P51VCMJTIFSFYF ΤϛϡϨʔλΞϓϦ  ϨϙδτϦͷத਎ʢύοέʔδࡁΞϓϦ෦෼ͷΈʣ ᶅࣗ෼ͷ؀ڥʹదͨ͠ΞϓϦΛىಈ
  33. *P5%FWJDFपลͷৄࡉ  #-& .255 1VCMJTI Թ࣪౓ηϯαʔ ʢ#-&1FSJQIFSBMʣ (8 ʢ#-&$FOUSBMʣ "84*P5

    ͜͜Ͱͷઃఆର৅ 1FSJQIFSBM͔ΒૹΒΕͯ͘Δηϯαʔ஋Λऔಘͯ͠ɺ "84*P5ʹϝοηʔδʢσʔλʣΛύϒϦογϡ͢Δ #-&$FOUSBMͷಈ͖ *P5%FWJDFपลͷߏ੒
  34. *P5%FWJDFपลͷৄࡉʢଓ͖ʣ 132 #-& .255 1VCMJTI ηϯαʔ ʢ#-&1FSJQIFSBMʣ (8 ʢ#-&$FOUSBMʣ "84*P5

    1FSJGFSBMͷ஋ΛಡΈऔΔ/PEFKTίʔσΟϯά "84*P5ΞΫηε༻ͷূ໌ॻͷηοτ "84*P54%,Λ༻͍ͨ"84*P5ΞΫηε༻ͷ/PEFKTίʔσΟϯά #-&$FOUSBMͷઃఆ಺༰
  35. *P5%FWJDFͷઃఆ  \LJOUPOFͷϩάΠϯ*%ʢྫVTFSʣ^ ᵚHJOHB@JPUKT ηϯαʔͷ஋Λऔಘͯ͠1VCMJTI͢ΔϝΠϯͷ/PEFKTϑΝΠϧ  ᵚOPEF@NPEVMF /PEFKTͷϞδϡʔϧ܈  ᴺᵚBXTJPUEFWJDFTEL

    ূ໌ॻΛ൐͏"84*P5΁ͷΞΫηε  ᴺᵚOPCMF #-&$FOUSBMͷ࡞੒  ᴺᵎNPNFOU ೔࣌ૢ࡞  ᵎDFSU ূ໌ॻϑΝΠϧ܈  ᵚSPPU$"QFN ϧʔτূ໌ॻ  ᵚDFSUJGJDBUFQFN ূ໌ॻ  ᵎQSJWBUFQFN ൿີݤ #-&$FOUSBMͷϑΥϧμߏ੒ ϑΥϧμ໊ΛʮHJOHBʯ͔ΒLJOUPOFͷ ϩάΠϯ*%ʹ֤ࣗमਖ਼͓ͯ͘͠
  36. // constant parameters const DEVICE_NAME = 'dev1'; const TOPIC =

    "devsumi/ginga"; // MQTT TOPIC const MY_ADDRESS = "f3:2f:85:98:69:ce"; // MAC Address of BLE peripheral, "GINGA" const REGION = 'ap-northeast-1'; *P5%FWJDFͷઃఆ 134 HJOHB@JPUKTͷฤूՕॴʢߦ໨ʙʣ ᶃ1FSJQIFSBMͷʮ%FW ʯΛهೖ ᶄ1FSJQIFSBMͷΞυϨεΛهೖ
  37. // libraries var noble = require('noble'); // BLE central module

    var awsIot = require('aws-iot-device-sdk'); // AWS IoT SDK var moment = require('moment'); // Date library $FOUSBMίʔυʮHJOHB@JPUKTʯͷ෦෼ղઆ 136 /PEFKTͷϞδϡʔϧ܈ʢߦ໨ʙʣ // constant parameters const DEVICE_NAME = 'dev1'; const TOPIC = "devsumi/ginga"; // MQTT TOPIC const MY_ADDRESS = "f3:2f:85:98:69:ce"; // MAC Address of BLE peripheral, "GINGA" const REGION = 'ap-northeast-1'; ઃఆ஋ʢఆ਺ʣʢߦ໨ʙʣ "84*P5ϧʔϧΤϯδϯͰઃఆͨ͠5PQJDͱಉ͡஋ "84*P5ͷઃఆΛߦͬͨ౦ژϦʔδϣϯ
  38. // device config. var device = awsIot.device({ keyPath: './certs/private.pem', certPath:

    './certs/certificate.pem', caPath: './certs/rootCA.pem', clientId: DEVICE_NAME, region: REGION }); $FOUSBMίʔυʮHJOHB@JPUKTʯͷ෦෼ղઆʢଓ͖ʣ 137 "84*P5%FWJDF4%,ʹ͓͚ΔEFWJDFΦϒδΣΫτͷॳظԽʢߦ໨ʙʣ ূ໌ॻͷύε
  39. // parse & publish temperature/humidity data function publish_ginga(data) { //

    parse temperature data var t1 = parseInt(data[0]); var t2 = parseInt(data[1]) / 100; var temp_value = t1 + t2; // temperature value // parse humidity data var h1 = parseInt(data[2]); var h2 = parseInt(data[3]) / 100; var humidity_value = h1 + h2; // humidity value // create & publish message var message = { "device": DEVICE_NAME, "sensor": 'ginga', "time": moment().format(), "temp": temp_value, "humidity": humidity_value }; message = JSON.stringify(message); console.log("# Publish: " + message); device.publish(TOPIC, message); // publish } $FOUSBMίʔυʮHJOHB@JPUKTʯͷ෦෼ղઆʢଓ͖ʣ 138 (*/("ͷσʔλΛύʔεͯ͠"84*P5ʹ1VCMJTI͢Δؔ਺ʢߦ໨ʙʣ Թ౓Λύʔε ࣪౓Λύʔε 1VCMJTI͢Δϝοηʔδ "84*P5΁ͷ1VCMJTI
  40. // event for BLE peripheral "discover" noble.on('discover', function(peripheral) { noble.stopScanning();

    if (MY_ADDRESS == peripheral.address) { var serviceUUID = peripheral.advertisement.serviceUuids[0]; console.log('# Service UUID: ' + serviceUUID); peripheral.connect(function(error) { if (error) console.log('# Connect error: ' + error); console.log('# Connected to ' + peripheral.uuid); peripheral.discoverServices([serviceUUID], function(error, services) { if (error) console.log('## discoverServices error: ' + error); console.log('## services.length: ' + services.length); var service = services[0]; service.discoverCharacteristics(null, function(error, characteristics) { if (error) console.log('## discoverCharacteristics error: ' + error); console.log('## characteristics.length: ' + characteristics.length); characteristics[0].notify(true, function(error) { if (error) console.log('## notify error: ' + error); setInterval(function() { characteristics[0].read(function(error, data) { if (data) { // publish_humidity(data); // publish humidity // publish_temp(data); // publish temperature publish_ginga(data); // publish GINGA data } }); }, 10 * 1000); // per 10sec }); }); } ); }); } else { console.log("# No my device is discovered"); } }); $FOUSBMίʔυʮHJOHB@JPUKTʯͷ෦෼ղઆʢଓ͖ʣ 139 1FSJQIFSBM͔ΒͷBEWFSUJTFNFOUड৴࣌ͷॲཧʢߦ໨ʙʣ #-&1FSJQIFSBM͔ΒͷBEWFSUJTFNFOUΛड͚ͯ σʔλ͕औΕͨΒɺ1VCMJTI͢Δؔ਺ΛݺͿ OPCMFϞδϡʔϧͰ#-&ͷBEWFSUJTFNFOU Λड͚ͨ࣌ͷΠϕϯτ BEWFSUJTFNFOU͕ࢦఆͨ͠ΞυϨε͔Βͷ ࣌ʹ͸σʔλऔಘͷͨΊʹॲཧଓߦ
  41. LJOUPOFʹอଘͨ͠ϑΝΠϧηοτΛ$FOUSBMʹσϓϩΠ  $ curl -X GET "https://kintone-iot.cybozu.com/k/v1/file.json?fileKey={ϑΝΠϧΩʔ}"\ -H "X-Cybozu-Authorization:{ʮID:Passwrodʯͷbase64Τϯίʔυ஋}"\ -o

    ./{kintoneͷϩάΠϯIDʢྫ: user1ʣ}.zip ϑΝΠϧऔಘͷίϚϯυ $ unzip ./{kintoneͷϩάΠϯIDʢྫ: user1ʣ}.zip ϑΝΠϧηοτͷల։ ࠓճ͸୅දԿ໊͔෼Λ࣮ࢪ͍͖ͤͯͨͩ͞·͢N @@ N
  42. ࠓճ΍ͬͨ಺༰ͷ͓͞Β͍  X X .255 3&45 "1* ᶃূ໌ॻͰඥ͍ͭͨϞϊ͔Β ϝοηʔδʢσʔλʣΛ1VCMJTI &WFOU

    ᶄड͚औͬͨϝοηʔδΛ 5PQJD౳ͰϑΟϧλ ᶅड͚औͬͨϝοηʔδΛ -BNCEBͰॲཧͯ͠LJOUPOF΁࿈ܞ MQTT { "temp": "14", "humidity": "80", "time": "2016-02-19T12:23:56+09:00", "device": "Lambda test" } ϝοηʔδ LJOUPOFͷ Ϩίʔυ
  43. ࠓճߦͬͨઃఆͷ͓͞Β͍  X X .255 3&45 "1* &WFOU ʲLJOUPOFʳ ᶃΞϓϦ࡞੒

    ᶄ"1*τʔΫϯͷऔಘ MQTT ʲ"84-BNCEBʳ ᶃFWFOUͰૹΒΕ͖ͯͨϝοηʔδΛ ॲཧͯ͠ɺLJOUPOFʹ࿈ܞ͢Δ-BNCEB ؔ਺ͷ࡞੒ ʲ"84*P5ʳ ᶃϞϊͷ࡞੒ ᶄϧʔϧͷ࡞੒ ᶄϙϦγʔͷ࡞੒ ᶆূ໌ॻͷੜ੒ ᶇূ໌ॻͱϞϊͷඥ෇͚ ᶈূ໌ॻͱϙϦγʔͷඥ෇͚ ʲ*P5%FWJDFʳ ᶃ#-&ʹΑΔηϯαʔ஋औಘ ᶄ"84*P5%FWJDF4%,ʹΑΔ ηϯαʔ஋ͷ.2551VCMJTIͷઃఆ