Hey! I’m working on this project in which I intend to have a subscriber esp8266 client to three different esp8266 client publishers. I’m using the clusters from the HiveMQ Cloud and for each esp client, I’ve created a unique credential.
I’ve been able to subscribe to each one individually but I’m struggling to subscribe to them all at once.
For more context, the subscriber esp is going to control a motor and the other publisher esps are responsible for certain sensors - so depending if the subscriber esp receives a True or False from each of the publishers, it will or will not activate the motor.
I’ve tried doing this:
String callback(char* topic, byte* payload, unsigned int length) {
/* This funcion is responsible for printing out what has been caught
* in the subscribe of the topic. */
// front sensors
if (strcmp(topic,"Subscribe")==0){
// whatever you want for this topic
Serial.print("Message from Sensores Frontais arrived - ");
Serial.print(topic);
for (int i = 0; i < length; i++) {
// response from the front sensors
has_object = (char)payload[i];
Serial.print(has_object);
}
Serial.println();
}
// lidar
if (strcmp(topic,"Lidar_Publisher")==0){
// whatever you want for this topic
Serial.print("Message from Lidar arrived - ");
Serial.print(topic);
for (int i = 0; i < length; i++) {
// response from lidar
has_pedestrian = (char)payload[i];
Serial.print(has_pedestrian);
}
Serial.println();
}
has_object_str = String(has_object);
has_pedestrian_str = String(has_pedestrian);
String mqtt_string = has_object_str + "&" + has_pedestrian_str + "!";
return mqtt_string;
}
void reconnect() {
// Loop until we’re reconnected
while (!client->connected()) {
Serial.print("Attempting MQTT connection…");
String clientId = "ESP8266Client - MyClient_Sub";
// Attempt to connect to client
if (client->connect(clientId.c_str(), "<subscriber's credential name>", "<subscriber's credential password>")) {
Serial.println("connected");
// … and subscribe
client->subscribe("testTopic");
client->subscribe("testTopic2");
} else {
Serial.print("failed, rc = ");
Serial.print(client->state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}```
But this doesnt work because when I check the Serial Monitor, instead of seeing the subscribed data, I get this error in a loop:
'
Connecting to <wifi name>
........
WiFi connected
IP address:
xxx.xxx.xx.xx
Waiting for NTP time sync: .
CET Sat Sep 10 02:48:35 2022
Number of CA certs read: 164
Attempting MQTT connection…connected
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (28):
epc1=0x4000bdc8 epc2=0x00000000 epc3=0x4000065c excvaddr=0x00000000 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffd70 end: 3fffffc0 offset: 0190
3fffff00: 3fffdad0 3ffeed74 3fffff01 40210a59
3fffff10: 3fffdad0 3ffeed74 3ffeec78 40205aaa
3fffff20: 00000000 001c001f 00000000 3ffeee14
3fffff30: 3fffdad0 3ffeec78 3fffff90 40206677
3fffff40: 00000000 72207300 00000000 4020a455
3fffff50: ffffffff 00000000 3fffff90 4020cbf4
3fffff60: 3fffdad0 3ffeecd8 3fffff90 4020cca8
3fffff70: 00000000 00000000 3ffeee00 3ffeee14
3fffff80: 3fffdad0 00000000 3ffeee00 40206781
3fffff90: 212d7c26 72207300 04000000 402103b4
3fffffa0: 3fffdad0 00000000 3ffeee00 4020d318
3fffffb0: feefeffe feefeffe 3ffe864c 401010d9
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
ets Jan 8 2013,rst cause:2, boot mode:(3,7)
load 0x4010f000, len 3460, room 16
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4
tail 4
chksum 0xc9
csum 0xc9
v0006af30
~ld
Connecting to <wifi name>
............................
'
What am I doing wrong? How do I subscribe to more than one topic from different clients?