DNS lookup failed after multiple succesful mqtt messages

stefbryssinckx
Posts: 2
Joined: Thu Mar 18, 2021 4:00 pm

DNS lookup failed after multiple succesful mqtt messages

Postby stefbryssinckx » Mon Aug 28, 2023 1:08 pm

Hi everyone,


I'm developing an IoT device that publishes mqtt messages every once in a while. I'm currently testing this by sending the messages to test.mosquitto.org. This works, but 10-50 hours after startup the messages no longer reach the broker and the esp32 outputs the following errors every time it attempts to publish a message:
TRANS_TCP: DNS lookup failed err=202 res=0x0
MQTT_CLIENT: Error transport connect
I'm using this library to handle the WiFi connection: https://github.com/tzapu/WiFiManager

Here are some relevant pieces of code. Any help on where to look would be appreciated!

Setup:

Code: Select all

#include <sstream>

#include <WiFiManager.h>
WiFiManager wm;

#include "mqtt.h"
Mqtt mqtt;

void setup() {
    Serial.begin(115200);
    while(!Serial) { ; } // wait for serial
	
    WiFi.onEvent( WiFiStationConnected, SYSTEM_EVENT_AP_START );
    WiFi.onEvent( WiFiConnected, SYSTEM_EVENT_STA_GOT_IP );
    
    mqtt.setup();
    char* broker = (char*)mqtt.broker.c_str();
    WiFiManagerParameter custom_mqtt_broker("broker", "MQTT broker (starts with mqtt://)", broker, 40);
    wm.addParameter(&custom_mqtt_broker);
    char* topic = (char*)mqtt.topic.c_str();
    WiFiManagerParameter custom_mqtt_topic("topic", "MQTT topic", topic, 100);
    wm.addParameter(&custom_mqtt_topic);
    
    bool res;
    res = wm.autoConnect("Sensor");
    if(!res) {
        Serial.println("Failed to connect");
    } else {
        //if you get here you have connected to the WiFi  
        mqtt.updateSettings( custom_mqtt_broker.getValue(), custom_mqtt_topic.getValue() );  
        Serial.println("Connected to WiFi");
    }
}
This line publishes the message, where ss is a stringstream object:

Code: Select all

mqtt.publish( ss.str().c_str() );
These are the contents of mqtt.h:

Code: Select all

#include <Preferences.h>
#include <mqtt_client.h>

class Mqtt {
public:  
  esp_mqtt_client_handle_t mqtt_client;
  
  String broker;
  String topic;
  Preferences preferences;

  void setup();
  void updateSettings( String broker, String topic );

  void reconnect();
  void publish( String message );
};
And these are the contents of mqtt.cpp:

Code: Select all

#include "mqtt.h"
#include "Preferences.h"

void
Mqtt::setup() {
  // Initialize NVS storage to store thresholds
  preferences.begin( "mqtt" );
  // Read values from NVS
  broker = preferences.getString( "broker", "" );
  Serial.print( "MQTT broker initialized as '" );
  Serial.print( broker );
  Serial.println( "'" );
  topic = preferences.getString( "topic", "" );
  Serial.print( "MQTT topic initialized as '" );
  Serial.print( topic );
  Serial.println( "'" );

}

void
Mqtt::updateSettings( String broker, String topic ) {
  this->broker = broker;
  this->topic = topic;
  preferences.putString( "broker", broker );
  preferences.putString( "topic", topic );
  reconnect();
}

void 
Mqtt::reconnect() {
  Serial.print( "Connecting to mqtt broker " );
  Serial.println( broker );
  esp_mqtt_client_config_t mqtt_cfg = { 0 };
  mqtt_cfg.uri = const_cast<char*>( broker.c_str() );
  mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
  esp_mqtt_client_start(mqtt_client);
  Serial.println( "Connected to mqtt broker" );
}

void
Mqtt::publish( String message ) {
  if (mqtt_client == NULL)
    reconnect();
  if( mqtt_client != NULL )
    esp_mqtt_client_publish(mqtt_client, (char*)topic.c_str(), (char*)message.c_str(), message.length(), 0, 0);
}

Who is online

Users browsing this forum: No registered users and 79 guests