odelot / aws-mqtt-websockets

Implementation of a middleware to use AWS MQTT service through websockets, aiming the ESP8266 plataform
GNU Lesser General Public License v3.0
231 stars 67 forks source link

How to subscribe to multiple topic using paho MQTT in arduino #25

Closed sunilsud closed 7 years ago

sunilsud commented 7 years ago

i am able to publish to multiple topic how do i subscribe to multiple topic1 and topic 2 right now only topic1 i can subscribe

void subscribe(){

//subscript to a topic

client->subscribe(topic1,MQTT::QOS0,messageArrived1);

client->subscribe(topic2,MQTT::QOS0,messageArrived2);

}

void messageArrived1(MQTT::MessageData& md) { MQTT::Message &message = md.message;

char* msg = new char[message.payloadlen+1](); memcpy (msg,message.payload,message.payloadlen); json_msg = msg; Serial.print(json_msg); parse_json(); delete msg;

} void messageArrived2(MQTT::MessageData& Md) { MQTT::Message &message = Md.message;

char* msg = new char[message.payloadlen+1](); memcpy (msg,message.payload,message.payloadlen); json_msg = msg; Serial.print(json_msg); parse_json(); delete msg;

}

odelot commented 7 years ago

When you create the client, you need to inform the max number os message handlers.

In the example, it is set to 1. Use 2.

const int maxMQTTMessageHandlers = 1;
MQTT::Client<IPStack, Countdown, maxMQTTpackageSize, maxMQTTMessageHandlers> *client = NULL;
sunilsud commented 7 years ago

its working

sunilsud commented 7 years ago

thanks

rakeshcomavia commented 7 years ago

thank you for help, its working