EspMQTTClient icon indicating copy to clipboard operation
EspMQTTClient copied to clipboard

How to configure for anonymous broker connection?

Open Jibun-no-Kage opened this issue 2 years ago • 4 comments

How to configure for anonymous broker connection? I set user and password in configuration_t, but getting errors? Per the documentation user and password can be omitted? But still errors.

Jibun-no-Kage avatar Aug 04 '22 04:08 Jibun-no-Kage

providing the error might help finding the cause. omitting user and password is possible for sure. You can also set them to NULL.

This is basically my minimal setup:

EspMQTTClient client(
  "WifiSSID",
  "WifiPassword",
  "hostname",       // MQTT Broker server ip
  "TestClient"      // Client name that uniquely identify your device
);

EdJoPaTo avatar Aug 04 '22 05:08 EdJoPaTo

Odd, that I did should have worked...

EspMQTTClient::EspMQTTClient( const char* wifiSsid, const char* wifiPassword, const char* mqttServerIp, const char* mqttClientName, const short mqttServerPort) : EspMQTTClient(wifiSsid, wifiPassword, mqttServerIp, NULL, NULL, mqttClientName, mqttServerPort)

I qualified ssid, passphrase, broker ip, client id, then port, but it generates and error.

no matching function for call to 'EspMQTTClient::EspMQTTClient(const String&, const String&, const String&, const String&, const int&)'

EspMQTTClient theClient( NETWORK, PASSWORD, BROKER, ID, PORT );

First 4 are string constants, and port is a int constant.

Jibun-no-Kage avatar Aug 05 '22 04:08 Jibun-no-Kage

The method is expecting a const char* and not a String. (Basically C vs C++)

You can convert the String to a c style const char* with c_str() like this: something.c_str().

EdJoPaTo avatar Aug 05 '22 07:08 EdJoPaTo

Personally I use #define BROKER "hostname" instead of String constants. This has the benefit of concating multiple ones at compile time like this:

#define DEVICE_NAME something
#define BASE_TOPIC DEVICE_NAME "/status/"

BASE_TOPIC will be something/status/ and thats done on compile time so no runtime String logic to be done.

for all the rest I prefer const Variables over defines (like the PORT)

EdJoPaTo avatar Aug 05 '22 07:08 EdJoPaTo