Problems with MQTT PubSubClient
Posted: Wed May 13, 2015 5:22 am
Hello all,
I've been trying to implement a simple MQTT example using Arduino firmware on my ESP-01. For some reason however, the MQTT client on the ESP8266 stops responding after about three to four minutes consistently. I'd had this same problem on NodeMCU firmware on another ESP-01 with the same result. I added some diagnostic code after the MQTT bits in the loop() function (a non-blocking LED blink with millis()) so I could tell where the problem was occuring, and it turns out that when the client stops responding, the LED also stops blinking, indicating that its the whole sketch that stops running for some reason. I turned on WiFi diagnostics via Serial, and it seems as though the ESP disconnects and reconnects to the router, but I don't know why. Has anyone experienced similar issues, and perhaps solved them?
Here's my code:
Thanks in advance,
M
I've been trying to implement a simple MQTT example using Arduino firmware on my ESP-01. For some reason however, the MQTT client on the ESP8266 stops responding after about three to four minutes consistently. I'd had this same problem on NodeMCU firmware on another ESP-01 with the same result. I added some diagnostic code after the MQTT bits in the loop() function (a non-blocking LED blink with millis()) so I could tell where the problem was occuring, and it turns out that when the client stops responding, the LED also stops blinking, indicating that its the whole sketch that stops running for some reason. I turned on WiFi diagnostics via Serial, and it seems as though the ESP disconnects and reconnects to the router, but I don't know why. Has anyone experienced similar issues, and perhaps solved them?
Here's my code:
Code: Select all
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
const char* ssid = "myssid";
const char* password = "password";
char* topic = "/pong";
char* server = "192.168.1.168";
bool sendHollerBack = false;
long currTime = millis();
bool blinkState = false;
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
void callback(char* topic, byte* payload, unsigned int length) {
sendHollerBack = true;
}//end callback
void setup() {
Serial.begin(115200);
delay(10);
Serial.setDebugOutput(true);
pinMode(2,OUTPUT);
digitalWrite(2,blinkState);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp";
//uint8_t mac[6];
//WiFi.macAddress(mac);
//clientName += macToStr(mac);
//clientName += "-";
//clientName += String(micros() & 0xff, 16);
Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);
if (client.connect((char*) clientName.c_str())) {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);
}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
if (!client.subscribe("/ping")) {
Serial.println("Subscribe failed. Resetting..");
abort();
}
else {
Serial.println("Subscribe successful.");
}
}//end setup
void loop() {
Serial.println("Alive!");
if (!client.connected()){
while (!client.connect("esp")) {
Serial.println("Reconnecting to MQTT Broker...");
}
}
yield();
client.loop();
yield();
if (sendHollerBack) {
if (client.publish(topic, "hollerback")) {
Serial.println("Publish successful");
}
else Serial.println("Publish error");
}//end if-hollerback
sendHollerBack = false;
yield();
//diagnostic code: blinky LED on GPIO2
if ((millis() - currTime) > 500) {
blinkState = !blinkState;
digitalWrite(2,blinkState);
currTime = millis();
}
yield();
}
Thanks in advance,
M