Mbed OS example to connect to AWS IoT Core
Mbed OS example for AWS cloud
The example project is part of the Arm Mbed OS Official Examples. It contains an application that connects to an AWS MQTT broker and publishes a message every 1 second for 10 seconds or until a message is received.
You can build the project with all supported Mbed OS build tools. However, this example project specifically refers to the command-line interface tool Arm Mbed CLI.
(Note: To see a rendered example you can import into the Arm Online Compiler, please see our import quick start.)
Downloading this project
Clone this repository on your system, and change the current directory to where the project was cloned:
$ git clone https://github.com/ARMmbed/mbed-os-example-for-aws.git && cd mbed-os-example-for-aws $ mbed deploy
Alternatively, you can download the example project with Arm Mbed CLI using the
$ mbed import mbed-os-example-for-aws && cd mbed-os-example-for-aws
Configuring the AWS IoT Core service
Create an AWS account if you don’t have one, and login to it.
NOTE: If you have an admin for your AWS account, please contact them to add a user to the account. You should obtain your login credentials from your admin in this case.
Set up device credentials and policy via the AWS IoT console. You can refer to the AWS documentation here. Follow the steps there to
- create a thing
- generate a device certificate and keys
- create an IoT policy and attach that policy to your device.
Also download “Amazon Root CA 1” from here.
Once you have downloaded the credentials, you will need to place them in the
aws_credentials.hfile of this example.
The example includes a python script to automate converting the credentials you downloaded from AWS into C-compatible arrays/strings. First, create a new folder in the project to store your credential files, eg:
mkdir aws-credentials. Copy the previously-downloaded credential files into this subdirectory.
Then, you can run the script to automatically generate the necessary code from the credentials:
python aws-cert-converter.py aws-credentials
For more details on how to use the convert script, simply pass in the
-hflag to print the help documentation.
The above command will read your credential files and place them into a C header file for you:
Once you have created the “thing”, you will need to obtain the custom endpoint name from the console. At the time of writing this document, you can find it under “Settings” in the IoT console.
aws-endpoint.valueto be that of the custom endpoint.
Set a topic that both your device and the cloud can publish messages to.
mbed_app.json file, set
aws-mqtt-topic.value to a name you prefer, or use the default one. On the AWS console, you will then need to subscribe to the same topic name. At the time of writing this document, you can find this under “Test” on the console.
- Give your device a name by setting
Building and running
If using WiFi (e.g. on DISCO_L475VG_IOT01A), enter your network’s SSID and password in
mbed_app.json(see here). Keep any existing
\"s. (If you use a different WiFi-enabled target, you may need to manually import its WiFi driver as described here.)
For Ethernet (e.g on K64F), connect a cable to the port.
Connect a USB cable between the USB port on the board and the host computer.
Alternatively, you can manually copy the binary to the board, which you mount on the host computer over USB.
The binary is located at
Depending on the target, you can build the example project with the
ARM toolchain. Run the command below to determine which toolchain supports your target:
$ mbed compile -S
Once the example starts to run, you can monitor a serial terminal to see that the device connects to your network, exchanges some TLS handshakes, connects to AWS and publishes to the topic you just subscribed. This can be seen on the AWS console as incoming messages.
The application publishes a message every second for 10 seconds, or until it receives a message from the cloud:
[INFO][Main]: sending warning message: Warning: Only 10 second(s) left to say your name ! [INFO][Main]: sending warning message: Warning: Only 9 second(s) left to say your name ! [INFO][Main]: sending warning message: Warning: Only 8 second(s) left to say your name ! [INFO][Main]: sending warning message: Warning: Only 7 second(s) left to say your name !
You can send a message to your device via the AWS IoT console (Test -> Publish to a topic). Use the same topic name you set in
On receipt of a message, the application displays on the console the message prefixed with
If you have problems, you can review the documentation for suggestions on what could be wrong and how to fix it.
- Mbed OS Stats API.
- Mbed OS Configuration.
- Mbed OS Serial Communication.
- Mbed OS bare metal.
- Mbed boards.
- AWS IoT Core
- AWS IoT Core - Embedded C SDK
License and contributions
The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more info.
This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.