1

I have a program that depending on user input, it creates one or several objects that will publish data to a MQTT broker, each one with a different topic. This is already done. The ESP8266 also has subscribe itself to the same topics it publishes. Now, the problem is that I have to create a dynamic callback in the ESP8266 to read the all the posible arriving topics to which the ESP8266 has subscribed and I am having trouble with that.

The callback function model is:

void MQTTcallback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}

The possible topics can be:

ObjectTL/TL_1

ObjectTL/TL_2

ObjectTL/TL_3

...

ObjectTL/TL_X

The number (TL_X) depends of the user input I mention at the beginning. Thanks!

asked Nov 5, 2019 at 17:18
1
  • Without knowing more about the structure of your program and objects this is impossible to answer. Do you employ polymorphism for your objects? Do you have some storage where these objects are stored and are accessible? Do you have some form of list or array of the topics and their relationship to the objects? Commented Nov 6, 2019 at 10:43

1 Answer 1

1

I think using sscanf may help you

void MQTTcallback(char* topic, byte* payload, unsigned int length) {
 sscanf(data, "ObjectTL/TL_%d", dynamicTopicName);
 // handle message arrived
 your_dynamic_function(dynamicTopicName,byte* payload, unsigned int length)
}
answered Nov 6, 2019 at 11:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.