ESP8266 WiFi disconnecting every 15s

Hello everyone. I'm trying to setup a basic MQTT communication scheme that enables a user to control a servo motor (that acts as a simple ramp). The code worked perfectly on my local WiFi up until yesterday when i tried to run it again, and found that my NodeMCU ESP8266 disconnects from WiFi exactly every 15s or so, which messes with the MQTT broker connection so the whole project does not work as it should. The code used is as follows:

#include <Servo.h>
#include "EspMQTTClient.h"

#define LED_RED D2
#define LED_GREEN D1
#define SERVO D4

Servo servo;

EspMQTTClient client(
  "mySSID",       // WiFi SSID
  "myPassword",         // WiFi Password
  "myServerIP",  // MQTT Broker server ip
  "myClientName",         // Client name that uniquely identify your device
  1883                  // The MQTT port, default to 1883. this line can be omitted
);

// This function is called once everything is connected (Wifi and MQTT)
void onConnectionEstablished() {
  client.publish("USprojekat/Konekcija", "Device Online", true);
  client.publish("USprojekat/ZelenoSvjetlo", "0");
  client.publish("USprojekat/CrvenoSvjetlo", "1");
  
  // Subscribe to "USprojekat/Servo" to work with all the functionalities
  client.subscribe("USprojekat/Servo", 
    [](const String & payload) {
      Serial.println(payload);

      if(payload == "1") {
        // Set LEDs to GO
        client.publish("USprojekat/ZelenoSvjetlo", "1");
        client.publish("USprojekat/CrvenoSvjetlo", "0");
        digitalWrite(LED_RED, LOW);
        digitalWrite(LED_GREEN, HIGH);

        // Raise the ramp
        Serial.println("Podizem rampu...");
        client.publish("USprojekat/Info", "Podizem rampu...");
        int pos = 0;
        for(pos; pos <= 180; pos += 3) 
          servo.write(pos);

        // Wait 5s for the car to pass
        Serial.println("Rampa podignuta!");
        client.publish("USprojekat/Info", "Rampa podignuta!");
        delay(5000);

        // Set LEDs to GO
        client.publish("USprojekat/ZelenoSvjetlo", "0");
        client.publish("USprojekat/CrvenoSvjetlo", "1");
        digitalWrite(LED_RED, HIGH);
        digitalWrite(LED_GREEN, LOW);

        // Lower the ramp
        Serial.println("Spustam rampu...");
        client.publish("USprojekat/Info", "Spustam rampu...");
        for(pos = 180; pos >= 0; pos -= 2) 
          servo.write(pos); 
        
        Serial.println("Rampa spustena.");
        client.publish("USprojekat/Info", "Rampa spustena.");
        client.publish("USprojekat/Servo", "0");    
      }
    });
}

void setup() {
  Serial.begin(74880);

  client.enableLastWillMessage("USprojekat/Konekcija", "Device Offline", true);  

  servo.attach(SERVO);            // Set the pin for the servo
  servo.write(0);                 // Set the servo to 0 position
  pinMode(LED_RED, OUTPUT);       // Set the pin for STOP LED
  pinMode(LED_GREEN, OUTPUT);     // Set the pin for GO LED

  // Set LEDs to STOP position on start
  digitalWrite(LED_RED, HIGH);
  digitalWrite(LED_GREEN, LOW);
}

void loop() {
  // Scan for new messages every 250ms
  client.loop();
  delay(250);
}

As you can see, the code also includes control for a couple of LEDs that are signaling if the ramp is being raised or lowered. The MQTT client sends some Info and Status messages as well. What could be the problem?

Thanks in advance!

see the documentation for your library

Change the keep alive interval (15 seconds by default)
void setKeepAlive(uint16_t keepAliveSeconds);

so you need to send a message at leat every 15s or the connection will be dropped (or change the keepAlive duration)

I was reading the documentation and found this method right as you posted your reply, so i tried setting it to 65535, the max for uint16_t, but it is still defaulting to 15s and reconnecting the MQTT client. The WiFi connection seems fine.

underneath your library is the pubSubClient library

see what they say about keepAlive :

PubSubClient* setKeepAlive (keepAlive)

Sets the keep alive interval used by the client. This value should only be changed when the client is not connected.

By default, it is set to 15 seconds - as defined by the MQTT_KEEPALIVE constant in PubSubClient.h.

Parameters
  • keepAlive uint16_t - the keep alive interval, in seconds
Returns
  • PubSubClient* - the client instance, allowing the function to be chained

I'd investigate based on that

Glad to say I have resolved the issue. The problem was indeed in the keepAlive time in the pubsub class, however, instead of toying with different macros defined in that class, it was enough to add

client.enableMQTTPersistence();

in the setup() function. That makes the client connect with a persistent connection, i.e. it will never kill the MQTT connection.

Thanks for Your input.

1 Like

Thanks for sharing your solution

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.