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

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

kumamotone

March 20, 2018
Tweet

More Decks by kumamotone

Other Decks in Programming

Transcript

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

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

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

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

  5. 6

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

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

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

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

  10. 12

  11. 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
  12. Android Things 4 2018೥3݄ݱࡏɺRaspberry Pi 3 ͷ΄͔ɺ NXP Pico i.MX7D,

    NXP Pico i.MX6UL, NXP Argon i.MX6UL Ͱಈ࡞ ࠓճ͸ࠃ಺ͰखʹೖΓ΍͍͢ Raspberry Pi 3 Λ࢖͍·͢ 14
  13. 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
  14. 17

  15. 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
  16. ໨࣍ 1. ༧උ஌ࣝ (Google Home, IFTTT, Firebase, Android Things, Raspberry

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

  18. 21

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

    ͏ʹઃఆ͢Δ ϓϩδΣΫτ໊-xxxxx └ googlehome ɹ └ word: "" 24
  20. 26

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

    a text ingredientʯΛ ࢖͏ ిؾ $ʢిؾ͚ͭͯɺిؾফͯ͠ɺ …ʣͱ͍͏ίϚϯυ͕࢖͑ΔΑ͏ʹͳ Γɺ$ ͸ thatʹૹΒΕΔ 27
  22. 29

  23. 33

  24. 35

  25. 43

  26. 44

  27. 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
  28. 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
  29. 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
  30. 4 ͍ͨͩ·, ͓΍͢ΈͳͲͷෳ਺΍Δ͜ͱ͕͋ΔίϚϯυ͸ async/await Ͱ private fun imhome() { launch

    { async { sendToRemoconWithoutCoroutine(getString(R.string.light_on)) }.await() async { sendToRemoconWithoutCoroutine(getString(R.string.heat)) }.await() } } 49
  31. 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
  32. 53

  33. 54

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

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

    Firebase Λ࢖͏ͱ͍͍ͩͨͷ෺͕ܨ͛ΒΕΔ 4 ৭ʑͳٕज़Λͪΐ͍ͪΐ͍৮Εָ͍ͯ͠ 57
  36. 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
  37. 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