diff --git a/src/config/settings.h b/src/config/settings.h index 25891813..d262ea33 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -123,6 +123,7 @@ typedef struct { typedef struct { char broker[MQTT_ADDR_LEN]; uint16_t port; + char clientId[MQTT_CLIENTID_LEN]; char user[MQTT_USER_LEN]; char pwd[MQTT_PWD_LEN]; char topic[MQTT_TOPIC_LEN]; diff --git a/src/defines.h b/src/defines.h index d060515b..c97d069d 100644 --- a/src/defines.h +++ b/src/defines.h @@ -85,6 +85,7 @@ enum {MQTT_STATUS_OFFLINE = 0, MQTT_STATUS_PARTIAL, MQTT_STATUS_ONLINE}; #define NTP_ADDR_LEN 32 // DNS Name #define MQTT_ADDR_LEN 64 // DNS Name +#define MQTT_CLIENTID_LEN 65 #define MQTT_USER_LEN 65 // there is another byte necessary for \0 #define MQTT_PWD_LEN 65 #define MQTT_TOPIC_LEN 65 diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 0e160f73..65dfef28 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -76,17 +76,23 @@ class PubMqtt { if((strlen(mCfgMqtt->user) > 0) && (strlen(mCfgMqtt->pwd) > 0)) mClient.setCredentials(mCfgMqtt->user, mCfgMqtt->pwd); - snprintf(mClientId, 24, "%s-", mDevName); - uint8_t pos = strlen(mClientId); - mClientId[pos++] = WiFi.macAddress().substring( 9, 10).c_str()[0]; - mClientId[pos++] = WiFi.macAddress().substring(10, 11).c_str()[0]; - mClientId[pos++] = WiFi.macAddress().substring(12, 13).c_str()[0]; - mClientId[pos++] = WiFi.macAddress().substring(13, 14).c_str()[0]; - mClientId[pos++] = WiFi.macAddress().substring(15, 16).c_str()[0]; - mClientId[pos++] = WiFi.macAddress().substring(16, 17).c_str()[0]; - mClientId[pos++] = '\0'; - - mClient.setClientId(mClientId); + if(strlen(mCfgMqtt->clientId) > 0) + { + snprintf(mClientId, 24, "%s-", mCfgMqtt->clientId); + mClient.setClientId(mCfgMqtt->clientId); + }else{ + snprintf(mClientId, 24, "%s-", mDevName); + uint8_t pos = strlen(mClientId); + mClientId[pos++] = WiFi.macAddress().substring( 9, 10).c_str()[0]; + mClientId[pos++] = WiFi.macAddress().substring(10, 11).c_str()[0]; + mClientId[pos++] = WiFi.macAddress().substring(12, 13).c_str()[0]; + mClientId[pos++] = WiFi.macAddress().substring(13, 14).c_str()[0]; + mClientId[pos++] = WiFi.macAddress().substring(15, 16).c_str()[0]; + mClientId[pos++] = WiFi.macAddress().substring(16, 17).c_str()[0]; + mClientId[pos++] = '\0'; + + mClient.setClientId(mClientId); + } mClient.setServer(mCfgMqtt->broker, mCfgMqtt->port); mClient.setWill(mLwtTopic, QOS_0, true, mqttStr[MQTT_STR_LWT_NOT_CONN]); mClient.onConnect(std::bind(&PubMqtt::onConnect, this, std::placeholders::_1)); diff --git a/src/web/RestApi.h b/src/web/RestApi.h index fba6486e..74d9934b 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -385,6 +385,7 @@ class RestApi { void getMqtt(JsonObject obj) { obj[F("broker")] = String(mConfig->mqtt.broker); + obj[F("clientId")] = String(mConfig->mqtt.clientId); obj[F("port")] = String(mConfig->mqtt.port); obj[F("user")] = String(mConfig->mqtt.user); obj[F("pwd")] = (strlen(mConfig->mqtt.pwd) > 0) ? F("{PWD}") : String(""); diff --git a/src/web/html/setup.html b/src/web/html/setup.html index 52c4634d..c2bc863d 100644 --- a/src/web/html/setup.html +++ b/src/web/html/setup.html @@ -249,6 +249,10 @@
Port
+
+
Client Id (optional)
+
+
Username (optional)
@@ -696,7 +700,7 @@ } function parseMqtt(obj) { - for(var i of [["Addr", "broker"], ["Port", "port"], ["User", "user"], ["Pwd", "pwd"], ["Topic", "topic"], ["Interval", "interval"]]) + for(var i of [["Addr", "broker"], ["Port", "port"], ["ClientId", "clientId"], ["User", "user"], ["Pwd", "pwd"], ["Topic", "topic"], ["Interval", "interval"]]) document.getElementsByName("mqtt"+i[0])[0].value = obj[i[1]]; } diff --git a/src/web/web.h b/src/web/web.h index a289f230..7ff90324 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -582,6 +582,7 @@ class Web { addr.toCharArray(mConfig->mqtt.broker, MQTT_ADDR_LEN); } else mConfig->mqtt.broker[0] = '\0'; + request->arg("mqttClientId").toCharArray(mConfig->mqtt.clientId, MQTT_CLIENTID_LEN); request->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN); if (request->arg("mqttPwd") != "{PWD}") request->arg("mqttPwd").toCharArray(mConfig->mqtt.pwd, MQTT_PWD_LEN);