Booting the ESP32 with Amazon FreeRTOS and Connecting it to AWS

After testing several cloud providers' different tools and functions, Amazon's FreeRTOS got our attention. Amazon FreeRTOS is a secure and easy to install-deploy-connect IoT operating system for micro-controllers. It is based on FreeRTOS kernel, which you have probably heard about before, and it’s extended with several software libraries for cloud connections, messaging protocols and sensor data acquisition. FreeRTOS also makes things easier when you see your messaging on the cloud, i.e. by using Amazon Greengrass. The idea of using FreeRTOS operating system will help us to simplify cloud-device connections and increase the compatibility. IoT Core feature of AWS has already been tested in a previous blogpost here.

Today we will boot our ESP32 with Amazon FreeRTOS and connect it to the IoT Core service of Amazon Web Service. The device will use MQTT protocol in order publish the messages and subscribe them from specific topic.

Let's start! For this, you will need:

  • Amazon AWS Account (free)
  • ESP32 and a USB Cable

Image 1: ESP32 MCU
Image 1: ESP32 MCU

1) Installing AWS CLI Tool:

AWS CLI - Command Line Interface - is a unified interface for managing and controlling your AWS service, additionally the automation with scripts is possible. Here is the documentation about the tool: https://aws.amazon.com/cli/?nc1=h_ls

  • In order to start, let's install AWS CLI by running the following command:
$ pip install awscli --upgrade --user
  • Find the path that AWS CLI is placed in. For us, AWS CLI is installed under:
/Users/cassini/Library/Python/2.7/bin
  • To use the command line tool, let's add this path to the shell:
$ cd 
$ nano .bash_profile
  • On the last line, add your path as follows:
######### AWS CLI path
PATH=$PATH:/Users/cassini/Library/Python/2.7/bin
export PATH
  • Save the profile and restart your computer in order to make changes active.
$ source ~/.bash_profile
  • After the restart, now you can test your path update and see the actual version of AWS CLI tool:
$ aws --version
aws-cli/1.15.62 Python/2.7.11 Darwin/17.7.0 botocore/1.10.61

Now you have it! Let's move on with the next step.

2) Installing BOTO-3 AWS SDK:

We need BOTO-3 tool as a proper AWS Software Development Kit. Boto is the Amazon Web Services (AWS) SDK for Python, which allows Python developers to write software that makes use of Amazon services like S3 and EC2. Boto provides an easy to use, object-oriented API as well as low-level direct service access.

More clarification and the documentation can be found on: http://boto3.readthedocs.io

To install the Boto3 tool:

$ pip install boto3

Now you need to give your security credentials. To find your Access Key and Secret Access Key, please:

  • Log in to your AWS Management Console.
  • On top right, click on your username
  • Navigate to the My SecurityCredentials link from the drop-down menu
  • Click on the Access keys (access key ID and secret access key) section, and copy or create the Access Key ID.

Image 2: AWS Console Image 2: AWS Console

Image 3: AWS Console - Security Credentials Image 3: AWS Console - Security Credentials

Image 4: Access & Secret Key ID's
Image 4: Access & Secret Key ID's

  • Set up the credentials now, give your access key ID and secret access key:
$ aws configure
  • The region can be eu-west-1 and as default output can be passed null.

Image 5: Typing the Access & Secret Key ID's
Image 5: Typing the Access & Secret Key ID's

  • Open the Python interpreter and do following:
CC-C02SG159FVH6:~ a069$ python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 12:54:16)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> s3 = boto3.resource('s3')
>>> for bucket in s3.buckets.all():print(bucket.name)

3) Installing Xtensa Toolchain:

Xtensa Toolchain supports several architectures like NodeMCU and ESP32. The following step is installing the toolchain.

  • Please install necessary tools first:
$ sudo easy_install pip
$ sudo pip install pyserial
$ mkdir -p ~/esp 
$ cd ~/esp
$ tar -xzf ~/Downloads/xtensa-esp32-elf-osx-1.22.0-80-g6c4433a-5.2.0.tar
  • Add the toolchain path to your .bash_profile in home directory and restart your computer again:
######### Xtensa esp32 path
PATH=$PATH:/Users/cassini/esp/xtensa-esp32-elf/bin
export PATH

4) Downloading the Repository and Flashing:

The toolchain, SDK and the command line tools are ready. We can now download the ready-to-go repository from Amazon and test it with our setup.

  • To do so, please download Amazon FreeRTOS repository from:
$ cd esp/
$ git clone https://github.com/aws/amazon-freertos
$ unset IDF_PATH
  • Set your WIFI credentials for your connection:

$ cd esp/amazon-freertos/demos/common/tools/aws_config_quick_start/ $ nano configure.json

{
 "thing_name":"tftest",
 "wifi_ssid":"YOUR_SSID_NAME",
 "wifi_password":"YOUR_SSID_PASS",
 "wifi_security":"YOUR_SSID_SECURITY"
}
  • For your security parameter, you can choose one of them:

Image 6: WIFI Security Parameters
Image 6

  • Get endpoint from AWS to see if it is already defined:
$ aws iot describe-endpoint
"endpointAddress": "a2ukrv9kz0way2.iot.eu-west-1.amazonaws.com"
  • Get boto3 if you have not already and proceed. This script will create an IoT thing, the certificate and its policy. It will attach the IoT policy to the certificate and the certificate to the IoT thing.

  • The files aws_clientcredential.h will be updated with your AWS IoT endpoint and WIFI credentials. In the end, it will reset your certificate and private key and write them to the aws_clientcredential_keys.h
$ pip install boto3
$ python SetupAWS.py setup
  • Go to following directory, open the framework and define upload port and baud rate:
$ cd esp/amazon-freertos/demos/espressif/esp32_devkitc_esp_wrover_kit/make
$ make menuconfig

Image 7: Framework Configuration – Menu Image 7

Image 8: Framework Configuration - Upload Port and Monitor Baud Rate
Image 8

  • Run the project and observe the console output:
$ make flash monitor

Image 9: Uploading Process
Image 9: Uploading Process

Now, it will be seen that the ESP32 is connected to the internet and the cloud, sending and receiving successful MQTT messages:

Image 10: Console Output Image 10: Console Output

Image 11: AWS Console - Messaging Dashboard Image 11: AWS Console - Messaging Dashboard

This is the result of the sample code under the repository. If you wish to develop something new, you can start with playing the source code under:

 ~BASEDIR/esp/amazon-freertos/demos/espressif/esp32_devkitc_esp_wrover_kit/common/application_code/

Conclusion:

That's it! Connecting FreeRTOS and AWS to each other went really smooth and easy. It looks like Amazon found a perfect way to simplify cloud-device communications. Acquiring the command line tools and the repository makes you ready for MQTT messaging between your micro-controller and the cloud. If you want to go deeper with several sensors or actuators, just start modifying the application code.

As ThingForward, we are dealing with every field of IoT and testing the new technologies. Stay tuned for upcoming blogposts!

Eren Mert Irez

Follow ThingForward on Twitter, Facebook and Linkedin!