[SOLVED] MQTT Disconnects and callback not called upon reconnection

Good Day

This may be more of a coding issue than a UBIDOTS issue but I need some help please.

My code runs for a few minutes, sometimes hours, and then my MQTT disconnects. When it reconnects my callback function is not called and I no longer get values from UBIDOTS (Subscribe). I do have block of code to reconnect but it does not help.

I am relatively new to IOT development.

Please Assist

Greetings, it is very hard that a member of the community gives you assistance with the few information that you are sharing. Can you expand a little bit more your issue, please? Are you using an ubidots library? If yes, which one? may you please share your code? What broker answer are you retrieving from the server once you reconnect to our broker?

We will be attentive

Apologies

I am using the UbidotsESPMQTT.h library. After the broker reconnects everything on my code runs normally except for my ‘subscribe’. The callback function does not get called at all.

Hi @mpeni,

Are you under an STEM/trial account? Is that’s the case, why don’t you give it a go to the below line and put it on your setup function, just as the following:

void setup() {
  client.ubidotsSetBroker("industrial.api.ubidots.com");
}

Doing so will make you sure to use the correct broker URL.

But as @jotathebest, it would be best if you could share snippets of your code, set the debug message and maybe advise what’s printed to the serial monitor.

–David

Hello @jotathebest @dsr

As requested here is a snippet of my code with all of the relevant pieces of code that have to do with my brokers and connectivity and the callback function. I have excluded other functions but if you need more information I can include them.

// * Include Libraries/

#include "UbidotsESPMQTT.h"
#include <Type4067Mux.h>


// * Define Constants

#define TOKEN "UBIDOTS TOKEN" // Your Ubidots TOKEN
#define WIFINAME "WIFI NAME" //Your SSID
#define WIFIPASS "WIFI PASSWORD" // Your Wifi Pass

//  MUX Signal Pin Configuration
Type4067Mux mux(A0, INPUT, ANALOG, 5, 4, 2);

Ubidots client(TOKEN);

//Auxillary Functions
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
}

void setup() {
  // put your setup code here, to run once:

   // put your setup code here, to run once:
  client.wifiConnection(WIFINAME, WIFIPASS);
  Serial.begin(9600);
  client.ubidotsSetBroker("industrial.api.ubidots.com");
  client.setDebug(true);
  client.begin(callback);
  client.ubidotsSubscribe("esp8266","pump");
  client.ubidotsSubscribe("esp8266","hummin");
  client.ubidotsSubscribe("esp8266","hummax");
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  

}

void loop() {
  // put your main code here, to run repeatedly:

  if(!client.connected()){      
      client.reconnect();
      client.ubidotsSubscribe("esp8266","pump"); 
      client.ubidotsSubscribe("esp8266","hummin");
      client.ubidotsSubscribe("esp8266","hummax");
      }

}

 delay(2000);
 client.loop();  

-Thank You