MQTT missing messages

I am testing a 2 way interface between Ubidots dashboard and my Arduino Nano 33 IOT with a simple program to switch on/off a led in the Nano by means of a button widget on the dashboard and to display the status of the led on a metric widget on the dashboard. The message from the dashboard to the Nano always work toggling the status of the led, but the message from the Nano to the dashboard starts working for some cycles and after some time stops being received in Ubidots broker. If I reset the Nano, the value of the variable is updated again for a short time and for a few on/off cycles. Then it stops working. The problem is that the variable does not receive the message from the Nano as I can check by looking at the variable value, and, hence, it is not updated.

I am new on this, and I don’t know how to proceed. It is not a matter of taking the message some time to arrive. The proble is that it does not reach the broker.

Please, somebody can suggest me any tip about how to proceed ?

Thanks very much !

Joan

I enclose my callback function:

void callback(char* topic, byte* payload, unsigned int length) { //Callback function
boolean retain = true;
// int qos = 1;

Serial.print(“Message received from [”);
Serial.print(topic);
Serial.print("] : ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();

if (strcmp(topic,subTopic)==0) {
if ((char)payload[0] == ‘1’) { // Switch on the LED if 1 was received as first character and return 1 in received-text
digitalWrite(LED_PIN, HIGH);
ledState = 1;
if (mqttclient.publish(pubTopic, “1”, retain)) { // 1 is published
Serial.print(“Message published to [”);
Serial.print(pubTopic);
Serial.println("] : 1");
} else {
Serial.print("*** Error publishing 1 in “);
Serial.println(pubTopic);
}
}
else { // Switch off the LED otherwise and return 0 in received-text
digitalWrite(LED_PIN, LOW);
ledState = 0;
if (mqttclient.publish(pubTopic, “0”, retain)) { // 0 is published
Serial.print(“Message published to [”);
Serial.print(pubTopic);
Serial.println(”] : 0");
} else {
Serial.print("*** Error publishing 0 in");
Serial.println(pubTopic);
}
}
}
}

Hello @jivaro ,

I hope you’re doing well.

According to your code, you’re sending data to Ubidots once the Callback function has been triggered, bear in mind that this function will be triggered only when you receive new data from the Ubidots Broker (updating your switch in the Dashboard). Is this the expected behavior of your code?

Best Regards,

– Leonardo

Yes: the idea is to confirm the reception of the data sent from the dashboard by returning it and displaying it in the dashboard.

If everything goes ok, after turning on a button in the dashboard, a metric gadget displays a 1. After turning off the button, the metric gadget displays a 0. Here the problem is that while it is understandable that some time may ellapse between toggling the button and getting the feedback on the metric gadget in the dashboard, sometimes, the metric gadget does not reflect the change even if waiting for a loooooong time. Then after sometime it can happen that it syncs again after toggling again the button, stops syncing, etc…

I cannot see any way to set a qos=1 in the pubsubclient, and this is why I do such a manual variable change confirmation, to make sure the new data has really been received by the arduino