0

I am doing a project where an ESP8266 receives data from an Arduino Uno and publishes it via MQTT. After a random amount of time, it will stop sending data to my MQTT broker. It might be an hour, it might be a day or two.

It's set up to send data every second, and I made a debug output telling me the WiFi signal quality as well. Seems like that's not it.

I have tried making checks that will reconnect to MQTT if it looses connection, and that it resets itself if it looses WiFi connection. I'm out of ideas right now, any help appreciated!

Here's my MQTT reconnnect function

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("pressure/debug", "ESP8266: reconnected!");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

and here's where it checks for WiFi

void checkWifiConnection() {
  if (WiFi.status() != WL_CONNECTED)
  {
    int countdown = millis() + 15000;
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      if (countdown > millis()) {
      delay(500);
      Serial.println("Connecting to WiFi..");
      } else {
        Serial.println("Timed out! Restarting...");
        ESP.reset();
      }
  }
  }
}

Both are run every loop.

The arduino sends the data like it's supposed to, and I've lowered the connection speed to 19200bps in order to ensure a more stable connection, using SoftwareSerial. Any more things i could monitor? I tried it with two different ESP8266MOD/"ESP07" boards with the same result.

Here's a debug output:

09:13:05.039 -> unpub data 0/ AP:Akvasafe/ RSSI:-69 dB / Vcc:3519mV / free heap:49440 / fragmentation:1%
  • use esp8266 arduino core 2.4.2 – Juraj Apr 3 at 12:37
  • Have you tried a more stable MQTT library? You haven't said which one you're using now. – Jaromanda X Apr 3 at 22:45
  • @Juraj I'm using 2.5.0, is it not advisable? – Sir Vegard G Apr 11 at 7:20
  • @JaromandaX I'm using PubSubClient, it seemed to me like it was the most popular one out there. – Sir Vegard G Apr 11 at 7:21
  • 2.5.0 has many problems – Juraj Apr 11 at 8:15
0

I use adafruit mqtt library. After a few fixes, it is running very stable.

Tips:

1) Make sure you put subscribe line BEFORE connecting to mqtt (you can put it in setup and connect to mqtt in loop)

Like:

Setup: mqtt.subscribe(&motor);

Loop: MQTT_connect();

Yes, you can both publish and subscribe at the same time.

2) IN THE LINE: Adafruit_MQTT_Client(server,port,user,pass)

CHANGE TO THIS: Adafruit_MQTT(server,port,clientid,user,pass)

Use this with client id (MUST BE UNIQUE (EG: Different for all clients)) or your client will be kicked off when other client with same id connects.

3) Convert value.lastread from char* to int : int val = atoi((char *)sensor.lastread);

More about ESP8266 & ESP32 : http://github.com/ajaybnl

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.