ESP8266 - EspMQTTClient - Error: MQTT! Trying to publish when disconnected, skipping

Hello everyone,

This is my first attempt to use an ESP8266 board. My goal is to attach 3x DHT22 sensors to it, which will monitor a non-smart fridge located in my garage.
I managed to create code using examples and documentation found online, the code compiles and uploads to the ESP8266, however I receive this error in serial output:

MQTT! Trying to publish when disconnected, skipping.

The ESP8266 connects to Wi-Fi and receives an IP, I can ping it as well. The error must be related to MQTT connection to the broker. I must add that I do receive one MQTT message on the broker (the lastwill message).

I think I messed something up in the loop, but I am not quite sure what. Guidance would be very much appreciated!

The code is below, I have obfuscated Wi-Fi SSID and password:

#include <ESP8266WiFi.h>
#include "DHT.h"   //https://github.com/adafruit/DHT-sensor-library
#include "EspMQTTClient.h"

#ifndef STASSID
#define STASSID "obfuscatedssid"
#define STAPSK "obfuscatedpassword"
#define STAHOSTNAME "ESP8266_Frigider_Garaj"
#endif

// MQTT Client Setup
EspMQTTClient client(
  "obfuscatedssid",
  "obfuscatedpassword",
  "192.168.2.11",  // MQTT Broker server ip
  "mosquitto",     // Can be omitted if not needed
  "obfuscatedpassword",    // Can be omitted if not needed
  "ESP8266_Frigider_Garaj",  // Client name that uniquely identifies your device
  1883              // The MQTT port, default to 1883. this line can be omitted
);

// Create the DHT temperature and humidity sensor objects
DHT sensor1(D4, DHT22); // Change the pin D4 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22
DHT sensor2(D3, DHT22); // Change the pin D3 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22
DHT sensor3(D2, DHT22); // Change the pin D2 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22

const char* ssid = STASSID;
const char* password = STAPSK;
const char* hostname = STAHOSTNAME;

// Declare the variables that will hold the sensor readings
float sensor1_temp, sensor2_temp, sensor3_temp;
float sensor1_humi, sensor2_humi, sensor3_humi;

char s1_temp[10], s2_temp[10], s3_temp[10];
char s1_humi[10], s2_humi[10], s3_humi[10];

void setup(void) {
  Serial.begin(115200);
  sensor1.begin();
  sensor2.begin();
  sensor3.begin();

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.setHostname(hostname);
  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());

  // Optional functionalities of EspMQTTClient
  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater();    // Enable the web updater
  client.enableOTA();               // Enable OTA (Over The Air) updates
  client.enableLastWillMessage("ESP8266_Frigider_Garaj/lastwill", "I am going offline"); // Last will message
}

void onConnectionEstablished() {
  // Subscribe to topics and display received messages to Serial
  client.subscribe("$SYS/broker/version", [](const String & payload) {
    Serial.println(payload);
  });

  client.subscribe("$SYS/broker/uptime", [](const String & payload) {
    Serial.println(payload);
  });

  // Publish a message to "mytopic/test"
  client.publish("ESP8266_Frigider_Garaj/test", "This is a message");

  // Execute delayed instructions
  client.executeDelayed(5 * 1000, []() {
    client.publish("ESP8266_Frigider_Garaj/test2", "This is a message sent 5 seconds later");
  });
}

void loop() {


  // Read DHT temperature and humidity values for Sensor-1
  sensor1_temp = sensor1.readTemperature();
  sensor1_humi = sensor1.readHumidity();
  dtostrf(sensor1_temp, 3, 2, s1_temp);
  dtostrf(sensor1_humi, 3, 2, s1_humi);

  // Read DHT temperature and humidity values for Sensor-2
  sensor2_temp = sensor2.readTemperature();
  sensor2_humi = sensor2.readHumidity();
  dtostrf(sensor2_temp, 3, 2, s2_temp);
  dtostrf(sensor2_humi, 3, 2, s2_humi);

  // Read DHT temperature and humidity values for Sensor-3
  sensor3_temp = sensor3.readTemperature();
  sensor3_humi = sensor3.readHumidity();
  dtostrf(sensor3_temp, 3, 2, s3_temp);
  dtostrf(sensor3_humi, 3, 2, s3_humi);

  client.loop();

  if (client.isConnected()) {
    // Publish strings to MQTT only if the client is connected
    client.publish("ESP8266_Frigider_Garaj/sensor1_temperature", s1_temp);
    client.publish("ESP8266_Frigider_Garaj/sensor1_humidity", s1_humi);
    client.publish("ESP8266_Frigider_Garaj/sensor2_temperature", s2_temp);
    client.publish("ESP8266_Frigider_Garaj/sensor2_humidity", s2_humi);
    client.publish("ESP8266_Frigider_Garaj/sensor3_temperature", s3_temp);
    client.publish("ESP8266_Frigider_Garaj/sensor3_humidity", s3_humi);
  } else {
    Serial.println("MQTT! Trying to publish when disconnected, skipping.");
  }

  Serial.print("Sensor-1 Readings:  ");
  Serial.print(sensor1_temp, 1); Serial.print(String(char(176)) + "C  ");
  Serial.print(sensor1_humi, 1); Serial.println("%RH");
  Serial.println();

  Serial.print("Sensor-2 Readings:  ");
  Serial.print(sensor2_temp, 1); Serial.print(String(char(176)) + "C  ");
  Serial.print(sensor2_humi, 1); Serial.println("%RH");
  Serial.println();

  Serial.print("Sensor-3 Readings:  ");
  Serial.print(sensor3_temp, 1); Serial.print(String(char(176)) + "C  ");
  Serial.print(sensor3_humi, 1); Serial.println("%RH");
  Serial.println();

  delay(30000); // Wait for 30 seconds before the next loop
}

