Wemos D1 Mini Pro lost conection to MQTT Broker

Hello,
I play with Wemos D1 Mini Pro and MQTT Broker (Mosquitto).

On my "playground" is:

  • Green LED - Power On
  • Yellow LED - Connected to Wifi
  • Blue LED - Connected to MQTT Broker (Mosquitto)

Everything is "fine": green led is on, yellow led is on (when my router is on), BUT blue led in interval (approx. 30-60sec) shut down and yes reconnect and is OK, but on next interval shot down... When I check Serial monitor I see error message MQTT Connection Error: -2

Note: If MQTT Connection is OK (blue LED is ON), my Wemos correctly communicate with MQTT broker - send button status

Why I lost connection to MQTT Broker?

Thank You.

Here is my sketch:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define LED_Green D6
#define LED_Yellow D5
#define LED_Blue D0
#define Button D1

int buttonState = 0;
int previousState = 0;

const char* ssid = "SSID";
const char* password = "PWD";
const char* mqttServer = "IP";
const int mqttPort = 1883;
const char* mqttUser = "YourMQTTUsername";
const char* mqttPassword = "YourMQTTPassword";

void callback(char* topic, byte* payload, unsigned int length);

WiFiClient espClient;
PubSubClient client(espClient);

void wifi_connect() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_Yellow,HIGH);
    digitalWrite(LED_Blue, HIGH);
    Serial.println("Connecting to WiFi..");
    delay(500);
  }
  digitalWrite(LED_Yellow, LOW);
  Serial.println("Connected to the WiFi network");
}

void callback(char* topic, byte* payload, unsigned int length) {
  byte* p = (byte*)malloc(length);
  memcpy(p,payload,length);
  client.publish("test/esp8266/status", p, length);
  free(p);
}

void mqtt_connect() {
  if (WiFi.status() != WL_CONNECTED) {
    wifi_connect();
  }
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
  while (!client.connected()) {
  Serial.println("Connecting to MQTT...");
  digitalWrite(LED_Blue,HIGH);
    if(client.connect("ESP8266", mqttUser, mqttPassword ))
    {
      Serial.println("MQTT connected");
      digitalWrite(LED_Blue,LOW);
    }else{
      Serial.print("failed with state ");
      Serial.println(client.state());
      digitalWrite(LED_Blue,HIGH);
      delay(500);
    }
  }
  client.publish("test/esp8266/status","ESP Connected");
  client.subscribe("test/esp8266/text");
}

void setup()
{
  Serial.begin(115200);
  pinMode(LED_Green,OUTPUT);
  pinMode(LED_Yellow,OUTPUT);
  pinMode(LED_Blue,OUTPUT);
  pinMode(Tlacidlo, INPUT);
  digitalWrite(LED_Green, LOW);
  digitalWrite(LED_Yellow, HIGH);
  digitalWrite(LED_Blue, HIGH);
}

void loop()
{
  if (WiFi.status() != WL_CONNECTED) {
    wifi_connect();
  }
  if (!client.connected()) {
    mqtt_connect();
  } else {
    buttonState = digitalRead(Button);
    if (previousState!=buttonState) {
      if (buttonState == HIGH) {
        client.publish("test/esp8266/status","Button OK");
        
      } else if (buttonState == LOW) {
        client.publish("test/esp8266/status","Button NOK");
      }
      previousState=buttonState;
    }
  }
  client.loop();
}

In Arduino IDE I have selected LOLIN(WEMOS) D1 R2 & mini board.

When I check Serial monitor I see error message MQTT Connection Error: -2

That error message was not generated by the posted code!

Why I lost connection to MQTT Broker?

Most probably because you have some network problem. A low WiFi signal might be a reason, a router restart another one. A NAT device that times out the connection is also possible. But most probably it's not a problem in your code.

Specifying the IP address as a string is not good practice but that's not responsible for the connection drops.

Specifying the IP address as a string is not good practice but that's not responsible for the connection drops.

What is better way?

Most probably because you have some network problem. A low WiFi signal might be a reason, a router restart another one.

Yellow LED is always ON (device is connected to Wifi) and Serial monitor not print any error from Wifi.status

A NAT device that times out the connection is also possible.

I use as MQTT Broker Mosquitto on virtualised (Oracle VM VirtualBox) Linux

What is better way?

As type IPAddress.

I use as MQTT Broker Mosquitto on virtualised (Oracle VM VirtualBox) Linux

The VM uses bridged network mode? The host running VirtualBox is connected to the network by wire?

As type IPAddress.

Thanks, I can try it.

The VM uses bridged network mode? The host running VirtualBox is connected to the network by wire?

Yes, bridged mode and yes - by wire.

Yes, bridged mode and yes - by wire.

Can you ping the Wemos from there? I mean, when the connection seems to be lost.