IOT Project - How to transfer data from ESP32 to MQTT server

 


This project explains what is MQTT protocol and how to publish/subscribe data from AWS to ESP32 via the MQTT protocol.  There are many clouds that provide services to establish a connection with IoT things and subscribe/publish data on things, but for this project, I am using Amazon web service as an MQTT broker/server, because nowadays many companies are using it and it provides some of the services free for a limited period of time.



What is the MQTT protocol?

MQTT is a client-server-based publish/subscribe transport protocol. It is lightweight, simple, open, and designed such that it is easy to implement. These properties make it use for machine-to-machine communication and also for the internet of things.

 Why MQTT is used for IoT devices? These are following advantages make MQTT suitable for IoT devices:

  • MQTT protocol is lightweight, which increases response time among devices
  • It runs on low-bandwidth
  • Less power consumption protocol

How does the MQTT protocol work? MQTT protocol follows a publisher-subscriber pattern.

  • The server connects to the client (publisher/subscriber) via the internet or local network.
  •  Publisher publishes data on the topic.
  •  The subscriber gets data from the topic.

  Note: In this project, AWS is used as a server/broker and also as a publisher.



Pre-requisite

  • ESP32 board
  • Arduino IDE 
  • Amazon Web Services (AWS) Account
  • LED
  • Jumpers wires


Create an Account with AWS


Amazon web service allows you to log in with 2 different accounts - Root user and IAM user. But it only allows creating an account as a root user. 


  • Root User
  • Access Management(IAM User)

Root User has full access to all resources and has all permission. There is a specific task that can be only performed by the root user for example only the root user can create/close an IAM user account.


Access Management(IAM user) has access to controlled resources. This type of account is very much required when there are multiple users having different requirements to access your AWS account. For example, create an IAM account having read-only permission to your AWS account and distribute the unique credential of it to one of the users, without providing the root key.


Note: For this project, create an account as the root user.



AWS IoT certificate

To subscribe/publish data from the device to the AWS server or vice versa, we need certificates (AWS Root CA1 certificate,  Private key, and Device certificate) and endpoint. 


We can generate certificates in the following way:


Go to the Amazon AWS management console and click on Complete Sign up. 

Go to the AWS Management Console dashboard and search for IoT Core in all services. 

In the left navigation panel, click on the Manage option and choose Things.

    Click on create things and choose to Create a single thing.

In Specify thing properties - mention thing name and Device shadow as no shadow.


In Configure device certificate - select Auto-generate a new certificate option.


In Attach policies to the certificate - choose to create policy.


In Create policy - give the policy name and policy document, and select all options as shown in the image.


    In create a thing, go to Attach policies to certificate and select a policy that you have created and click on create a thing.


On successful creation of a thing, you will see the option to download a device certificate, private key, and Root CA1 certificate.



    AWS Endpoint is required to connect  AWS IoT. Copy it from Device Software > Settings > Device Data endpoint.



Setup Arduino IDE


To run the program on the esp32 board, add the esp32 link to File > Preferences > Additional Boards Manager URLs


https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json


To use the MQTT protocol, Go to Sketch > Include Library > Managed Library > installed PubSubClient and AdruinoJson library.


installed the latest version of the PubSubClient library

Coding

Create a "Certificate.h" file and add the Amazon Root CA1 certificate, Device certificate, and Device private key.


Create the "esp32_mqtt.ino" file to handle the connection between AWS IOT core and esp32 using the MQTT protocol.


I hope you liked this project. I will really appreciate it if you share valuable feedback. Also if you have any queries, please mention them in the comment section.




Post a Comment

Previous Post Next Post