Connect ESP32 to AWS IoT
With Google Cloud Platform retiring its IoT Core service in August next year, let’s take a look at AWS IoT Core Service. In this article, we’ll show you how to connect to AWS IoT and publish message using ESP32 board.

Getting started with AWS IoT Core
Working in the Amazon Web Services is very simple and easy. With Amazon Web Services, the user is granted inexpensive cloud computing services which are reliable and interactive. With AWS IoT Core Service, AWS makes it very easy to securely connect devices to the cloud.
Let’s sign-in to our AWS Account and open IoT Core

We need to create a Thing for the IoT device we want to connect to cloud. An IoT Thing is a representation and record of your physical device in the cloud. A physical device needs a Thing record in order to work with AWS IoT.
We will have to follow three main steps in order to do this:
- Specifying Thing properties
- Configuring device certificate
- Attaching policies to certificate
Let’s first create a Thing go to Manage > Things. This opens the Things interface. Click ‘Create things’

Select ‘create a single thing.’ Then click ‘Next.’
Let’s give a name to our Thing. You can use any name according to your preference. Leave the other properties as default.

Scroll down and click ‘Next’ to move to the next step.
Select the options as shown below to generate the certificate associated with the Thing and click ‘Next’ to move to the next step.

To attach a policy with our certificate click ‘Create policy.’

This will open a new tab that lets us create a new policy. Give a name to your policy and add statements. In our case we have named the policy as ‘ESP32_Policy’. Create two policy statement as follows
╔═══════════════╦═════════════════╦════════════╗
║ Policy Effect ║ Policy Action ║ Resource ║
╠═══════════════╬═════════════════╬════════════╣
║ Allow ║ iot:Connect ║ * ║
║ Allow ║ iot:Publish ║ * ║
╚═══════════════╩═════════════════╩════════════╝

Click ‘Create’ to finish creating the policy. You will get the notification of successfully creating the policy.
Close this tab and go back to Create single thing tab. You will notice that the ESP32_Policy has already been attached with our thing. Click ‘Create thing’ to finish the process of creating the thing.

Now we will have to download the certificates and key files. These certificates will be used to communicate with the AWS server for authentication. Download the device certificate and the private key. Keep these safe with you and do not share with anyone

We will get the notification of successfully creating a Thing and its certificate

Let’s create a Shadow for this device. Click Create Shadow.

In the popup window, select Unnamed (classic) Shadow and click Create.

Hornbill AWS IoT Library
We will require Hornbill library for our Sketch. To download Hornbill library and examples go to this repo and download the zip file as shown below.

Extract the zip file and copy Hornbill-Examples-master > arduino-esp32 > AWS_IOT to Documents > Arduino > libraries

We will modify the AWS_IOT file which we placed in the Arduino library and set it with the certificates and keys which was provided when we created our Thing.
Open AWS_IOT > src > aws_iot_certificates.c

We will add the Amazon root CA1, the device certificate and the private key which we previously saved, inside this file.
Open the Amazon root CA1 which we saved using your text editor. Copy the Amazon root CA1 data which you obtain and head over to the aws_iot_certificates.c file. Locate const char aws_root_ca_pem[] inside the file. In the const char aws_root_ca_pem[], remove all the ‘XXXX’ and paste the root CA1 data inside it. Make sure the ‘\n\’remains after every line.
Add the private key and the device certificate in the aws_iot_certificates.c file as well. Copy and paste the contents of that file in the const char private_pem_key[]. Make sure to add ‘\n\’ after every line.
Add the device certificate, copy the contents from the device certificate file which we saved and paste it in the const char certificate_pem_crt[]. Make sure to add ‘\n\’ after every line.
Time to write our ESP32 Arduino Sketch
#include <WiFi.h>
#include <AWS_IOT.h>const char* ssid = "******"; //Write your SSID
const char* password = "*****"; //Write your password#define CLIENT_ID "ESP32"
// Open the Device Shadow we created earlier and check the MQTT properties for the following
#define MQTT_TOPIC "$aws/things/esp-32/shadow/update"
#define AWS_HOST "xxxxxx.iot.us-east-1.amazonaws.com"AWS_IOT aws;void setup(){
Serial.begin(115200);
Serial.println("Initializing....");
WiFi.begin(ssid,password);
Serial.println("Connecting to WiFi...");
while(WiFi.status()!= WL_CONNECTED){
Serial.print(".");
delay(300);
}
Serial.print("Starting connection with AWS");
if(aws.connect(AWS_HOST,CLIENT_ID)==0){
Serial.println("Connected to AWS!");
}
else{
Serial.println("Connection Failed! Check AWS HOST and Client ID");
}
}void loop(){
char payload[40];
String temp_payload = "{'Message':'Hello World'}";
temp_payload.toCharArray(payload,40);
Serial.println("Publishing...");
Serial.println(payload);
if(aws.publish(MQTT_TOPIC,payload)==0){
Serial.println("Success");
}else{
Serial.println("Failed");
}
delay(10000);}
Click on the upload button to upload the code to your ESP32 development board.

After you have uploaded your code to the development board, press its Reset button.

Open Arduino Serial Monitor and check for logs.

Let’s check if our message is published in the AWS via MQTT protocol. You can use the available MQTT test client and subscribe to the $aws/things/esp-32/shadow/update topic. We can see the payload sent by our ESP32 device.

In this article, we learned how to use AWS IoT core and use ESP32 to publish messages. This was accomplished via MQTT protocol. Using AWS MQTT, we can subscribe to message topics published by various IoT nodes. Similarly, we can also publish on specific topics from the AWS IoT core portal. If you find this helpful, kindly donate so I could continue writing articles like this.