Uso de CreateThing con un SDK de AWS o la CLI - AWS IoT Core

Uso de CreateThing con un SDK de AWS o la CLI

Los siguientes ejemplos de código muestran cómo utilizar CreateThing.

C++
SDK para C++
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

//! Create an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateThingRequest createThingRequest; createThingRequest.SetThingName(thingName); Aws::IoT::Model::CreateThingOutcome outcome = iotClient.CreateThing( createThingRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created thing " << thingName << std::endl; } else { std::cerr << "Failed to create thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • Para obtener detalles sobre la API, consulte CreateThing en la Referencia de la API de AWS SDK para C++.

CLI
AWS CLI

Ejemplo 1: creación de un registro de objetos en el registro

En el siguiente ejemplo de create-thing se crea una entrada para un dispositivo en el registro de objetos de AWS IoT.

aws iot create-thing \ --thing-name SampleIoTThing

Salida:

{ "thingName": "SampleIoTThing", "thingArn": "arn:aws:iot:us-west-2: 123456789012:thing/SampleIoTThing", "thingId": " EXAMPLE1-90ab-cdef-fedc-ba987EXAMPLE " }

Ejemplo 2: definición de un objeto que está asociado a un tipo de objeto

En el siguiente ejemplo de create-thing se crea un objeto que tiene el tipo de objeto especificado y sus atributos.

aws iot create-thing \ --thing-name "MyLightBulb" \ --thing-type-name "LightBulb" \ --attribute-payload "{"attributes": {"wattage":"75", "model":"123"}}"

Salida:

{ "thingName": "MyLightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "thingId": "40da2e73-c6af-406e-b415-15acae538797" }

Para obtener más información, consulte How to Manage Things with the Registry y Thing Types en la Guía para desarrolladores de AWS IoT.

  • Para obtener más información sobre la API, consulte CreateThing en la Referencia de comandos de la AWS CLI.

Java
SDK para Java 2.x
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

/** * Creates an IoT Thing with the specified name asynchronously. * * @param thingName The name of the IoT Thing to create. * * This method initiates an asynchronous request to create an IoT Thing with the specified name. * If the request is successful, it prints the name of the thing and its ARN value. * If an exception occurs, it prints the error message. */ public void createIoTThing(String thingName) { CreateThingRequest createThingRequest = CreateThingRequest.builder() .thingName(thingName) .build(); CompletableFuture<CreateThingResponse> future = getAsyncClient().createThing(createThingRequest); future.whenComplete((createThingResponse, ex) -> { if (createThingResponse != null) { System.out.println(thingName + " was successfully created. The ARN value is " + createThingResponse.thingArn()); } else { Throwable cause = ex.getCause(); if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else { System.err.println("Unexpected error: " + cause.getMessage()); } } }); future.join(); }
  • Para obtener detalles sobre la API, consulte CreateThing en la Referencia de la API de AWS SDK for Java 2.x.

Kotlin
SDK para Kotlin
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

suspend fun createIoTThing(thingNameVal: String) { val createThingRequest = CreateThingRequest { thingName = thingNameVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.createThing(createThingRequest) println("Created $thingNameVal}") } }
  • Para obtener más información sobre la API, consulte CreateThing en la Referencia de la API de AWS SDK para Kotlin.

Para obtener una lista completa de las guías para desarrolladores de AWS SDK y ejemplos de código, consulte Uso de AWS IoT con un AWS SKD. En este tema también se incluye información sobre cómo comenzar a utilizar el SDK y detalles sobre sus versiones anteriores.