I was able to fix it while my original post was waiting for review.
Some code needed to be moved from the loop itself into the onConnectionEstablished section.
Adjusted code below. There are some other kinks to be resolved, but for now I am troubleshooting those, I will update if I still need help.

#include <ESP8266WiFi.h>
#include "DHT.h"   //https://github.com/adafruit/DHT-sensor-library
#include "EspMQTTClient.h"

#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "obfuscatedpassword"
#define STAHOSTNAME "ESP8266_Frigider_Garaj"
#endif

// MQTT Client Setup
EspMQTTClient client(
  "ssid",
  "obfuscatedpassword",
  "192.168.2.11",  // MQTT Broker server ip
  "mosquitto",     // Can be omitted if not needed
  "obfuscatedpassword",    // Can be omitted if not needed
  "ESP8266_Frigider_Garaj",  // Client name that uniquely identifies your device
  1883              // The MQTT port, default to 1883. this line can be omitted
);

// Create the DHT temperature and humidity sensor objects
DHT sensor1(D4, DHT22); // Change the pin D4 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22
DHT sensor2(D3, DHT22); // Change the pin D3 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22
DHT sensor3(D2, DHT22); // Change the pin D2 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22

const char* ssid = STASSID;
const char* password = STAPSK;
const char* hostname = STAHOSTNAME;

//Declare Delay

unsigned long previousMillis = 0UL;
unsigned long interval = 30000UL;

// Declare the variables that will hold the sensor readings
float sensor1_temp, sensor2_temp, sensor3_temp;
float sensor1_humi, sensor2_humi, sensor3_humi;

char s1_temp[10], s2_temp[10], s3_temp[10];
char s1_humi[10], s2_humi[10], s3_humi[10];

void setup(void) {
  Serial.begin(115200);
  sensor1.begin();
  sensor2.begin();
  sensor3.begin();

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.setHostname(hostname);
  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());

  // Optional functionalities of EspMQTTClient
  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater();    // Enable the web updater
  client.enableOTA();               // Enable OTA (Over The Air) updates
  client.enableLastWillMessage("ESP8266_Frigider_Garaj/lastwill", "I am going offline"); // Last will message
}

void onConnectionEstablished() {

  // Read DHT temperature and humidity values for Sensor-1
  sensor1_temp = sensor1.readTemperature();
  sensor1_humi = sensor1.readHumidity();
  dtostrf(sensor1_temp, 3, 2, s1_temp);
  dtostrf(sensor1_humi, 3, 2, s1_humi);

  // Read DHT temperature and humidity values for Sensor-2
  sensor2_temp = sensor2.readTemperature();
  sensor2_humi = sensor2.readHumidity();
  dtostrf(sensor2_temp, 3, 2, s2_temp);
  dtostrf(sensor2_humi, 3, 2, s2_humi);

  // Read DHT temperature and humidity values for Sensor-3
  sensor3_temp = sensor3.readTemperature();
  sensor3_humi = sensor3.readHumidity();
  dtostrf(sensor3_temp, 3, 2, s3_temp);
  dtostrf(sensor3_humi, 3, 2, s3_humi);

  if (client.isConnected()) {
    // Publish strings to MQTT only if the client is connected
    client.publish("ESP8266_Frigider_Garaj/sensor1_temperature", s1_temp);
    client.publish("ESP8266_Frigider_Garaj/sensor1_humidity", s1_humi);
    client.publish("ESP8266_Frigider_Garaj/sensor2_temperature", s2_temp);
    client.publish("ESP8266_Frigider_Garaj/sensor2_humidity", s2_humi);
    client.publish("ESP8266_Frigider_Garaj/sensor3_temperature", s3_temp);
    client.publish("ESP8266_Frigider_Garaj/sensor3_humidity", s3_humi);
  } else {
    Serial.println("MQTT! Trying to publish when disconnected, skipping.");
  }

  Serial.print("Sensor-1 Readings:  ");
  Serial.print(sensor1_temp, 1); Serial.print(String(char(176)) + "C  ");
  Serial.print(sensor1_humi, 1); Serial.println("%RH");
  Serial.println();

  Serial.print("Sensor-2 Readings:  ");
  Serial.print(sensor2_temp, 1); Serial.print(String(char(176)) + "C  ");
  Serial.print(sensor2_humi, 1); Serial.println("%RH");
  Serial.println();

  Serial.print("Sensor-3 Readings:  ");
  Serial.print(sensor3_temp, 1); Serial.print(String(char(176)) + "C  ");
  Serial.print(sensor3_humi, 1); Serial.println("%RH");
  Serial.println();
  delay(30000);
}

void loop() {
  client.loop();
//    unsigned long currentMillis = millis();
//
//  if(currentMillis - previousMillis > interval)
//  {
	/* The Arduino executes this code once every second
 	*  (interval = 1000 (ms) = 1 second).
 	*/

 	// Don't forget to update the previousMillis value
// 	previousMillis = currentMillis;
//  }
}