EspMQTTClient
EspMQTTClient copied to clipboard
How to configure for anonymous broker connection?
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.
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
);
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.
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()
.
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
)