mrdunk / esp8266_mdns

mDNS queries and responses on esp8266
MIT License
77 stars 16 forks source link

After using mDNS to obtain address, after a random time wifi seems to die #7

Closed lorneb closed 7 years ago

lorneb commented 8 years ago

Using the example mdns_test as a base, after I obtain IP address for MQTT service and connect using: PubSubClient @ http://pubsubclient.knolleary.net/

I have verified IP address for server is fine, connection is made, publish is sent, after a what seems a random amount of time mqtt disconnects, and will not reconnect.

same sketch with mDNS query commented out and IP address hard coded runs forever.

void loop()
{
  if ( do_mDNS ){
    //my_mdns.Check();

    hosts[0][HOSTS_ADDRESS] = "192.168.0.240";

    if ( hosts[0][HOSTS_ADDRESS] != "" )
    {
        do_mDNS = false;
        Serial.println("have an address for service:" + String(hosts[0][HOSTS_ADDRESS]) );
        //mqttServer="192.168.0.240";
        mqttServer = String(hosts[0][HOSTS_ADDRESS]) ;
        client.setServer(mqttServer.c_str(), 1883);
        client.setCallback(callback);  

        mqtt_ready = true;
    }

  }

  if ( mqtt_ready ) {
    if (!client.connected()) {
      reconnect();
    }
    client.loop();

    long now = millis();
    if (now - lastMsg > 2500) {
      lastMsg = now;
      ++value;
      snprintf (msg, 75, "hello world #%ld", value);
      Serial.print("Publish message: ");
      Serial.println(msg);
      client.publish("outTopic", msg);
    }  

  } 

}
mrdunk commented 8 years ago

i can't see anything obviously wrong with your code snippet. when you say "random amount of time" are we talking seconds, minutes, hours...? i presume you are still getting your "hello world" message every 2500ms?

have you tried playing with some of the definitions in https://github.com/mrdunk/esp8266_mdns/blob/master/mdns.h ?

in particular, this library carves off a huge chunk of memory in which to store incoming network data. (if i ever work on this code again i'll be reworking this section so the large buffer is not required.) it's possible you are running out of memory on the esp8266? try lowering MAX_PACKET_SIZE in mdns.h . also try enabling debugging in mdns.h .

i've just made a small change to address a potential buffer overflow when reading data from the network: https://github.com/mrdunk/esp8266_mdns/commit/ce84aa49ccacfa475746789a29211cc3a350c5a4#diff-807d3ef722282e3efc185703761877f2 this makes it safe to experiment with lower MAX_PACKET_SIZE values without risking a buffer overflow. test the return value of my_mdns.Check() to make sure data was successfully read from the network.

you can see how i started using this library here: https://github.com/mrdunk/home_automation_2/tree/master/esp8266/TestServer i can't actually remember if that code does anything useful in it's current state... it's been a year since i did anything with it also uses mDNS to connect to MQTT brokers.

SteveQuinn1 commented 8 years ago

No it doesn't do anything useful in it's currnet state, the code won't compile as it stands it's missing the definition for the MQTT type.

TestServerMrDunk:88: error: 'MQTT' does not name a type

void mqtt_callback(const MQTT::Publish& pub) {

                      ^

TestServerMrDunk:88: error: expected unqualified-id before '&' token

void mqtt_callback(const MQTT::Publish& pub) {

                                   ^