MQTT stands for Messaging queue telemetry transport
Broker & server is termed as the same meaning
It is a lightweight publish/subscribe messaging protocol
We will go through this example
https://github.com/espressif/esp-idf/tree/master/examples/protocols/mqtt/tcp
The example uses ESP-MQTT library https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/protocols/mqtt.html
ESP-MQTT library is used to implement the MQTT protocol client
By default MQTT client uses the event Loop library to post MQTT events so in code you have to create the default event loop so that events can be captured in the code
MQTT related Events are
To make a ESP-MQTT based client a esp_mqtt_client_config_t structure type variable should be created
Here the CONFIG_BROKER_URI is the address of the broker to which the MQTT client will connect. This MACRO is set by idf.py menuconfig
URI example mqtt://mqtt.eclipse.org
The MQTT client configuration structure has many members, i am going to list out the important ones here
After the configuration of the structure, we have to create a MQTT Client handle based on the configuration using the following function.
We can assume that this handle is our mqtt Client , we have to use this handle in other parts of the code
Now we have to register for the mqtt event using the following function provided by the ESP-MQTT API
We have registered the client for any ESP EVENTS
Here esp_event_handler_t type parameter event_handler is a pointer to a function . it has the following signature
It is a pointer to a function. the function should return void and the argument list is unspecified. As the API is written in C , so it is not required for the function to be prototyped before being defined or used.
Ref: https://stackoverflow.com/questions/3982470/what-does-typedef-void-something-mean
we pass mqtt_event_handler() function as an argument for this parameter and define this function in our application source code
You can see that this handler function calls another callback function mqtt_event_handler_cb()
This callback function is the function where the MQTT events are processed
At registration the handler function was passed now , we will start the client
Now what happens is that the ESP-MQTT client starts running and when MQTT events occur the mqtt_event_handler() function gets called, this function prints the event type and passes the event_data argument to mqtt_event_handler_cb() function where the events get processed
The event_data argument is of type esp_mqtt_event_handle_t which is actually a pointer of type esp_mqtt_event_t
Now that we have understood the code, let's build the example code
Broker Address Set
We will use a public broker to test the code, i have used mqtt.eclipse.org
As it is a public broker it may be unavailable, so let’s check if it is active or not in our host
We will open a terminal [CTRL+ALT+T] where we will subscribe on a topic, then we will open another terminal and publish a message to the broker on the previously input topic
>>subscribe
>>publish at another terminal
If at the subscribed terminal you see ‘hello world’ message printed then MQTT communication is okay
Now let’s set the broker address in our esp32 MQTT example code using menuconfig
After setting the broker to build a flash the program to the ESP32 and if everything is okay you will mqtt communication is working
0 comments:
Post a Comment