Hello Ian,
The full program is rather lengthy since I am parsing a configuration file and initializing different stuffs and data structure for my board.
In the code I increment a counter for each message published so that I can compare the number of publications with the messages I receive.
However, the following code sample might sufficient.
I also added below the output of my program.
You should be able to observe that, though I register to the topics
- MyMod1Al/IN6/ChangeDetected
- MyMod1A1/IN3/ChangeDetected
I only receive messages related to the first subscription, not to the second.
If needed, I could try to write a simplified code to see if I can get more info. Please let me know if this is necessary.
/**********************************************************************************/
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
if(verbose==1)
printf("Connecting to %s:%d\n", config->hostname, config->port);
int rc = ipstack.connect(config->hostname, config->port);
if ((rc != 0)&&(verbose==1))
printf("rc from TCP connect is %d\n", rc);
if(verbose==1)
printf("MQTT connecting\n");
MQTTPacket_connectData data = ""
data.MQTTVersion = 3;
data.clientID.cstring = (char*)config->clientID;
rc = client.connect(data);
if ((rc != 0)&&(verbose==1))
printf("rc from MQTT connect is %d\n", rc);
if(verbose==1)
printf("MQTT connected\n");
for(i=0;i<4;i++){
if (strcmp(config->relayTopic[i],"None")!=0){
switch(i){
case 0:
rc = client.subscribe(config->relayTopic[i], MQTT::QOS2, switchRelay1);
break;
case 1:
rc = client.subscribe(config->relayTopic[i], MQTT::QOS2, switchRelay2);
break;
case 2:
rc = client.subscribe(config->relayTopic[i], MQTT::QOS2, switchRelay3);
break;
case 3:
rc = client.subscribe(config->relayTopic[i], MQTT::QOS2, switchRelay4);
break;
default:
break;
}
if (verbose==1){
if (rc != 0)
printf("rc from MQTT subscribe (Relay %d, topic %s) is %d\n",
i+1,config->relayTopic[i],rc);
else
printf("MQTT subscription for relay activation %d - topic %s\n",
i+1,config->relayTopic[i]);
}
}
}
MQTT::Message message;
switch(config->QoS){
case 0:
message.qos = MQTT::QOS0;
break;
case 1:
message.qos = MQTT::QOS1;
break;
case 2:
message.qos = MQTT::QOS2;
break;
default:
message.qos = MQTT::QOS0;
}
if(verbose==1)
printf("Selected message QoS is %d\n",config->QoS);
while(!toStop){
/*if(verbose==1)
printf("Sample loop\n");*/
for(i=0;i<7;i++){
if(config->activateInput[i]==1){
if(sampleChangedInput(fdADC,i,&res,config) != 0)
return -1;
if(res==1){
if(verbose==1)
printf("Change detected on input %d - state value %d\n",i+1,inputState[i]);
sprintf(topic, "%s/IN%d/ChangeDetected",config->topic,i+1);
sprintf(buf,"In%d - New state value is %d",i+1,inputState[i]);
message.payload = (void*)buf;
message.payloadlen = strlen(buf)+1;
rc = client.publish(topic, message);
if (verbose==1){
printf("Publishing(%d): \nTopic is %s\nBuf is %s\n",eventCounter,topic,buf);
}
eventCounter++;
if ((rc != 0)&&(verbose==1))
printf("Error %d from sending message\n", rc);
while (arrivedcount == config->QoS)
client.yield(100);
}
}
}
usleep(config->samplingTime);
}
/**********************************************************************************/
The output I get from the program is the following:
Parsing configuration file MQTTTest.conf
The content of the configuration file is the following:
Broker hostname is 192.104.167.67
Broker port num is 1883
Client ID is MyModule1
Client QoS is 1
Client default topic is MyMod1Al
Relay switching activation topic are:
Relay 1 is MyMod1Al/IN6/ChangeDetected
Relay 2 is MyMod1A1/IN3/ChangeDetected
Relay 3 is MyMod1A1/IN1/ChangeDetected
Relay 4 is None
Input activation configurations are:
Input 1 is 1
Input 2 is 0
Input 3 is 1
Input 4 is 0
Input 5 is 0
Input 6 is 1
Input 7 is 0
Connecting to 192.104.167.67:1883
MQTT connecting
MQTT connected
MQTT subscription for relay activation 1 - topic MyMod1Al/IN6/ChangeDetected
MQTT subscription for relay activation 2 - topic MyMod1A1/IN3/ChangeDetected
MQTT subscription for relay activation 3 - topic MyMod1A1/IN1/ChangeDetected
Selected message QoS is 1
Change detected on input 6 - state value 1
Publishing(1):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 1
Change detected on input 6 - state value 0
Message 1 arrived: qos 1, retained 0, dup 0, packetid 1
Payload (len is 27): In6 - New state value is 1
Topic - MyMod1Al/IN6/ChangeDetected
Publishing(2):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 0
Message 2 arrived: qos 1, retained 0, dup 0, packetid 2
Payload (len is 27): In6 - New state value is 0
Topic - MyMod1Al/IN6/ChangeDetected
Change detected on input 6 - state value 1
Publishing(3):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 1
Change detected on input 6 - state value 0
Publishing(4):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 0
Message 3 arrived: qos 1, retained 0, dup 0, packetid 3
Payload (len is 27): In6 - New state value is 1
Topic - MyMod1Al/IN6/ChangeDetected
Change detected on input 3 - state value 1
Publishing(5):
Topic is MyMod1Al/IN3/ChangeDetected
Buf is In3 - New state value is 1
Message 4 arrived: qos 1, retained 0, dup 0, packetid 4
Payload (len is 27): In6 - New state value is 0
Topic - MyMod1Al/IN6/ChangeDetected
Change detected on input 3 - state value 0
Publishing(6):
Topic is MyMod1Al/IN3/ChangeDetected
Buf is In3 - New state value is 0
Change detected on input 6 - state value 1
Publishing(7):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 1
Change detected on input 6 - state value 0
Publishing(8):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 0
Message 5 arrived: qos 1, retained 0, dup 0, packetid 5
Payload (len is 27): In6 - New state value is 1
Topic - MyMod1Al/IN6/ChangeDetected
Change detected on input 3 - state value 1
Publishing(9):
Topic is MyMod1Al/IN3/ChangeDetected
Buf is In3 - New state value is 1
Message 6 arrived: qos 1, retained 0, dup 0, packetid 6
Payload (len is 27): In6 - New state value is 0
Topic - MyMod1Al/IN6/ChangeDetected
Change detected on input 3 - state value 0
Publishing(10):
Topic is MyMod1Al/IN3/ChangeDetected
Buf is In3 - New state value is 0
Change detected on input 6 - state value 1
Publishing(11):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 1
Change detected on input 6 - state value 0
Publishing(12):
Topic is MyMod1Al/IN6/ChangeDetected
Buf is In6 - New state value is 0
Message 7 arrived: qos 1, retained 0, dup 0, packetid 7
Payload (len is 27): In6 - New state value is 1
Topic - MyMod1Al/IN6/ChangeDetected
Change detected on input 3 - state value 1
Publishing(13):
Topic is MyMod1Al/IN3/ChangeDetected
Buf is In3 - New state value is 1
Message 8 arrived: qos 1, retained 0, dup 0, packetid 8
Payload (len is 27): In6 - New state value is 0
Topic - MyMod1Al/IN6/ChangeDetected
Change detected on input 3 - state value 0
Publishing(14):
Topic is MyMod1Al/IN3/ChangeDetected
Buf is In3 - New state value is 0
^CFinishing with 8 messages received