256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 230 forks source link

How to read message of subscribed topics? #288

Closed alve89 closed 1 year ago

alve89 commented 1 year ago

As far as I understand the available methods, there only one way to access message to subscribed topics:

String currentTopic = "";
String currentPayload = "";

void messageReceived(String, String);

void setup() {
  // Do all configuration here
}

void loop() {
  mqttClient.onMessage(messageReceivced);
}

void messageReceived(String &topic, String &payload) {
  currentTopic = topic;
  currentPayload = payload;
} 

Especially when using OOP this is a quite inefficient way. What do you think of adding a method like String readTopic(String topic) which returns the current payload of the topic?

mikaelj commented 1 year ago

@alve89 Can't you simply create a wrapper yourself that does this for you? the messageReceived() gives you the message that was just recevied.

256dpi commented 1 year ago

Closing as probably solved.