paint-brush
Cloud Home Automation Series Part 1 : Connect ESP32 to AWS IoT with Arduino codeby@gourav-das
12,390 reads
12,390 reads

Cloud Home Automation Series Part 1 : Connect ESP32 to AWS IoT with Arduino code

by Ask Gourav DasFebruary 9th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Cloud Home Automation Series Part 1 : Connect ESP32 to AWS IoT with Arduino code. Series split into four parts (refer below) with very simple and clear instructions to provision a home automation system to control house appliances through the web. The entire series mainly focuses on the Hardware integration with AWS Cloud to make a system of interrelated computing devices. This tutorial mostly covers hands-on, prior knowledge of AWS IoT is an advantage. The readership, essentially Embedded System Engineer & Cloud Engineer will able to learn to create Automated IoT Solutions using ESP32.

Company Mentioned

Mention Thumbnail
featured image - Cloud Home Automation Series Part 1 : Connect ESP32 to AWS IoT with Arduino code
Ask Gourav Das HackerNoon profile picture

Welcome to 

SIMPLE LEARNING
AWS Cloud Home AutomationZero to Hero Series, where the readership, essentially Embedded System Engineer & Cloud Engineer will able to learn to create Automated IoT Solutions using ESP32 Module (Hardware), AWS IoT portfolio services (MQTT & Integration assistance) & Arduino sketch (Code). The entire series mainly focuses on the Hardware integration with AWS Cloud to make a system of interrelated computing devices.
The following series split into four parts (refer below) with very simple and clear instructions to provision a home automation system to control house appliances through the web.
Everything covered from scratch you won't face any difficulty understanding. In case of any clarification, drop me a note on LinkedIn. Feel free to explore them with ease, skip to the one which is relevant to you.

We are going to use ESP32 MCU which is a

low-cost & low-power device
, with inbuilt Wi-Fi module, thus acts as a Cloud Communication Module (CCM) & we can create wonders with the conjunction of sensors & modules if used in ESP32.

Security of connected devices is of predominant concern. The widely used Transport Layer Security (TLS) version 1.2 is the de-facto standard used on the web for secure connections including banking and financial institute. There are very few micro-controllers that supports TLS 1.2 and ESP32 is one of them. Good news is that AWS IoT Core maintains TLS 1.2 and above, thus making the security robust 
. 

Prerequisites:

  1. Arduino IDE (arduino.cc/en/main/software).
  2. ESP32 Board (Easily available at Amazon.com/Amazon.in/Aliexpress or neighborhood Robotics store), they cost around 6$ or 350-450 INR.
  3. AWS Free-Tier Account. (Services used here will cover up in free tier limit). Choose the N. Virginia Region only throughout the series.
  4. This tutorial mostly covers hands-on, prior knowledge of AWS IoT is an advantage. Click here to check AWS Official IoT Documentation.

Learning Objectives:

  1. Install Arduino core for the ESP32.  [Assuming Arduino IDE preinstalled]
  2. Install AWS ESP32 Hornbill Library which binds ESP32 with AWS Core.
  3. In AWS Console, will create a Device thing (for ESP32), Certificates & Private Key (for Authentication) & Policy (for Authorization).
  4. Upload the Certificates & Arduino sketch in ESP32 to send dummy Temperature & Humidity sensor data to AWS MQTT Broker.
  5. Bonus Challenge: Create IoT rule to set a notification whenever temperature crosses the threshold.

So let's see something happen now.

Kindly look for the step-by-step screenshots to follow the instructions vividly, kept it separate to make the document minimalistic. 

1. Install Arduino core for the ESP32

To Install the espressif/arduino-esp32 core, click here

 for step-by-step instructions
.  Too lazy to be redirected to a new tab, follow the below steps (Easy Hack). If you have already installed Arduino core for the ESP32 manually (ESP32 Essential Libraries & Board configuration) you can skip to the next part.

  • Open Arduino Software (IDE), installed on your local system. Go to File in the toolbar and select Preferences.
  • Next, enter https://dl.espressif.com/dl/package_esp32_index.json into the “Additional Board Manager URLs” field as shown in the figure below.
  • Now select Boards Manager in the toolbar. Go to Tools > Board > Boards Manager.
  • Here Search for ESP32 and press install button for the “ESP32 by Espressif System”.
  • After that, you will able to see a progress bar below (It may take a few seconds to several minutes depending on internet speed). Once installation completed you be able to see installed next to Espressif System.

2. Install Hornbill Library to Connect ESP32 with AWS

In this part, we will install the Hornbill AWS library which integrates the micro-controller, ESP32 with AWS Cloud, securely and anchored the communication channel with TLS 1.2. Click here to download the AWS_IOT Hornbill library. Exact the Hornbill-Examples-master.zip & go-to Hornbill-Examples-master\arduino-esp32, copy AWS_IOT and paste it to C:\Users\{Your User Name}\Documents\Arduino\libraries.

