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);
}
}
}
}