Posted by Wayne Piekarski, Developer Advocate for IoT +WaynePiekarski @WaynePiekarski
We're releasing a client library to make it easy to use Google Cloud IoT Core from Android Things devices. With just a few lines of code, you can easily connect to the IoT Core MQTT bridge, authenticate the device, publish device telemetry and state, subscribe to configuration changes, and handle errors and network outages.
What is Cloud IoT Core?
Cloud IoT Core is a fully managed service on Google Cloud Platform that allows you to easily and securely connect, manage, and ingest data from millions of globally dispersed devices. Cloud IoT Core, in combination with other services which make up Google's Cloud IoT platform, provides a complete solution for collecting, processing, analyzing, and visualizing IoT data in real time, to support improved operational efficiency, compliance, or revenue management. Android Things is designed to support everything from collecting telemetry data to powerful computer vision, audio processing, and machine learning applications, all on device, and using Cloud IoT Core, push your data into Google Cloud Platform for further analysis.
Cloud IoT Core client library
The Cloud IoT Core client library was designed to enable Android Things developers to get started with just a few lines of code. The client library handles the networking, threading, and message handling, implementing best practices for authentication, security, error handling, and offline operation.
Cloud IoT Core maintains a device registry that keeps track of approved devices, and each device uses a public key to authenticate with the server. Android Things provides many features to support secure IoT applications, including a hardware-backed Android Keystore that ensures cryptographic key material is protected. The client library supports both RSA and ECC keys, and implements the generation of JSON Web Tokens (JWTs) for authentication with Cloud IoT Core.
Once the connection is established, devices can publish their telemetry data to one or more buckets in the telemetry topic, as well as report their internal state to a separate device state topic. The device state is intended to store information such as software versions or the number of working sensors. The telemetry messages are for all other data from the device, such as actual sensor measurements. Devices can also subscribe to configuration changes published from Cloud IoT Core.
Because IoT devices operate in the real world with poor wireless conditions, the client library provides extensive support for handling errors, and for caching and retransmitting events later. For developers requiring custom offline behavior, the library's queue is configurable and even replaceable. This provides detailed control over which events to save and the order in which they are sent when back online.
Device provisioning and authentication with Android Things
The Cloud IoT Core client library is part of our overall vision for device provisioning and authentication with Android Things. To learn more about this, watch the video of our presentation from Google I/O 2018:
Sample code
Getting started with the Cloud IoT Core client library is simple. You can simply add the following to the build.gradle file in your Android Things project:
implementation 'com.google.android.things:cloud-iot-core:1.0.0'
The library is also available as open source on GitHub if you prefer to build it yourself. We also have a sample that shows how to implement a sensor hub on Android Things, collecting sensor data from connected sensors and publishing them to a Google Cloud IoT Pub/Sub topic.
It is easy to start using the client library in your own code. The following Kotlin example demonstrates how to create a new configuration and client 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 can publish telemetry information or device state, using the following 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 event = TelemetryEvent(examplePayload, topicSubpath, TelemetryEvent.QOS_AT_LEAST_ONCE)
iotCoreClient.publishTelemetry(event)
}
private 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 can learn more about building for Android Things at the developer site. For more information about getting started with Cloud IoT Core, visit the information page and documentation. Finally, join Google's IoT Developers Community on Google+ to let us know what you're building with Android Things and Cloud IoT Core!
No comments:
Post a Comment