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


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 or code (Firmware). The entire series mainly focuses on the Hardware integration with AWS Cloud to make a system of interrelated computing devices.

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. The series divided into 4 parts (Refer Below) with very simple & clear instructions from scratch so that anyone could apprehend it easily without any difficulties. In case of any clarification, drop me a note in LinkedIn. Feel free to explore them with ease, skip to the one which is relevant to you. 

  1. Part I – Connect your device (ESP32) to AWS cloud.
  2. Part II – Use Device Shadow Service (AWS IoT) to control ESP32 inbuilt led.
  3. Part III – Create secure web-endpoint hosted in Node-RED to control devices from the web. [Coming Soon]
  4. Part IV – The Real Deal: Create an automation system to control the Home appliance from the web. [Coming Soon]

Security of connected devices is of predominant concern. The widely used Transport Layer Security (TLS) version 1.2 is the latest standard used on the web for secure connections including banking. There are very few micro-controller based solutions that implement TLS 1.2 due to lack of resources. 

ESP32 is one of the most affordable options out there and integrates well with AWS as it supports only TLS 1.2

.

Prerequisites:

  1. Arduino IDE (arduino.cc/en/main/software).
  2. ESP32 Board (Easily available in Amazon.com/Amazon.in/Aliexpress or Local Robotics Shop), they cost around 6$ or 350-450 INR.
  3. AWS Free-Tier Account. (Services used here will cover up in free tier limit)
  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 ESP32 espressif/arduino-esp32 library , which the main source library of ESP32 board and contains all necessary functions.  [Assuming Arduino IDE preinstalled]
  2. Install AWS ESP32 Hornbill Library which binds ESP32 with AWS.
  3. In AWS Console, will create a Device thing (for ESP32), Certificate (for Authentication) & Policy (for Authorization).
  4. Upload the Certificates & Arduino sketch in ESP32 to send 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. To Install the espressif/arduino-esp32 library in windows,

click here

 for step-by-step instructions

. To install in other platforms, click here and select the required platform in the Installation Instructions. 

Too lazy to follow the instructions, follow the below steps for 

Windows

 (Easy Hack). 
  • Create the last three highlighted folders :
    C:Users{UserName}DocumentsArduino
    hardwareespressifesp32
     

    and make the complete folder structure as shown.

  • Either clone the repository, select esp32 as the target folder or click here and download the master zip. Extract arduino-esp32-master.zip inside esp32 folder, make sure all the extracted files inside the arduino-esp32-master folder copied to esp32 folder.
  • Open the following folder:
    C:Users{Username}DocumentsArduinohardwareespressifesp32tools

    and open get.exe (Ran as an Admin). When get.exe finishes, you should see the following files in the directory → Click here .

  • Plug your ESP32 board to your Desktop/Laptop using USB cable and wait for the drivers to install.
2. In this part, we will install the essential 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-masterarduino-esp32, copy AWS_IOT and paste it to C:Users{Your User Name}DocumentsArduinolibraries.
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 the policy (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.

Click-here

 to refer the step-by-step screenshots to create policy

.

Lastly, we will select Things under the

Manage

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

Click-here

 to refer the step-by-step screenshots to register the Thing.
  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 provisioing the digital representation of ESP32 board in AWS. Next, we will do the hardware integration.
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:UsersYourUsernameDocumentsArduinolibrariesAWS_IOTsrc

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

Click here

 to refer the step-by-step screenshots to enrol the Certificates and the Private Key.
Next, we will work on the Arduino IDE and upload the Arduino code in the ESP32 Board. First, restart the Arduino IDE after following the instructions in section 1 & Plug your ESP32 board to your Desktop/Laptop using a USB cable. In the code, we are sending dummy Humidity and Temperature data, replicating a DHT11/22 Sensor to Topic ESP32/DHT11. 

Click here

 to refer the step-by-step screenshots to implement the Arduino Code in ESP32 Board.
  1. Click here to download Arduino Code 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 firmware 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. 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 Challenge (Optional): Create IoT rule to set a notification whenever temperature crosses the threshold ?

In the next part,

we will turn ON/OFF the inbuilt led of ESP32 using AWS Device Shadow.Hope to see you again :)
Don't forget to share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *