Amazon IoT data examples using SDK for C++ - Amazon SDK for C++
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 C++

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

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon 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 Amazon 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 Amazon 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(); }