2

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 <WiFi.h >
#include <Adafruit_MQTT_Client.h>
#include <MPU9250.h>

#define wifi "********"
#define password "********"
#define server "io.adafruit.com"
#define port 1883
#define username "*******"
#define key "********"

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

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

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

  if (mqtt.connected()) {
    int data=IMU.getAccelX_mss();
    Serial.println("\nSending accelero data");
    Serial.println(data);
    Serial.println("....");
    if (feed.publish(data)) {
      Serial.println("Success");
    } else {
      Serial.println("Fail");
    }
    delay(800);
  }
}

3
  • I believe there should be a / betweeen the username and the "feeds/..." portion in your feed definition. I.e.: Adafruit_MQTT_Publish feed=Adafruit_MQTT_Publish(&mqtt,username"/feeds/accelerodata"); – StarCat Dec 9 '20 at 10:38
  • I think it should not matter as long as they are inside " ". Anyways I tried changing that too... – Ajay Raj Dec 9 '20 at 14:05
  • 1
    It does matter because username and the quoted string form a (longer) single string. Adafruit IO uses the first part of the string (up to the first / ) to identify the individual users using their service. The part after the first / is used to find the feed you (as an Adafruit IO user) have defined yourself. Look at the examples provided with the Adafruit IO library. They all use the /. – StarCat Dec 9 '20 at 15:08
1

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("Connecting to MQTT... ");

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
  }
  Serial.println("MQTT Connected!");
}
4
  • Thanks for the link.. But the connection is not being established. The above function outputs "connection failed". What could be the possible issues? How to troubleshoot the problem – Ajay Raj Dec 9 '20 at 2:42
  • Did you use the correct username and key? Try some of the example code from the Adafruit IO library. – StarCat Dec 9 '20 at 10:37
  • If you have a free Adafruit IO account, also take care not to exceed the maximum data rate (30 data points per minute). – StarCat Dec 9 '20 at 10:43
  • The key is correct.. In-fact the collection frequency was higher initially(higher than 30 points per minute) which I reduced. No improvement in the state though. – Ajay Raj Dec 9 '20 at 14:04

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.