MQTT connection not being recognised in void loop() I am trying to publish MPU 9250 readings in the Adafruit IO sending the data from ESP32. The WiFi connection is being established, but the code is not going inside the if(mqtt.connected()) inside the void loop() whereas it's going inside the same command inside the void setup().
I am sure there are no connection issues because once I change if(mqtt.connected()) to while(mqtt.connected()) inside setup(), code is going inside the while loop continuously.
Why is the code not going inside the if command and how can this be solved?
#include &lt;WiFi.h &gt;
#include &lt;Adafruit_MQTT_Client.h&gt;
#include &lt;MPU9250.h&gt;

#define wifi &quot;********&quot;
#define password &quot;********&quot;
#define server &quot;io.adafruit.com&quot;
#define port 1883
#define username &quot;*******&quot;
#define key &quot;********&quot;

WiFiClient esp;
MPU9250 IMU(Wire,0x68);
Adafruit_MQTT_Client mqtt(&amp;esp, server, port, username, key);
Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&amp;mqtt, username&quot;feeds/accelerodata&quot;);

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println(&quot;Adafruit MQTT Demo&quot;);
  Serial.println(&quot;Connecting to&quot;);
  Serial.println(wifi);
  WiFi.begin(wifi,password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println(&quot;Connecting...&quot;);
  }
  Serial.println(&quot;Connected&quot;);
  Serial.println(&quot;IP Address: &quot;);
  Serial.println(WiFi.localIP());
  Serial.println(&quot;Connecting to MQTT&quot;);
  
  if (mqtt.connect()) {
    Serial.println(&quot;MQTT Connected&quot;);
  }
} 

void loop () {
  Serial.println(&quot;Test line to check if code reaches inside loop&quot;);
  delay(1000);

  if (mqtt.connected()) {
    int data=IMU.getAccelX_mss();
    Serial.println(&quot;\nSending accelero data&quot;);
    Serial.println(data);
    Serial.println(&quot;....&quot;);
    if (feed.publish(data)) {
      Serial.println(&quot;Success&quot;);
    } else {
      Serial.println(&quot;Fail&quot;);
    }
    delay(800);
  }
}
I believe your problem is that you are attempting to use the MQTT connection before it's actually connected.  So your check:
if(mqtt.connected())

is always FALSE since it's not really connected.  The Adafruit library information here:
Adafruit MQTT
Says to initialize MQTT as:
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print(&quot;Connecting to MQTT... &quot;);

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println(&quot;Retrying MQTT connection in 5 seconds...&quot;);
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
  }
  Serial.println(&quot;MQTT Connected!&quot;);
}