MQTT client.connect() question

I have long been using the hostname + the last three bytes of the MAC address for the client name in client.connect().
It produces a client name like: "door-7BCF3A", but I still occasionally get an MQTT disconnect.

I used code recommended by @Idahowalker to generate the unique identifier from two bytes of the MAC address, for example "140207", and the disconnect goes away.

Any guesses why?

Is there a length limitation or other restriction to the client name that I am missing?

Thanks

I wonder if the "-" is a issue source.

How long is the array where you’re storing the name ?

24 bytes.

I've been using this method for years and only occasionally getting a dropped connection to the MQTT server. But using @Idahowalker 's method, I haven't seen but one dropout.

Here is the MQTT Broker's function to create a new client.

### mosquitto_new

> libmosq_EXPORT struct mosquitto *mosquitto_new( const char * id,
> bool clean_session,
> void * obj )

Create a new mosquitto client instance.

#### Parameters

|id|String to use as the client id. If NULL, a random client id will be generated. If id is NULL, clean_session must be true.|
| --- | --- |
|clean_session|set to true to instruct the broker to clean all messages and subscriptions on disconnect, false to instruct it to keep them. See the man page mqtt(7) for more details. Note that a client will never discard its own outgoing messages on disconnect. Calling [mosquitto_connect](https://mosquitto.org/api/files/mosquitto-h.html#mosquitto_connect) or [mosquitto_reconnect](https://mosquitto.org/api/files/mosquitto-h.html#mosquitto_reconnect) will cause the messages to be resent. Use [mosquitto_reinitialise](https://mosquitto.org/api/files/mosquitto-h.html#mosquitto_reinitialise) to reset a client to its original state. Must be set to true if the id parameter is NULL.|
|obj|A user pointer that will be passed as an argument to any callbacks that are specified.|

#### Returns

Pointer to a struct mosquitto on success. NULL on failure. Interrogate errno to determine the cause for the failure:

* ENOMEM on out of memory.
* EINVAL on invalid input parameters.

from the MQTT Eclipse documentaton, mosquitto.h

what is return code from CONNACK message ? Or it just timeouts ?
If the library does not tell the reason, I recommend you to use Wireshark to capture MQTT packet, and then analyze it.

-2 : MQTT_CONNECT_FAILED

Possible reasons:

  • Incorrect authentication method or information
  • The code does not contain client.loop() in the loop. This function need to be call to do ping message if idle

Neither. The code with the hostname-MAC for the client identifier works well most of the time, but every few minutes the connection fails, then reconnects immediately. When I generate the unique identifier from two bytes of the MAC address, for example "140207", it rarely disconnects. (My RSSI for the WiFi is about -80, so the signal is pretty weak which would account for the rare dropout).

Either client ID is unique on the LAN, so why would a client ID of "mysensor-7BCF3A" be a problem while a client ID of "140207" is not? What is the max length of the client ID in pubSub.h?

You can go to pubsubclient/src at master · knolleary/pubsubclient · GitHub to find that answer.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.