Introduction: Monitoring-Temp-and-Humidity-using-AWS-ESP32

In this tutorial, we will measure different temperature and humidity data using Temp and humidity sensor. You will also learn how to send this data to AWS.


Hardware :

  • ESP-32:The ESP32 makes it easy to use the Arduino IDE and the Arduino Wire Language for IoT applications. This ESp32 IoT Module combines Wi-Fi, Bluetooth, and Bluetooth BLE for a variety of diverse applications. This module comes fully-equipped with 2 CPU cores that can be controlled and powered individually, and with an adjustable clock frequency of 80 MHz to 240 MHz. This ESP32 IoT WiFi BLE Module with Integrated USB is designed to fit in all IoT products. Monitor sensors and control relays, FETs, PWM controllers, solenoids, valves, motors and much more from anywhere in the world using a web page or a dedicated server. We manufactured our own version of the ESP32 to fit into NCD IoT devices, offering more expansion options than any other device in the world! An integrated USB port allows easy programming of the ESP32. The ESP32 IoT WiFi BLE Module is an incredible platform for IoT application development. This ESP32 IoT WiFi BLE Module can be programmed using the Arduino IDE.
  • IoT Long Range Wireless Temperature And Humidity Sensor:Industrial Long Range Wireless Temperature Humidity Sensor. Grade with a Sensor Resolution of ±1.7%RH ±0.5° C.Up to 500,000 Transmissions from 2 AA Batteries.Measures -40°C to 125°C with Batteries that Survive these Ratings.Superior 2-Mile LOS Range & 28 miles with High-Gain Antennas.Interface to Raspberry Pi, Microsoft Azure, Arduino and More
  • Long-Range Wireless Mesh Modem with USB InterfaceLong-Range Wireless Mesh Modem with USB Interface

Software Used:

  • Arduino IDE
  • AWS

Library Used:

  • PubSubClient Library
  • Wire.h
  • AWS_IOT.h

Step 2: Uploading the Code to ESP32 Using Arduino IDE:

As esp32 is an important part to publish your temperature and humidity data to AWS.

  • Download and include the PubSubClient Library , Wire.h Library, AWS_IOT.h, Wifi.h.
  • Download the Zip file of AWS_IoT, from the given linkand after extracting, paste the library in your Arduino library folder.

#include <Wire.h>
#include <PubSubClient.h>
#include <HardwareSerial.h></p>
  • You must assign your unique AWS MQTT_TOPIC, AWS_HOST, SSID (WiFi Name) and Password of the available network.
  • MQTT topic and AWS HOST can get inside Things-Interact at AWS-IoT console.
#define WIFI_SSID "xxxxx" // your wifi ssid

#define WIFI_PASSWD "xxxxx" //your wifi password

#define CLIENT_ID "xxxxx"// thing unique ID, can be any unique id

#define MQTT_TOPIC "xxxxxx" //topic for the MQTT data

#define AWS_HOST "xxxxxx" // your host for uploading data to AWS
  • Define variable name on which the data will send to AWS.
int temp;
int Humidity;
  • Code to publish data to AWS:
if (temp == NAN || Humidity == NAN) { // NAN means no available data

   Serial.println("Reading failed.");
  else {
    //create string payload for publishing
    String temp_humidity = "Temperature: ";
    temp_humidity += String(temp);
    temp_humidity += " °C Humidity: ";
    temp_humidity += String(Humidity);
    temp_humidity += " %";</p><p>    temp_humidity.toCharArray(payload, 40);
    Serial.println("Publishing:- ");
    if (aws.publish(MQTT_TOPIC, payload) == 0) { // publishes payload and returns 0 upon success
    else {
  • Compile and upload theESP32_AWS.ino code.
  • To verify the connectivity of the device and the data sent, open the serial monitor. If no response is seen, try unplugging your ESP32 and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200.

Step 3: Serial Monitor Output.

Step 4: Making the AWS Work.


THING: It is a virtual representation of your device.

CERTIFICATE: Authenticates the identity of a THING.

  • Open AWS-IoT.
  • Click on manage -THING -Register THING.
  • Click on create a single thing.
  • Give the Thing name and type.
  • Click on next.
  • Now your certificate page will open, Click on Create Certificate.
  • Download these Certificates, mainly private key, a certificate for this thing and root_ca and keep them in a separate folder.Inside root_ca certificate click on Amazon root CA1-Copy it-Paste it to notepad and save it as a root_ca.txt file in your certificate folder.

Step 5: Create Policy

It defines which operation a device or user can access.

  • Go to the AWS-IoT interface, Click on Secure-Policies.
  • Click on Create.
  • Fill all the necessary details such as policy name, Click Create.
  • Now go back to the AWS-IoT interface, Click on Secure-Certificates and attach the policy created just now to it.

Step 6: Add Private Key, Certificate and Root_CA to Code.

  • Open your downloaded certificate in your text editor(Notepad++), mainly private key, root_CA and certificate of thing and edit them as given below.
  • Now open your AWS_IoT folder in your Arduino library -My Document. Go to C:\Users \xyz\Documents\Arduino\libraries\AWS_IOT\src, click on aws_iot_certficates.c, open it on an editor and paste all the edited certificate they're at the required place, save it.

Step 7: Getting Output-

  • Go to test in the AWS_IoT console.
  • Fill your MQTT topic to Subscription topic in your test credentials.
  • Now you can view your temp and humidity data.

Step 8: OUTPUT