ashlin 0 Posted January 16, 2019 Share Posted January 16, 2019 I am running the following code in EK-TM4C1294XL Launchpad.The program gets downloaded successfully and I am able to see the published messages in the MQTT BOX application.But when I download the same program in another launchpad and try to run both at the same time , one launchpad stopped sending data.How to run two MQTT publishers publishing to same topic .Is there any changes required in the code? Thanks #define MQTTCLIENT_QOS2 1 #include <SPI.h> #include <Ethernet.h> #include <EthernetStack.h> #include <Countdown.h> #include <MQTTClient.h> char printbuf[100]; int arrivedcount = 0; void messageArrived(MQTT::MessageData& md) { MQTT::Message &message = md.message; sprintf(printbuf, "Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n", ++arrivedcount, message.qos, message.retained, message.dup, message.id); Serial.print(printbuf); sprintf(printbuf, "Payload %s\n", (char*)message.payload); Serial.print(printbuf); } EthernetStack ipstack; MQTT::Client<EthernetStack, Countdown> client = MQTT::Client<EthernetStack, Countdown>(ipstack); const char* topic = "TEST TOPIC"; void connect() { char hostname[] = "iot.eclipse.org"; int port = 1883; sprintf(printbuf, "Connecting to %s:%d\n", hostname, port); Serial.print(printbuf); int rc = ipstack.connect(hostname, port); if (rc != 1) { sprintf(printbuf, "rc from TCP connect is %d\n", rc); Serial.print(printbuf); } Serial.println("MQTT connecting"); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; data.clientID.cstring = (char*)"energia-sample"; rc = client.connect(data); if (rc != 0) { sprintf(printbuf, "rc from MQTT connect is %d\n", rc); Serial.print(printbuf); } Serial.println("MQTT connected"); rc = client.subscribe(topic, MQTT::QOS2, messageArrived); if (rc != 0) { sprintf(printbuf, "rc from MQTT subscribe is %d\n", rc); Serial.print(printbuf); } Serial.println("MQTT subscribed"); } void setup() { Serial.begin(115200); Serial.println("Starting Ethernet"); Ethernet.enableLinkLed(); Ethernet.enableActivityLed(); Ethernet.begin(0); Serial.println("\nIP Address obtained"); // We are connected and have an IP address. Serial.println(Ethernet.localIP()); Serial.println("MQTT Hello example"); connect(); } void loop() { if (!client.isConnected()) connect(); MQTT::Message message; arrivedcount = 0; // Send and receive QoS 0 message char buf[100]; sprintf(buf, "Count2"); Serial.println(buf); message.qos = MQTT::QOS0; message.retained = false; message.dup = false; message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; int rc = client.publish(topic, message); while (arrivedcount == 0) client.yield(500); delay(500); } Quote Link to post Share on other sites
LiviuM 43 Posted January 16, 2019 Share Posted January 16, 2019 I would try with different IDs for each client. 1 hour ago, ashlin said: data.clientID.cstring = (char*)"energia-sample"; Quote Link to post Share on other sites
energia 485 Posted January 16, 2019 Share Posted January 16, 2019 The key here is to as @LiviuM mentioned to assign a unique client ID to each MQTT client. I have just confirmed that with 2 different client ID's all works as expected. Quote Link to post Share on other sites
ashlin 0 Posted January 18, 2019 Author Share Posted January 18, 2019 Thanks Livium and energia for the correction.It works just as expected.The ID has to be unique for each client. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.