Arduino custom library and pubsubclient call back

Months back when I tried to make my own library consisting of all necessary code (“ESPMetRED” with it’s user friendly API) which included few other libraries such as PubSubClient MQTT Library, I experienced callback issue with PubSubClient MQTT library written by former IBM guy Nick O’Leary. I had raised an issue over github and few guys tried to help me but I failed to understand. After much effort I have resolved the issue and I think it’s worth to share my solution.

 

I am using Nick’s PubSubClient library which can be download from here. Let’s explain it bit more.

MQTTClient.setServer(MQTT_SERVER, 1883);
MQTTClient.setCallback([this] (char* topic, byte* payload, unsigned int length) { this->callback(topic, payload, length); });

In your library instantiate function, set MQTT callback as shown above and make a function in your library which should be called in your sketch loop at every cycle. In this specific function add following code.

MQTTClient.loop();

Where MQTTClient the instantiation of PubSubClient library in our custom library. Now We’ll be able to interact with MQTT messages received in the same way as We do while using PubSubClient library in our Arduino IDE sketch. Good Luck Hobbyists  ;-).

3.5 4 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x