esp8266_mdns
esp8266_mdns copied to clipboard
Including MDNS library causing MQTT connection to drop
Hi,
I am using your fantastic library to discover the MQTT server on my network. All seems to work fine for about 5mins, but eventually the MQTT connection will drop and sit in the reconnect loop in the ESP. It only seems to be a problem when I include the esp8266_mdns library in the project, even if I'm not using it in my application.
Have been trying to figure out the cause for the past 24hrs, so if you have any pointers on where I can start looking to debug the issue, it will be much appreciated (I've got a feeling it's a memory leak).
Hi apatel, My first reaction is that maybe you are running out of memory. Not necessarily because of a leak but just plain allocating too many things. The memory management on the esp8266 is very poor. There is no reliable way to know how much you have left. I've had lots of strange problems as memory usage increases. The sort of errors you see are varied and tend to change between runs.
My esp8266_mdns library allocates it's self a huge static chunk of memory at startup to use as cache for the incoming packet. (1024bytes by default) You could try decreasing the size of the buffer you pass to the Mdns constructor. This will increase the chances that some Mdns packet will be larger than fits in the buffer; only the first part of the packet will be processed. But it will improve stability if you are indeed running out of memory.
I have used this library reliably with the Aduino PubSubClient library. You can see a project i started here: https://github.com/mrdunk/esp8266_mqtt_kitchensink That project got far too complicated so i never did finish it but the MQTT connection was stable.
Good luck! dunk.
Thanks for this, my project is pretty simple in that it used PubSubClient and Wifi manager, so could be any combination of issues here.
Try a smaller buffer size. eg:
#define MAX_MDNS_PACKET_SIZE 512
byte buffer[MAX_MDNS_PACKET_SIZE];
mdns::MDns my_mdns(NULL, NULL, answerCallback, buffer, MAX_MDNS_PACKET_SIZE);
This example demonstrates this: https://github.com/mrdunk/esp8266_mdns/blob/master/examples/mdns_test/mdns_test.ino
Have been using 512 already. Went down to 256, but that caused other issues as seems too low.
Yea, the buffer size is a balance between using enough of the mDNS packet and not using all the memory. 512 works well enough on my home network.
Well i'm out of ideas. If i were you i'd probably try creating a minimal program with just the esp8266_mdns and PubSubClient together to confirm they work together as expected.
good luck!