Amazon IoT data examples using SDK for Kotlin - Amazon SDK for Kotlin
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon IoT data examples using SDK for Kotlin

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Kotlin with Amazon IoT data.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use GetThingShadow.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun getPayload(thingNameVal: String?) { val getThingShadowRequest = GetThingShadowRequest { thingName = thingNameVal } IotDataPlaneClient { region = "us-east-1" }.use { iotPlaneClient -> val getThingShadowResponse = iotPlaneClient.getThingShadow(getThingShadowRequest) val payload = getThingShadowResponse.payload val payloadString = payload?.let { java.lang.String(it, Charsets.UTF_8) } println("Received shadow data: $payloadString") } }
  • For API details, see GetThingShadow in Amazon SDK for Kotlin API reference.

The following code example shows how to use UpdateThingShadow.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun updateShawdowThing(thingNameVal: String?) { // Create the thing shadow state document. val stateDocument = "{\"state\":{\"reported\":{\"temperature\":25, \"humidity\":50}}}" val byteStream: ByteStream = ByteStream.fromString(stateDocument) val byteArray: ByteArray = byteStream.toByteArray() val updateThingShadowRequest = UpdateThingShadowRequest { thingName = thingNameVal payload = byteArray } IotDataPlaneClient { region = "us-east-1" }.use { iotPlaneClient -> iotPlaneClient.updateThingShadow(updateThingShadowRequest) println("The thing shadow was updated successfully.") } }