ESP32/IoT B.Tech/Diploma Beginner

Control Relay/LED/Lamp with AWS IoT Core using ESP32

How to Control Relay/LED/Lamp with Amazon AWS IoT Core using ESP32 WiFi Module & publish subscribe data from AWS Dashboard with Arduino Code

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

About this project

Overview
In this tutorial, we will learn how to Control Relay/LED/Lamp with Amazon AWS IoT Core using ESP32 WiFi Module. Earlier we learned about Getting Started with AWS IoT Core with
ESP32
and also
ESP8266
. The AWS IoT Core is a managed
cloud service
that lets connected devices easily and securely interact with
cloud applications
and other devices.
This tutorial is all about Controlling Peripherals like AC Appliances like Lamp or LED or any Relay from AWS IoT Dashboard using ESP32 WiFi Module. The tutorial comprises multiple sections
Setting up ESP32 Hardware
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 from AWS Dashboard
Bill of Materials
We need following components to work on this project. You can purchase all these components from Amazon using the given purchase link.
S.N.
Components
Quantity

ESP32 WiFi Module

Amazon
AliExpress

LED 5mm Any Color

Amazon
AliExpress

Resistor 560-ohm

Amazon
AliExpress

Resistor 1K

Amazon
AliExpress

1 Channel 5V Relay Module

Amazon
AliExpress

Transistor BC547

Amazon
AliExpress

Jumper Wires

Amazon
AliExpress

Breadboard

Amazon
AliExpress
Hardware Setup & Connection
The connection between ESP32 WiFi Module and LED is very simple.
Connect the positive terminal of LED to GPIO23 of ESP32 via a 560-ohm resistor. Connect its negative terminal to GND. You can use a breadboard for connection.
For a simple demo you can use an LED. But if you want to control some AC electrical appliances, you can use the 5V Relay Module. But the Relay doesn’t function using the 3.3V logic as ESP32 GPIO Pin only gives 3.3V output.
Therefore we need to use an NPN transistor which will act as a switch and control the relay even with 3.3V logic. Here is the connection diagram for this part.
Project PCB Gerber File & PCB Ordering Online
If you don’t want to assemble the circuit on a breadboard and you want PCB for the project, then here is the PCB for you. The PCB Board for AWS IoT is designed using
EasyEDA
online Circuit Schematics & PCB designing tool. First the schematic is designed and then the schematic is converted into PCB.
The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from
ALLPCB
at
1$
only.
Download Gerber File:
AWS IoT Relay
You can use this Gerber file to order high quality PCB for this project. To do that visit the
ALLPCB
official website by clicking here:
https://www.allpcb.com/
You can now upload the Gerber File by choosing the Quote Now option. From these options, you can choose the Material Type, Dimensions, Quantity, Thickness, Solder Mask Color and other required parameters.
After filling all details, select your country and shipping method. Finally you can place the order.
Setting up Amazon AWS IoT Core Dashboard
The signing up part for the Amazon Web Services is already explained in the previous post. You can follow the
AWS IoT Core Getting Started
tutorial.
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_LED
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 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.
Source Code/Program
The Source Code/Program to Control Relay/LED/Lamp with AWS IoT Core using ESP32 is written in Arduino IDE. Before moving to the code part, we need to install few libraries to the Arduino IDE. Download the following libraries and add it to the Arduino IDE library folder,
1. Arduino JSON Library:
https://github.com/bblanchon/ArduinoJson
2. PubSubClient Library:
https://github.com/knolleary/pubsubclient
The code is divided into two sections. One is the main file and the other is the header file.
Main.ino
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"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
#define lamp 23
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
)
;
// Create a message handler
client
setCallback
(
messageHandler
)
;
Serial
println
(
"Connecting to AWS IOT"
)
;
while
(
!
client
connect
(
THINGNAME
)
)
{
Serial
(
"."
)
;
delay
(

)
;
}
if
(
!
client
connected
(
)
)
{
Serial
println
(
"AWS IoT Timeout!"
)
;
return
;
}
// Subscribe to a topic
client
(
AWS_IOT_SUBSCRIBE_TOPIC
)
;
Se

Technologies used

ESP32ESP8266ArduinoMQTTWiFiBLERelayAWSArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • Relay

Software & tools

  • Arduino IDE
  • MQTT
  • EasyEDA

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.