ESP8266 - AWS IoT

This tutorial instructs you how to use ESP8266 with AWS IoT Core. Specifically, we'll cover the following topics in detail:

By following these steps, you'll gain a comprehensive understanding of integrating the ESP8266 with AWS IoT Core for seamless communication.


Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Jumper Wires
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of ESP8266 and AWS IoT

The ESP8266 establishes a connection with AWS IoT Core through the MQTT protocol. Although libraries are available to simplify this connection, it's more complex than connecting to a local MQTT broker such as Mosquitto on your PC. This complexity arises from AWS IoT Core's stringent security measures, necessitating configuration steps to acquire authentication credentials and authorization for integration into the ESP8266 code. In essence, the process involves two primary steps:

  • Configuring AWS IoT Core: This step configures AWS IoT Core to generate the required authentication credentials, which will subsequently be incorporated into the ESP8266 code.
  • Writing the ESP8266 Code: After obtaining the authentication credentials from AWS IoT Core, the subsequent step involves writing the ESP8266 code. This code integrates the essential authentication and communication protocols required for seamless interaction with AWS IoT Core.

Let's take a closer look at each step to understand it better.

Configuring AWS IoT Core for use with ESP8266

The objectives of this step include:

  • Creating a representation of the ESP8266 device on AWS IoT Core, referred to as a "Thing."
  • Configuring the necessary authorization to allow the ESP8266 device to connect, publish, and subscribe to/from AWS IoT Core, known as a "Policy."
  • Generating the AWS credentials required for authentication, known as "Certificates." These credentials will be downloaded and integrated into the Arduino ESP8266 code.

Below are instructions on configuring AWS IoT Core for use with ESP8266 via the AWS IoT Console. Please note that while the User Interface may change over time, the process should remain similar to the instructions provided below:

  • Sign in to the AWS IoT Console
  • Create a Thing by going to Manage All devices Things
AWS IoT creates Things
  • Click the Create things button.
  • Select Create single things and click the Next button.
AWS IoT Core creates Things
  • Specify the Thing name, for example, ESP8266-thing and click the Next button at the bottom of the page.
AWS IoT Core Thing name
  • Generate the credentials by selecting the Auto-generate a new certificate option, and click the Next button.
AWS IoT Core generates certificate
  • Now, a Certificate is created and linked to the Thing.
  • Create a policy by clicking the Create policy button.
AWS IoT Core Create policy
  • A new tab will be opened
AWS IoT Core Create policy ESP8266 NodeMCU
  • Specify the Policy name, for example, ESP8266-policy and click the JSON button.
  • Copy the below JSON policy content and paste it to the Policy document area:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "*", "Resource": "*" } ] }
  • Click the Create button at the bottom of the page to create the policy.
  • Now, a Policy is created and attached to the Certificate. Close that page and back to the Thing page.
  • Check the the ESP8266-policy and Click the Create thing button to create the Thing.
AWS IoT Core creates Thing ESP8266 NodeMCU
  • A popup window appears that allows you to download the credentials files. Download all files and store them in a safe location on your PC and keep them confidentially.
AWS IoT Core credentials file
  • Then, click the Done button.

Among downloaded files, there are three files will be used in the ESP8266 code in the next steps:

  • AmazonRootCA1.pem
  • xxxxxxxxxx-certificate.pem.crt
  • xxxxxxxxxx-private.pem.key

These files can be opened by any text editor such as Notepad, or Notepad++

Writing the ESP8266 code to connect to AWS IoT Core

/* * This ESP8266 NodeMCU code was developed by * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #include "secrets.h" #include <ESP8266WiFi.h> #include <MQTTClient.h> #include <ArduinoJson.h> // The MQTT topics that this device should publish/subscribe #define AWS_IOT_PUBLISH_TOPIC "esp8266/esp8266-to-aws" #define AWS_IOT_SUBSCRIBE_TOPIC "esp8266/aws-to-esp8266" #define PUBLISH_INTERVAL 5000 // 5 seconds BearSSL::WiFiClientSecure network; MQTTClient client = MQTTClient(256); unsigned long lastPublishTime = 0; void setup() { Serial.begin(9600); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); setClock(); connectToAWS(); } void loop() { client.loop(); if (millis() - lastPublishTime > PUBLISH_INTERVAL) { sendToAWS(); lastPublishTime = millis(); } } // Set time via NTP, as required for x.509 validation void setClock() { configTime(3 * 3600, 0, "", ""); Serial.print("Waiting for NTP time sync: "); time_t now = time(nullptr); while (now < 8 * 3600 * 2) { delay(500); Serial.print("."); now = time(nullptr); } Serial.println(""); struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.print("Current time: "); Serial.print(asctime(&timeinfo)); } void connectToAWS() { // Configure the AWS IoT device credentials network.setTrustAnchors(new BearSSL::X509List(AWS_CERT_CA)); network.setClientRSACert(new BearSSL::X509List(AWS_CERT_CRT), new BearSSL::PrivateKey(AWS_CERT_PRIVATE)); // Connect to the MQTT broker on the AWS endpoint we defined earlier client.begin(AWS_IOT_ENDPOINT, 8883, network); // Create a handler for incoming messages client.onMessage(messageHandler); Serial.print("ESP8266 connecting to AWS IOT"); while (!client.connect(THINGNAME)) { Serial.print("."); delay(100); } Serial.println(); if (!client.connected()) { Serial.println("ESP8266 - AWS IoT Timeout!"); return; } // Subscribe to a topic, the incoming messages are processed by messageHandler() function client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC); Serial.println("ESP8266 - AWS IoT Connected!"); } void sendToAWS() { StaticJsonDocument<200> message; message["timestamp"] = millis(); message["data"] = analogRead(0); // Or you can read data from other sensors char messageBuffer[512]; serializeJson(message, messageBuffer); // print to client client.publish(AWS_IOT_PUBLISH_TOPIC, messageBuffer); Serial.println("sent:"); Serial.print("- topic: "); Serial.println(AWS_IOT_PUBLISH_TOPIC); Serial.print("- payload:"); Serial.println(messageBuffer); } void messageHandler(String &topic, String &payload) { Serial.println("received:"); Serial.println("- topic: " + topic); Serial.println("- payload:"); Serial.println(payload); // You can process the incoming data as json object, then control something /* StaticJsonDocument<200> doc; deserializeJson(doc, payload); const char* message = doc["message"]; */ }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE
  • Type MQTT on the search box, then look for the MQTT library by Joel Gaehwiler.
  • Click Install button to install MQTT library.
