Skip to content

Commit aa12ba8

Browse files
committedSep 18, 2024·
Checkpoint

File tree

6 files changed

+860
-43
lines changed

6 files changed

+860
-43
lines changed
 

‎samples/shadow/v2/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
# note: cxx-17 requires cmake 3.8, cxx-20 requires cmake 3.12
3+
project(shadowv2 CXX)
4+
5+
file(GLOB SRC_FILES
6+
"*.cpp"
7+
"../../utils/CommandLineUtils.cpp"
8+
"../../utils/CommandLineUtils.h"
9+
)
10+
11+
add_executable(${PROJECT_NAME} ${SRC_FILES})
12+
13+
set_target_properties(${PROJECT_NAME} PROPERTIES
14+
CXX_STANDARD 14)
15+
16+
#set warnings
17+
if (MSVC)
18+
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX)
19+
else ()
20+
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wno-long-long -pedantic -Werror)
21+
endif ()
22+
23+
find_package(aws-crt-cpp REQUIRED)
24+
find_package(IotShadow-cpp REQUIRED)
25+
26+
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
27+
28+
target_link_libraries(${PROJECT_NAME} PRIVATE AWS::aws-crt-cpp AWS::IotShadow-cpp)

‎samples/shadow/v2/README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Shadow
2+
3+
[**Return to main sample list**](../../README.md)
4+
5+
This sample uses the AWS IoT [Device Shadow](https://docs.aws.amazon.com/iot/latest/developerguide/iot-device-shadows.html) Service to keep a property in sync between device and server. Imagine a light whose color may be changed through an app, or set by a local user.
6+
7+
Once connected, type a value in the terminal and press Enter to update the property's "reported" value. The sample also responds when the "desired" value changes on the server. To observe this, edit the Shadow document in the AWS Console and set a new "desired" value.
8+
9+
On startup, the sample requests the shadow document to learn the property's initial state. The sample also subscribes to "delta" events from the server, which are sent when a property's "desired" value differs from its "reported" value. When the sample learns of a new desired value, that value is changed on the device and an update is sent to the server with the new "reported" value.
10+
11+
Your IoT Core Thing's [Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) must provide privileges for this sample to connect, subscribe, publish, and receive. Below is a sample policy that can be used on your IoT Core Thing that will allow this sample to run as intended.
12+
13+
<details>
14+
<summary>Sample Policy</summary>
15+
<pre>
16+
{
17+
"Version": "2012-10-17",
18+
"Statement": [
19+
{
20+
"Effect": "Allow",
21+
"Action": [
22+
"iot:Publish"
23+
],
24+
"Resource": [
25+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get",
26+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update"
27+
]
28+
},
29+
{
30+
"Effect": "Allow",
31+
"Action": [
32+
"iot:Receive"
33+
],
34+
"Resource": [
35+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get/accepted",
36+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get/rejected",
37+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/accepted",
38+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/rejected",
39+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/delta"
40+
]
41+
},
42+
{
43+
"Effect": "Allow",
44+
"Action": [
45+
"iot:Subscribe"
46+
],
47+
"Resource": [
48+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/get/accepted",
49+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/get/rejected",
50+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/accepted",
51+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/rejected",
52+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/delta"
53+
]
54+
},
55+
{
56+
"Effect": "Allow",
57+
"Action": "iot:Connect",
58+
"Resource": "arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
59+
}
60+
]
61+
}
62+
</pre>
63+
64+
Replace with the following with the data from your AWS account:
65+
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
66+
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.
67+
* `<thingname>`: The name of your AWS IoT Core thing you want the device connection to be associated with
68+
69+
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
70+
71+
</details>
72+
73+
## How to run
74+
75+
To run the Shadow sample use the following command:
76+
77+
``` sh
78+
./mqtt5-shadow-sync --endpoint <endpoint> --cert <path to the certificate> --key <path to the private key> --thing_name <thing name> --shadow_property <shadow property name>
79+
```
80+
81+
You can also pass a Certificate Authority file (CA) if your certificate and key combination requires it:
82+
83+
``` sh
84+
./mqtt5-shadow-sync --endpoint <endpoint> --cert <path to the certificate> --key <path to the private key> --thing_name <thing name> --shadow_property <shadow property name> --ca_file <path to root CA>
85+
```
86+
87+
88+
## Service Client Notes
89+
### Difference between MQTT5 and MQTT311 IotShadowClient
90+
The IotShadowClient with Mqtt5 client is identical to Mqtt3 one. We wrapped the Mqtt5Client into MqttClientConnection so that we could keep the same interface for IotShadowClient.
91+
The only difference is that you would need setup up a Mqtt5 Client for the IotShadowClient. For how to setup a Mqtt5 Client, please refer to [MQTT5 UserGuide](../../../documents/MQTT5_Userguide.md) and [MQTT5 PubSub Sample](../../mqtt5/mqtt5_pubsub/)
92+
93+
<table>
94+
<tr>
95+
<th>Create a IotShadowClient with Mqtt5</th>
96+
<th>Create a IotShadowClient with Mqtt311</th>
97+
</tr>
98+
<tr>
99+
<td>
100+
101+
```Cpp
102+
// Build Mqtt5Client
103+
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
104+
105+
// Create shadow client with mqtt5 client
106+
Aws::Iotshadow::IotShadowClient shadowClient(client);
107+
```
108+
109+
</td>
110+
<td>
111+
112+
```Cpp
113+
// Create mqtt311 connection
114+
Aws::Iot::MqttClient client = Aws::Iot::MqttClient();
115+
auto connection = client.NewConnection(clientConfig);
116+
117+
// Create shadow client with mqtt311 connection
118+
Aws::Iotshadow::IotShadowClient shadowClient(connection);
119+
120+
```
121+
122+
</td>
123+
</tr>
124+
</table>
125+
126+
### Mqtt::QOS v.s. Mqtt5::QOS
127+
As the service client interface is unchanged for Mqtt3 Connection and Mqtt5 Client,the IotShadowClient will use Mqtt::QOS instead of Mqtt5::QOS even with a Mqtt5 Client.

0 commit comments

Comments
 (0)
Please sign in to comment.