[SOLVED] Problem with Ubditots MQTT Connection (ESP8266)

Hello,

I have been using the ESP8266 to transfer data to the Ubidots platform over the MQTT connection for the last year. When I tried it again today, it did not work. The code has not changed. As an alternative I used the “base code” for the MQTT connection again and tried to transfer just a simple Variable, where I also get the same error message:

“Attempting MQTT connection…failed, rc=-4 try again in 3 seconds”

I am sure that my TOKEN is correct, so I can rule that out as the cause.

Can you help me?

Thank you very much in advance!

Code:

#include "UbidotsESPMQTT.h"


#define TOKEN "" // Your Ubidots TOKEN
#define WIFINAME "" //Your SSID
#define WIFIPASS "" // Your Wifi Pass

Ubidots client(TOKEN);



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]);
  }
  Serial.println();
}


void setup() {
  // put your setup code here, to run once:
  //client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
  
  float value1 = 5.3; 
  client.add("temperature", value1);
  client.ubidotsPublish("my-new-device");
  client.loop();
}

Greetings, a return code equals to -4 means a connection timeout issue, it is generally related with the access point or network connection, can you try in another site or network?

All the best

Hello,
thank you for your anaswer! Yes, i tried 2 different networks and the internet connection seems to work.

This is what i get in the Serial Monitor:
1384, room 16
12:51:23.225 -> tail 8
12:51:23.225 -> chksum …WiFi connected
12:52:08.296 -> IP address:
12:52:08.296 -> 172.20.10.6
12:52:08.296 -> entra
12:52:08.296 -> Attempting MQTT connection…failed, rc=-4 try again in 3 seconds
12:52:26.537 -> Attempting MQTT connection…

Greets
Lennart

I have just uploaded the firmware below to an ESP8266 and it works properly, please give it a try:

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "..." // Your Ubidots TOKEN
#define WIFINAME "..." //Your SSID
#define WIFIPASS "...." // Your Wifi Pass

Ubidots client(TOKEN);

/****************************************
 * Auxiliar 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]);
  }
  Serial.println();
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("industrial.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
 }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
  
  // Publish values to 2 different data sources
  
  client.add("stuff", 10.2); //Insert your variable Labels and the value to be sent
  client.ubidotsPublish("source1");
  client.add("stuff", 10.2);
  client.add("more-stuff", 120.2);
  client.ubidotsPublish("source2");
  client.loop();
  delay(10000);
  }

Your issue seems related with your network, and unfortunately it is hard for me to throubleshoot that kind of errors from my side.

All the best