ESP8266 NodeMCU MQTT library
  • Type ArduinoJson on the search box, then look for the ArduinoJson library by Benoit Blanchon.
  • Click Install button to install ArduinoJson library.
ESP8266 NodeMCU Json library
  • Copy the above code and paste it to Arduino IDE.
  • Create the secrets.h file On Arduino IDE by:
    • Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
    Arduino IDE 2 adds file
    • Give file's name secrets.h and click OK button
    Arduino IDE 2 adds file secrets.h
    • Copy the below code and paste it to the created secrets.h file.
    #include <pgmspace.h> #define SECRET #define THINGNAME "ESP8266-thing" const char WIFI_SSID[] = ""; // CHANGE IT TO MATCH YOUR OWN NETWORK CREDENTIALS const char WIFI_PASSWORD[] = ""; // CHANGE IT TO MATCH YOUR OWN NETWORK CREDENTIALS const char AWS_IOT_ENDPOINT[] = ""; // Amazon Root CA 1 static const char AWS_CERT_CA[] PROGMEM = R"EOF( -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- )EOF"; // Device Certificate static const char AWS_CERT_CRT[] PROGMEM = R"KEY( -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- )KEY"; // Device Private Key static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY( -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- )KEY";
    • Update the following information in the secrets.h
      • The WIFI_SSID and WIFI_PASSWORD of your WiFi network
      • The AWS_CERT_CA, AWS_CERT_CRT, and AWS_CERT_PRIVATE. These infornation are in the files you downloaded in the previous step.
      • The AWS_IOT_ENDPOINT. This information can be found on AWS IoT Console by going to Setting as below image:
      AWS IoT endpoint
      • Compile and upload code to ESP8266 board by clicking Upload button on Arduino IDE

Sending data from ESP8266 to AWS IoT

The above ESP8266 code periodically read data from an analog pin and send it to AWS IoT every 4 seconds. If opening the Serial Monitor on Arduino IDE, you will see the log like below:

ESP8266 connecting to AWS IOT. ESP8266 - AWS IoT Connected! sent: - topic: esp8266/esp8266-to-aws - payload:{"timestamp":12743,"data":0} sent: - topic: esp8266/esp8266-to-aws - payload:{"timestamp":16745,"data":130}
Autoscroll Show timestamp
Clear output
9600 baud  

To check if the data are received by AWS IoT or not, do the following steps:

  • In AWS IoT Console, navigate to Test MQTT Test Client
AWS IoT MQTT Test Client ESP8266 NodeMCU
  • Click the Subcribe to a topic button.
  • Type esp8266/esp8266-to-aws to the Topic filter. You can change the topic but MUST be matched the topic on the ESP8266 code.
  • Click the Subcribe button.
  • You will be able to see the data sent from ESP8266 on the AWS IoT Console.

Sending data from AWS IoT to ESP8266

You are able to send the data from AWS IoT Console to the ESP8266 by doing the following steps:

  • On Arduino IDE, Open the Serial Monitor
  • On AWS IoT Console, navigate to Test MQTT Test Client
AWS IoT MQTT Test Client ESP8266 NodeMCU
  • Click the Publish to a topic button.
  • Type esp8266/aws-to-esp8266 to the Topic name. You can change the topic but MUST be matched the topic on the ESP8266 code.
  • Optionally, you can change the message payload, or just keep it as default.
  • Click the Publish button.
  • Check out the Serial Monitor on Arduino IDE, you will see the message sent from AWS IoT Console.
received: - topic: esp8266/aws-to-esp8266 - payload: { "message": "Hello from AWS IoT console" }
Autoscroll Show timestamp
Clear output
9600 baud  

Do more with AWS

Now, you can establish bidirectional communication between the ESP8266 and AWS IoT Core. This means you can send data from the ESP8266 to AWS IoT Core and receive data from AWS IoT Core on the ESP8266. Additionally, you have the capability to configure IoTRules, enabling the ESP8266 to seamlessly connect with other AWS services such as Lambda, DynamoDB, Amplify, and RDS. With IoTRules, you can automate actions based on data received from the ESP8266, enabling a wide range of IoT applications and integrations.


  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!