AWS IoT Hands-On — A Practical Tutorial

STM32 Discovery kit IoT node in front of the data sent to AWS IoT cloud

In short what this is about

In this article or tutorial I will explain how to connect an ST Microelectronics STM32 Discovery kit IoT Node with AWS IoT using ARM Mbed OS to display temperature, humidity and air pressure on an S3 website hosted through CloudFront.

What is AWS IoT about?

In order to understand this article it is absolutely recommended to have at least an idea of what AWS, the Amazon cloud, does. Beside all these large scale services that AWS offers, it also offers a nice set of IoT services. These include, but are not limited to, the following.

  • AWS IoT Core for managing IoT devices or device fleets
  • AWS Greengrass (Won’t cover it in this article, but it’s awesome!)
  • AWS IoT 1-Click — the Dash Button’s services
The AWS IoT User Interface — A view of the Monitor dashboard

Starting off with this Thing — STM32L4

In order to start using AWS IoT, the Cloud for the Internet Of Things, you need of course a “Thing”. A Thing in the case of IoT is a small microprocessor with communications either on the chip or on the board. The Thing I got myself is an STM32 Discovery kit IoT node that comes with an ARM Cortex-M4 core 80 Mhz STM32L475VGT6 MCU equipped with WiFi, NFC, BLE and Sub-Ghz plus tons of sensors directly on the board. I got one at around $40.

STMicroelectronics STM32 Discovery kit IoT Node — connected to my Mac
The Mbed OS Online Compiler

Creating your first Thing in AWS IoT

In order to connect your STM32L4 device to AWS IoT, you need to create it as a Thing in the AWS console. You can easily get to the interface by picking “IoT Core” from the “Services”-menu at the top. In the left menu under “Manage” and then “Things” you will see a “Create” button on top right.

The AWS console’s “Creating AWS IoT things” screen
“Add your device to the thing registry” screen on AWS IoT
AWS IoT Thing certificate successfully created
“Advanced mode” of the “Create a policy” screen
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Connect",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iot:Publish",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iot:Subscribe",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iot:Receive",
"Resource": "*"
}
]
}

Adding the Thing certificate and key to the program

You can now insert the contents of your certificate and key file into the corresponding section of the “MQTT_server_setting.h” configuration file of the program.

const char MQTT_SERVER_HOST_NAME[] = "INSERT_THING_ENDPOINT_HERE.iot.eu-central-1.amazonaws.com";

const char* SSL_CLIENT_CERT_PEM = "-----BEGIN CERTIFICATE-----\n"
"INSERT YOUR THING CERTIFICATE HERE!\n"
"-----END CERTIFICATE-----\n";

const char* SSL_CLIENT_PRIVATE_KEY_PEM = "-----BEGIN RSA PRIVATE KEY-----\n"
"INSERT YOUR THING PRIVATE KEY HERE!\n"
"-----END RSA PRIVATE KEY-----\n";
“Settings” screen with the AWS IoT endpoint domain name

Compile your IoT program for the STM32L4

Back in your Mbed online compiler with the program code open, the certificate and key inserted as well as the Wifi configuration, you can now compile the program code for your STM32L4 board.

Mbed online compiler while compiling the program code

Run the program on your STM32L4

Your STM32L4 will show up as a USB mass storage device on your system as well as a serial interface. I’m using a Mac on which I start the terminal and then use screen to connect to the USB serial interface.

STM32L4 USB console debug information

Checking your MQTT messages in the AWS IoT monitor

When opening the AWS IoT console’s monitor you should see that connections are being made and that messages are received by AWS IoT.

AWS IoT Monitor showing a chart with incoming MQTT messages

Act — Doing things with the Thing’s things

As MQTT messages are now flowing into AWS IoT we need to do something with the Thing’s data. If we stop here, then AWS IoT will just discard all the messages coming in and not actually do anything with them. When looking around in the AWS IoT console, you might have seen a section called “Act”. Hit the “Create” button to create a new rule for our messages.

AWS IoT Rule to forward message to Lambda

Pushing the Thing’s data onto S3

If you just want to display your IoT data from the MQTT messages that AWS IoT received, you can push it onto S3. The AWS IoT Rule for that is quite simple.

AWS IoT pushing MQTT message date to S3 with an AWS IoT Rule
S3 Bucket with MQTT message data from AWS IoT as JSON — and an HTML frontend

Serving the AWS IoT data on S3 through CloudFront

If you intend to present your IoT data in a web frontend, you can switch your S3 Bucket to public access using the following Bucket policy. You can then serve an HTML page that will just display the contents of the JSON file using JavaScript.

{
"Version": "2012-10-17",
"Id": "IotS3DataBucketPolicy",
"Statement": [
{
"Sid": "AllObjectAccess",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::iotsensordata/*"
}
]
}
Custom IoT Frontend on S3 served through CloudFront

Review of AWS IoT and what it can do

AWS IoT is not just an MQTT Gateway with a number of nice tools and services attached to it. It has got fleet management and numerous other awesome features (Thing Shadows!) that we have not explored in this quick Hands-On tutorial. Anyone who had dealt with IoT or embedded devices with sensors before knows the cumbersome work needed to distribute that data over the web for scale.

STM32L4 with a USB battery pack in a water-proof case for outdoor use

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jan Kammerath

Jan Kammerath

I love technology, programming, computers, mobile devices and the world of tomorrow.