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);
}
}
1 Answer 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:
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!");
}
-
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 problemAjay Raj– Ajay Raj2020年12月09日 02:42:24 +00:00Commented Dec 9, 2020 at 2:42
-
Did you use the correct username and key? Try some of the example code from the Adafruit IO library.StarCat– StarCat2020年12月09日 10:37:29 +00:00Commented Dec 9, 2020 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– StarCat2020年12月09日 10:43:27 +00:00Commented Dec 9, 2020 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– Ajay Raj2020年12月09日 14:04:56 +00:00Commented Dec 9, 2020 at 14:04
/
betweeen theusername
and the"feeds/..."
portion in your feed definition. I.e.:Adafruit_MQTT_Publish feed=Adafruit_MQTT_Publish(&mqtt,username"/feeds/accelerodata");
/
) 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/
.