Connect ESP32 to AWS IoT MQTT and Publish Sensor Readings

In this user guide, you will learn how to use the AWS IoT core MQTT using an ESP32 to publish sensor readings to AWS MQTT. Through AWS IoT core, we’ll show you how to publish sensor readings to Amazon web services using Arduino IDE over MQTT protocol. The sensor readings will be displayed in a dashboard. Most importantly, you can access this sensor data from anywhere in the world.

aws iot esp32 arduino ide

Any appropriate sensor can be used such as DS18B20, BME680, LM35, and MPU6050 but for this project, we will use a DHT22 sensor which is used to measure temperature and humidity. Through Amazon web services you will be able to control sensor readings using your mobile as well as on the web dashboard.

Previously, we controlled the ESP module’s outputs through applications such as Telegram, Google Firebase, Blynk App, and Arduino IoT Cloud. You can view the tutorials by accessing the links below:

We also built our personal ESP32 IoT application by using Google Firebase and MIT App Inventor. This displayed DHT11/DHT22 sensor readings on the Android application:

ESP32 Send Sensor Readings to Google Firebase and Build an Android app to display Data

This time we will look into another great application Amazon Web Services through which we will publish sensor readings obtained from the DHT22 sensor connected with the ESP32 development board.

We will require the following for our project:

Hardware Required:

  • ESP32
  • DHT22 Sensor
  • Connecting Wires
  • Breadboard

Software Required:

  • Amazon Web Services
  • Arduino IDE

Prerequisites

We will use Arduino IDE to program our ESP32 development board. Therefore, you should have the latest version of Arduino IDE. Additionally, you also need to install the ESP32 plugin in Arduino IDE. If your IDE does not have the plugin installed you can visit the link below:

Installing ESP32 library in Arduino IDE and upload code

Introduction to DHT22 sensor

The DHT22 is an inexpensive sensor which measures relative humidity and temperature sensor. It provides a calibrated digital output with a 1-wire protocol. It measures temperature and humidity with higher accuracy and supports a wider range as compared to DHT11.

DHT sensors are pre-calibrated. We can directly connect them with ESP32 board to obtain sensor output reading. They are internally composed of a humidity sensing sensor and a thermistor. These two components measure humidity and temperature.

DHT22 Pinout

The following figure shows the pinout diagram of DHT sensors. DHT sensor consists of four pins. But on DHT modules only three pins are exposed to the pinout of the module and the 10k ohm pull-up resistor is internally connected to pin 2.

dht11 dht22 pinout diagram

The following lists the pinout of the DHT sensor and their brief description. Pin number starts from left to right when you hold the sensor from the front end. It also shows how these pins will be connected with the ESP32 board.

DHT22 PinESP32
1 (VCC)3.3V
2 (Data Out)Any GPIO pins of ESP32 board along with 10k ohm pull-up resistor
3 (NC)Not used
4 (GND)Ground
  • VCC is the power supply pin. Apply voltage in a range of 3.3 V to 5.0 V to this pin
  • Data Out is the digital output pin. It sends out the value of measured temperature and humidity in the form of serial data
  • N/C is not connected
  • GND: Connect the GND pin

DHT22 Interfacing with ESP32 board

Connect the DHT22 to ESP32 along with a 10K ohm pull-up resistor. The connection diagram is shown in the picture below.

DHT22 (4 pin) with ESP32
Schematic Diagram

  • The first pin is the power supply (VCC) pin. Connect it with the 3.3 volt pin of ESP32.
  • Data out is the pin through which we get temperature and humidity samples from the DHT sensor. Connect this pin with GPIO27 of ESP32 and also connect the data pin with a 10k pull-up resistor. You can also use any appropriate digital pin of ESP32.

A Pull-up resistor is used to keep the data pin high for proper communication between the microcontroller and sensor. You can check the datasheet of DHT11 and DHT22 to get more information about it. DHT22 is also known by the name of AM2302.

  • Third pin is not used
  • Connect the fourth pin (GND) to the ground pin of the ESP32 board

AWS IoT Core

With Amazon Web Services, the user is granted inexpensive cloud computing services which are reliable and interactive. Although this web service is free to join, you will have to pay for any services which you use. For the user, it becomes extremely fun and easy to use this application to build IoT projects from anywhere over the internet. Only the Amazon web service account and a steady internet connection in your device (smartphone, laptop, tablet, etc.) are a requirement.

