AWS IoT data examples using SDK for C++ - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

AWS IoT data examples using SDK for C++

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for C++ with AWS 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 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.

//! Get the shadow of an AWS IoT thing. /*! \param thingName: The name for the thing. \param documentResult: String to receive the state information, in JSON format. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::getThingShadow(const Aws::String &thingName, Aws::String &documentResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoTDataPlane::IoTDataPlaneClient iotClient(clientConfiguration); Aws::IoTDataPlane::Model::GetThingShadowRequest request; request.SetThingName(thingName); auto outcome = iotClient.GetThingShadow(request); if (outcome.IsSuccess()) { std::stringstream ss; ss << outcome.GetResult().GetPayload().rdbuf(); documentResult = ss.str(); } else { std::cerr << "Error getting thing shadow: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see GetThingShadow in AWS SDK for C++ API Reference.

The following code example shows how to use UpdateThingShadow.

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 the shadow of an AWS IoT thing. /*! \param thingName: The name for the thing. \param document: The state information, in JSON format. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::updateThingShadow(const Aws::String &thingName, const Aws::String &document, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoTDataPlane::IoTDataPlaneClient iotDataPlaneClient(clientConfiguration); Aws::IoTDataPlane::Model::UpdateThingShadowRequest updateThingShadowRequest; updateThingShadowRequest.SetThingName(thingName); std::shared_ptr<std::stringstream> streamBuf = std::make_shared<std::stringstream>( document); updateThingShadowRequest.SetBody(streamBuf); Aws::IoTDataPlane::Model::UpdateThingShadowOutcome outcome = iotDataPlaneClient.UpdateThingShadow( updateThingShadowRequest); if (outcome.IsSuccess()) { std::cout << "Successfully updated thing shadow." << std::endl; } else { std::cerr << "Error while updating thing shadow." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }