IOT

Home Automation using Amazon AWS IoT Core & ESP32

This is a simple project for home automation utilizing the ESP32 WiFi Module and Amazon AWS IoT Core. Getting Started with AWS IoT Core using ESP32 and ESP8266 was already covered. Additionally, we used the ESP32 to create an AWS IoT Lamp that was controlled by the AWS Dashboard.

A managed cloud service called AWS IoT Core enables connected devices to quickly and securely communicate with other gadgets and cloud-based software. You can send and receive MQTT messages to and from AWS IoT Core using the MQTT messaging service. We may essentially receive or send any data to the AWS IoT Core dashboard using the Publish/Subscribe functionality.

This article explains how to use an ESP32 WiFi module to control peripherals like AC appliances and lightbulbs through a relay from an AWS IoT dashboard. Essentially, we would refer to it as a home automation project. The autonomous management of your house’s technological gadgets is known as home automation. Due to their Internet connectivity, these gadgets can be managed from a distance.

Hardware Required

S.N.COMPONENTS NAMEDESCRIPTION
1ESP32ESP-WROOM-32 ESP32 ESP-32S Development Board
2Relay5V Relay
3Voltage RegulatorLM7805 5V IC
4DC Power JackDCJ0202
5Diode1N4007
6Resistor330ohm
7NPN TransistorBC547
8Terminal Block5mm
9LED5mm LED Any Color
10Female Header2.54mm Female Header

Circuit Diagram & Hardware

The AWS IoT Core & ESP32 home automation circuit is fairly straightforward. The schematic was created using the Fritzing software. Only the breadboard Assembly is shown in the diagram below.

Home Automation ESP32 AWS IoT Core

There are several GPIO pins on the ESP32 Board. For the purpose of controlling the relay in this project, GPIO19, GPIO21, GPIO22, and GPIO23 will be used. For testing and demonstration, we’ll place bulbs across the relay’s output. The design can be powered by a 9V DC adapter because the ESP32’s Vin is connected to the output of the 7805 Voltage Regulator IC.

Home Automation AWS IoT Core ESP32

Project PCB Gerber File & PCB Ordering Online

Here is the PCB you need if you don’t want to put the circuit together on a breadboard and want it for your project. I created the PCB using EasyEDA. As shown below, the PCB Board for Home Automation using ESP32 looks like.

Home Automation ESP32 PCB

The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from https://www.nextpcb.com/

Download Gerber File: ESP32 Home Automation PCB

Now you can visit the NextPCB official website by clicking here: https://www.nextpcb.com/. So you will be directed to NextPCB website.

You can assemble the components on the PCB Board.

Setting up Amazon AWS IoT Core Dashboard

The AWS Management Console window will open after successful login. IoT core should be typed into the services search bar at the top and entered.

AWS IoT Core Dashboard

You can select IoT Core to bring up the AWS IoT Dashboard.

There are a tonne of settings on the dashboard’s left side. But in this case, we only have two choices. The manage option is one, and the secure option is another.

  • Creating a Thing

Now we have to make something related to our project. To achieve this, take the following actions:

  • Defining object characteristics
  • Establishing the device certificate
  • Certificates with policies attached

Click Thing under the manage option. We must now make a Thing in this place. Therefore, click here on Create Things.

You have the option to either create one object or multiple things. However, choose design a single thing for our applications. then select Next.

  • Specify Thing Properties

Here, we must specify the properties of the thing. Give the object a name first. Any name you like can be used. For illustration, I’ll call it Home Automation.

There is no need for modifications in other setups.

Choose the first option, No shadow, under the device shadow option. then select Next.

  • Generate Device Certificate

Configuring the device certificate is the next step. Therefore, you have the option to skip this step, upload a CSR, use your own certificate, or have one automatically generated.

However, choosing the Auto Generate New Certificate is what AWS advises. then select Next.

  • Create & Attach Policy

We now need to give the Things we made a policy. But at this time, there are no policies. Therefore, we must first draught a policy.

Therefore, select Create Policy. Here, you may give the insurance any name. For illustration, I’ll call it “Automation Policy.”

The portion about the add statement is now crucial. Enter “IoT” under the action. Multiple choices will so appear. We only need to Subscribe, Connect, and Receive from here.

To create the policy, click the Create button now. Therefore, the policy was successfully formed.

Return to the Create Thing option now. Thus, a policy alternative will be presented. The policies must be joined to the certificate. Choose the policy that appears, then click Create Thing.

Downloading Certificates and Keys

From this list, we must now download the necessary certifications.

Download the device certificate first, and then rename it to reflect that for identifying purposes.

Additionally, save the public key as a public key after downloading it. The private key can then be downloaded and renamed to a private key.

There are two certificates among the Root CA Certificates. But first, download the Root CA1 certificate since that is all we require.

So we have downloaded all the certificates that we need for our project.

Source Code/Program

Before continuing on to the code phase, we need to install a few libraries to the Arduino IDE. Download the following libraries and add them to the Arduino IDE library folder, then open the Arduino IDE and select “Add Library”.

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

The following code should be pasted into a new sketch and saved in the Arduino IDE.

#include “secrets.h”
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include “WiFi.h”

#define AWS_IOT_SUBSCRIBE_TOPIC1 “esp32/lamp1”
#define AWS_IOT_SUBSCRIBE_TOPIC2 “esp32/lamp2”
#define AWS_IOT_SUBSCRIBE_TOPIC3 “esp32/lamp3”
#define AWS_IOT_SUBSCRIBE_TOPIC4 “esp32/lamp4”

#define lamp1 19
#define lamp2 21
#define lamp3 22
#define lamp4 23

WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);


void messageHandler(char* topic, byte* payload, unsigned int length)
{
Serial.print(“incoming: “);
Serial.println(topic);

/*##################### Lamp 1 #####################*/
if ( strstr(topic, “esp32/lamp1”) )
{
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
String Relay1 = doc[“status”];
int r1 = Relay1.toInt();
if(r1==1)
{
digitalWrite(lamp1, LOW);
Serial.print(“Lamp1 is ON”);
}
else if(r1==0)
{
digitalWrite(lamp1, HIGH);
Serial.print(“Lamp1 is OFF”);
}
}

/*##################### Lamp 2 #####################*/
if ( strstr(topic, “esp32/lamp2”) )
{
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
String Relay2 = doc[“status”];
int r2 = Relay2.toInt();
if(r2==1)
{
digitalWrite(lamp2, LOW);
Serial.print(“Lamp2 is ON”);
}
else if(r2==0)
{
digitalWrite(lamp2, HIGH);
Serial.print(“Lamp2 is OFF”);
}
}

/*##################### Lamp 3 #####################*/
if ( strstr(topic, “esp32/lamp3”) )
{
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
String Relay3 = doc[“status”];
int r3 = Relay3.toInt();
if(r3==1)
{
digitalWrite(lamp3, LOW);
Serial.print(“Lamp3 is ON”);
}
else if(r3==0)
{
digitalWrite(lamp3, HIGH);
Serial.print(“Lamp3 is OFF”);
}
}

/*##################### Lamp 4 #####################*/
if ( strstr(topic, “esp32/lamp4”) )
{
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
String Relay4 = doc[“status”];
int r4 = Relay4.toInt();
if(r4==1)
{
digitalWrite(lamp4, LOW);
Serial.print(“Lamp4 is ON”);
}
else if(r4==0)
{
digitalWrite(lamp4, HIGH);
Serial.print(“Lamp4 is OFF”);
}
}
Serial.println();
}


void setup()
{
Serial.begin(115200);

pinMode (lamp1, OUTPUT);
pinMode (lamp2, OUTPUT);
pinMode (lamp3, OUTPUT);
pinMode (lamp4, OUTPUT);

digitalWrite(lamp1, HIGH);
digitalWrite(lamp2, HIGH);
digitalWrite(lamp3, HIGH);
digitalWrite(lamp4, HIGH);

WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

Serial.println(“Connecting to Wi-Fi”);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}

// 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.print(“.”);
delay(100);
}

if (!client.connected())
{
Serial.println(“AWS IoT Timeout!”);
return;
}

// Subscribe to a topic
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC1);
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC2);
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC3);
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC4);

Serial.println(“AWS IoT Connected!”);
}


void loop()
{
client.loop();
delay(1000);
}

Secrets.h

In the Arduino IDE, create a new tab and give it the name Secrets.h. then add the subsequent code to this file.

#include <pgmspace.h>
#define SECRET
#define THINGNAME “***********” //change this
const char WIFI_SSID[] = “***********”; //change this
const char WIFI_PASSWORD[] = “***********”; //change this
const char AWS_IOT_ENDPOINT[] = “***********”; //change this
// Amazon Root CA 1
static const char AWS_CERT_CA[] PROGMEM = R”EOF(
—–BEGIN CERTIFICATE—–

—–END CERTIFICATE—–
)EOF”;
// Device Certificate //change this
static const char AWS_CERT_CRT[] PROGMEM = R”KEY(
—–BEGIN CERTIFICATE—–

—–END CERTIFICATE—–
)KEY”;
// Device Private Key //change this
static const char AWS_CERT_PRIVATE[] PROGMEM = R”KEY(
—–BEGIN RSA PRIVATE KEY—–

—–END RSA PRIVATE KEY—–

)KEY”;

Modifying Arduino Sketch according to the Thing

It’s time to change the Arduino Sketch File at this point. Start making the change in the secrets.h tab.

Here, an object name must be included. You can copy the item name by going to the things area of AWS Console. Home Automation is the project’s Things name. To the next line of code, paste the thing’s name.

#define THINGNAME “***************” 

Enter the WiFi SSID and password for your local network under the WiFi SSID and password field.

const char WIFI_SSID[] = “***************”;
const char WIFI_PASSWORD[] = “***************”;

The AWS IoT Endpoint must now be added here. Navigate to the AWS Dashboard’s settings page to obtain the endpoint. You will receive the finished result.

To copy the endpoint, click the copy icon. Paste it on the following line when you return to the Arduino IDE.

const char AWS_IOT_ENDPOINT[] = “***************”;  

You need to insert the Amazon Root CA1 in between the following line.

// Amazon Root CA 1
static const char AWS_CERT_CA[] PROGMEM = R”EOF(
—–BEGIN CERTIFICATE—–

—–END CERTIFICATE—–
)EOF”;

We must therefore return to the certificate that we earlier downloaded for this. Copy the entire document’s contents by opening it in Notepad++.

Then return to the Arduino IDE and enter the pasted text between the beginning and ending certificates.

We must paste the device certificate text between the “Device Certificate” lines. Therefore, open the device certificate file that we previously downloaded. Copy the content once more, and then paste it in between the beginning and ending certificate sections.

// Device Certificate
static const char AWS_CERT_CRT[] PROGMEM = R”KEY(
—–BEGIN CERTIFICATE—–

—–END CERTIFICATE—–

We must enter the device’s private key under “Device Private Key.” Return to the downloaded folder and use Notepad++ to access the device’s private key file. To paste the content between the begin and end areas, copy it once again.

// Device Private Key
static const char AWS_CERT_PRIVATE[] PROGMEM = R”KEY(
—–BEGIN RSA PRIVATE KEY—–

—–END RSA PRIVATE KEY—–

Therefore, the Arduino Sketch has been fully modified for AWS IoT Lamp/LED/Relay.

Home Automation using Amazon AWS IoT Core & ESP32

Connect the ESP32 to your computer once all of the modifications have been made. Then pick the ESP32 Board that you are using for this project under Tools. Additionally, pick the COM port. Then, to submit the code to the ESP32 board, select the upload option.

Note: Disconnect the relay from the power supply when downloading the code.

Open the Serial Monitor once the code uploading is complete. The ESP32 will attempt a WiFi network connection. It will attempt to connect to the AWS IoT Server once it is connected to the WiFi network. This notification will appear on the Serial Monitor.

We must now determine whether we can publish a topic to control all of the household appliances.

You need to publish a subject in order to achieve that. Enter “esp32/lamp1” in the topic filter section to do that. Enter the next command under Message Payload.

{
“status”: “1”
}

Do nothing under further configuration. then select “Publish.” The communication transmitted to the serial monitor is immediately visible.

Home Automation Serial

At this point, Relay 1 will immediately turn on. Additionally, any devices linked to Relay1 will switch on.

Home Automation AWS IoT Core ESP32

Sending 0 instead of 1 from the publish window of AWS Dashboard will now turn off the Relay1. Immediately switch off the relay.

Home Automation Project using Amazon AWS Iot Core and ESP32

The topics “esp32/lamp2,” “esp32/lamp3,” and “esp32/lamp4” are published to manage Relay2, Relay3, and Relay4. The commands “on” and “off” are sent by the numbers 1 and 0, respectively.

This is how you can create your own Home Automation project and control any household appliances using Amazon AWS IoT Core and an ESP32 WiFi Module. LEDs in 4 distinct hues are used for the demonstration. You can use Relay to control any appliance in place of LEDs.

Conclusion

I hope all of you have understood how to design Home Automation using Amazon AWS IoT Core & ESP32. We MATHA ELECTRONICS will be back soon with more informative blogs soon.

Leave a Reply

Your email address will not be published.