Getting Started with AWS IoT

Working in the Amazon Web Services is very simple and easy. Thus, follow the steps given below to successfully set up the ESP32 board and start with your very first project.

Signing in

Firstly, type https://aws.amazon.com/ in your browser search tab and press enter. This will open the following homepage of AWS. if you do not have an account click ‘Create an AWS Account’ otherwise click ‘My Account.’

AWS getting started pic1

The following page opens up. Proceed with your account details or click ‘Sign in to an existing AWS account.’

AWS getting started pic2

AWS IoT Core

After you have successfully signed in, the AWS Management Console window will open. In the services search tab at the top write ‘IoT core’ and press enter. Choose IoT core as shown in the picture below.

AWS getting started pic3

Opening up the IoT core, the following window appears. This is the IoT core homepage. Go to ‘Monitor’ to open the dashboard.

AWS getting started pic4

Creating a Thing

To successfully create a thing associated with our project we will have to follow three main steps in order. These are:

  1. Specifying thing properties
  2. Configuring device certificate
  3. Attaching policies to certificate

First, to create a Thing go to Manage > Things. This opens the Things interface. Click ‘Create things’ to proceed further.

AWS getting creating thing pic1

The following window will open. Select ‘create a single thing.’ Then click ‘Next.’

AWS getting creating thing pic2

Specify Thing Properties

Now, give a name to your thing. You can use any name according to your preference. In our case, we have specified it as ‘DHT22_Sensor_Data.’ Leave the other properties as default. Scroll down and click ‘Next’ to move to the next step.

AWS getting creating thing pic3

Configure Device Certificate

Next, select the options as shown below to generate the certificate associated with the thing.

AWS getting creating thing pic4

Attach Policy

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

AWS creating policy

A new tab will open to create the policy. Give a name to your policy and add statements. In our case we have named the policy as ‘DHT22_Policy’ and set * signs in both Action and Resource ARN to allow ALL. Click ‘Create’ to finish creating the policy.

AWS creating policy pic2

You will get the notification of successfully creating the policy.

AWS creating policy pic3

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

AWS creating policy pic4

Downloading Certificates and Keys

Now we will have to download the key files for our certificate. 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 otherwise the security of your project might get compromised.

AWS device certificate and keys pic1

Scroll down and you will be able to find the Root CA certificates. Download and save Amazon Root CA 1. We will use it in a later step. After the downloading is complete click ‘Done.’

AWS device certificate and keys pic2

Now, we will get the notification of successfully creating the thing and creating its certificate. You will notice that ‘DHT22_Sensor_Data’ thing has been created.

AWS getting creating thing pic6

Installing Required Libraries

Now, let’s move to the next step. We will be required to download two libraries for this project. These include:

  1. Hornbill Examples
  2. DHT sensor library
  • We will use GitHub to download the libraries. To download Hornbill examples, click here. Download the zip file as shown below.
AWS downloading library Hornbill examples

Click the Code button and go to the Download Zip option as highlighted in the figure. Your zip file will get downloaded to your computer right away.

  • Likewise, to download the DHT sensor library click here. Download the zip file as shown below.
AWS downloading DHT sensor library

Click the Code button and go to the Download Zip option as highlighted in the figure. Your zip file will get downloaded to your computer right away.

After both libraries are downloaded, place them in a folder ‘AWS ESP32 project.’ Then extract both of them. Rename the DHT-sensor-library-master as ‘DHT-sensor-library.’ Open this folder. Inside it you will find the following files.

AWS downloading DHT sensor library pic2

Delete the following files: DHT_U.cpp and DHT_U.h

These are also highlighted in the picture. For our project, we do not require these files. Copy this folder and go to Documents > Arduino > libraries and paste it inside the Arduino libraries folder.

Now go to Hornbill-Examples-master > arduino-esp32 > AWS_IOT.

AWS downloading library Hornbill examples pic2

Copy this AWS_IOT file and place it in the Arduino libraries folder.

Modifying the aws_iot_certificates.c according to the Thing

Now, we will modify the AWS_IOT file which we placed in the Arduino library according to certificates and keys associated with our Thing. First go to AWS_IOT > src > aws_iot_certificates.c and open it in MS Word. It will look something like this:

