1

I'm using the PubSubClient on some ESP-01 modules to make smart/connected devices around the house. I'm not super fond of the way all incoming messages get routed to the single callback. When I get a message, I have to parse the topic and deal with each topic in a switch statement. There's probably a better way to do this but I haven't thought of anything yet.

It would be nice if I could assign a separate callback for each subscribed topic. Right now I just have a list of strings, with the last item being "END", and I loop through subscribing to the items in the list until I hit END. That's fine, but I'd like to be able to dynamically load the topics from the network eventually, so I can't have a hard-coded list or switch statement.

Is there a way to do this? I understand that I'd need a callback for each potential topic but that's fine, I just don't know how to assign callbacks to the topics I'm subscribed to.

4
  • you could have a "lookup key" included in the topic name ... something like /livingroom/lights/staircase/top/4 ... the 4 would be easily parsed ........ the key could also be part of the message
    – jsotola
    Sep 14, 2021 at 0:32
  • Currently I have a list, for example "set/state" and "set/pixel". When I subscribe I take the chipId, which I'm using as a device id for now, and prepend it to each item from the list then subscribe, so I get "[chipId]/set/state" for example. When I receive a message, I have to strip the chipId then run through a hard-coded switch statement. The switch statement doesn't use the list, I don't know how to link a specific topic with the function that it should execute. Sep 14, 2021 at 23:53
  • There are multiple of these devices. I want them all to run the same code, but using the device id still be able to talk to each one individually and assign functions to each one. The base code would have all the functions, the device might just not be subscribed to all of them. I'm not sure what to do with the hash function link you posted. Sep 14, 2021 at 23:56

1 Answer 1

2

Short answer no, the library only takes a single callback, which is passed the topic of the incoming message as a parameter.

Longer answer, there is nothing to stop you implementing this yourself.

You just have a big if/else block in the callback function that just calls individual methods for each topic based on the topic value passed into the callback function.

What you've described wouldn't let you dynamically assign topics anyway, you'd still need a pre-defined function for each possible topic (or at least each possible action to take, that could be mapped to the required topic)

1
  • Damn. I did realize I would still need the function present for each potential topic, but I could still dynamically subscribe to them with a list. Basically I need to make a list of function names and map it to the list of subscribed topics. Currently I am using a switch statement. Sep 14, 2021 at 23:46

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.