Amazon IoT 1-Click Devices examples using Amazon CLI - Amazon Command Line Interface
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 1-Click Devices examples using Amazon CLI

The following code examples show you how to perform actions and implement common scenarios by using the Amazon Command Line Interface with Amazon IoT 1-Click Devices.

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 and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, 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 claim-devices-by-claim-code.

Amazon CLI

To claim one or more Amazon IoT 1-Click devices using a claim code

The following claim-devices-by-claim-code example claims the specified Amazon IoT 1-Click device using a claim code (instead of a device ID).

aws iot1click-devices claim-devices-by-claim-code \ --claim-code C-123EXAMPLE

Output:

{ "Total": 9 "ClaimCode": "C-123EXAMPLE" }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

The following code example shows how to use describe-device.

Amazon CLI

To describe a device

The following describe-device example describes the specified device.

aws iot1click-devices describe-device \ --device-id G030PM0123456789

Output:

{ "DeviceDescription": { "Arn": "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789", "Attributes": { "projectRegion": "us-west-2", "projectName": "AnytownDumpsters", "placementName": "customer217", "deviceTemplateName": "empty-dumpster-request" }, "DeviceId": "G030PM0123456789", "Enabled": false, "RemainingLife": 99.9, "Type": "button", "Tags": {} } }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

  • For API details, see DescribeDevice in Amazon CLI Command Reference.

The following code example shows how to use finalize-device-claim.

Amazon CLI

To finalize a claim request for an Amazon IoT 1-Click device using a device ID

The following finalize-device-claim example finalizes a claim request for the specified Amazon IoT 1-Click device using a device ID (instead of a claim code).

aws iot1click-devices finalize-device-claim \ --device-id G030PM0123456789

Output:

{ "State": "CLAIMED" }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

The following code example shows how to use get-device-methods.

Amazon CLI

To list the available methods for a device

The following get-device-methods example lists the available methods for a device.

aws iot1click-devices get-device-methods \ --device-id G030PM0123456789

Output:

{ "DeviceMethods": [ { "MethodName": "getDeviceHealthParameters" }, { "MethodName": "setDeviceHealthMonitorCallback" }, { "MethodName": "getDeviceHealthMonitorCallback" }, { "MethodName": "setOnClickCallback" }, { "MethodName": "getOnClickCallback" } ] }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

The following code example shows how to use initiate-device-claim.

Amazon CLI

To initiate a claim request for an Amazon IoT 1-Click device using a device ID

The following initiate-device-claim example initiates a claim request for the specified Amazon IoT 1-Click device using a device ID (instead of a claim code).

aws iot1click-devices initiate-device-claim \ --device-id G030PM0123456789

Output:

{ "State": "CLAIM_INITIATED" }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

The following code example shows how to use invoke-device-method.

Amazon CLI

To invoke a device method on a device

The following invoke-device-method example invokes the specified method on a device.

aws iot1click-devices invoke-device-method \ --cli-input-json file://invoke-device-method.json

Contents of invoke-device-method.json:

{ "DeviceId": "G030PM0123456789", "DeviceMethod": { "DeviceType": "device", "MethodName": "getDeviceHealthParameters" } }

Output:

{ "DeviceMethodResponse": "{\"remainingLife\": 99.8}" }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

The following code example shows how to use list-device-events.

Amazon CLI

To list a device's events for a specified time range

The following list-device-events example lists the specified device's events for the specified time range.

aws iot1click-devices list-device-events \ --device-id G030PM0123456789 \ --from-time-stamp 2019-07-17T15:45:12.880Z --to-time-stamp 2019-07-19T15:45:12.880Z

Output:

{ "Events": [ { "Device": { "Attributes": {}, "DeviceId": "G030PM0123456789", "Type": "button" }, "StdEvent": "{\"clickType\": \"SINGLE\", \"reportedTime\": \"2019-07-18T23:47:55.015Z\", \"certificateId\": \"fe8798a6c97c62ef8756b80eeefdcf2280f3352f82faa8080c74cc4f4a4d1811\", \"remainingLife\": 99.85000000000001, \"testMode\": false}" }, { "Device": { "Attributes": {}, "DeviceId": "G030PM0123456789", "Type": "button" }, "StdEvent": "{\"clickType\": \"DOUBLE\", \"reportedTime\": \"2019-07-19T00:14:41.353Z\", \"certificateId\": \"fe8798a6c97c62ef8756b80eeefdcf2280f3352f82faa8080c74cc4f4a4d1811\", \"remainingLife\": 99.8, \"testMode\": false}" } ] }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

The following code example shows how to use list-devices.

Amazon CLI

To list the devices of a specified type

The following list-devices example lists the devices of a specified type.

aws iot1click-devices list-devices \ --device-type button

This command produces no output.

Output:

{ "Devices": [ { "remainingLife": 99.9, "attributes": { "arn": "arn:aws:iot1click:us-west-2:123456789012:devices/G030PM0123456789", "type": "button", "deviceId": "G030PM0123456789", "enabled": false } } ] }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

  • For API details, see ListDevices in Amazon CLI Command Reference.

The following code example shows how to use list-tags-for-resource.

Amazon CLI

To list the tags for a device

The following list-tags-for-resource example list the tags for the specified device.

aws iot1click-devices list-tags-for-resource \ --resource-arn "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789"

Output:

{ "Tags": { "Driver Phone": "123-555-0199", "Driver": "Jorge Souza" } }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

The following code example shows how to use tag-resource.

Amazon CLI

To add tags to a device Amazon resource

The following tag-resource example adds two tags to the specified resource.

aws iot1click-devices tag-resource \ --cli-input-json file://devices-tag-resource.json

Contents of devices-tag-resource.json:

{ "ResourceArn": "arn:aws:iot1click:us-west-2:123456789012:devices/G030PM0123456789", "Tags": { "Driver": "Jorge Souza", "Driver Phone": "123-555-0199" } }

This command produces no output.

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

  • For API details, see TagResource in Amazon CLI Command Reference.

The following code example shows how to use unclaim-device.

Amazon CLI

To unclaim (deregister) a device from your Amazon account

The following unclaim-device example unclaims (deregisters) the specified device from your Amazon account.

aws iot1click-devices unclaim-device \ --device-id G030PM0123456789

Output:

{ "State": "UNCLAIMED" }

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

  • For API details, see UnclaimDevice in Amazon CLI Command Reference.

The following code example shows how to use untag-resource.

Amazon CLI

To remove tags from a device Amazon resource

The following untag-resource example removes the tags with the names Driver Phone and Driver from the specified device resource.

aws iot1click-devices untag-resource \ --resource-arn "arn:aws:iot1click:us-west-2:123456789012:projects/AnytownDumpsters" \ --tag-keys "Driver Phone" "Driver"

This command produces no output.

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.

  • For API details, see UntagResource in Amazon CLI Command Reference.

The following code example shows how to use update-device-state.

Amazon CLI

To update the ``enabled`` state for a device

The following update-device-state sets the state of the specified device to enabled.

aws iot1click-devices update-device-state \ --device-id G030PM0123456789 \ --enabled

This command produces no output.

For more information, see Using Amazon IoT 1-Click with the Amazon CLI in the Amazon IoT 1-Click Developer Guide.