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!

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?
    – Majenko
    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)

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.