MQTT (Message Queuing Telemetry Transport) Protocol is a M2M (Machine to Machine) lightweight messaging protocol widely used in IOT (Internet of Things) that provides a resource constrained network clients. Almost IOT cloud platform support MQTT protocol to send and receive data from smart objects in other words it is a simple way to distribute telemetry information which uses a publish & subscribe communication pattern together with your favorite microcontrollers & SBC. for further reading please refer to this link.

The main objective to this demonstrations is how we going to publish our topic to MQTT using ESP32 and Arduino IDE below are the required applications we need.

Required Applications

When you finish the signup procedure go to instance information page. Keep note to the credentials you will going to use it on your ESP32 code for SERVER, USER, PASSWORD, PORT

This is an easy way to integrate MQTT library via Arduino IDE Library Manager. This library comes with a number of example sketches. See File > Examples > PubSubClient within the Arduino application. Full API documentation is available here: http://pubsubclient.knolleary.net

The code below needed to connect into MQTT broker to push a message for a topic. However still you need to include some required libraries such as <Wifi.h> library in demand to be connected to MQTT broker as global variable.

You can now declare object of class WiFiClient which is required to establish a connection to the defined IP address and Port Number. Next is you required to declare an object of class PubSubClent and pass as input of the constructor for the previously defined WiFiClient

At the function area, you need to open the serial communication, this is an easy way to see the output status from the ESP8266 / ESP32 connected and publish topic to the MQTT broker.

Setting the address and the communication port number of the MQTT server, which is previously set to the global variable. Then call the connect method passing as input parameter for the client along with the username and password.

Now we successfully sending a message topic to the CloudMQTT, as “client.publish(“esp/test”, “Message from ESP8266/ESP32”);” Below are the full source code, you can just call the loop method of the “PubSubClient” this method should be called regularly.

For testing you will need MQTTLens which is available in Chrome Application which is allow you to see a receive topics which is publish to MQTT. client.publish(“esp/test”, “Message from ESP8266/ESP32”); you can open your Serial Communication Monitor to view similar output on MQTTLens.

Downloads

Pushing Topics to MQTT Protocol with ESP32 / ESP8266
Facebooktwittergoogle_plusredditpinterestmail
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *