Use UpdateThing with an AWS SDK or CLI
The following code examples show how to use UpdateThing.
- .NET
-
- SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /// <summary> /// Updates an IoT Thing with attributes. /// </summary> /// <param name="thingName">The name of the Thing to update.</param> /// <param name="attributes">Dictionary of attributes to add.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> UpdateThingAsync(string thingName, Dictionary<string, string> attributes) { try { var request = new UpdateThingRequest { ThingName = thingName, AttributePayload = new AttributePayload { Attributes = attributes, Merge = true } }; await _amazonIoT.UpdateThingAsync(request); _logger.LogInformation($"Updated Thing {thingName} with attributes"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot update Thing - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't update Thing attributes. Here's why: {ex.Message}"); return false; } }-
For API details, see UpdateThing in AWS SDK for .NET API Reference.
-
- C++
-
- SDK for C++
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. //! Update an AWS IoT thing with attributes. /*! \param thingName: The name for the thing. \param attributeMap: A map of key/value attributes/ \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::updateThing(const Aws::String &thingName, const std::map<Aws::String, Aws::String> &attributeMap, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::UpdateThingRequest request; request.SetThingName(thingName); Aws::IoT::Model::AttributePayload attributePayload; for (const auto &attribute: attributeMap) { attributePayload.AddAttributes(attribute.first, attribute.second); } request.SetAttributePayload(attributePayload); Aws::IoT::Model::UpdateThingOutcome outcome = iotClient.UpdateThing(request); if (outcome.IsSuccess()) { std::cout << "Successfully updated thing " << thingName << std::endl; } else { std::cerr << "Failed to update thing " << thingName << ":" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }-
For API details, see UpdateThing in AWS SDK for C++ API Reference.
-
- CLI
-
- AWS CLI
-
To associate a thing with a thing type
The following
update-thingexample associates a thing in the AWS IoT registry with a thing type. When you make the association, you provide values for the attributes defined by the thing type.aws iot update-thing \ --thing-name"MyOtherLightBulb"\ --thing-type-name"LightBulb"\ --attribute-payload "{"attributes": {"wattage":"75", "model":"123"}}"This command does not produce output. Use the
describe-thingcommand to see the result.For more information, see Thing Types in the AWS IoT Developers Guide.
-
For API details, see UpdateThing
in AWS CLI Command Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. suspend fun updateThing(thingNameVal: String?) { val newLocation = "Office" val newFirmwareVersion = "v2.0" val attMap: MutableMap<String, String> = HashMap() attMap["location"] = newLocation attMap["firmwareVersion"] = newFirmwareVersion val attributePayloadVal = AttributePayload { attributes = attMap } val updateThingRequest = UpdateThingRequest { thingName = thingNameVal attributePayload = attributePayloadVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> // Update the IoT thing attributes. iotClient.updateThing(updateThingRequest) println("$thingNameVal attributes updated successfully.") } }-
For API details, see UpdateThing
in AWS SDK for Kotlin API reference.
-
For a complete list of AWS SDK developer guides and code examples, see Using AWS IoT with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.