- Main
- Tutorials
- Library
- Samples

How to build AWS IoT SDK for CPP

Tutorial to build the AWS IoT SDK for CPP.

# ioThinx-4530-aws-iot

> 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

1. 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

2. On the Creating AWS IoT things page, choose Create a single thing.

   create_device_02

3. On the Add your device to the thing registry page, fill in the necessary information then choose Next.

   create_device_03
   create_device_04

4. On the Add a certificate for your thing page, choose Create thing without certificate.

   create_device_05

5. Finish creating the device

   create_device_06

### Create Policy

1. 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

2. 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

3. Finish creating the policy

   create_policy_04

### Create Certificate

> You will need this infomation in the section Build the SDK

1. 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

2. On the Create a certificate page, choose Create certificate.

   create_certificate_02

3. 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

4. Finish creating the certificate

   create_certificate_05

### Active Certificate

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

- In the left navigation pane, choose Settings. On the Custom endpoint item, you can found the Endpoint that allows you to connect to AWS IoT.

  copy_device_endpoint

### View Device Messages

> You can view the following device messages after section Execute the SDK

1. 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

2. Device messages result

   view_device_messages_02
   view_device_messages_03

## Host (x86_64-linux)

> Cross-compiling the SDK

### Setup the Environment

1. Setup a network connection to allow host able to access the network.
2. Install GNU cross-toolchain provide by MOXA.
3. Install following package from package manager.
   1 cmake git rsync tree vim

### Build the SDK

1. Clone repository of MOXA cloud connectivity tool from github.
   1 user@Linux:~$ git clone https://github.com/MoxaCorp/ioThinx-4530-aws-iot.git
2. Setup dependencies and SDK to output directory.
   1 user@Linux:~$ cd ioThinx-4530-aws-iot
   1 user@Linux:~/ioThinx-4530-aws-iot$ ./setup.sh
   - For more setup.sh options.
     1 user@Linux:~/ioThinx-4530-aws-iot$ ./setup.sh --help
     2
     3 Usage: ./setup.sh [options]
     4
     5 Options:
     6 -git Git repository of SDK.
     7 Default: https://github.com/aws/aws-iot-device-sdk-cpp.git
     8
     9 -ver Version of SDK.
     10 Default: v1.4.0
     11
     12 --toolchain GNU cross-toolchain directory.
     13 Default: /usr/local/arm-linux-gnueabihf
     14
     15 --help Display this help and exit.
     16
     17 Examples:
     18 Default ./setup.sh
     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
3. Copy the certificate, private key, and the root CA that downloaded from the cloud to the following directory. [Download Certificate]
   1 user@Linux:~/ioThinx-4530-aws-iot$ tree output/sdk_aws/certs
   2 output/sdk_aws/certs
   3 ├── abd17825b2-certificate.pem.crt
   4 ├── abd17825b2-private.pem.key
   5 └── AmazonRootCA1.pem
4. Add the endpoint and the path of certificate, private key, and the root CA to SampleConfig.json file. [Copy Device Endpoint]
   1 user@Linux:~/ioThinx-4530-aws-iot$ vim output/sdk_aws/common/SampleConfig.json
   1 {
   2 "endpoint": "example.amazonaws.com",
   3 "mqtt_port": 8883,
   4 "https_port": 443,
   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,
   12 "aws_region": "",
   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
   27 }
5. Build the whole SDK.
   1 user@Linux:~/ioThinx-4530-aws-iot$ ./build.sh
   - All compiled SDK program can be found in the following directory, including example pub-sub-sample.
     1 user@Linux:~/ioThinx-4530-aws-iot$ tree output/sdk_aws/build_cmake/bin
     2 output/sdk_aws/build_cmake/bin
     3 ├── aws-iot-integration-tests
     4 ├── aws-iot-unit-tests
     5 ├── certs
     6 │ ├── abd17825b2-certificate.pem.crt
     7 │ ├── abd17825b2-private.pem.key
     8 │ └── AmazonRootCA1.pem
     9 ├── config
     10 │ ├── IntegrationTestConfig.json
     11 │ └── SampleConfig.json
     12 ├── jobs-sample
     13 ├── pub-sub-sample
     14 ├── shadow-delta-sample
     15 └── TestParser.json

- You can also reference to the MOXA sample code with ioThinx I/O library moxa_sample_mqtt.cpp in the following directory. This sample code can be executed directly on the target device with module 45MR-2606 located in slot 1.
  1 user@Linux:~/ioThinx-4530-aws-iot$ tree sample
  2 sample
  3 ├── binary
  4 │ ├── certs
  5 │ │ ├── abd17825b2-certificate.pem.crt
  6 │ │ ├── abd17825b2-private.pem.key
  7 │ │ ├── AmazonRootCA1.pem
  8 │ ├── config
  9 │ │ └── SampleConfig.json
  10 │ └── moxa_sample_mqtt
  11 └── source
  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

1. Setup a network connection to allow target able to access the network.
2. Install following package from package manager.
   1 tree
3. Copy compiled SDK program from host to target.
   1 moxa@Moxa:~$ tree
   2 .
   3 ├── certs
   4 │ ├── abd17825b2-certificate.pem.crt
   5 │ ├── abd17825b2-private.pem.key
   6 │ └── AmazonRootCA1.pem
   7 ├── config
   8 │ └── SampleConfig.json
   9 └── pub-sub-sample

### Execute the SDK

1. Execute SDK program that cross-compiled by host.
   1 moxa@Moxa:~$ sudo ./pub-sub-sample
2. 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