ESP32/IoT B.Tech/Diploma Beginner

Connecting ESP32 to Amazon AWS IoT Core using MQTT

Getting started on Amazon AWS IoT Core with ESP32 via MQTT to publish/subscribe Sensor data, creating thing, policy, certificates for Arduino

  • Suitable forB.Tech/Diploma ESP32/IoT students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

Overview
This is a getting started tutorial about
Amazon Web Services
, i.e
AWS IoT Core
with
ESP32
. The AWS IoT Core is a managed
cloud service
that lets connected devices easily and securely interact with
cloud applications
and other devices.
In this tutorial, we will learn how you can connect the
ESP32 with AWS IoT Core
& publish sensor reading to
AWS MQTT
. For a demo, we will use the
DHT11 Sensor
and read the humidity temperature data. The ESP32 will connect to the local WiFi network and will post the DHT11 Sensor data to
AWS IoT Cloud
. Not only posting data, but we can also receive the data from AWS Dashboard. Earlier we posted the sensor Data to AWS Dashboard using
AWS IoT Core & ESP8266
The tutorial comprises multiple sections
Signing up & setting the Amazon Web Services
Installing Necessary
Libraries
to Arduino IDE & Writing an Arduino Sketch for the project
Creating a
Thing
is AWS
Creating
Policy
and attaching to Thing
Generating
Certificates
Modifying Arduino Sketch according to Thing Data & Credentials
&
Publish
Data to and from AWS Dashboard
This tutorial is for beginners who want to learn about the
Amazon AWS IoT Core
for
IoT Applications
. Earlier we learned about the IoT platform like
Google Firebase
and
Arduino IoT Cloud
. But with AWS IoT Core, you can build and manage devices for commercial applications.
Hardware Setup
The hardware required for this project is an ESP32 Wifi Module. And for the sensor part, we will use DHT11 Humidity and Temperature Sensor.
Connect the DHT11 Sensor to ESP32 Board as per circuit diagram here.
You can use a breadboard for connection or simply use a male-to-female connector wire.
What is Amazon AWS IoT Core?
AWS offers Internet of Things (IoT) services and solutions to connect and manage billions of devices. These cloud services connect your IoT devices to other devices and AWS cloud services. AWS IoT provides device software that can help you integrate your IoT devices into AWS IoT-based solutions. If your devices can connect to AWS IoT, AWS IoT can connect them to the cloud services that AWS provides.
AWS IoT lets you select the most appropriate and up-to-date technologies for your solution. To help you manage and support your IoT devices in the field, AWS IoT Core supports these protocols:
MQTT (Message Queuing and Telemetry Transport)
MQTT over WSS (Websockets Secure)
HTTPS (Hypertext Transfer Protocol – Secure)
LoRaWAN (Long Range Wide Area Network)
Getting Started with Amazon AWS IoT Core with ESP32
Getting started with AWS IoT core is very simple. You just need to follow the following steps given below to successfully set up the ESP32 board and start with your very first project.
Signing in
Go to your web browser and search visit following link:
aws.amazon.com/iot-core/
Basically we need to set up the AWS Account now. Therefore create an account using the email ID and password. The account also requires your bank Credit card information. There will be no charges but AWS just needs a verification using your bank account. It will also ask for phone number verification. Hence the account will be successfully created.
AWS IoT Core Dashboard
After successfully signing in, the AWS Management Console window will open. In the services search tab at the top write ‘IoT core’ & hit enter.
You can click on IoT Core, so an AWS IoT Dashboard will appear now.
On the left side of the dashboard, there are so many options. But we need to work with two options here. One is the
manage
option and the other one is the
secure
option.
Creating a Thing
Now we need to create a thing associated with our project. For this, follow the following steps:
Specifying thing properties
Configuring device certificate
Attaching policies to certificate
Under the manage option click on Thing. Now we need to create a Thing here. So, click on Create Things here.
You can select whether create a single thing or create many things. But for our applications, select create a single thing. Then click on Next.
Specify Thing Properties
Here we need to specify the Thing properties. First, give a thing name. You can name it anything. For example, I will name it
ESP32_DHT11
Under additional configurations, there is no need to make any changes.
Under the device shadow option, select the first option as No shadow. Then click on Next.
Generate Device Certificate
Now you need to configure device certificate. So here you can auto-generate a new certificate or use your own certificate or upload CSR or skip this.
But the AWS recommendation is to select the Auto Generate New Certificate. Then click on Next.
Create & Attach Policy
Now we need to attach a policy to the Things we created. But no policies are here right now. So we need to create a policy first.
So click on create policy. Here give any name to the policy. For example, I will give it a name as
“
ESP32_Policy
“
Now the add statement part is very important. Under the action, type IoT. So multiple options will pop up. From here we will only need to publish, Subscribe, Connect and Receive.
Now click on create to create the policy. So the policy has been created successfully.
Now go back to Create Thing option. So a policy option will appear. We need to attach the policies to the certificate. So select the appeared policy and click on create a thing.
Downloading Certificates and Keys
Now we need to download the required certificates from this list.
First, download the
device certificate
and then rename it as a device certificate for identification.
Also, download the
public key
and rename it as a public key. Then download the
private key
and rename it as a private key.
In the Root CA Certificates, there are two certificates here. But we just need a
Root CA1 certificate
, so download it as well.
So we have downloaded all the certificates that we need for our project.
Installing Necessary Arduino Libraries
1. ArduinoJSON Library
So first go to the library manager and search for “JSON” & install the library as shown in the figure below.
2. PubSubClient Library
Again go to the library manager and search for “PubSubClient” & install the library from Nick O’Leary.
3. DHT11 Sensor Library
Search for “dht11” & install the library as shown below.
Source Code/Program for connecting AWS IoT Core with ESP32
The code/program that
interfaces ESP32 with DHT11 Sensor
& connects to the
Amazon AWS IoT Core
is written in Arduino IDE. The code is divided into two sections. One is the main ino file and other the header file.
Main .ino File
Open a new sketch in Arduino IDE & paste the following code and save it.

#include "secrets.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include "DHT.h"
#define DHTPIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
float
h
;
float
t
;
DHT
dht
(
DHTPIN
DHTTYPE
)
;
WiFiClientSecure
net
=
WiFiClientSecure
(
)
;
PubSubClient
client
(
net
)
;
void
connectAWS
(
)
{
WiFi
mode
(
WIFI_STA
)
;
WiFi
begin
(
WIFI_SSID
WIFI_PASSWORD
)
;
Serial
println
(
"Connecting to Wi-Fi"
)
;
while
(
WiFi
status
(
)
!=
WL_CONNECTED
)
{
delay
(

)
;
Serial
(
"."
)
;
}
// Configure WiFiClientSecure to use the AWS IoT device credentials
net
setCACert
(
AWS_CERT_CA
)
;
net
setCertificate
(
AWS_CERT_CRT
)
;
net
setPrivateKey
(
AWS_CERT_PRIVATE
)
;
// Connect to the MQTT broker on the AWS endpoint we defined earlier
client
setServer
(
AWS_IOT_ENDPOINT
8883
)
;
//

Technologies used

ESP32ESP8266ArduinoLoRaLoRaWANMQTTFirebaseWiFiDHT11AWSArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • DHT11

Software & tools

  • Arduino IDE
  • Firebase
  • MQTT

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.