Thursday, October 11, 2018

Android Things Customer Library For Google Cloud Iot Core

Posted past times Wayne Piekarski, Developer Advocate for IoT +WaynePiekarski @WaynePiekarski

We're releasing a customer library to arrive tardily to utilization Google Cloud IoT Core from Android Things devices. With exactly a few lines of code, you lot tin dismiss easily connect to the IoT Core MQTT bridge, authenticate the device, pose out device telemetry as well as state, subscribe to configuration changes, as well as own got errors as well as network outages.

What is Cloud IoT Core?

Cloud IoT Core is a fully managed service on Google Cloud Platform that allows you lot to easily as well as securely connect, manage, as well as ingest information from millions of globally dispersed devices. Cloud IoT Core, inwards combination alongside other services which brand upwards Google's Cloud IoT platform, provides a consummate solution for collecting, processing, analyzing, as well as visualizing IoT information inwards existent time, to back upwards improved operational efficiency, compliance, or revenue management. Android Things is designed to back upwards everything from collecting telemetry information to powerful figurer vision, good processing, as well as machine learning applications, all on device, as well as using Cloud IoT Core, force your information into Google Cloud Platform for farther analysis.

Cloud IoT Core customer library

The Cloud IoT Core customer library was designed to enable Android Things developers to acquire started alongside exactly a few lines of code. The customer library handles the networking, threading, as well as message handling, implementing best practices for authentication, security, mistake handling, as well as offline operation.

Cloud IoT Core maintains a device registry that keeps rails of approved devices, as well as each device uses a world key to authenticate alongside the server. Android Things provides many features to back upwards secure IoT applications, including a hardware-backed Android Keystore that ensures cryptographic key cloth is protected. The customer library supports both RSA as well as ECC keys, as well as implements the generation of JSON Web Tokens (JWTs) for authentication alongside Cloud IoT Core.

Once the connectedness is established, devices tin dismiss pose out their telemetry information to ane or to a greater extent than buckets inwards the telemetry topic, equally good equally study their internal nation to a split upwards device nation topic. The device nation is intended to shop information such equally software versions or the number of working sensors. The telemetry messages are for all other information from the device, such equally actual sensor measurements. Devices tin dismiss likewise subscribe to configuration changes published from Cloud IoT Core.

Because IoT devices operate inwards the existent Blue Planet alongside miserable wireless conditions, the customer library provides extensive back upwards for treatment errors, as well as for caching as well as retransmitting events later. For developers requiring custom offline behavior, the library's queue is configurable as well as fifty-fifty replaceable. This provides detailed command over which events to relieve as well as the companionship inwards which they are sent when dorsum online.

Device provisioning as well as authentication alongside Android Things

The Cloud IoT Core customer library is utilization of our overall vision for device provisioning as well as authentication alongside Android Things. To larn to a greater extent than nearly this, sentry the video of our presentation from Google I/O 2018:

Sample code

Getting started alongside the Cloud IoT Core customer library is simple. You tin dismiss only add together the next to the build.gradle file inwards your Android Things project:

implementation 'com.google.android.things:cloud-iot-core:1.0.0' 

The library is likewise available equally opened upwards source on GitHub if you lot prefer to construct it yourself. We likewise accept a sample that shows how to implement a sensor hub on Android Things, collecting sensor information from connected sensors as well as publishing them to a Google Cloud IoT Pub/Sub topic.

It is tardily to inaugural of all using the customer library inwards your ain code. The next Kotlin lawsuit demonstrates how to do a novel configuration as well as customer based on your project.

var configuration = IotCoreConfiguration.Builder().                          .setProjectId("my-gcp-project")                          .setRegistry("my-device-registry", "us-central1")                          .setDeviceId("my-device-id")                          .setKeyPair(keyPairObject)                          .build()  var iotCoreClient = IotCoreClient.Builder()               .setIotCoreConfiguration(configuration)               .setOnConfigurationListener(onConfigurationListener)               .setConnectionCallback(connectionCallback)               .build()  iotCoreClient.connect() 

Next, you lot tin dismiss pose out telemetry information or device state, using the next Kotlin examples.

private fun publishTelemetry(temperature: Float, humidity: Float) {     // payload is an arbitrary, application-specific array of bytes     val examplePayload = """{         |"temperature" : $temperature,         |"humidity": $humidity         |}""".trimMargin().toByteArray()     val number = TelemetryEvent(examplePayload, topicSubpath, TelemetryEvent.QOS_AT_LEAST_ONCE)     iotCoreClient.publishTelemetry(event) }  someone fun publishDeviceState(telemetryFrequency: Int, enabledSensors: Array<String>) {     // payload is an arbitrary, application-specific array of bytes     val examplePayload = """{         |"telemetryFrequency": $telemetryFrequency,         |"enabledSensors": ${enabledSensors.contentToString()}         |}""".trimMargin().toByteArray()     iotCoreClient.publishDeviceState(examplePayload) } 

Additional resources

You tin dismiss larn to a greater extent than nearly edifice for Android Things at the developer site. For to a greater extent than information nearly getting started alongside Cloud IoT Core, catch the information page as well as documentation. Finally, bring together Google's IoT Developers Community on Google+ to allow us know what you're edifice alongside Android Things as well as Cloud IoT Core!

Related Post

Android Things Customer Library For Google Cloud Iot Core
4/ 5
Oleh