martin-ger / uMQTTBroker

MQTT Broker library for ESP8266 Arduino
MIT License
442 stars 105 forks source link

Error: The connection was lost #38

Open philippesikora opened 4 years ago

philippesikora commented 4 years ago

The program defines a custom broker class with callbacks, I connect from a remote client and publish as below. After 6 iterations, I have the following message "Error: The connection was lost"

MQTT client: root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400"
After 1st iteration, program works well root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" after 6 iteration, MQTT broker is down with the following message Error: The connection was lost.

MQTT broker running on ESP8266:


include "uMQTTBroker.h"

include

include

int ref_index=0; char ssid[] = "your SSID"; // your network SSID (name) char password[] = "your password"; // your network password

class myMQTTBroker: public uMQTTBroker { public: virtual bool onConnect(IPAddress addr, uint16_t client_count) { Serial.println(addr.toString()+" connected"); return true; }

virtual bool onAuth(String username, String password) {
  Serial.println("Username/Password: "+username+"/"+password);
  return true;
}

virtual void onData(String topic, const char *data, uint32_t length) {
  String temp;
  char data_str[length+1];
  os_memcpy(data_str, data, length);
  data_str[length] = '\0';
  Serial.println("received topic '"+topic+"' with data '"+(String)data_str+"'");
  if (topic == "MQTT_ref") {
    temp = String(data_str);
    ref_index= temp.toInt();
    Serial.println("MQTT OK");
  } 

}

};

myMQTTBroker myBroker;

void setup() { Serial.begin(9600); WiFi.begin ( ssid, password ); // Attente de la connexion au réseau WiFi / Wait for connection while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); // Connexion WiFi établie / WiFi connexion is OK }

Serial.println("Starting MQTT broker"); //// Start the broker myBroker.init();

myBroker.subscribe("#"); //Subscribe to anything

}

void loop() { Serial.println("loop"); delay ( 3000 ); }


martin-ger commented 4 years ago

Do you use lwip 1.4 or 2.0?

Am 4. Dezember 2019 00:31:35 schrieb philippesikora notifications@github.com:

The program defines a custom broker class with callbacks, I connect from a remote client and publish as below. After 6 iterations, I have the following message "Error: The connection was lost"MQTT client: root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" After 1st iteration, program works well root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" root@MT300N-V2: mosquitto_pub -h 192.168.7.237 -p 1883 -t "MQTT_ref" -m "400" after 6 iteration, MQTT broker is down with the following message Error: The connection was lost.MQTT broker running on ESP8266:

include "uMQTTBroker.h"

include

include int ref_index=0;

char ssid[] = "your SSID"; // your network SSID (name) char password[] = "your password"; // your network passwordclass myMQTTBroker: public uMQTTBroker { public: virtual bool onConnect(IPAddress addr, uint16_t client_count) { Serial.println(addr.toString()+" connected"); return true; }virtual bool onAuth(String username, String password) { Serial.println("Username/Password: "+username+"/"+password); return true; } virtual void onData(String topic, const char *data, uint32_t length) { String temp; char data_str[length+1]; os_memcpy(data_str, data, length); data_str[length] = '\0'; Serial.println("received topic '"+topic+"' with data '"+(String)data_str+"'"); if (topic == "MQTT_ref") { temp = String(data_str); ref_index= temp.toInt(); Serial.println("MQTT OK"); } }

}; myMQTTBroker myBroker; void setup() { Serial.begin(9600); WiFi.begin ( ssid, password ); // Attente de la connexion au réseau WiFi / Wait for connection while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); // Connexion WiFi établie / WiFi connexion is OK }Serial.println("Starting MQTT broker"); //// Start the broker myBroker.init();myBroker.subscribe("#"); //Subscribe to anything } void loop() { Serial.println("loop"); delay ( 3000 ); }— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

philippesikora commented 4 years ago

I did several test with LwIP v2 lower Memory , Higher Memory (no features), ... It does not work with LwIP v2. I compiled with v1.4 Higher Bandwidth and it works !!!! Thank you -:) Can I keep v1.4 version for the project or did you find a workaround with v2 ?

martin-ger commented 4 years ago

Please keep v1.4, I still don't know how to solve the problem with v2.0.

Am 4. Dezember 2019 09:47:22 schrieb philippesikora notifications@github.com:

I did several test with LwIP v2 lower Memory , Higher Memory (no features), ... It does not work with LwIP v2. I compiled with v1.4 Higher Bandwidth and it works !!!! Thank you -:) Can I keep v1.4 version for the project or did you find a workaround with v2 ?— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

philippesikora commented 4 years ago

Thank you for this great support.

fostiras commented 4 years ago

I Had the exact same problem. I use platformio and added "build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH" in platformio.ini to use LwIP1.4 as Martin suggests. This gave me a compile error in espconn.h. Followed this issue https://github.com/esp8266/Arduino/issues/5753 that Martin had opened and added these lines in espconn.h :

ifndef ipv4_addr_t

define ipv4_addr_t ip_addr_t

endif

It compiled fine and the broker now works also fine no matter how many times a client connects/disconnects. Martin thank you so much for this broker and the great support !!!

3gyptian commented 3 years ago

I too have had to add manually add the above three lines into espconn.h (located in ~/.platformio/packages/framework-arduinoespressif8266/tools/sdk/include/) in order to get a working compile out of "build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH" and build with lwip 1.4.

This fixed the issue of the broker coming to a halt after 5 connections but I'm concerned that the next time the framework is upgraded this change will get stomped on.

Can someone suggest an alternative approach?

martin-ger commented 3 years ago

This option exists and works for several years now and assume, it will persist. If not, exchanging the lwip implemantation manually is still an option. I am still not aware what causes the "5 connection" problem.