ESP8266 - AWS IoT
This tutorial instructs you how to use ESP8266 with AWS IoT Core. Specifically, we'll cover the following topics in detail:
- Establishing the connection between the ESP8266 and AWS IoT Core.
- Programming the ESP8266 to transmit data to AWS IoT Core.
- Programming the ESP8266 to receive data from AWS IoT Core.
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 | × | USB Cable Type-C | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Recommended) Screw Terminal Expansion Board for ESP8266 | |
1 | × | (Recommended) Power Splitter For ESP8266 Type-C |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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
- Click the Create things button.
- Select Create single things and click the Next button.
- Specify the Thing name, for example, ESP8266-thing and click the Next button at the bottom of the page.
- Generate the credentials by selecting the Auto-generate a new certificate option, and click the Next button.
- Now, a Certificate is created and linked to the Thing.
- Create a policy by clicking the Create policy button.
- A new tab will be opened
- 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:
- 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.
- 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.
- 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
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.
- Type ArduinoJson on the search box, then look for the ArduinoJson library by Benoit Blanchon.
- Click Install button to install ArduinoJson 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.
- Give file's name secrets.h and click OK button
- Copy the below code and paste it to the created secrets.h file.
- 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:
- 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:
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
- 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
- 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.
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.