Hello everybody,
inside the Unit3 MQTT Project – MQTT Client ESP32 #1:
There is one snipet I don’t understand in the code: more specifically I would say that something is missing: Rui says that the following snipet checks if a new message was published on the esp32/led topic:
if (strcmp(topic, "esp32/led") == 0) {
As for me:
- this line only browses the different topics and executes the code inside the “if” brackets when the browsing of the topics reaches “esp32/led”.
- In this code, we do not test if a change has occured in the topic. Can someone tell me more about this “if” statement and how do we test if a change has occured in the topic?
Thank you in advance 🙂
Rémi
Hi Remi.
Probably the explanation is not very clear.
And yes, you are right. The snippet doesn’t check if a new message was received. It just checks if the message was received on that specific topic.
When a new message is received on a specific topic (it doesn’t matter which topic), the onMqttMessage() function runs. And inside that function, we check if the topic is the topic that we are interested in— in this case, we are interested in the “esp32/led” topic.
The topic we are receiving messages from is saved on the topic variable, and the message received is saved on the messageTemp variable.
I hope this is clear.
Let me know if you need further help.
Regards,
Sara
Hi Sara,
And thank you for this quick and precise answer. Now this is clear for me.
Still, could we focus on a part of your explanation: when you say “when a new message is received, the onMqttMessage() function runs”, can I understand:
“when a message is received on the Mosquitto broker (in the topic esp32/led), this Mosquitto broker sends a packet to the subscribers of the topic esp32/led (for example, the ESP32 client #1). This packet “tells” the subscribers to trigger their function onMqttMessage()?
Regards,
Rémi
Ho.
Yes, it’s exactly that. Your description is correct!
I wouldn’t have explained it better.
Regards,
Sara
Hi!
Thank you for this confirmation. I really appreciate your reactivity, it is quiet rare!
And by the way, thank you (and Rui ) for the courses, these are very helpful!
Regards,
Rémi