Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Home Automation with Android Things and the Google Assistant

812 views

Published on

Thanks for attending ❤️

ありがとうございました

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Home Automation with Android Things and the Google Assistant

  1. 1. Home Automation with Android Things and the Google Assistant
  2. 2. Android is everywhere
  3. 3. Google Assistant Shoebox Robot(01) (01) http://nilhcem.com/android-things/create-your-google-assistant-robots
  4. 4. In your Home?
  5. 5. OK Google,
  6. 6. Philips Hue Bridge
  7. 7. https://developers.google.com/actions/
  8. 8. OAuth2 server
  9. 9. Device Actions • SYNC • EXECUTE • QUERY
  10. 10. SYNC Request: { "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.SYNC" }] }
  11. 11. SYNC Response: { "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "devices": [{ "id": "1", "type": "action.devices.types.LIGHT", "traits": [ "action.devices.traits.OnOff", "action.devices.traits.ColorSpectrum" ], "name": { "name": "Lightbulb" }, "willReportState": true }] } }
  12. 12. Device types: Camera Refrigerator Dishwasher Switch Dryer Thermostat Light Vacuum Outlet Washer
  13. 13. Device traits: Brightness OnOff CameraStream RunCycle ColorSpectrum Scene ColorTemperature StartStop Dock TemperatureSetting Modes Toggles
  14. 14. "Ok Google, "
  15. 15. Smart Home Device Traits(03) (03) https://developers.google.com/actions/smarthome/traits/
  16. 16. https://cloud.google.com/functions/?hl=ja
  17. 17. exports.ha = function(req, res) { if (!isAuthTokenValid(req)) return; let intent = req.body.inputs[0].intent; switch (intent) { case 'action.devices.SYNC': sync(req, res); break; case 'action.devices.EXECUTE': execute(req, res); break; case 'action.devices.QUERY': query(req, res); break; } };
  18. 18. function sync(req, res) { res.status(200).json({ requestId: req.body.requestId, payload: { devices: [{ id: 'lightbulb', type: 'action.devices.types.LIGHT', traits: [ 'action.devices.traits.OnOff', 'action.devices.traits.ColorSpectrum' ], name: { name: 'Lightbulb' }, willReportState: true }] } }); }
  19. 19. $ gcloud beta functions deploy ha --trigger-http
  20. 20. https://us-central1-your-id.cloudfunctions.net/ha
  21. 21. Device Actions • SYNC • EXECUTE • QUERY
  22. 22. EXECUTE Request: { "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.EXECUTE", "payload": { "commands": [{ "devices": [{ "id": "lightbulb" }], "execution": [{ "command": "action.devices.commands.OnOff", "params": { "on": true } }] }] } }] }
  23. 23. EXECUTE Response: { "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "commands": [{ "ids": ["lightbulb"], "status": "SUCCESS", }] } }
  24. 24. Device Actions • SYNC • EXECUTE • QUERY
  25. 25. QUERY Request: { "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.QUERY", "payload": { "devices": [{ "id": "lightbulb" }] } }] }
  26. 26. QUERY Response: { "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "devices": { "lightbulb": { "on": false, "online": true, "color": { "spectrumRGB": 16777215 } } } } }
  27. 27. Raspberry Pi 3
  28. 28. FirestoreLiveData.kt class FirestoreLiveData : LiveData<Device>() { override fun onActive() { authenticateWithFirebase { user -> listenForFirestoreChanges("users/${user.uid}/devices") { change -> postValue(change) } } } }
  29. 29. const admin = require('firebase-admin'); admin.initializeApp({ credential: admin.credential.applicationDefault() });
  30. 30. var db = admin.firestore(); var docRef = db.doc(`users/${userId}/devices/lightbulb`); docRef.set({ on: true, spectrumRGB: 255 });
  31. 31. Lightbulbs
  32. 32. Bluetooth lightbulbs
  33. 33. Reverse engineering Bluetooth Low Energy devices(04) (04) http://nilhcem.com/iot/reverse-engineering-simple-bluetooth-devices
  34. 34. Capture Bluetooth packets
  35. 35. $ adb pull /sdcard/Android/data/btsnoop_hci.log
  36. 36. • Value: 56RRGGBB00f0aa • Characteristic: ffe9 • Service: ffe5
  37. 37. Android Things app
  38. 38. Converting a color to a ByteArray private fun colorToByteArray(color: Int): ByteArray { val red = Color.red(color) val green = Color.green(color) val blue = Color.blue(color) return byteArrayOf(0x56, red, green, blue, 0x00, 0xf0, 0xaa) }
  39. 39. Send the color data to the lightbulb private fun writeCharacteristic(gatt: BluetoothGatt, color: Int) { val service = gatt.getService(SERVICE_UUID) // 0xFFE5 val characteristic = service.getCharacteristic(CHARACTERISTIC_UUID) // 0xFFE9 characteristic.value = colorToByteArray(color) gatt.writeCharacteristic(characteristic) }
  40. 40. MainActivity.kt firestoreLiveData.observe({ lifecycle }) { device -> when (device) { is Lightbulb -> lightbulb.updateState(device) } }
  41. 41. Update Firestore
  42. 42. Fan + Relay module
  43. 43. Android Things GPIO API viewModel.firestoreLiveData.observe({ lifecycle }) { device -> when (device) { is Lightbulb -> lightbulb.onStateChanged(device) is Fan -> relay.value = device.isOn } }
  44. 44. Radio-controlled Power Outlets
  45. 45. Radio Frequency Emitter / Receiver(05) (05) https://github.com/Nilhcem/home-automation-talk-resources/tree/master/radio-power-outlet
  46. 46. Android Things UART API private fun on() = sendUart('1') private fun off() = sendUart('0') private fun sendUart(char: Char) { uartDevice?.write(byteArrayOf(char.toByte()), 1) } private fun openUart() { uartDevice = PeripheralManagerService().openUartDevice(UART_NAME).apply { setBaudrate(9600) setDataSize(8) setParity(UartDevice.PARITY_NONE) setStopBits(1) } }
  47. 47. Infrared blaster
  48. 48. Infrared Receiver (VS1838B)(06) (06) https://github.com/Nilhcem/home-automation-talk-resources/tree/master/infrared-lamp/01-arduino_receiver
  49. 49. Infrared Receiver (VS1838B)
  50. 50. Infrared dump Encoding : UNKNOWN Code : E318261B (32 bits) Timing[67]: +8950, -4550 + 500, - 650 + 500, - 650 + 500, - 650 + 500, - 650 + 500, - 650 + 500, - 650 + 500, - 650 + 500, - 650 + 500, -1750 + 500, -1700 + 500, -1800 + 450, -1750 + 450, -1800 + 450, -1750 + 450, -1750 + 500, -1750 + 450, -1750 + 500, - 650 + 500, -1750 + 500, - 650 + 500, - 650 + 500, - 650 + 500, -1750 + 450, - 700 + 450, - 700 + 450, -1750 + 500, - 700 + 450, -1750 + 500, -1700 + 500, -1750 + 500, - 650 + 500, -1750 + 450 unsigned int rawData[67] = {8950,4550, 500,650, 500,650, 500,650, 500,650, 500,650, 500,650, 500,650, 500,650, 500,1750, 500,1700, 500,1800, 450,1750, 450,1800, 450,1750, 450,1750, 500,1750, 450,1750, 500,650, 500,1750, 500,650, 500,650, 500,650, 500,1750, 450,700, 450,700, 450,1750, 500,700, 450,1750, 500,1700, 500,1750, 500,650, 500,1750, 450}; // UNKNOWN E318261B
  51. 51. ESP8266(07) (07) https://github.com/Nilhcem/home-automation-talk-resources/tree/master/infrared-lamp/03- esp8266_transmitter
  52. 52. Retrofit call @GET("color/{code}") fun setBulbColor(@Path("code") code: Char): Call<Any>
  53. 53. Takeaway • Actions on Google for chatbots & smart home apps. For companies & individuals • Android Things simplifies building secured & advanced IoT devices • Create your own devices, or hack existing ones for interoperability • Easier than it seems
  54. 54. Additional information • Source code for all the projects: https://github.com/Nilhcem/home-automation-talk-resources • Home automation with the Google Assistant: http://nilhcem.com/android-things/google-assistant-smart-home • Actions on Google Smart Home documentation: https://developers.google.com/actions/smarthome/ • Reverse engineering BLE devices: http://nilhcem.com/iot/reverse-engineering-simple-bluetooth-devices • Reverse engineering IR devices: http://nilhcem.com/iot/reverse-engineering-ir-rgb-bulb
  55. 55. Home Automation with Android Things and the Google Assistant • Twitter: @Nilhcem

×