Allow anonymous MQTT broker connections
This commit is contained in:
+10
-5
@@ -296,9 +296,9 @@ static void mqttSubscribe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool mqttConnect() {
|
static bool mqttConnect() {
|
||||||
if (!mqttCfg.enabled || strlen(mqttCfg.host) == 0 || strlen(mqttCfg.user) == 0 || strlen(mqttCfg.pass) == 0) {
|
if (!mqttCfg.enabled || strlen(mqttCfg.host) == 0) {
|
||||||
Serial.printf("[MQTT] connect skipped en=%d host=%d user=%d pass=%d\n",
|
Serial.printf("[MQTT] connect skipped en=%d host=%d\n",
|
||||||
mqttCfg.enabled, strlen(mqttCfg.host) > 0, strlen(mqttCfg.user) > 0, strlen(mqttCfg.pass) > 0);
|
mqttCfg.enabled, strlen(mqttCfg.host) > 0);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,8 +316,13 @@ static bool mqttConnect() {
|
|||||||
mqttClient.setCallback(mqttCallback);
|
mqttClient.setCallback(mqttCallback);
|
||||||
mqttClient.setBufferSize(1024);
|
mqttClient.setBufferSize(1024);
|
||||||
|
|
||||||
|
// Pass NULL for empty credentials so the broker treats it as an anonymous
|
||||||
|
// connection rather than an empty-string login (which some brokers reject).
|
||||||
|
const char* user = strlen(mqttCfg.user) > 0 ? mqttCfg.user : nullptr;
|
||||||
|
const char* pass = strlen(mqttCfg.pass) > 0 ? mqttCfg.pass : nullptr;
|
||||||
|
|
||||||
Serial.printf("[MQTT] connecting to %s:%d as %s\n", mqttCfg.host, mqttCfg.port, clientId);
|
Serial.printf("[MQTT] connecting to %s:%d as %s\n", mqttCfg.host, mqttCfg.port, clientId);
|
||||||
bool ok = mqttClient.connect(clientId, mqttCfg.user, mqttCfg.pass,
|
bool ok = mqttClient.connect(clientId, user, pass,
|
||||||
statusTopic, 0, true, "online: false");
|
statusTopic, 0, true, "online: false");
|
||||||
|
|
||||||
if (ok) {
|
if (ok) {
|
||||||
@@ -334,7 +339,7 @@ static bool mqttConnect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void mqttLoop() {
|
static void mqttLoop() {
|
||||||
if (!mqttCfg.enabled || strlen(mqttCfg.host) == 0 || strlen(mqttCfg.user) == 0 || strlen(mqttCfg.pass) == 0) return;
|
if (!mqttCfg.enabled || strlen(mqttCfg.host) == 0) return;
|
||||||
|
|
||||||
if (!mqttClient.connected()) {
|
if (!mqttClient.connected()) {
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
|
|||||||
Reference in New Issue
Block a user