Easy AWS IoT with the Espressif IoT Development Framework
Tags: iot, tutorial, aws, platformio
Published: 2020-03-14 12:55:32

---
title: Easy AWS IoT with the Espressif IoT Development Framework
published: true
description: Developing simple AWS IoT projects using the Espressif IoT Development Framework (ESP-IDF) and PlatformIO
canonical_url: https://devopstar.com/2020/03/14/easy-aws-iot-with-esp-idf
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/pcevf2u7irtr930d5piq.png
tags: iot, tutorial, aws, platformio
---

> Please reach out to me on [Twitter @nathangloverAUS](https://twitter.com/nathangloverAUS) if you have follow up questions!

*This post was originally written on [DevOpStar](https://devopstar.com/)*. Check it out [here](https://devopstar.com/2020/03/14/easy-aws-iot-with-esp-idf)

When working on IoT projects its common that you will need to bake in libraries that allow you to interface with features on your embedded device. [Espressif](https://www.espressif.com/) manufacture the commonly used ESP32/8266 devices and it's within their best interests to make it as easy as possible to start building on their platform. To accommodate they maintain the [Espressif IoT Development Framework (ESP-IDF)](https://docs.espressif.com/projects/esp-idf/en/latest/index.html) which bundles all these libraries for you making it really easy to get started with projects.

In this post we are exploring how to use the [Espressif IoT Development Framework (ESP-IDF)](https://docs.espressif.com/projects/esp-idf/en/latest/index.html) with [PlatformIO](https://platformio.org/) to make developing projects for [AWS IoT](https://aws.amazon.com/iot/) even easier.

## Prerequisites

Before getting started on this post it will help if you have most of the following:

#### ESP32 Device

![ESP32 FireBeetle device from https://core-electronics.com.au/](https://dev-to-uploads.s3.amazonaws.com/i/gotx6eeryxgakx0i5kdf.png)

I recommend having an ESP32 device to work with. I personally use the [FireBeetle ESP32](https://core-electronics.com.au/firebeetle-esp32-iot-microcontroller-supports-wi-fi-bluetooth.html) micro however any mainstream device should be fine. Some of these alternatives are:

* [SparkFun ESP32 Thing](https://www.sparkfun.com/products/13907)
* [SparkFun Thing Plus - ESP32 WROOM](https://core-electronics.com.au/sparkfun-thing-plus-esp32-wroom.html)
* [Beetle ESP32 Microcontroller](https://core-electronics.com.au/beetle-esp32-microcontroller.html)

> **NOTE**: There are many boards available out there, most of which should work however note that your experiences may vary.

#### PlatformIO

[PlatformIO](https://platformio.org/) is a very well established tool chain for developing on embedded devices. It serves itself as an IDE extension for popular editors like VSCode and enables developers to write the same code and deploy it to different architectures seamlessly.

![PlatformIO Logo](https://dev-to-uploads.s3.amazonaws.com/i/7i78mut6v4krijz46u1w.png)

In this post we will be making use of the PlatformIO Core CLI. To install this follow the guide at [PlatformIO Core](https://platformio.org/install/cli) or install it via pip using the following command

```bash
pip install -U platformio
```

You can also achieve everything through the fantastic plugin for VSCode. If you have VSCode installed already simply install the PlatformIO Core plugin

![PlatformIO VSCode plugin](https://dev-to-uploads.s3.amazonaws.com/i/kueo70yb6avogbmlg29d.png)

#### Sample Project

The final requirement is the sample project available at [t04glovern/esp-idf-aws-iot](https://github.com/t04glovern/esp-idf-aws-iot) on GitHub. You can clone it down by running the following commands

```bash
https://github.com/t04glovern/esp-idf-aws-iot.git
cd esp-idf-aws-iot
```

> We'll go over the individual details shortly, however feel free to check the README and jump ahead.

#### AWS IoT Device

Create a new AWS IoT device by following the excellent guide on the [AWS IoT documentation page](https://docs.aws.amazon.com/iot/latest/developerguide/register-device.html). By the end of the short guide you should have the following:

* **Three certificates** (used in the next steps)
* **One IoT thing** with a policy attached
  * When creating the policy, if asked for a topic use `test_topic/esp32`

## Create New Project

When working with PlatformIO all project details can be provided in one simple `platformio.ini` file. Along with this are a couple generally used files such as:

* **src** - Directory with C/C++ code
* **.gitignore** - Defines what shouldn't be checked into Git
* **.pio** - Ignored directory where compiled source gets stored
* **.vscode** - VSCode configuration for your project

![PlatformIO project files](https://dev-to-uploads.s3.amazonaws.com/i/us7anv5t8yjfes7q100f.png)

#### platformio.ini

The `platformio.ini` file is the heart of your ESP-IDF and any other IoT projects you are working on. It defines details such as the platform, framework, what embedded board you are building for and other adhoc things.

Check out the one we're using for this project below

```ini
[env:esp32dev]
platform = espressif32
framework = espidf
board = esp32dev
monitor_speed = 115200
build_flags =
  -DCONFIG_WIFI_SSID=\"wifissid\"
  -DCONFIG_WIFI_PASSWORD=\"wifipass\"
  -DCONFIG_AWS_IOT_TOPIC=\"test_topic/esp32\"

board_build.embed_txtfiles =
  src/private.pem.key
  src/certificate.pem.crt
  src/aws-root-ca.pem
```

> **Your Task**: Define the WiFi SSID and Password you want your IoT device to use to connect to the internet. Update these fields with your own WiFi details

#### Certificates

Did you take note of the `embed_txtfiles` field? This is where we define where the certificates live that need to be loaded onto our board and used to communicate with AWS IoT.

* **src/aws-root-ca.pem** - This certificate should contain the AWS CA (or your own private CA certificate). You can obtain this certificate from the Amazon trust [HERE](https://www.amazontrust.com/repository/AmazonRootCA1.pem).
* **src/private.pem.key**: Private Key certificate you received when creating your AWS IoT Device
* **src/certificate.pem.crt**: Device certificate you received when creating your AWS IoT Device

#### sdkconfig.h

A small change needs to be made to the `src/sdkconfig.h` file on the configuration option `CONFIG_AWS_IOT_MQTT_HOST`. You need to update it to be the AWS IoT MQTT endpoint address which can be found via one of the two ways:

* Retrieve the data endpoint by navigating to the [AWS IoT Settings UI](https://console.aws.amazon.com/iot/home?region=us-east-1#/settings). **Make sure you are in the correct region as well**.

![AWS IoT Custom Endpoint UI](https://dev-to-uploads.s3.amazonaws.com/i/wvnhasfj5k6h632lm8lr.png)

* Retrieve the endpoint by running the following CLI command

```bash
aws iot describe-endpoint --endpoint-type iot:Data-ATS
```

#### subscribe\_publish\_sample.c

The sample C code is taken from the existing example from the [platformio/platform-espressif32 repository](https://github.com/platformio/platform-espressif32/tree/master/examples/espidf-aws-iot). You shouldn't need to change too much (if anything) within it, however take not that the custom version in my repository stubs out the AWS IoT Topic to a config variable.

Take a look at the imports at the top, there's heaps! The best part is we don't need to worry about them! ESP-IDF and PlatformIO will come together in sweet harmony and pull everything together for us.

```cpp
// Lots of imports we don't need to worry about!!!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"

#include "nvs.h"
#include "nvs_flash.h"

#include "aws_iot_config.h"
#include "aws_iot_log.h"
#include "aws_iot_version.h"
#include "aws_iot_mqtt_client_interface.h"
```

## Compile & Deploy

We're not ready to compile and deploy our project and it's as simple as one command. Plug in your ESP32 device to your computer over the USB cable it would have came with.

![ESP32 Plugged in](https://dev-to-uploads.s3.amazonaws.com/i/pcevf2u7irtr930d5piq.png)

Then run the following to build and deploy

```bash
platformio run --target upload
```

#### Debugging

Once the firmware is deployed you can monitor the output of the board by running the following command

```bash
platformio device monitor
# Looking for advanced Serial Monitor with UI? Check http://bit.ly/pio-advanced-monitor
# --- Miniterm on /dev/ttyACM0  115200,8,N,1 ---
# --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
```

## Summary

The [Espressif IoT Development Framework (ESP-IDF)](https://docs.espressif.com/projects/esp-idf/en/latest/index.html) is a fantastic way to get started developing on the ESP32 and other Espressif devices. When you partner it with PlatformIO as well you'll start to realise how simple it is to develop embedded devices in 2020.

Have you used ESP-IDF for something interesting? Or maybe you're an avid user of PlatformIO and have some tips for me? Reach out to me on [Twitter @nathangloverAUS](https://twitter.com/nathangloverAUS) and let me know!