3. AWS Integration

3.1 Create AWS IoT Policy

Click-here

 to refer the step-by-step screenshots to create AWS IoT Policy
.

Click Here to go to AWS IoT Console we will select Policies under the

Secure
option from the Sidebar and click Create [Top Right Corner]. In the next step, we will give a name to this policy (e.g. ESP32Policy), under Action copy-paste the following; iot:Publish, iot:Connect, iot:Subscribe, iot:Receive, under Resource ARN put *, under Effect mark allow and click Create to provision the policy. In short, we are trying to create an AWS policy which will provide limited access to ESP32 to AWS resources.

3.2 Register the ESP32 Thing

Click-here

to refer the step-by-step screenshots to register the Thing.

Lastly, we will select Things under the

Manage
option from the Sidebar and click Create [Top Right Corner] and follow the below steps.

  1. Next, select Create a single Thing
  2. In the next step, give a name for Example; ESP32 and keep everything Default & click next
  3. In the Next step, Select Create Certificate. 
  4. In the final step, we will download the Thing Certificate and the Private Key and keep it safe as we need it in the next section (No need to download the Public Key), select activate and click Attach a policy (Note: Don't press Done or else you need to repeat the step) and select the policy (ESP32Policy) created in the previous section. Thus, we are done provisioning the digital representation of ESP32 board in AWS. Next, we will do the hardware integration.

4. Hardware Integration

4.1 Certificates and Private Key Enrollment

Click here

to refer the step-by-step screenshots to enrol the Certificates and the Private Key.

Here comes the fun part where we can see our real progress, firstly we will register the certificates and private key to our AWS_IOT library. Go to the following directory, C:\Users\YourUsername\Documents\Arduino\libraries\AWS_IOT\src, open the file aws_iot_certficates.c in an editor (Notepad++). Here we need to attach the Thing Certificate and Private Key which we have downloaded in the previous section along with the CA certificate, click here to copy or download the CA certificate. Now the tricky part, as we can see the blank spaces, represented with multiple:x, here we will inscribe the certificates and the private key. In aws_root_ca_pem[] we will set the CA certificate, in private_pem_key[] we will set the private key and  certificate_pem_crt[] we will set the Thing Certificate.

4.2 Arduino Code in ESP32

Click here 

to refer the step-by-step screenshots to upload the Arduino Code in ESP32 Board and test the incoming messages in AWS MQTT Client.

Next, we will work on the Arduino IDE and upload the Arduino code in the ESP32 Board. Make sure, you have restarted the Arduino IDE after following the instructions in the beginning & Plug your ESP32 board to your Desktop/Laptop using a USB cable. In the code, we are going to send dummy Humidity and Temperature data, replicating a DHT11/22 Sensor to Topic ESP32/DHT11.

  1. Click here to download Arduino Code (Ino file) and open it using Arduino IDE.
  2. Select Tools (from the Toolbar) and choose the following options; for Board select ESP32 Dev Module, for Upload Speed select 115200 & for Port select COM3 (Though I will suggest check COM Port No. from device manager → Ports (COM & LPT) as it varies).
  3. In the code, you need to add your Wifi Name & Password and Host_Address which you will find in AWS IoT Core main dashboard (Click Here to go to AWS IoT Main Dashboard), go to Settings (Second last option in the Sidebar) & under Custom endpoint, you will find the Endpoint (Host_Address) copy this and add this as the HOST_ADDRESS[] in the code.
  4. Click Upload (Look for the right arrow at the Top) in Arduino IDE to start the code transfer to ESP32. Hold the Boot Button in ESP32 until the code gets uploaded completely (You will able to see a message "Done Uploading" below, once done).
  5. Open the Serial Monitor from Tools, a separate console will open. Select 115200 baud below. If you can see the publish messages you are almost done.
  6. Final mission: Check the messages coming to AWS, Click Here to go to AWS IoT Console, Region: N. Virginia. Choose Test from the Sidebar it will connect the MQTT client. Under Subscription topic, put ESP32/DHT11 and keep everything default and Click Subscribe to topic and after that, you be able to see all the published messages coming from your ESP32.
    Voila! You did the integration successfully :)

Bonus (Optional): Create IoT rule to set a notification whenever temperature crosses the threshold ?

Complete this bonus section. Refer the following Document for more details. If you find any difficulties. Connect me @ https://linkedin.com/in/dasgourav

In the next part, we will turn ON/OFF the inbuilt led of ESP32 using AWS Device Shadow.Hope to see you again :)

Kindly Share & Comment only if you find it useful and help me on my mission to educate and familiarize people in the world of digitization 💪 #This is a Free tutorial and all my upcoming tutorials will be free and accessible from Public forums# Appreciate if you drop me a note on 
LinkedIn
 & share your opinion. Don't worry, I don't bite 👻 so don't shy away 🏃🏻‍♀️ 🏃🏻. Your feedback will help me to come up  with more awesome contents on the internet.