[SOLVED] MQTT Connection fails

Hi,

I’m doing a simple experiment using a NodeMCU and a proximity sensor. My intent is to publish the captured data values to ubidots. However I’m experiencing a problem in terms of connecting to the network using MQTT.
I keep getting the message:

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

What confuses me is that I can easily connect to shared network from my phone and send data values to ubidots. However, whenever I’m try to connect to a regular network connection at any institution (library, company, etc.), it fails and I keep getting the above-mentioned message.

Can anyone recognise this error?

Regards

PS: the following code was my starting point and modified it (of course) according to my particular experiment:


include “UbidotsESPMQTT.h”

define TOKEN “…” // Your Ubidots TOKEN
define WIFINAME “…” //Your SSID
define WIFIPASS “…” // Your Wifi Pass
define MQTTCLIENTNAME “…” // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name

Ubidots client(TOKEN, MQTTCLIENTNAME);

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:
Serial.begin(115200);
client.setDebug(true); // Pass a true or false bool value to activate debug messages
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();
}


Hello, if the NodeMCU connects successfully to your cellphone the issue comes from the public AP at your regular network. Keep in mind that our library connects by default to networks with WPA2 security, if you use WEP you will have to edit the method for connecting.

Regards

Hi,

Thanks for the reply.

Could you elaborate a bit on “edit the method for connecting”? Is it editing the method in the NodeMCU program or in the particular network that I want to connect to?

Regards

Hello, at .cpp file you can find the wifiConnection() method which implements the routine for connecting the device, what I recommend you is to make a test for connecting to the place where you have issues without using our library and once you can make the connection replace the routine of the connection method. As I said befor, by default is set to WPA2, so if you experience issues with the connection they ma be attached to security configurations or hardware problems.

Regards