Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

SoMs, SoCs and what not SoMs, SoCs and what not

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Breakout Boards Breakout Boards

Slide 7

Slide 7 text

Android Things Android Things

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Android Things Android Things

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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 :-)

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

AndroidManifest.xml AndroidManifest.xml /> > /> />

Slide 14

Slide 14 text

build.xml build.xml dependencies { dependencies { //... //... compileOnly compileOnly 'com.google.android.things:androidthings:0.6-devpreview' 'com.google.android.things:androidthings:0.6-devpreview' //… } }

Slide 15

Slide 15 text

Supported Protocols Supported Protocols ● GPIO GPIO ● PWM PWM ● I²C I²C ● I²S I²S ● SPI SPI ● UART UART

Slide 16

Slide 16 text

GPIO GPIO

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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) )

Slide 19

Slide 19 text

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) } } } }

Slide 20

Slide 20 text

PWM PWM

Slide 21

Slide 21 text

PWM PWM

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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) ); ;

Slide 24

Slide 24 text

UART UART ● Serial transmission (medium speed) Serial transmission (medium speed) ● Full duplex Full duplex

Slide 25

Slide 25 text

UART UART

Slide 26

Slide 26 text

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)

Slide 27

Slide 27 text

I²C I²C

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

I²C I²C

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

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) } }

Slide 32

Slide 32 text

SPI SPI

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

SPI SPI

Slide 35

Slide 35 text

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 } }

Slide 36

Slide 36 text

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) ) } }

Slide 37

Slide 37 text

Drivers Drivers

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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)))

Slide 41

Slide 41 text

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 } }

Slide 42

Slide 42 text

Why Android Things Why Android Things

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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!)

Slide 46

Slide 46 text

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)

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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()

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

„ „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

Slide 51

Slide 51 text

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)

Slide 52

Slide 52 text

Firebase with Android Things Firebase with Android Things Note: Firebase Auth is available; only user facing parts are not

Slide 53

Slide 53 text

Cloud IoT Core Cloud IoT Core

Slide 54

Slide 54 text

Q & A Q & A www.grokkingandroid.com www.wolfram-rittmeyer.de twitter.com/RittmeyerW