AWS modifying sketch pic1

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. As it is a .pem file so we will use a file converter to open the document. We are using the following website: https://filext.com/file-extension/PEM. This will open the .pem file and you will be able to access the data inside it. Copy the Amazon root CA1 data which you obtain and head over to the aws_iot_certificates.c file which we opened in MS Word. Locate const char aws_root_ca_pem[] inside the file.

AWS modifying sketch pic2
Before

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.

AWS modifying sketch pic3
After adding root CA1

Likewise, we will add the private key and the device certificate in the aws_iot_certificates.c file as well.

First, to add the private key open the private key .test file which you saved. Copy and paste the contents of that file in the const char private_pem_key[]. Make sure to add ‘\n\’ after every line.

AWS modifying sketch pic4

Similarly, to 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.

AWS modifying sketch pic5

AWS IoT ESP32 Arduino Sketch

Open your Arduino IDE and create a new file. Copy the code given below in that file. You need to enter your network credentials. Additionally, you also have to provide your Client ID, MQTT topic, and AWS host.

#include <WiFi.h>
#include "DHT.h"
#include <AWS_IOT.h>

#define DHT_PIN 27
#define DHT_TYPE 22

const char* ssid = "*********"; //Write your SSID
const char* password = "**********";   //Write your password

#define CLIENT_ID "Temp_Humidity"
#define MQTT_TOPIC "$aws/things/DHT22_Sensor_Data/shadow/update"
#define AWS_HOST "a1722qfsc********-ats.iot.ap-south-1.amazonaws.com"

DHT dht(DHT_PIN,DHT_TYPE);
AWS_IOT aws;

void setup(){
  Serial.begin(115200);
  Serial.print("Initializing....");
  
  WiFi.begin(ssid,password);
   Serial.print("Connecting to WiFi...");
  while(WiFi.status()!= WL_CONNECTED){
    Serial.print(".");
    delay(300);
  }
  dht.begin();
  Serial.print("Starting connection with AWS");
  if(aws.connect(AWS_HOST,CLIENT_ID)==0){
    Serial.print("Connected to AWS!");
  }
  else{
    Serial.print("Connection Failed! Check AWS HOST and Client ID");
  }
}

void loop(){
  float hum = dht.readHumidity();
  float temp = dht.readTemperature();  
  
   if (isnan(hum) || isnan(temp)  ){
    Serial.println(F("Failed to read from DHT sensor!"));
    return;}
    else{
      String temp_humidity = "Tempertaure: ";
      temp_humidity += String(temp);
      temp_humidity += " degree Celsius Humdity: ";
      temp_humidity += String(hum);
      temp_humidity += "%";
      char payload[40];
      temp_humidity.toCharArray(payload,40);
      Serial.println("Publishing...");
      Serial.println(payload);

      if(aws.publish(MQTT_TOPIC,payload)==0){
        Serial.println("Success");
      }
      else{
Serial.println("Failed");
      }
    }
    delay(1000);
   }

How does the Code Work?

Now, let us understand how each part of the code works.

Including Libraries

Firstly, we will include the relevant libraries which are necessary for this project. We are using three libraries: WiFi.h, AWS_IOT.h and DHT.h
WiFi.h library is used to connect our ESP32 module with the local WIFI network. The other libraries are the ones that we previously installed and will be required for the AWS and sensor functionality.

#include <WiFi.h>
#include "DHT.h"
#include <AWS_IOT.h>

Defining DHT Sensor

The following lines of code will specify the type of DHT sensor and the GPIO pin of the ESP32 board which we will connect with the data pin of the sensor.

#define DHT_PIN 27
#define DHT_TYPE 22
DHT dht(DHT_PIN,DHT_TYPE);

Defining Network Credentials

Next, we will create two global parameters of type char which will hold the SSID and password. Replace the ‘*’ with your network credentials.

const char* ssid = "********"; //Write your SSID
const char* password = "***********";   //Write your password

Defining AWS Parameters

Now, we will specify the client ID which we will call ‘Temp_Humidity.’ We are using MQTT protocol to transfer data from our ESP32 board to the Amazon web services. For MQTT topic and AWS host we will use the values which will be accessed from the AWS web site.

Go to AWS IoT > Manage > Things > DHT22_Sensor_Data > Classis Shadow. Scroll down and click MQTT topic. Copy the ‘update’ MQTT topic associated with the ‘Publish’ action and define it in the Arduino sketch as ‘MQTT_TOPIC.’

AWS MQTT topic

