Cloud Home Automation Series: Part 1 Connect ESP32 to AWS IoT with Arduino code
AWS Cloud Home Automation, Zero 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.
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.
- Part I – Connect your device (ESP32) to AWS cloud.
- Part II – Use Device Shadow Service (AWS IoT) to control ESP32 inbuilt led.
- Part III – Create secure web-endpoint hosted in Node-RED to control devices from the web. [Coming Soon]
- 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
- Arduino IDE (arduino.cc/en/main/software).
- ESP32 Board (Easily available in Amazon.com/Amazon.in/Aliexpress or Local Robotics Shop), they cost around 6$ or 350-450 INR.
- AWS Free-Tier Account. (Services used here will cover up in free tier limit)
- This tutorial mostly covers hands-on, prior knowledge of AWS IoT is an advantage. Click here to check AWS Official IoT Documentation.
- Install ESP32 espressif/arduino-esp32 library , which the main source library of ESP32 board and contains all necessary functions. [Assuming Arduino IDE preinstalled]
- Install AWS ESP32 Hornbill Library which binds ESP32 with AWS.
- In AWS Console, will create a Device thing (for ESP32), Certificate (for Authentication) & Policy (for Authorization).
- Upload the Certificates & Arduino sketch in ESP32 to send Temperature & Humidity sensor data to AWS MQTT Broker.
- 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.
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
- Create the last three highlighted folders :
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:
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.
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.
to refer the step-by-step screenshots to create policy
to refer the step-by-step screenshots to register the Thing.
- Next, select Create a single Thing.
- In the next step, give a name for Example; ESP32 and keep everything Default & click next.
- In the Next step, Select Create Certificate.
- 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.
, open the file aws_iot_certficates.c in an editor (Notepad++).
to refer the step-by-step screenshots to enrol the Certificates and the Private Key.
to refer the step-by-step screenshots to implement the Arduino Code in ESP32 Board.
- Click here to download Arduino Code and open it using Arduino IDE.
- 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).
- 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.
- 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).
- 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.
- 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 ?
we will turn ON/OFF the inbuilt led of ESP32 using AWS Device Shadow.Hope to see you again :)