mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-25 14:56:11 +02:00
Add client id for mqtt
This commit is contained in:
parent
3a944d182b
commit
5f9015f7ad
6 changed files with 25 additions and 11 deletions
|
@ -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];
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -76,6 +76,11 @@ class PubMqtt {
|
|||
|
||||
if((strlen(mCfgMqtt->user) > 0) && (strlen(mCfgMqtt->pwd) > 0))
|
||||
mClient.setCredentials(mCfgMqtt->user, mCfgMqtt->pwd);
|
||||
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];
|
||||
|
@ -87,6 +92,7 @@ class PubMqtt {
|
|||
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));
|
||||
|
|
|
@ -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("");
|
||||
|
|
|
@ -249,6 +249,10 @@
|
|||
<div class="col-12 col-sm-3 my-2">Port</div>
|
||||
<div class="col-12 col-sm-9"><input type="number" name="mqttPort"/></div>
|
||||
</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-12 col-sm-3 my-2">Client Id (optional)</div>
|
||||
<div class="col-12 col-sm-9"><input type="text" name="clientId"/></div>
|
||||
</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-12 col-sm-3 my-2">Username (optional)</div>
|
||||
<div class="col-12 col-sm-9"><input type="text" name="mqttUser"/></div>
|
||||
|
@ -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]];
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue