Overview
This is a getting started tutorial about Amazon Web Services, i.e AWS IoT Core with ESP8266. The AWS IoT Core is a managed cloud service that lets connected devices easily and securely interact with cloud applications and other devices.
In this tutorial, we will learn how you can connect the ESP8266 with AWS IoT Core & publish sensor reading to AWS MQTT. For a demo, we will use the DHT11 Sensor and read the humidity temperature data. The NodeMCU ESP8266 will connect to the local WiFi network and will post the DHT11 Sensor data to AWS IoT Cloud. Not only posting data, but we can also receive the data from AWS Dashboard. Earlier, we learned how to post sensor data to AWS Dashboard using ESP32 & AWS IoT Core. But in the case of ESP8266 the process is little different.
The tutorial comprises multiple sections
- Signing up & setting the Amazon Web Services
- Installing Necessary Libraries to Arduino IDE & Writing an Arduino Sketch for the project
- Creating a Thing is AWS
- Creating Policy and attaching to Thing
- Generating Certificates
- Modifying Arduino Sketch according to Thing Data & Credentials
- Subscribe & Publish Data to and from AWS Dashboard
This tutorial is for beginners who want to learn about the Amazon AWS IoT Core for IoT Applications. Earlier we learned about the IoT platform like Google Firebase and Arduino IoT Cloud. But with AWS IoT Core, you can build and manage devices for commercial applications.
Hardware Setup
The hardware required for this project is an ESP8266 Wifi Module. And for the sensor part, we will use DHT11 Humidity and Temperature Sensor.
Connect the DHT11 Sensor to ESP8266 Board as per circuit diagram here. Connect the VCC & GND pin of DHT11 to 3.3V & GND of ESP8266. Similarly, connect the signal pin of DHT11 to D2 (GPIO4) of NodeMCU ESP8266.
You can use a breadboard for connection or simply use a male-to-female connector wire.
What is Amazon AWS IoT Core?
AWS offers Internet of Things (IoT) services and solutions to connect and manage billions of devices. These cloud services connect your IoT devices to other devices and AWS cloud services. AWS IoT provides device software that can help you integrate your IoT devices into AWS IoT-based solutions. If your devices can connect to AWS IoT, AWS IoT can connect them to the cloud services that AWS provides.
AWS IoT lets you select the most appropriate and up-to-date technologies for your solution. To help you manage and support your IoT devices in the field, AWS IoT Core supports these protocols:
- MQTT (Message Queuing and Telemetry Transport)
- MQTT over WSS (Websockets Secure)
- HTTPS (Hypertext Transfer Protocol – Secure)
- LoRaWAN (Long Range Wide Area Network)
Getting Started with Amazon AWS IoT Core with ESP8266
Getting started with AWS IoT core is very simple. You just need to follow the following steps given below to successfully set up the ESP8266 board and start with your very first project.
Signing in
Go to your web browser and search visit following link: aws.amazon.com/iot-core/.
Basically we need to set up the AWS Account now. Therefore create an account using the email ID and password. The account also requires your bank Credit card information. There will be no charges but AWS just needs a verification using your bank account. It will also ask for phone number verification. Hence the account will be successfully created.
AWS IoT Core Dashboard
After successfully signing in, the AWS Management Console window will open. In the services search tab at the top write ‘IoT core’ & hit enter.
You can click on IoT Core, so an AWS IoT Dashboard will appear now.
On the left side of the dashboard, there are so many options. But we need to work with two options here. One is the manage option and the other one is the secure option.
Creating a Thing
Now we need to create a thing associated with our project. For this, follow the following steps:
- Specifying thing properties
- Configuring device certificate
- Attaching policies to certificate
Under the manage option click on Thing. Now we need to create a Thing here. So, click on Create Things here.
You can select whether create a single thing or create many things. But for our applications, select create a single thing. Then click on Next.
Specify Thing Properties
Here we need to specify the Thing properties. First, give a thing name. You can name it anything. For example, I will name it ESP8266.
Under additional configurations, there is no need to make any changes.
Under the device shadow option, select the first option as No shadow. Then click on Next.
Generate Device Certificate
Now you need to configure device certificate. So here you can auto-generate a new certificate or use your own certificate or upload CSR or skip this.
But the AWS recommendation is to select the Auto Generate New Certificate. Then click on Next.
Create & Attach Policy
Now we need to attach a policy to the Things we created. But no policies are here right now. So we need to create a policy first.
So click on create policy. Here give any name to the policy. For example, I will give it a name as “ESP8266_Policy“.
Now the add statement part is very important. Under the action, type IoT. So multiple options will pop up. From here we will only need to publish, Subscribe, Connect and Receive.
Now click on create to create the policy. So the policy has been created successfully.
Now go back to Create Thing option. So a policy option will appear. We need to attach the policies to the certificate. So select the appeared policy and click on create a thing.
Downloading Certificates and Keys
Now we need to download the required certificates from this list.
First, download the device certificate and then rename it as a device certificate for identification.
Also, download the public key and rename it as a public key. Then download the private key and rename it as a private key.
In the Root CA Certificates, there are two certificates here. But we just need a Root CA1 certificate, so download it as well.
So we have downloaded all the certificates that we need for our project.
Installing Necessary Arduino Libraries
1. ArduinoJSON Library
So first go to the library manager and search for “JSON” & install the library as shown in the figure below.
2. PubSubClient Library
Again go to the library manager and search for “PubSubClient” & install the library from Nick O’Leary.
3. DHT11 Sensor Library
Search for “dht11” & install the library as shown below.
Source Code/Program for connecting AWS IoT Core with ESP8266
The code/program that interfaces ESP8266 with DHT11 Sensor & connects to the Amazon AWS IoT Core is written in Arduino IDE. The code is divided into two sections. One is the main ino file and other the header file.
Main .ino File
Open a new sketch in Arduino IDE & paste the following code and save it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
#include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include <time.h> #include "secrets.h" #include "DHT.h" #define TIME_ZONE -5 #define DHTPIN 4 // Digital pin connected to the DHT sensor #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); float h ; float t; unsigned long lastMillis = 0; unsigned long previousMillis = 0; const long interval = 5000; #define AWS_IOT_PUBLISH_TOPIC "esp8266/pub" #define AWS_IOT_SUBSCRIBE_TOPIC "esp8266/sub" WiFiClientSecure net; BearSSL::X509List cert(cacert); BearSSL::X509List client_crt(client_cert); BearSSL::PrivateKey key(privkey); PubSubClient client(net); time_t now; time_t nowish = 1510592825; void NTPConnect(void) { Serial.print("Setting time using SNTP"); configTime(TIME_ZONE * 3600, 0 * 3600, "pool.ntp.org", "time.nist.gov"); now = time(nullptr); while (now < nowish) { delay(500); Serial.print("."); now = time(nullptr); } Serial.println("done!"); struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.print("Current time: "); Serial.print(asctime(&timeinfo)); } void messageReceived(char *topic, byte *payload, unsigned int length) { Serial.print("Received ["); Serial.print(topic); Serial.print("]: "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void connectAWS() { delay(3000); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println(String("Attempting to connect to SSID: ") + String(WIFI_SSID)); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1000); } NTPConnect(); net.setTrustAnchors(&cert); net.setClientRSACert(&client_crt, &key); client.setServer(MQTT_HOST, 8883); client.setCallback(messageReceived); Serial.println("Connecting to AWS IOT"); while (!client.connect(THINGNAME)) { Serial.print("."); delay(1000); } if (!client.connected()) { Serial.println("AWS IoT Timeout!"); return; } // Subscribe to a topic client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC); Serial.println("AWS IoT Connected!"); } void publishMessage() { StaticJsonDocument<200> doc; doc["time"] = millis(); doc["humidity"] = h; doc["temperature"] = t; char jsonBuffer[512]; serializeJson(doc, jsonBuffer); // print to client client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer); } void setup() { Serial.begin(115200); connectAWS(); dht.begin(); } void loop() { h = dht.readHumidity(); t = dht.readTemperature(); if (isnan(h) || isnan(t) ) // Check if any reads failed and exit early (to try again). { Serial.println(F("Failed to read from DHT sensor!")); return; } Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.println(F("°C ")); delay(2000); now = time(nullptr); if (!client.connected()) { connectAWS(); } else { client.loop(); if (millis() - lastMillis > 5000) { lastMillis = millis(); publishMessage(); } } } |
Secrets.h
Open a New Tab in Arduino IDE and name it as Secrets.h. And paste the following code to this file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <pgmspace.h> #define SECRET const char WIFI_SSID[] = "*****************"; //TAMIM2.4G const char WIFI_PASSWORD[] = "*****************"; //0544287380 #define THINGNAME "**************" int8_t TIME_ZONE = -5; //NYC(USA): -5 UTC const char MQTT_HOST[] = "******************************"; static const char cacert[] PROGMEM = R"EOF( -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- )EOF"; // Copy contents from XXXXXXXX-certificate.pem.crt here ▼ static const char client_cert[] PROGMEM = R"KEY( -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- )KEY"; // Copy contents from XXXXXXXX-private.pem.key here ▼ static const char privkey[] PROGMEM = R"KEY( -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- )KEY"; |
Modifying Arduino Sketch according to the Thing
Now it’s time to modify the Arduino Sketch File. Go to secrets.h tab and begin the modification.
Here we need to include a thing name. You can go to the things section of AWS Console and copy the thing name.
Paste the thing name to the following line of code.
|
1 |
#define THINGNAME "***************" |
Under the WiFi SSID and password, enter the WiFi SSID and Password of your local network.
|
1 2 |
const char WIFI_SSID[] = "***************"; const char WIFI_PASSWORD[] = "***************"; |
Now, we need to insert the AWS IoT Endpoint here. To get the endpoint, go to the settings part of AWS Dashboard. Yow will get the endpoint.
Click on the copy icon to copy the endpoint. Go back to Arduino IDE and paste it on the following line.
|
1 |
const char MQTT_HOST[] = "******************************"; |
You need to insert the Amazon Root CA1 in between the following line.
|
1 2 3 4 5 6 |
// Amazon Root CA 1 static const char AWS_CERT_CA[] PROGMEM = R"EOF( -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- )EOF"; |
So for this, we need to go back to the certificate that we downloaded earlier. Open this file with Notepad++ and copy all the text.
Then go back to Arduino IDE and insert the copied text between begin certificate and the end certificate.
Under the “Device Certificate” lines, we need to paste the device certificate text. So open the device certificate file that we downloaded earlier. And again copy the text and paste it between the begin certificate and end certificate section.
|
1 2 3 4 5 |
// Device Certificate static const char AWS_CERT_CRT[] PROGMEM = R"KEY( -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- |
Under the “Device Private Key“, we need to insert the device’s private key. So go to the downloaded folder again and open the device’s private key file using Notepad++. Again copy the text and paste it between begin & end parts.
|
1 2 3 4 5 |
// Device Private Key static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY( -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- |
So all the modification of the Arduino ESP8266 Sketch related to AWS IoT Core is done now.
Testing the Publishing & Subscription of Data
Once all the modification is done, connect the NodeMCU ESP8266 to your computer. Then go to the tools & select NodeMCU 1.0 Board that you are using for this project. Also, select the COM port. Then click on the upload option to upload the code to the ESP8266 board.
Once the code uploading is done, open the Serial Monitor. The ESP8266 will try connecting to the WiFi Network. Once it gets connected to the WiFi Network, it will try connecting to the AWS IoT Server.
The humidity and temperature value will appear on the Serial Monitor.
Subscribing Sensor Data to AWS Dshboard
The same thing should also be posted to the AWS Server. To check that, go to the test section of AWS Dashboard. Under the test section, we have an option for subscribe and publish.
Now to see the data, you need to subscribe to a topic. For that type “esp8266/pub“ under the topic filter section. In the additional configuration, you can make changes if you want.
Then click on subscribe. When you hit the subscribe button, immediately the data from ESP8266 will be uploaded to AWS Dashboard. Thus, you have successfully sent the DHT11 Sensor data & time data to Amazon AWS IoT Core using ESP8266.
The data is updated here after an interval of every one second. This is really amazing as we are able to receive the data to AWS IoT Core Dashboardsent from ESP8266 via MQTT protocol. This is how we read the subscribed data.
Publishing Data to Serial Monitor
Now let’s see if we are able to publish the data from AWS IoT core to ESP8266 or not.
Now to see the data, you need to publish to a topic. For that type “esp8266/sub“ under the topic filter section. Under additional configuration do nothing. Then click on publish.
Immediately you can see the message sent to the Serial Monitor. This is amazing again. You can use this method to Control an LED using publish method.
This is how you can send or receive data from Amazon AWS IoT Core using ESP8266. Using AWS MQTT, we can subscribe to sensor readings topics published by various IoT nodes. This is a basic beginner’s tutorial for the users who want to get started with Amazon Web Services for their IoT devices.
You may do an Industrial Automation using ESP8266 & AWS IoT Core for Industrial Applications.




































8 Comments
Your codes works really well, thank you.
How do I send the output to aws IoT via mqtt? The result in serial monitor is either 0 or 1.
void loop() {
if (portOne.available())
Serial.write(portOne.read());
After compiling my code it says “No such harder life ” and also gives error on PubSubClint.h
need help with any solution for it.
Hey, thanks for this amazing project. Which esp8266 library version did you use?
I have tried the code on my WEMOS D1 Mini, and it never executes beyond the client.connect(THINGNAME) which always returns False… Do you know what I might be doing wrong?
I have dove deep and debugged the connection function it seems to get stuck in a while loop inside the PubSubClient.cpp line264
while (!_client->available())
Although the BearSSL handshake returns value of 1.
Hi, did you ever resolve this? I’m having the same problem. Thanks.
Same issue here. This is my full output:
Connecting to AWS fpm close 3
mode : sta(c8:c9:a3:54:d6:01)
add if0
Attempting to connect to SSID: HIDDEN
…scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 24
cnt
connected with HIDDEN, channel 1
dhcp client start…
……….pm open,type:2 0
…………………………………………………………………………… (and so on!)
Furthermore, the article states that you need to select your COM port via the tools menu. In my editor that option is not available. See this screenshot: https://imgur.com/a/wPKFEsr
Hi
Hi, Wonderful project, its working perfectly. Now I have my own smarthome skill linked to the IOT thing, how to see the changes in thing shadow (status) in the serial monitor?