Google Home と Android Things で家電を操作する

Google Home と Android Things で家電を操作する

shibuya.apk #23 の発表資料です。
https://shibuya-apk.connpass.com/event/80104/

コードです
https://github.com/kumamotone/ERemoteThingsSample

参考リンク

- Google Homeに話しかけてエアコンを操作してみる
- https://qiita.com/miso_develop/items/204b2e16b1e58e52dc07
- 日曜HackでHomeKitと戯れた話
- https://speakerdeck.com/tattn/ri-yao-hackdehomekittoxi-retahua
- Droidkaigi2018で「AndroidThingsとFirebaseで始めるホームセキュリティ」というタイトルで登壇してきました
- http://crossbridge-lab.hatenablog.com/entry/2018/02/10/150429

---

- Google Home と Android ThingsでLチカする
- http://crossbridge-lab.hatenablog.com/entry/googlehome-androidthings-led
- HomebridgeとWiresharkでAPI非公開の学習リモコンをHomeKit (Siri) 対応してみた
- https://qiita.com/tattn/items/dc022890b686b6fd2a99
- JavaでUDP通信をしてみた
- https://qiita.com/niusounds/items/7cb0989aa7792bcb0681
- Supported Platforms
- https://developer.android.com/things/hardware/index.html
- はじめての Android Things
- https://www.slideshare.net/akirasasaki1/android-things-81001368
- 入門Kotlin coroutines
- https://qiita.com/k-kagurazaka@github/items/8595ca60a5c8d31bbe37

266af7918931b0ea15eae9e8f42ecfa6?s=128

kumamotone

March 20, 2018
Tweet

Transcript

  1. Google Home ͱ Android Things ͰՈ ిΛૢ࡞͢Δ shibuya.apk #23 2018/3/20

    Kazumasa Kumamoto (@kumamo_tone) 1
  2. ໨࣍ 1. ༧උ஌ࣝ (Google Home, IFTTT, Firebase, Android Things, Raspberry

    Pi, ੺֎ઢϦϞίϯ) 4 ࣌ؒͷ౎߹্ Android Things, ੺֎ઢϦϞίϯ Ҏ֎ͷ આ໌͸ඈ͹͠·͢ 2. ࣮ࡍͷ΍Γํͷ঺հ 3. ·ͱΊ 2
  3. ۽ຊ ࿨ਖ਼ 4 iOS/Android ΤϯδχΞˏϠϑʔ 4 Bonfire iOS ͱ͍͏ษڧձΛ΍ͬͯ ͍·͢

    4 potatotips ͱ͍͏ษڧձͰΑ͘ϒ ϩάΛॻ͍͍ͯ·͢ https://twitter.com/ kumamo_tone 3
  4. ໨࣍ 1. ༧උ஌ࣝ (Google Home, IFTTT, Firebase, Android Things, Raspberry

    Pi, ੺֎ઢϦϞίϯ) 4 ࣌ؒͷ౎߹্ Android Things, ੺֎ઢϦϞίϯ Ҏ֎ͷ આ໌͸ඈ͹͠·͢ 2. ࣮ࡍͷ΍Γํͷ঺հ 3. ·ͱΊ 4
  5. 5

  6. 6

  7. Google Home mini ͕࣌ʑηʔϧͰ 3,000 ԁͰചͬͯ Δ ࢲత͓ؾʹೖΓίϚϯυɿ 4 X෼ޙʹىͯ͜͠

    4 Իָֻ͚ͯ 4 ࠓ೔ͷఱؾ͸ʁ 4 ࡿཁΔ͔Ͳ͏͔ 4 ిؾ͚ͭͯ / ΤΞίϯ͚ͭͯ 7
  8. 8

  9. IFTTT 4 IF/THEN ͷ૊Έ߹ΘͤͰ֤छαʔϏεΛ૊Έ߹ΘͤΒΕΔ αʔϏε 4 ྫ: GoogleAssistant IFϨγϐ 4

    ʮOK Google, ͓΍͢Έʯ Ͱىಈ 4 ྫ: WebHook THEN Ϩγϐ 4 APIΛୟ͘ 9
  10. 10

  11. Firebase Realtime Database 4 NoSQLσʔλϕʔε 4 ΞϓϦଆ͔Β؂ࢹͯ͠ϦΞϧλΠϜʹॲཧ͢Δ͜ͱ͕Մೳ 4 ΦϑϥΠϯΩϟογϡػೳ΋͋Γ 4

    Android Studio ͔Β؆୯ʹ࢖͑Δ 11
  12. 12

  13. Android Things IoT޲͚ͷϓϥοτϑΥʔϜ ≒ Raspberry PiͳͲͷϓϥοτϑΥʔϜʢ࣍ϖʔδͰ આ໌ʣ্Ͱಈ͘Android 4 Android ͷࢿ࢈͕ྲྀ༻Ͱ͖ΔʹAndroid

    ΞϓϦΛ࡞ΔཁྖͰ։ൃ 4 Android Studio, Java/Kotlin, Gradle, JUnit ͕࢖͑Δ API24ʙ 4 Peripheral I/O API ͷ௥Ճ 4 GPIO(൚༻ೖग़ྗ), I2C, PWM SPI ... ͳͲ IoT༻ͷAPI 4 Android Things Console Ͱ OTAʢOver-The-Airʣιϑτ΢ΣΞΞοϓσʔτ 13
  14. Android Things 4 2018೥3݄ݱࡏɺRaspberry Pi 3 ͷ΄͔ɺ NXP Pico i.MX7D,

    NXP Pico i.MX6UL, NXP Argon i.MX6UL Ͱಈ࡞ ࠓճ͸ࠃ಺ͰखʹೖΓ΍͍͢ Raspberry Pi 3 Λ࢖͍·͢ 14
  15. ͪͳΈʹ DroidKaigi ͷ Codelab Ͱ࢖ͬͯͨͷ͸ ͨͿΜ NXP i.MX7D aStarter Kit

    ͱ͍͏΍ͭ 15
  16. Raspberry Pi 3 4 ҰൠతͳΧʔυͱಉ͙͡Β͍ͷখܕPC Wi- Fi, Bluetooth, GPIO ͳͲ͕͍͍ͭͯΔ

    4 Raspbian OS(debianϕʔε)ͷ΄͔ Android ThingsͷΠϝʔδ΋ىಈͰ͖Δ 4 ઈରඞཁͳ΋ͷ 4 ຊମ + USBిݯ(5V 3.0A) 4 ͋Δͱศར 4 HDMIͰܨ͕ΔσΟεϓϨΠ 4 ώʔτγϯΫ෇͖έʔε 16
  17. 17

  18. eRemote mini 6,000 ԁ͙Β͍ͷ੺֎ઢϦϞίϯ Amazon.co.jp ϕετηϥʔ 4 BroadLink RM Mini

    3 4 eRemoteͷւ֎൛ جຊతʹػೳ͸ಉ ͬ͡Ά͍ $10+ૹྉ ͙Β͍ɹٕదͳ͠ 4 Alexa ʹ͸ରԠ͍ͯ͠ΔΒ͍͠ 4 API͕ެ։͞Ε͍ͯͳ͍ɹAPI͕ެ։͞ Ε͍ͯΔ Nature Remo(¥13,000) ͷ΄ ͏͕Կ͔ͱศར͔΋… 18
  19. ໨࣍ 1. ༧උ஌ࣝ (Google Home, IFTTT, Firebase, Android Things, Raspberry

    Pi, ੺֎ઢϦϞίϯ) 4 ࣌ؒͷ౎߹্ Android Things, ੺֎ઢϦϞίϯ Ҏ֎ͷ આ໌͸ඈ͹͠·͢ 2. ࣮ࡍͷ΍Γํͷ঺հ 3. ·ͱΊ 19
  20. 20

  21. 21

  22. Firebase ͷઃఆ Firebase ίϯιʔϧ͔Β࡞੒ https:// console.firebase.google.com/u/1/ 22

  23. Firebase ͷઃఆ ϓϩδΣΫτͷ࡞੒ 23

  24. Firebase ͷઃఆ 4 Database > Realtime Database > σʔλ ͔Βɺͨͱ͑͹ҎԼͷΑ

    ͏ʹઃఆ͢Δ ϓϩδΣΫτ໊-xxxxx └ googlehome ɹ └ word: "" 24
  25. Firebase ͷઃఆ 4 ϧʔϧ͔ΒಡΈॻ͖ΛڐՄʢຊ֨ తʹӡ༻͞ΕΔ৔߹͸஫ҙʣ { "rules": { ".read": "true",

    ".write": "true" } } 25
  26. 26

  27. IFTTT ͷઃఆ - if this Google AssistantϨγϐͷʮSay a phrase with

    a text ingredientʯΛ ࢖͏ ిؾ $ʢిؾ͚ͭͯɺిؾফͯ͠ɺ …ʣͱ͍͏ίϚϯυ͕࢖͑ΔΑ͏ʹͳ Γɺ$ ͸ thatʹૹΒΕΔ 27
  28. IFTTT ͷઃఆ - then that WebhooksϨγϐ ҎԼͷURLʹPUT͢ Ε͹σʔλͷߋ৽͕Ͱ͖Δ https://ϓϩδΣΫτ໊- xxxxx.firebaseio.com/googlehome/

    word.json 4 light ͱ {{TextField}} ʹ͸εϖʔ εΛೖΕΔ 28
  29. 29

  30. eRemote mini ʹ৴߸ΛૹΔ 4 εϚϗΞϓϦ(e-Control)͕Ͳ͏΍ͬͯ௨৴͍ͯ͠Δ͔ݟ Δ 4 https://qiita.com/tattn/items/ dc022890b686b6fd2a99 ΛࢀߟʹwiresharkΛ࢖ͬͯ

    ௨৴಺༰ΛݟΔ 4 MacͰ΋brew caskͰೖΔ 30
  31. eRemote mini ʹ৴߸ΛૹΔ 4 Ͳ͏΍ΒQUICͱ͍͏UDPϕʔεͷϓϩτίϧͰ௨৴͍ͯ͠ΔΒ͠ ͍͜ͱ͕Θ͔Δ ಉҰωοτϫʔΫ্ʹܨ͕͍ͬͯΔػث͸ sudo arp-scan -I

    en0 -l ͳͲͰௐ΂ΒΕΔ 31
  32. eRemote mini ʹ৴߸ΛૹΔ ϖΠϩʔυΛ߇͓͑ͯ͘ 32

  33. 33

  34. ηοτΞοϓϓϩάϥ ϜΛμ΢ϯϩʔυ ͜ͷը໘͕ͳʹ͛ʹͲ͜ʹ͋Δͷ͔෼ ͔ΓͮΒ͍ 34

  35. 35

  36. microSD ʹ Android Things ͷΠϝʔδΛম ͘ ίϯιʔϧͰ࣭໰ʹ౴͍͑ͯ͘ ͍͍ͩͨ6෼͙Β͍ 36

  37. microSDΛૠͯ͠ىಈ 37

  38. microSDΛૠͯ͠ىಈ 38

  39. LANέʔϒϧΛࢗ͢ 39

  40. WiFiηοτΞοϓ ༗ઢ͚ͩͰ΋Ͱ͖Δ͕͍ͭͰʹ΍ͬͯ ͓͘ IPΞυϨεͱSSIDͷID/PASSΛ ࢦఆͯ͠ઃఆ͢Δ 40

  41. WiFiηοτΞοϓ DONE WiFi͕දࣔ͞ΕΔ 41

  42. Android ͰϓϩδΣΫτ࡞੒ 42

  43. 43

  44. 44

  45. Android Studio ͰϓϩδΣΫτ࡞੒ 4 ωοτϫʔΫӽ͠ʹసૹͰ͖Δ $ adb connect 192.168.3.13 45

  46. 4 Firebase Ͱ؂ࢹ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val

    reference = FirebaseDatabase.getInstance().getReference("googlehome").child("word") reference.setValue("") reference.addValueEventListener(object : ValueEventListener { override fun onDataChange(dataSnapshot: DataSnapshot) { val str = dataSnapshot.getValue(String::class.java) ?: return val words = str.split(" ") when (words.first()) { "light" -> { changeLight(words) } "aircon" -> { changeAircon(words) } "tadaima" -> { imhome() } "oyasumi" -> { oyasumi() } } } override fun onCancelled(databaseError: DatabaseError) {} }) } 46
  47. 4 ͷ, Λ ͳͲͷॿࢺ͸εΩοϓ 4 ʮ͚ͭͯʯ͚ͩͰͳ͘ʮ͚ͭ ͯʯͷΑ͏ʹೝࣝ͞ΕΔ͜ͱ ͕͋ΔͷͰɺ྆ํఆٛ private fun

    changeAircon(words: List<String>) { when (words.getOrNull(1)) { "ͷ", "Λ" -> { changeAircon(words.subList(1, words.count())) } "͚ͭ", "͚ͭͯ", "෇͚", "෇͚ͯ" -> { sendToRemocon(getString(R.string.heat)) } "͚͠", "͚ͯ͠", "ফ͠", "ফͯ͠" -> { sendToRemocon(getString(R.string.aircon_off)) } "আ࣪" -> { sendToRemocon(getString(R.string.humid)) } else -> { return } } } 47
  48. 4 java.nio Λ࢖͏ɻUDP ͱಉ͡ཁྖͰྑͦ͞͏ 4 ίϧʔνϯ, είʔϓؔ਺ use ΍ also,

    Λ࢖͖ͬͯͬ͢Γ private fun sendToRemocon(data: String) { launch { DatagramChannel.open().use { it.socket().bind(InetSocketAddress(9999 /* ૹΓݩϙʔτ */)) val buf = ByteBuffer.allocate(data.length).also { it.clear() it.put(data.hexStringToByteArray()) it.flip() } it.send(buf, InetSocketAddress(getString(R.string.ipaddress), 80)) } } } 48
  49. 4 ͍ͨͩ·, ͓΍͢ΈͳͲͷෳ਺΍Δ͜ͱ͕͋ΔίϚϯυ͸ async/await Ͱ private fun imhome() { launch

    { async { sendToRemoconWithoutCoroutine(getString(R.string.light_on)) }.await() async { sendToRemoconWithoutCoroutine(getString(R.string.heat)) }.await() } } 49
  50. 4 ͍ͨͩ·, ͓΍͢ΈͳͲͷෳ਺΍Δ͜ͱ͕͋ΔίϚϯυ͸ async/await Ͱ private fun oyasumi() { launch

    { async { sendToRemoconWithoutCoroutine(getString(R.string.light_off)) }.await() async { sendToRemoconWithoutCoroutine(getString(R.string.aircon_off)) }.await() async { sendToRemoconWithoutCoroutine(getString(R.string.timer7hours)) } } } 50
  51. ׬੒ʂ 51

  52. σϞ 52

  53. 53

  54. 54

  55. ࠓճ࢖ͬͨίʔυ https://github.com/kumamotone/ ERemoteThingsSample 55

  56. ໨࣍ 1. ༧උ஌ࣝ (Google Home, IFTTT, Firebase, Android Things, Raspberry

    Pi, ੺֎ઢϦϞίϯ) 4 ࣌ؒͷ౎߹্ Android Things, ੺֎ઢϦϞίϯ Ҏ֎ͷ આ໌͸ඈ͹͠·͢ 2. ࣮ࡍͷ΍Γํͷ঺հ 3. ·ͱΊ 56
  57. ·ͱΊ 4 Android Studio ͱ Kotlin ͕࢖͑Δͷྑ͍ʂ 4 IFTTT ͱ

    Firebase Λ࢖͏ͱ͍͍ͩͨͷ෺͕ܨ͛ΒΕΔ 4 ৭ʑͳٕज़Λͪΐ͍ͪΐ͍৮Εָ͍ͯ͠ 57
  58. ࠓޙ΍Γ͍ͨ͜ͱ 4 Android ThingsͷڧΈʮAndroidͷࢿ࢈Λ࠶ར༻Ͱ͖ ΔʯΛ͢Δ 4 AndroidΞϓϦ༻ͷIoT੡඼Λ͔ͭ͏ 4 ݰؔͷυΞ͕։ดͨ͠ΒిؾΛૢ࡞ͳͲ 4

    σʔλΛऔΔʢ͍ͭؼ͖͍ͬͯͯͭ৸ͯΔ͔Θ͔Δ͔΋ʣ 58
  59. References 4 Google Homeʹ࿩͔͚ͯ͠ΤΞίϯΛૢ࡞ͯ͠ΈΔ 4 https://qiita.com/miso_develop/items/204b2e16b1e58e52dc07 4 ೔༵HackͰHomeKitͱٔΕͨ࿩ 4 https://speakerdeck.com/tattn/ri-yao-hackdehomekittoxi-

    retahua 4 Droidkaigi2018ͰʮAndroidThingsͱFirebaseͰ࢝ΊΔϗʔϜηΩϡϦς Οʯͱ͍͏λΠτϧͰొஃ͖ͯ͠·ͨ͠ 4 http://crossbridge-lab.hatenablog.com/entry/2018/02/10/150429 59
  60. 4 Google Home ͱ Android ThingsͰLνΧ͢Δ 4 http://crossbridge-lab.hatenablog.com/entry/googlehome-androidthings-led 4 HomebridgeͱWiresharkͰAPIඇެ։ͷֶशϦϞίϯΛHomeKit

    (Siri) ରԠͯ͠Έͨ 4 https://qiita.com/tattn/items/dc022890b686b6fd2a99 4 JavaͰUDP௨৴Λͯ͠Έͨ 4 https://qiita.com/niusounds/items/7cb0989aa7792bcb0681 4 Supported Platforms 4 https://developer.android.com/things/hardware/index.html 4 ͸͡Ίͯͷ Android Things 4 https://www.slideshare.net/akirasasaki1/android-things-81001368 4 ೖ໳Kotlin coroutines 4 https://qiita.com/k-kagurazaka@github/items/8595ca60a5c8d31bbe37 60
  61. Thanks! ! 61