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

IoT Devices With Android Things

IoT Devices With Android Things

Talk about Android Things, which I held at the Devfest, Karlsruhe (http://devfestka.de/).

Wolfram Rittmeyer

December 09, 2017
Tweet

More Decks by Wolfram Rittmeyer

Other Decks in Technology

Transcript

  1. Android Things Android Things • Google‘s drive into IoT Google‘s

    drive into IoT – Evolution of Brillo Evolution of Brillo • Based on Android (duh!) Based on Android (duh!) • Targets SoM and has support for the RasPi 3 as well Targets SoM and has support for the RasPi 3 as well
  2. SoMs, SoCs and what not SoMs, SoCs and what not

    • MicroControllers MicroControllers – Arduino, ESP 8266, ESP 32, Micro Bit, Calliope Arduino, ESP 8266, ESP 32, Micro Bit, Calliope – Runs only one program Runs only one program • General Purpose Boards General Purpose Boards – Include OS (usually Linux or Linux based) Include OS (usually Linux or Linux based) – Thus can run multiple programms concurrently Thus can run multiple programms concurrently • Most IoT systems use SoCs or SoMs Most IoT systems use SoCs or SoMs
  3. SoMs, SoCs and what not SoMs, SoCs and what not

    • SoM vs. Development Boards SoM vs. Development Boards • Development Boards are Development Boards are – For development and prototyping For development and prototyping • SoMs are SoMs are – System on Module System on Module – Provide CPU, Memory, Interfaces, Timers, Voltage Regulators and Provide CPU, Memory, Interfaces, Timers, Voltage Regulators and Connectivity Connectivity – Need to be connected to the final production circuit board Need to be connected to the final production circuit board
  4. Android Things Android Things • Android programming model Android programming

    model • Support lib for hardware related stuf Support lib for hardware related stuf • Supports plenty of services Supports plenty of services – Subset of Firebase Subset of Firebase – Subset Google Play Services Subset Google Play Services – Subset of Android Subset of Android
  5. Android Things Android Things • No support for No support

    for – Notifications Notifications – typical ContentProviders like Calendar or Contacts typical ContentProviders like Calendar or Contacts • Screens are optional Screens are optional – And quite frankly not the most important feature for IoT And quite frankly not the most important feature for IoT – Instead use voice, game controllers, simple buttons and sensors Instead use voice, game controllers, simple buttons and sensors
  6. Android Things Android Things • Some oddities: Some oddities: –

    Permissions are always granted, but must be declared Permissions are always granted, but must be declared • All code can throw IOExceptions All code can throw IOExceptions – my sample code blissfully ignores this :-) my sample code blissfully ignores this :-)
  7. Coding Android Things Coding Android Things • Special support lib

    Special support lib – Peripheral I/O API Peripheral I/O API – User Driver API User Driver API • But first But first – AndroidManifest.xml AndroidManifest.xml – build.gradle build.gradle
  8. AndroidManifest.xml AndroidManifest.xml <uses-library <uses-library android android:name= :name="com.google.android.things" "com.google.android.things"/> /> <activity

    <activity android android:name= :name=".SongActivity" ".SongActivity"> > <intent-filter> <intent-filter> <action <action android android:name= :name="android.intent.action.MAIN" "android.intent.action.MAIN" /> /> <category <category android android:name= :name="android.intent.category.IOT_LAUNCHER" "android.intent.category.IOT_LAUNCHER" /> /> </intent-filter> </intent-filter> </activity> </activity>
  9. build.xml build.xml dependencies { dependencies { //... //... compileOnly compileOnly

    'com.google.android.things:androidthings:0.6-devpreview' 'com.google.android.things:androidthings:0.6-devpreview' //… } }
  10. Supported Protocols Supported Protocols • GPIO GPIO • PWM PWM

    • I²C I²C • I²S I²S • SPI SPI • UART UART
  11. GPIO GPIO • For binary data For binary data –

    switching something on or of switching something on or of – Reading the state of a binary switch Reading the state of a binary switch
  12. GPIO GPIO val val gpioDevice = gpioDevice = manager manager.openGpio(

    .openGpio(GPIO_NAME GPIO_NAME) ) gpioDevice.setDirection(Gpio. gpioDevice.setDirection(Gpio.DIRECTION_IN DIRECTION_IN) ) gpioDevice.setEdgeTriggerType(Gpio. gpioDevice.setEdgeTriggerType(Gpio.EDGE_FALLING EDGE_FALLING) ) callback callback = MyGpioCallback() = MyGpioCallback() gpioDevice. gpioDevice.registerGpioCallback( registerGpioCallback(callback callback) )
  13. GPIO GPIO private inner class private inner class MyGpioCallback :

    GpioCallback() { MyGpioCallback : GpioCallback() { override fun override fun onGpioEdge onGpioEdge(gpio: Gpio): Boolean { (gpio: Gpio): Boolean { if if (gpio. (gpio.value value && ! && !started started) { ) { startAudio() startAudio() } } return true return true } } override fun override fun onGpioError onGpioError(gpio: Gpio? (gpio: Gpio?, , error: Int) { error: Int) { Log.e(Constants. Log.e(Constants.LOG_TAG LOG_TAG, , "error while accessing gpio: " "error while accessing gpio: " + error) + error) } } } }
  14. PWM PWM • Frequency and modulation width Frequency and modulation

    width – Creating sound Creating sound – Steering a servo motor Steering a servo motor • Only for outgoing signals Only for outgoing signals
  15. PWM PWM device device.setEnabled( .setEnabled(true true) ); ; device device.setPwmFrequencyHz(

    .setPwmFrequencyHz(note note. .tone tone. .frequency frequency) ); ; device device.setPwmDutyCycle( .setPwmDutyCycle(0.5 0.5) ); ;
  16. UART UART • One to one connection One to one

    connection • Very old protocol Very old protocol • RS-232, USB and the like are based on it RS-232, USB and the like are based on it • Only a few devices need it Only a few devices need it • BUT: Can be used to read state of devices (Raspi) BUT: Can be used to read state of devices (Raspi)
  17. I²C I²C • Connect up to 128 devices per I²C

    bus Connect up to 128 devices per I²C bus – Actually 117 since some addresses are reserved Actually 117 since some addresses are reserved • Address is set by vendor Address is set by vendor – Sometimes adjustments via add-on pins are possible Sometimes adjustments via add-on pins are possible • Single data line, thus half-duplex transmissions Single data line, thus half-duplex transmissions • Can be based on registers or based on raw transfers Can be based on registers or based on raw transfers
  18. I²C I²C fun fun configureI2CDevice configureI2CDevice() { () { val

    val manager = PeripheralManagerService() manager = PeripheralManagerService() i2cDevice i2cDevice = manager.openI2cDevice( = manager.openI2cDevice("i2c_name" "i2c_name", , 0x40 0x40) ) } } // Modify the contents of a single register // Modify the contents of a single register fun fun setRegisterFlag setRegisterFlag(device: I2cDevice (device: I2cDevice, , address: Int) { address: Int) { // Read one register from slave // Read one register from slave var var value = device.readRegByte(address) value = device.readRegByte(address) // Set bit 6 // Set bit 6 value = value value = value or or 0x40 0x40 // Write the updated value back to slave // Write the updated value back to slave device.writeRegByte(address device.writeRegByte(address, , value) value) } }
  19. SPI SPI • High performance protocol (up to 1GHz, though

    typically High performance protocol (up to 1GHz, though typically slower) slower) • Uses clock signal for transmission Uses clock signal for transmission • Limited number of devices because of Chip Select pins Limited number of devices because of Chip Select pins
  20. SPI SPI fun fun configureSpiDevice configureSpiDevice(device: SpiDevice) { (device: SpiDevice)

    { device.setMode(SpiDevice. device.setMode(SpiDevice.MODE0 MODE0) ) // Low clock, leading edge transfer // Low clock, leading edge transfer device.setFrequency( device.setFrequency(16000000 16000000) ) // 16MHz // 16MHz device.setBitsPerWord( device.setBitsPerWord(8 8) ) // 8 BPW // 8 BPW device.setBitJustification( device.setBitJustification(false false) ) // MSB first // MSB first } }
  21. SPI SPI // Half-duplex data transfer // Half-duplex data transfer

    fun fun sendCommand sendCommand(device: SpiDevice (device: SpiDevice, , buffer: ByteArray) { buffer: ByteArray) { // Shift data out to slave // Shift data out to slave device.write(buffer device.write(buffer, , buffer. buffer.size size) ) // Read the response // Read the response val val response = ByteArray( response = ByteArray(32 32) ) device.read(response device.read(response, , response. response.size size) ) } }
  22. Drivers Drivers • Useful for separation within teams Useful for

    separation within teams • Useful if your device allows third party apps to run Useful if your device allows third party apps to run • Driver developer cares about nitty-gritty low-level details Driver developer cares about nitty-gritty low-level details • „ „Normal“ Android developer uses stuf as per usual Normal“ Android developer uses stuf as per usual
  23. Drivers Drivers • Input Input – Touch events and button

    presses Touch events and button presses • Sensors Sensors – Accelerometer, light, proximity… Accelerometer, light, proximity… • GPS GPS – Maps NMEA strings of sensors to Location objects Maps NMEA strings of sensors to Location objects • Audio Audio – For playing as well as for recording audio For playing as well as for recording audio
  24. Drivers - Code Drivers - Code override fun override fun

    onCreate onCreate() { () { super super.onCreate() .onCreate() inputDriver inputDriver = InputDriver.builder(InputDevice. = InputDriver.builder(InputDevice.SOURCE_CLASS_BUTTON SOURCE_CLASS_BUTTON) ) .setName( .setName(NAME NAME) ) .setVersion( .setVersion(VERSION VERSION) ) .setKeys( .setKeys(intArrayOf intArrayOf(KeyEvent. (KeyEvent.KEYCODE_ESCAPE KEYCODE_ESCAPE)) )) .build() .build() UserDriverManager.getManager().registerInputDriver( UserDriverManager.getManager().registerInputDriver(inputDriver inputDriver) ) } } override fun override fun onDestroy onDestroy() { () { super super.onDestroy() .onDestroy() UserDriverManager.getManager().unregisterInputDriver( UserDriverManager.getManager().unregisterInputDriver(inputDriver inputDriver) ) } } // in your GpioCallback emit the event: // in your GpioCallback emit the event: inputDriver inputDriver.emit .emit(arrayOf(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ESCAPE)))
  25. Drivers - Client Drivers - Client override fun override fun

    onKeyDown onKeyDown(keyCode: Int (keyCode: Int, , event: KeyEvent): Boolean { event: KeyEvent): Boolean { // Handle key pressed and repeated events // Handle key pressed and repeated events return true return true } }
  26. Why Android Things Why Android Things • Loads of devs

    know Android Loads of devs know Android • Also Java / JVM based Also Java / JVM based – Simplifies IoT dev for newcomers, even if unfamiliar with Android Simplifies IoT dev for newcomers, even if unfamiliar with Android – Java was IMHO one important aspect of Android‘s success Java was IMHO one important aspect of Android‘s success • Huge ecosystem around Android (libraries, Android Studio...) Huge ecosystem around Android (libraries, Android Studio...) • Firebase integration Firebase integration
  27. Why Android Things Why Android Things • Security Security –

    Android security architecture Android security architecture – OTA updates by Google even for vendor abandonded devices OTA updates by Google even for vendor abandonded devices • Though the lifespan of this guarantee is not yet defined Though the lifespan of this guarantee is not yet defined
  28. Why Android Things Why Android Things • SoM based SoM

    based – Small module containing CPU, memory, storage and networking Small module containing CPU, memory, storage and networking • Portability across supported SoM architectures Portability across supported SoM architectures – (still: keep your code independent of specifics!) (still: keep your code independent of specifics!)
  29. Some Use Cases Some Use Cases • Agriculture (precision farming,

    humidity and other sensors…) Agriculture (precision farming, humidity and other sensors…) • Sensors and monitoring for wind and solar energy plants Sensors and monitoring for wind and solar energy plants • Financial industries (ATMs, payment and information terminals) Financial industries (ATMs, payment and information terminals) • Logistics / warehouse robotics Logistics / warehouse robotics • Public transport (information systems and NFC-based Public transport (information systems and NFC-based monitoring/ticketing) monitoring/ticketing) • Sensors in production on ships or vehicles of all sorts Sensors in production on ships or vehicles of all sorts – Unless real time requirements are a must [airplanes, trains, safety systems] Unless real time requirements are a must [airplanes, trains, safety systems] • IoT Edge devices (hubs or bridges) IoT Edge devices (hubs or bridges)
  30. When not to use Android Things When not to use

    Android Things • Anything where energy efficiency is a must Anything where energy efficiency is a must • Very ressource sensitive projects Very ressource sensitive projects – CPU, memory and storage requirements of Android Things CPU, memory and storage requirements of Android Things • Real Time Requirements Real Time Requirements • Anything that must run on unsupported architectures Anything that must run on unsupported architectures
  31. Back to Coding Back to Coding • As usual: Starting

    point is an Activity As usual: Starting point is an Activity • From here on it depends From here on it depends – With touchscreen With touchscreen • Nothing changed Nothing changed – Without Without • You only need onCreate() and onDestroy() You only need onCreate() and onDestroy()
  32. onCreate() is the new main() onCreate() is the new main()

    • It‘s just a fancy starting point It‘s just a fancy starting point • You might need the context You might need the context
  33. „ „IoT is all about Data“ IoT is all about

    Data“ Designing Connected Products, O‘Reilly, 2015 Designing Connected Products, O‘Reilly, 2015 • That‘s where the Internet part of IoT comes into play That‘s where the Internet part of IoT comes into play – IoT provides data IoT provides data – IoT reacts to data IoT reacts to data
  34. The The Internet Internet of Things of Things • Google

    ofers two products Google ofers two products – Firebase Firebase – Google Cloud IoT Core (currently in beta) Google Cloud IoT Core (currently in beta)
  35. Firebase with Android Things Firebase with Android Things Note: Firebase

    Auth is available; only user facing parts are not