Likewise, the AWS host can be found if you go to AWS IoT > Settings. Copy the device data endpoint and define it as AWS_HOST in the Arduino sketch.

AWS host name
#define CLIENT_ID "Temp_Humidity"
#define MQTT_TOPIC "$aws/things/DHT22_Sensor_Data/shadow/update"
#define AWS_HOST "a1722qfsc******-ats.iot.ap-south-1.amazonaws.com"

Creating AWS_IOT object

We will also create an AWS_IOT object called ‘aws’ which we will use later on in the program code.

AWS_IOT aws;

setup()

Inside the setup() function, we will start the serial communication at a baud rate of 115200.

Serial.begin(115200);

The following section of code will connect our ESP32 board with the local network whose network credentials we already specified above. We will use the WiFi.begin() function. The arguments will be the SSID and the password which we defined earlier in the code.

WiFi.begin(ssid,password);
   Serial.print("Connecting to WiFi...");
  while(WiFi.status()!= WL_CONNECTED){
    Serial.print(".");
    delay(300);
  }

After the connection will be established, we will initiate the connection with the DHT sensor using dht.begin()

 dht.begin();

The following section of code will check for a successful connection with AWS by using aws.connect() and the AWS host and client ID parameters. When the ESP32 board makes a succesful connection with AWS the serial monitor will display “Connected to AWS.” Otherwise, it will display a failed connection message.

 Serial.print("Starting connection with AWS");
  if(aws.connect(AWS_HOST,CLIENT_ID)==0){
    Serial.print("Connected to AWS!");
  }
  else{
    Serial.print("Connection Failed! Check AWS HOST and Client ID");
  }

loop()

Inside the loop() function we will obtain the sensor readings and handle them accordingly. Firstly, through dht.readHumidity(), the sensor reading will get saved in the variable ‘hum.’ Likewise, for temperature, the sensor reading will get saved in the variable ‘temp.’

float hum = dht.readHumidity();
float temp = dht.readTemperature();  

If the connection between the module and the sensor is incorrect or the values are not being accessed properly then an error message will be printed. This will help us in debugging our circuit or a possible issue in the initialization of the sensor.

if (isnan(hum) || isnan(temp)  ){
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
}

Else, we will create a string variable ‘temp_humidity’ that will publish the temperature and humidity sensor readings to AWS via MQTT protocol.

 String temp_humidity = "Tempertaure: ";
      temp_humidity += String(temp);
      temp_humidity += " degree Celsius Humdity: ";
      temp_humidity += String(hum);
      temp_humidity += "%";
      char payload[40];
      temp_humidity.toCharArray(payload,40);
      Serial.println("Publishing...");
      Serial.println(payload);

      if(aws.publish(MQTT_TOPIC,payload)==0){
        Serial.println("Success");
      }
      else{
Serial.println("Failed");
      }

Subscribing Thing on AWS MQTT

To subscribe the DHT22_Sensor_Data Thing on AWS go to AWS IoT > Test. In the Topic filter search bar, copy and paste the MQTT topic for publishing. Then click Additional configuration.

AWS subscribe thing AWS pic1

Now change the MQTT payload display to ‘Display payloads as strings’ and hit Subscribe to finish the procedure.

AWS subscribe thing AWS pic2

We have successfully subscribed our Thing with Amazon Web Services for publishing data.

Demonstration

Choose the correct board and COM port before uploading your code to the board. Go to Tools > Board and select ESP32 Dev Module.

select esp32 board

Next, go to Tools > Port and select the appropriate port through which your board is connected.

Selecting COM PORT ESP32

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 ENABLE button.

ESP32 enable reset button

In your Arduino IDE, open up the serial monitor and you will be able to see the status of your WIFI and the AWS connection.

Shortly afterward, you will be able to view the temperature and humidity readings which will simultaneously get published in the AWS via MQTT protocol.

Open the AWS publish page to view the sensor data being published.

We will shortly update the article with a demo video.

Conclusion

In conclusion, we were able to learn about Amazon Web Services (AWS) and how to use AWS IoT core to get sensor readings. This was accomplished via MQTT protocol. Using AWS MQTT, we can subscribe to sensor readings topics published by various IoT nodes. Similarly, we can also publish on specific topics from the AWS IoT core portal.

1 thought on “Connect ESP32 to AWS IoT MQTT and Publish Sensor Readings”

Leave a Comment