MQTT configure connection parameters not working

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

MQTT configure connection parameters not working

Postby Palonso » Mon Oct 12, 2020 9:41 pm

Hi,

I'm trying to connect to an MQTT server and I found the next problem.

when setting the parameters one by one, like this:

Code: Select all

esp_mqtt_client_config_t mqtt_cfg;
memset(&mqtt_cfg, 0, sizeof(esp_mqtt_client_config_t));
mqtt_cfg.host = (char *)hostname;
mqtt_cfg.port = port;
mqtt_cfg.client_id = (char *)client_id;
mqtt_cfg.username = (char *)username;
mqtt_cfg.password = (char *)password;
mqtt_cfg.cert_pem = (const char *)mqttserver_pub_pem_start;
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
the ESP can't connect to the server and I get the next log output:

Code: Select all

I (6178) esp_netif_handlers: sta ip: 192.168.1.22, mask: 255.255.255.0, gw: 192.168.1.1
I (6178) mqtt: MQTT_EVENT_BEFORE_CONNECT
E (6258) TRANS_TCP: DNS lookup failed err=202 res=0x0
E (6258) MQTT_CLIENT: Error transport connect
I (6258) mqtt: MQTT_EVENT_ERROR
I (6258) mqtt: MQTT_EVENT_DISCONNECTED
BUT when setting the parameters like this:

Code: Select all

esp_mqtt_client_config_t mqtt_cfg = 
{
	.uri = (char *)hostname,
	.cert_pem = (const char *)mqttserver_pub_pem_start,
	.port = port,
	.client_id = (char *)client_id,
	.username = (char *)username,
	.password = (char *)password,
};
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
I'm able to connect to the server and got the next log output:

Code: Select all

I (4159) esp_netif_handlers: sta ip: 192.168.1.22, mask: 255.255.255.0, gw: 192.168.1.1
I (4159) mqtt: MQTT_EVENT_BEFORE_CONNECT
I (6339) mqtt: MQTT_EVENT_CONNECTED
Does anybody knows where is the problem with one way of configuring the MQTT server parameters and the other?

Best regards,
P

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: MQTT configure connection parameters not working

Postby PeterR » Mon Oct 12, 2020 10:12 pm

You need to supply the initialisers. Also would help to know if you are compiling C or C++.
Something in the structure wants to be true or else you get the DNS error.
memset() is always a good idea but in your case you want a member to be true. Won't take you long to examine the structure & trial & error.

May help to note that the initialiser rules changed between C & C++. In your code what you do not set will (typically) be non zero and so true.
Memset the structure then set what you want & sleep at night knowing that all is defined. If not sure what to set then examine the structure and binary chop setting bools till you find the flag.
& I also believe that IDF CAN should be fixed.

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

Re: MQTT configure connection parameters not working

Postby Palonso » Wed Oct 14, 2020 10:29 pm

I found that the difference with one way and the other is the member I was setting.

In one I was setting the host and the other the uri wich may seem to be the same but are really different.

Thanks for the advice.

Who is online

Users browsing this forum: Bing [Bot], MicroController and 263 guests