Tutorial to build the AWS IoT SDK for CPP.
AWS-IOT-SDK-CPP
Table of Contents
Cloud: Create device instance for connecting by SDK program
Host: Cross-compiling the SDK
Target: Executing the SDK program
Cloud (AWS)
Create device instance for connecting by SDK program
Sign in to Cloud
- Sign in to AWS IoT Cloud. If you do not have an account, please register a new one.
Create Device
In the left navigation pane, expand Manage and then choose Things. On the page that says You don't have any things yet, choose Register a thing.
create_device_01
On the Creating AWS IoT things page, choose Create a single thing.
create_device_02
On the Add your device to the thing registry page, fill in the necessary information then choose Next.
create_device_03
create_device_04
On the Add a certificate for your thing page, choose Create thing without certificate.
create_device_05
Finish creating the device
create_device_06
Create Policy
In the left navigation pane, expand Secure and then choose Policies. On the page that says You don't have any policies yet, choose Create a policy.
create_policy_01
On the Create a policy page, fill in the necessary information and select the Allow check box then choose Create.
create_policy_02
create_policy_03
Finish creating the policy
create_policy_04
Create Certificate
You will need this infomation in the section Build the SDK
In the left navigation pane, expand Secure and then choose Certificates. On the page that says You don't have any certificates yet, choose Create a certificate.
create_certificate_01
On the Create a certificate page, choose Create certificate.
create_certificate_02
On the Certificate created! page, choose Download for the certificate, private key, and the root CA (the public key need not be downloaded). Save each of them to your computer, and then choose Done.
create_certificate_03
- For downloading the root CA for AWS IoT, on the Server Authentication page, choose Amazon Root CA 1.
create_certificate_04
Finish creating the certificate
create_certificate_05
Active Certificate
In the left navigation pane, expand Secure and then choose Certificates. In the box for the certificate you created before, choose **...** to open a drop-down menu, and then choose Attach thing.
active_certificate_01
In the Attach things to certificate(s) dialog box, select the check box next to the device you created before, and then choose Attach.
active_certificate_02
In the left navigation pane, expand Secure and then choose Certificates. In the box for the certificate you created before, choose **...** to open a drop-down menu, and then choose Attach policy.
active_certificate_01
In the Attach policies to certificate(s) dialog box, select the check box next to the policy you created before, and then choose Attach.
active_certificate_02
In the left navigation pane, expand Secure and then choose Certificates. In the box for the certificate you created before, choose **...** to open a drop-down menu, and then choose Activate.
active_certificate_05
Finish activating the certificate
active_certificate_06
Copy Device Endpoint
Device Endpoint is the key to build connection between physical and virtual device on cloud
You will need this infomation in the section Build the SDK
View Device Messages
You can view the following device messages after section Execute the SDK
In the left navigation pane, choose Test. On the Subscribe item, fill in the necessary information then choose Subscribe to topic.
view_device_messages_01
Device messages result
view_device_messages_02
view_device_messages_03
Host (x86_64-linux)
Cross-compiling the SDK
Setup the Environment
- Setup a network connection to allow host able to access the network.
- Install GNU cross-toolchain provide by MOXA.
- Install following package from package manager.
1 cmake git rsync tree vim
Build the SDK
- Clone repository of MOXA cloud connectivity tool from github.
1 user@Linux:~$ git clone https://github.com/MoxaCorp/ioThinx-4530-aws-iot.git
- Setup dependencies and SDK to output directory.
1 user@Linux:~/aws$ ./setup.sh
- For more setup.sh options.
1 user@Linux:~/aws$ ./setup.sh --help
3 Usage: ./setup.sh [options]
6 -git Git repository of SDK.
7 Default: https://github.com/aws/aws-iot-device-sdk-cpp.git
12 --toolchain GNU cross-toolchain directory.
13 Default: /usr/local/arm-linux-gnueabihf
15 --help Display this help and exit.
19 Specify ./setup.sh -git https://github.com/aws/aws-iot-device-sdk-cpp.git -ver v1.4.0
20 ./setup.sh --toolchain /usr/local/arm-linux-gnueabihf
- Copy the certificate, private key, and the root CA that downloaded from the cloud to the following directory. [Download Certificate]
1 user@Linux:~/aws$ tree output/sdk_aws/certs
3 ├── abd17825b2-certificate.pem.crt
4 ├── abd17825b2-private.pem.key
- Add the endpoint and the path of certificate, private key, and the root CA to SampleConfig.json file. [Copy Device Endpoint]
1 user@Linux:~/aws$ vim output/sdk_aws/common/SampleConfig.json
2 "endpoint": "example.amazonaws.com",
5 "greengrass_discovery_port": 8443,
6 "root_ca_relative_path": "certs/AmazonRootCA1.pem",
7 "device_certificate_relative_path": "certs/abd17825b2-certificate.pem.crt",
8 "device_private_key_relative_path": "certs/abd17825b2-private.pem.key",
9 "tls_handshake_timeout_msecs": 60000,
10 "tls_read_timeout_msecs": 2000,
11 "tls_write_timeout_msecs": 2000,
13 "aws_access_key_id": "",
14 "aws_secret_access_key": "",
15 "aws_session_token": "",
16 "client_id": "CppSDKTesting",
17 "thing_name": "CppSDKTesting",
18 "is_clean_session": true,
19 "mqtt_command_timeout_msecs": 20000,
20 "keepalive_interval_secs": 600,
21 "minimum_reconnect_interval_secs": 1,
22 "maximum_reconnect_interval_secs": 128,
23 "maximum_acks_to_wait_for": 32,
24 "action_processing_rate_hz": 5,
25 "maximum_outgoing_action_queue_length": 32,
26 "discover_action_timeout_msecs": 300000
- Build the whole SDK.
1 user@Linux:~/aws$ ./build.sh
- All compiled SDK program can be found in the following directory, including example pub-sub-sample.
1 user@Linux:~/aws$ tree output/sdk_aws/build_cmake/bin
2 output/sdk_aws/build_cmake/bin
3 ├── aws-iot-integration-tests
6 │ ├── abd17825b2-certificate.pem.crt
7 │ ├── abd17825b2-private.pem.key
8 │ └── AmazonRootCA1.pem
10 │ ├── IntegrationTestConfig.json
11 │ └── SampleConfig.json
14 ├── shadow-delta-sample
- You can also reference to the MOXA sample code with ioThinx I/O library moxa_sample_mqtt.cpp in the following directory.
1 user@Linux:~/aws$ tree sample
5 │ │ ├── abd17825b2-certificate.pem.crt
6 │ │ ├── abd17825b2-private.pem.key
7 │ │ ├── AmazonRootCA1.pem
9 │ │ └── SampleConfig.json
10 │ └── moxa_sample_mqtt
12 ├── moxa_sample_mqtt.cpp
13 └── moxa_sample_mqtt.hpp
- The compiled MOXA program moxa_sample_mqtt will be generated after the whole SDK is built.
- Note
1 In general, the setup.sh only needs to be executed once.
2 The build.sh needs to be executed after any code change of the SDK.
Target (arm-linux)
Executing the SDK program
Setup the Environment
- Setup a network connection to allow target able to access the network.
- Install following package from package manager.
- Copy compiled SDK program from host to target.
4 │ ├── abd17825b2-certificate.pem.crt
5 │ ├── abd17825b2-private.pem.key
6 │ └── AmazonRootCA1.pem
8 │ └── SampleConfig.json
Execute the SDK
- Execute SDK program that cross-compiled by host.
1 moxa@Moxa:~$ sudo ./pub-sub-sample
- View device messages on cloud.
Reference
[1] https://github.com/aws/aws-iot-device-sdk-cpp
[2] https://docs.aws.amazon.com/iot/latest/developerguide/what-is-aws-iot.html