3

I have been trying to connect my ESP32 board to my laptop via MQTT. I have installed Mosquitto MQTT broker on my laptop but I fail to connect my ESP32 every time. This is the test code I am using to check MQTT connection.

#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "......";              //WiFi Name
const char* password = "......";      //WiFi Password
const char* server= "xxx.xxx.xx.xx";    //RPi or Machine IP on which the broker is

WiFiClient espClient;
PubSubClient client(espClient);

int setup_WiFi(){
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Attempting MQTT connection...");
  client.connect("esp32");      
  if (client.connect("esp32")){                 
      Serial.println("connected");
    }
    else {
      Serial.print("failed, rc=");
      Serial.println(client.state());
    }
    return 0;
}

int reconnect() {
  unsigned long startAttemptTime = millis();
  while (!client.connected())  
    {
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("esp32")){
      Serial.println("connected");
    } 
    else {
      Serial.print("failed, rc=");
      Serial.println(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
  return 0;
}

int send_mqtt(){
  setup_WiFi();
  char sss[15]="Hello World";
   if (!client.connected()){
    reconnect();
  } 
  client.publish("esp32/test", sss);    //send message
  WiFi.disconnect(true);
  Serial.println("Sent");
  return 0;
}
void setup() {
  Serial.begin(115200);
  client.setServer(server, 1883);   //mqtt server details
  setup_WiFi();
  reconnect();
    }

void loop() {
  send_mqtt();
  delay(10000);     //Wait 10 secs before next transmission
}

But each time I get the error

failed, rc=-2

I have been trying to do the same for the past 2 months or so but to no success. Meanwhile, I have searched the internet extensively, to see what was it that I am doing wrong. I make sure that both the devices are on the same LAN.

5
  • I missed mentioning that I have opened firewall settings. I am able to send a message from localhost to localhost but outside that not able to connect not even an android
    – P_K
    Jun 22 at 13:09
  • is your esp32 on the same network or are you trying to come in from an outside address.
    – Chad G
    Jun 22 at 15:34
  • Also, is there any thing of interest in the broker logs on your laptop?
    – Chad G
    Jun 22 at 15:35
  • @Chad G yes, both the ESP32 and the laptop are on the same network
    – P_K
    Jun 23 at 11:14
  • What OS are we talking about and be more specific with how your firewall rules look like and a copy of mosquitto.conf . The sparse info you have given is just turning this into a guessing game.
    – MatsK
    Aug 9 at 13:37
1

Mosquitto is only local (on the same computer) if no custom config is used, do you use a custom config?

What OS do you have on the laptop?

Under listener in the config example found here: mosquitto/mosquitto.conf.

On row 216 add listener 1883 0.0.0.0 to allow outside of the computer connections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.