In this previous post esp32-mqtt-tutorial we have seen how pub-sub and data receive events works in a simple application. In this post, we are going to do the same but the Design pattern will be different. we will be moving towards an Event-driven coding pattern so that later we can embed other code/features easily without any timing issue
We will follow the following example code to learn about the mqtt publish
https://github.com/espressif/esp-idf/tree/master/examples/protocols/mqtt/publish_test
This example uses freertos event groups
Freertos API is backward compatible
In freertos-based design we can make a task block waiting for a queue. if the length of the queue is 1 then we say that the task is blocked waiting for a semaphore. semaphore can be binary or counting type. if a binary semaphore is used for managing the access of a shared resource between multiple tasks then the semaphore is called mutex.
“You may think of an event group as a bit field where every bit is a flag that corresponds to a certain event. The event bit can be set or cleared. The state of the bits is altered to signal the status of a process to other functions.” ref: http://iot-bits.com/freertos-event-groups-quick-reference-notes/
Creates a new RTOS event group, and returns a handle by which the newly created event group can be referenced
The example has an infinite loop in the main function, this loop can be assumed as a task meaning we can make it block waiting for something like semaphore/queue / event group bit
xClearOnExit = false, means the bits set in the event group are not altered when the call to xEventGroupWaitBits() returns.
xWaitForAllBits = true means xEventGroupWaitBits() will return when either all the bits set in the value passed as the uxBitsToWaitFor parameter are set in the event group or the specified block time expires. In this example, only one bit (CONNECTED_BIT) is passed.
xTicksToWait = portMAX_DELAY, which means the maximum amount of time to wait for the CONNECTED_BIT to set
In the example code, the main loop task will wait for the CONNECTED_BIT to be set until portMAX_DELAY expires, after that the code will publish a message at QoS 0
At the event handler function mqtt_event_handler() when MQTT_EVENT_CONNECTED type event is got, the CONNECTED_BIT of the event group is set so that the main loop task can publish instantaneously
At each project build/config directory there is sdkconfig.h header file where all the CONFIGURATIONS are listed, these configurations are altered at menuconfig
I have changed the configurations as the following
You can see that the publish and subscribe both topics are the same and the broker URL of type TCP has been changed to mqtt.eclipse.org
Now build and run the example,
The code waits for user input so you will have to input into the terminal
[Note: nothing will be visible in the terminal so make sure you type rightly and press enter ]
I typed the above without quotation mark,
After the input, the MQTT client is started and when it gets connected at the event handler the CONNECTED_BIT of the event group gets set, this, in turn, unblocks the main loop task which publishes 10 times.
When the client was connected it subscribed too.
In the event handling function Event Data received is tracked so when all the 10 published message is received by the handler it will print success
0 comments:
Post a Comment