Building Production IoT Devices with Android Things (GDD India '17)



all right so welcome to day two I hope you're having a great time here so my name is Wayne Piekarski and today I'm gonna be talking about how to build production devices for Android things and so Android things is you know it's designed for making IOT devices we've got a big sandbox area ever in the room that you can go and check out our demo so definitely come and see us later on and you know we're here because we want to both educate you about what we're doing but also we want to find out more about what kinds of cool projects you want to build we want to be able to answer your questions and so forth so definitely come by and see us check out the demos talk to us and you know that's what we're here for so you see me walking around at lunch definitely come and say hi so if you've ever dreamed of making your own IOT devices then Android things is for you and today I'm gonna teach you how to go about doing that so you can make your own exciting ideas so what is Android things so Android things is an extension of the Android platform that allows you to build IOT devices so many of you are familiar with Android on phones Android on tablets we have Android wear devices we have Android and cars and now we have Android for building IOT devices and so it's nice because you've already got your existing developer experience you know many of you in the room here are already Android developers and so you can take advantage of your existing software stack to support your development also security is really hard and a lot of IOT device makers aren't doing security properly and so it's really hard to get it right from from the beginning and do it perfectly so Android things is there to support that to help you build really secure IOT applications and finally taking an IOT device and scaling it to large production runs is also very challenging as well and so while it's possible to make easy prototypes Android things helps you to get that product all the way to market and we're going to go through the steps as to how that's done so with Android things you get best-in-class multimedia connectivity Wi-Fi you know rich user interfaces and it helps you to accelerate the development of IOT application so if you want to build any like this there's definitely something you should be checking out so what can use Android things for so there's many things you can use it for but really it's ideal for powerful and intelligent devices so it's not just about little things that support switches and lights and whatever but it's about really complex applications so things like audio processing video processing doing on device machine learning these were the kinds of cool things that you can build with Android things and there's many different use cases you've got Enterprise like big companies you've got businesses and then you've got consumer devices that people going to use at home IOT is going to be a big thing and if you saw my talk yesterday I talked about how you know we had desktops and then laptops and then phones IOT is going to be bigger than even though so it's definitely something that you want to be a part of and so Android things is really good at applications that need high like large amounts of computation and Android things device is basically a phone without a screen so this is not a tiny microcontroller it's very powerful and you can communicate with the cloud there's all kinds of cool things you can do with it so let's get started so making hardware so IOT devices involves some kind of equipment that you're gonna have to give to a customer of some kind so it's really hard to make good electronics and so the reason is that when you're making a board like this so this is a little screen shot of a Raspberry Pi here the circuit board of it if you look at the actual board itself where that red arrow is you'll see there are these little squiggly tracks there and it turns out that when you're designing high-speed electronics you have to do a really good job about making sure that all the electrons travel at the speed of light correctly and that they all arrive at the same time and so doing this is really tricky it requires very highly skilled engineers who know how to design electronics and you know I'm a software person I don't make hardware so I have no idea how this stuff is done and so you know we want to try to make that easy so it requires very skilled designers to make really nice electronics but we're going to show you how we help you to do that also when you're making electronics it requires you to buy large quantities to be able to get good prices if you go to an electronics company and say I want to make ten of these devices they'll charge you like a thousand bucks each but if you want to buy a million then you can get the price down to a couple of dollars and so that's one of the other catches it's not like software that you can make one or a million copies for free hardware it costs money to make them and sometimes there are fixed costs and then finally you've got a lot of lead time when you make electronics you have to place the order for the parts wait for them to arrive you have to make things assemble it it takes a long period of time to get this right and if you make a mistake you can't just ship an update out like you do for software so how do it's quite tricky to do but we've got some solutions to making that easier and finally software which we thought was a solved problem it turns out that's tricky too so everyone's building these custom IOT devices with their own handmade Linux distributions but they don't really have a strategy for security that I have a strategy for sending updates out to people and this is becoming really critical for IOT devices because now there are these vulnerable insecure devices that are out there in the wild that hackers are breaking into and it's becoming a problem for companies who make these devices because well your device was used by someone to break into something so you know you're potentially on the hook for that so as a device maker now there's more and more incentives for you to do this right and so Android things is going to help you to do security and updates and all those things properly so that you can make devices that your consumers can feel confident with knowing that's made asbestos can be done right now and finally you don't really want to have to hire a huge number of engineers on your team whose job is to maintain Linux kernels and security updates and keep up to date with the latest patches for open SSL or something like that we take care of all that for you and that's a really big thing here so we support the ability to make easy prototypes but we also support the ability to then take that same design and take it all the way to the production and that's what we're going to talk about for the rest of this talk so firstly the design of Android things it's not just based on a like a single board computer it's based on this concept called a song and if you look at the bottom on that slide that on the slide there you can see there's a small little board it's about two centimeters by two centimeters it's very tiny but it contains the core computing pieces that are needed so the CPU the memory the Wi-Fi chip and fourth and you attach this board to a larger breakout device during development so at the top one there and that has all the breakout connectors that you use to attach things to and that's for testing and so when you're ready to move into production you take the developer board away and you make your own custom board that has just the components that you need and this is nice because the custom board that you make only has the pieces you want you only pay for the parts you need and you don't need to pay for this huge development board that you're not using any other parts for it and the nice part is that the Psalms the small computing modules they're made by someone else they're made in large quantities so you can buy them at good prices and then the boards that you need for yourself you can do a run and because they use low speed electronics that's not as hard as the design I showed previously low speed electronics are a lot cheaper to make and it's a lot easier for a lot of electronics designers to build for you so you can focus on that part and it makes it easy for you and then finally we have what's called a BSP or a board Support Package and so the board Support Package is software from Google that makes that song and that board of yours work by running Android things on it and so we provide the kernel the libraries necessary drivers and everything to make it work and as long as you stick to the design of the somme then everything will work for you and so it really sort of helps you to focus on you make your board and then we take care of everything else for you the other thing that's nice about Psalms is that you don't need to worry so much about product certification and testing so traditionally when you design a board you have to get the entire board certified and tested there are regulatory agencies like in the US it's called underwriters laboratory they do testing or FCC certification and doing all that certification is very expensive and it's very time-consuming however if your design is based around the psalm the psalm has already been certified and so it makes the certification process for the rest of your hardware a lot cheaper quicker and easier so it makes things easier for you and you can get your product to market quicker so with the way we've designed Android things it's a little different than the way people have traditionally like used a OS p to build their own custom Android devices what we do is we've broken Android things down into two parts there's the part that's managed by Google so we compile that we build it so it's things like the android framework the hardware library is the Linux kernel we build that as and we we give you binaries for that and they're distributed as part of the board Support Package and then the second part is managed by you as a developer and so you build apks like you build an android studio and you write what are called apps and you write user drivers and you put them onto the device and that's the part that you control and we've separated them so that the part that's managed by google has all the right permissions to do everything but you're a part that you run as a developer let's actually run with restricted permissions you have to use the Android API is to communicate with everything which helps to isolate your code off from the rest of the system so therefore any security problems it tends to keep them compartmentalized so the whole system won't get attacked if your app has a problem so that's one of the nice things about this separation is we handle certain parts you handle certain parts and you can focus on the parts that are important to you so what happens is that when you upload an apk that you've built you you upload it to a developer console and it's combined with the firmware image from Google and we build a single signed firmware image and this signed firmer image is then distributed to the devices that are your products and so we sign and verify the images so we sign them on the server and then we verified them when the images arrive on the device and we do this to make sure that the images haven't been tampered with during transmission to the user because we don't want a rogue third party changing the firmware on the way to the device they're also protected from corruption so that if the images are corrupted during transit then of course they'll be verified that way but also when the device boots we check to make sure it hasn't been tampered with or damaged on the device as well because you don't want to boot an image that's been corrupted or tampered in some way the next thing is we provide a be update rollback protection so when a new update arrives on the device we don't just clobber over the firmware image we actually staged it as a second image we put everything in place then we reboot the device and switch over to the other image now if for any reason when we boot this new image if it fails the system will then detect that and it will then roll back to the previous version and that way if a corrupt build is pushed or if you make a bug or something that causes the device not to boot it'll roll back so that the device will still be able to get future updates and so you won't brick the device even somehow you make a mistake with your updates this is a really nice feature because it's something that's really hard to build yourself we provide all that for you and then finally we provide automatic security updates so Android has a huge team of security engineers whose job is to look for vulnerabilities in Linux kernel and SSL libraries and all the other different pieces that make up the android framework and you get the benefits of having all of those engineers working for you so we make the updates we then push them out to your devices and so that you don't have to worry about keeping track of all these little things in the past if you were doing builds from AOSP you would have to merge the patches in yourself and then build your own images and then push them out this is very time consuming hard to do and so we're really trying to make that frictionless and easy for you and we take care of it all and then finally we provide the OTA mechanism to get the software updates out to your devices so the images are signed built on our server we then push them out and it uses a system that we've built for ourselves and it works very well it's very reliable and very easy to use so you don't have to worry about it you can focus on building your applications so let's look at some of the more technical things about how to build friend rude things so the first part is that this is like a diagram here that shows the traditional android framework model and so there's all the different parts that form the system we've kept most of them as part of Android things but what we did was is we remove some of the core pieces that aren't needed so for example an Android things device it's not a phone and it can't send text messages so we've removed all of those different pieces like wallpapers and notifications you don't need any of that stuff on an IOT device so we've taken all that away because we want to save space make the system boot quickly but we've provided all of the rest of the android framework and bits and pieces that you're used to dealing with that's all available for you to use when you're building your IOT devices and also you know the UI libraries that you're familiar with in Android studio for like laying out nice UIs that's all available to so Android things supports UI but displays are actually optional now so I always think about Android things as like a phone but without the screen on it but you can have a screen if you want one so different IOT devices some of them are embedded into different things or some of them have displays you can choose what you want so you don't have to have a screen but you don't not have to have one either you have the same user interface libraries that used to with Android and so it's based on activities and services just like you're familiar with for writing Android apps right now so all of the same things that you understand the same concepts are there for you as well and one little thing to be aware of is that you still need to have a main activity because the main activity is responsible for handling keyboard and mouse events and so forth so you still need something to be handling that because screens are optional there are times when if you need to request permission for example we don't pop up a permission dialog box on the screen because it might not be possible for the user to hit OK on it so Android things automatically grants permissions to different things so if you've got code that you've ported over from an Android phone it still works and the permissions automatically granted so you don't have to hit the ok button on it and then finally when you're building an Android things IOT app you're not necessarily going to be using a touchscreen anymore but you need to think about what kinds of alternative input device modalities do you want to use so now you have the ability to build wearable devices you could have a microphone you could have a camera you could have a GPS sensor or a compass and there's all kinds of different input modalities that you could consider as part of building your device so don't just lock yourself into using a touchscreen you could use any kind of input device and so there's pretty much all the options that you could think of or available and the Android SDK through the frameworks so things like sensors and so forth I read in exactly the same way as you do on a phone and then also you get to develop with the power of Android so you're using the same Android SDK that everyone uses for phone development you're using Android studio and you get to use any language supported by an art studio so you can use Kotlin for programming so if you're new to if you're excited about : you can use it on android things as well you have Google Play services with all parts that it provides firebase and tensorflow and Google Cloud IOT and you can embed the Google assistant into your device so all of these things that Google provides for phones are now available for you on a ot device as well so I've had a lot of IOT developers come up to me and so they're really excited about being able to put tensorflow into devices like this this is something that haven't been able to do before so one of the really cool things have been Android things is that you can now do machine learning on device you don't need the cloud you don't need Wi-Fi and you can do it on the device and that's something a very low-end microcontroller can't do so it's definitely a big win for Android things and as I said it really helps you to all of these tools and libraries that you're familiar with it's all available to you so the most important part of an editor things is adding hardware to it and so let's go through some examples of the api's that are available for you to go about doing this and so when you look at the developer board this is a Raspberry Pi here we actually support NXP devices which some of you have received a kit that you can play with at home so we support nxp devices we also support raspberry PI's right now and so this board here it shows you some of the different connections that are available so we support connecting things by USB so you can plug mice keyboards things like that in we support speakers microphones going in via the audio jack or the USB port there's an Ethernet connector there's Wi-Fi there's different HDMI outputs and connectors for cameras we support all of these things on the module so whatever you want to use to connect stuff up you can use that for whatever your application is and every developer kit also has a break out header pin so this is the connector pin diagram for the Raspberry Pi so you can see that there's all kinds of connectors which we'll talk about in a minute but every single pin that's available on there we have API is allow you to talk to that and so you can add additional devices to these lines here and then you can integrate them in whatever way you want so there's a whole bunch of different interfacing protocols exposed by that connector there so we support general purpose i/o we support pulse width modulation we support I to see buses I to s SPI and you arts so these are all different connection technologies that hardware designers use to interface the different devices so if you've done hardware design before you'll seen these but these are important because they're very cheap and high-speed ways of getting things connected to your devices that's easy to interact with directly and we're going to go through some more examples of these later and then also we have support for what it's called user drivers where we can add to the android framework and we can take sensor data we can take input devices and we can actually inject those events into the android framework so that they can be read and used by applications using the same sensor framework so if you've got a sensor that connects via I to s we can inject it into the sensor framework so that the code that uses it just sees a regular android sensor just like they would any other sensor that would be connected on a phone so here are some examples that show how different kinds of things would be connected so if we had a motor that was connected up via PWM that is something that would talk directly to your app if you had a sensor you can see that the example I talked about before it connects Phii to see it then has a user sensor driver it then puts it through the sensor manager into the framework and then if you had a GPS device you would connect it via a serial port or UART you would then write a GPS driver around it that generates framework events and then it goes through location manager into the app and actually an interesting thing is that Google Play Services has something called fused location provider that a lot of people like to use because it improves the quality of their GPS data because these events are being injected directly into the framework it means that Google Play services can use the fuse location provider to help enhance those as well so that's one of the really cool spin-offs of the user user driver framework so what kind of hardware can unify swith we talked about some concepts like GPIO and so forth I'm just going to show you some quick examples so GPIO is basically binary 1 and 0 signals and they tend to be like 0 to 5 volts or 0 to 3.3 volts and they have low and high values and they're done they're used by things like buttons when they're inputting into your device but you can also Drive LEDs so make lights turn on and off rotary switches a little sensor devices they all use just simple signals coming in and Android things can read those for you we support was called PWM the nice thing about pulse width relation is you use varying use signals with varying modulation widths and you can use it to control the intensity of an LED where you can use it to move a servo motor around we have native support for PWM and the boards have actual PWM Hardware onboard that can generate these signals very reliably so they're useful for motor controlling and things where accuracy is very important we have UART support which are also called serial ports or rs-232 so you can connect them up and a lot of devices like GPS units and so forth they use you art-style inputs so all the Android things devices all have at least one serial port connector on it and you can connect more of our USB we also have SPI and i2c there are companies that make dedicated little boards with sensors on them and so forth that talk via these high-speed serial buses so these are useful for times when rs-232 isn't fast enough and so you can connect all kinds of sensors directly in Android things and we handle those directly so that sensors now how do we go about building activities within Android studio that run on Android things device so everything in androids in Android things is based around an apk just like you would build for an Android phone so traditionally you would plug your phone into your laptop you would hit the run icon in Android studio and it puts the apk on your device you unplug it you plug in your Android things device and it works in exactly the same way so you get to use exactly the same Android studio tools and you just plug it in it works over ADB and it pretty much just works there's a few little differences you need to be aware of though so the first thing you do is in your build.gradle you need to include our Android things support library it has a few extra things that are not part of the standard android framework such as the peripheral io api and it brings those dependencies in so that you can use them in your code the next thing you need to do is on the bottom there you can see we've got some androidmanifest.xml entries you need to add an IOT launcher intent flag and an intent filter and we do that to tell Android things that that activity that's tagged with an IFC launcher is the one that we want to start up at boot so the device boots up it finds the first IOT launcher it starts it up and then that runs on the device from that point and you should only have one activity on the device with an IOT launcher if you have to then only one of them will start up but it's the way of you flagging which activity you want to run on startup and that's going to be the one that sets up all your devices and gets things going at the end of the day it's a regular Android apk and in many cases I've taken small toy examples of mine and I've run them on an Android thanks kit it just works so a lot of things that used to for my Android you can just easily do without changing anything so if we want to add peripheral i/o so we talked about GPIO so let's say we want to handle button presses from a switch that's connected to the device so we wire the switch up to the pin on the GPIO pin on the device and this is the code that we then use to do it so it's very similar to handling a button in an Android UI except now we're handling real physical buttons so we use what's called the peripheral manager service we create one of those we then call open GPIO with the name of the pin we then call some methods that set the directions we want to do input it's very important you specify the correct direction because you can't read on an output pin and vice versa we then also specify edge falling the purpose of edge falling is to say I want this code to trigger whenever the signal drops from high to low but not from low to high so it only goes on the falling edge you can specify rising and also both depending on what your application is then you register a callback and you can see we have an on GPIO edge meth met an NG pio edge method and that method will be called whenever the signal drops and so that's it I mean you've got a couple lines of code here most of its kind of boilerplate that just prints something out when a button is pressed and then you can do whatever you want once you've received that notification and then you can do different things in your code depending what you want to do and all of the peripheral IO devices like spi i2c all that they're all implemented with a very simple similar API as well so it's very easy to use if any of you have done embedded Linux before you have to go through the proc and sis FS file systems looking for different files to Twitter this is a lot easier and it's handled by a dedicated service that's isolated from your process so that it helps for security as well so we talked about drivers in user drivers we also have a peripheral driver library that we've written so some of the engineers on my team as well as people out in public have been writing drivers for different devices that plug into Android things so on github we publish all of these drivers so they're available for you so many common senses and devices and so forth the drivers are already available so you don't even need to write them yourself so we've handled a lot of these things reading the data sheets implementing a code for it so you don't have to worry about it so definitely check out that driver library if you want to find examples of code that makes it easy for you so you don't have to worry about reading these data sheets so in summary Android things really helps to provide the power of Android to help you make your applications you know help you make them easier and faster than you could have done before you don't have to learn a new tool chain you can use all your existing experience and it really is something that you can get started quickly so if you're an Android developer you can take one of our kids you can go home and you can have something working in a couple hours you can really get started quickly so it's definitely something I encourage you to check out the hardware and software is managed by Google so these Psalms are we work closely with the semiconductor partners to make the Psalms we help specify what the specifications will be and then we support them with kernels drivers libraries all bundled up into a binary that we then combine with your apk to ship to devices and because we handle all that you don't need to worry about handling all these kernel changes and patches and so forth we take care of all that for you and then finally with the help of our developer console where you upload your apks to and push them to the devices we handle all of the updates and all of the security and everything for you so that you can focus on building your application there's a lot of complex things involved with you know updating devices and making everything work we have some of the most amazing engineers I've ever worked with that talk to me about security about Android things and it's just amazing the level of detail and care they've put into the design of the system so it's very nice so as a developer you don't need to worry about these things you can focus on making your app and what makes your product great so what's next so as I mentioned before we have a sandbox area outside where across the street there where you can check out our demos and come chat to us and definitely take advantage of that while we're here and then also later on you're going to have questions we have some communities online where you can go to ask questions so on G+ we have an IOT developers community we also have hackster IO which is a place where you can post your projects that you've worked on and that's a great place to serve find other ideas of what people who are working on and you know you can look at their code and read their documentation this is a great place to sort of see fully built little ideas that you can study and then also we have the Android things documentation itself and develop render a TOCOM slash things and then we have a whole bunch of samples on github that you can look at that are sort of show best practices in how to do different things involved with the Android things kits and so also I do a lot of posting on Twitter and Google+ so definitely follow me on social media so I'm a developer advocate at Google where I talk about IOT the Google assistant and also the assistants DK and so definitely follow me there if you want to keep up to date with all the new things that are happening we're always doing new things like for example yesterday we actually launched Android things Developer Preview six so the latest Developer Preview has a whole bunch of fixes for previous issues but it also it added a lot of things for like making fast UI as possible and it supports cameras and you can run a lot of the Android samples so Google Maps now works on it it has the latest version of Google Play services so definitely check out developer preview 6 which just launched yesterday and that has a lot in there you should try out so that's it for today so thank you very much for coming and I look forward to seeing you in the sandbox thank you [Applause]

One Comment

  1. Vinoth M said:

    How connect mx7d board to firebase

    June 25, 2019
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *