Click here to Skip to main content
13,667,388 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.2K views
8 bookmarked
Posted 22 Mar 2018
Licenced CPOL

MQTT Publication to Amazon Web Services (AWS)

, 22 Mar 2018
This tutorial demonstrates how to use an UP Squared board together with the Arduino Create IDE to publish messages (sensor data) via MQTT, a messaging protocol suited for IoT applications, to the Amazon Web Services (AWS) cloud platform.

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

This tutorial demonstrates how to use an UP Squared* board together with the Arduino Create* IDE to publish messages (sensor data) via MQTT, a messaging protocol suited for IoT applications, to the Amazon Web Services (AWS)* cloud platform.

Note: we use the aws-iot-device-sdk-cpp.h library (a modified version of the AWS* IoT Device SDK).

Programming language C++ (Arduino)
Skill level Intermediate
Time to complete 15 minutes
Hardware needed UP Squared* Grove* IoT Development Kit
Target Operating System Ubuntu 16.04

What you'll learn

From this code sample tutorial you'll learn how to:

  • Setup an AWS account and a Thing (a representation of a cloud device)
  • Create a certificate for your Thing to authenticate your device's connection to AWS IoT
  • View MQTT Messages (sensor data) with the AWS IoT MQTT Client

Gather your materials

Hardware

Software

Software dependencies

  • libcrypto
  • libssl

Note: if you're using the UP Squared Grove IoT Development Kit, these software dependencies will already be installed. Otherwise you'll need to install them via apt-get.

Prerequisites

Before you begin

Complete the sensor setup below. Assuming you've already completed the prerequisites, connect the GrovePi+ to the UP Squared board and then connect the rotary angle sensor to A0 (analog pin 0).

Tutorial steps

Follow along with the steps below to send data from a rotary angle sensor (via MQTT) to the AWS cloud platform for viewing.

  1. Set up an AWS account

  2. Create a Thing

  3. Create and attach a policy to your Thing

  4. Get the HTTPS endpoint value

  5. Get the code

  6. View data sent via MQTT in the AWS console

Step 1: Set up an AWS account

a. Go to the AWS IoT console and register or sign in.

b. In the AWS services search bar, type and select "AWS IoT".

c. Click "Get started"

Step 2: Create a Thing

Register a Thing

A Thing is a representation of your device in the cloud.

a. From the left navigation panel, choose Manage > Things > and then click on "Register a thing"

b. To register an AWS IoT Thing, select "Create a singe thing"

c. Enter a name for your Thing (for example, "Thingy") and then click "Next" (bottom right hand corner of the screen)

Add a certificate to your Thing

a. Choose "Create certificate" to generate a certificate, public key and private key (used to authenticate your device's connection to AWS IoT).

b. Download all four files.

c. And then click ‘Activate’.

Step 3: Create and attach a policy to your Thing

Create a policy

a. From the navigation pane, choose Secure > Policies > and then click on "Create a policy"

b. Give your policy a name (for example, "my-iot-policy". To "Add statements" choose "iot:*" for Action, "*" for Resource ARN and then select "Allow" for Effect. Now click "Create".

Attach a policy to your Thing

a. On the main AWS IoT console page, navigate to Manage > Things > and select the Thing you created earlier.

b. Select the "Security" tab and click on the certificate you've attached to your Thing.

c. From the Actions dropdown, select "Attach policy".

d. Select the policy you created earlier and then click "Attach".

Step 4: Get the HTTPS endpoint value

You'll need the endpoint value when you modify the code sample in Arduino Create.

  1. In your Thing, select the Interact tab and copy the HTTPS endpoint value. This value will be used in the sample.

Step 5: Get the code

a. Open the MqttPubAWS example in Arduino Create* Examples > FROM LIBRARIES > UP SQUARED GROVE IOT DEV KIT > MqttPubAWS

Modify the code

a. Enter the endpoint value (ENDPOINT), client ID (CLIENT_ID) and your Thing's name (THING_NAME) in the code sample's #define. The client ID and Thing name are same. When you modify the code sample, you'll be prompted to create a copy (click "OK").

You can also update the sample pub-sub topic (optional).

Format certificates and keys

To authenticate your UP Squared board's connection to AWS, you'll need to format the files that were generated when you created a certificate. Find the following files you downloaded earlier on your host computer:

  • xxx-G5.pem
  • xxx-certificate.pem.crt
  • xxx-private.pem.key

To format the certificates and keys for use in the Arduino Create Secret tab, run the following command on each of the files listed above:

awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' your-certificate.crt

Secret tab

a. Once you've properly formatted the files, navigate to the Secret tab. Copy and paste the formatted strings (we recommend using Notepad++*) to the corresponding entries in the Secret tab.

SECRET_ROOT_CA: Content of file ending in xxx-G5.pem

SECRET_CLIENT_CERT: Content of file ending in xxx-certificate.pem.crt

SECRET_CLIENT_PRIVATE_KEY: Content of file ending in xxx-private.pem.key

b. Save your file.

Note: For added security, place the root certificate, client certificate and client private key on the device directly by transferring the files to your device (make note of the absolute file name and provide that in the values in the secret path). In the DEFAULTSAMPLECONFIG value (line 45), set "load\_crts\_as\_string" to false. This loads the certificates from the file paths instead of as strings.

Subscribe to a topic in AWS IoT console

a. Before you run the example code, go the AWS IoT Console and navigate to Test (left panel) to open the MQTT Test Client.

b. Enter "sdk/test/cpp" as the subscription topic and click "Subscribe to topic".

Go back to the Arduino Create environment and open the Serial Monitor (left hand panel of the IDE). The monitor will give you output when you run the example.

Run the code

Click Verify (optional) and then Upload to run the code.

Step 6: View sensor data

Monitor the topic in the AWS IoT dashboard

Navigate to AWS IoT MQTT Client and subscribe to ""sdk/test/cpp" if you are not already subscribed. You should see the sensor reading come in on topic sdk/test/cpp:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Intel Corporation
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
Group type: Organisation

44 members


You may also be interested in...

Pro

Comments and Discussions

 
QuestionGood!!! Pin
빌게이츠12-Apr-18 21:16
member빌게이츠12-Apr-18 21:16 
GeneralGood Review Pin
Member 1377207510-Apr-18 2:57
memberMember 1377207510-Apr-18 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180820.1 | Last Updated 22 Mar 2018
Article Copyright 2018 by Intel Corporation
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid