Browse Source

Merge branch 'martinriedel-development03' into development03

pull/1093/head
lumapu 1 year ago
parent
commit
b586d7e623
  1. 1
      src/config/settings.h
  2. 1
      src/defines.h
  3. 28
      src/publisher/pubMqtt.h
  4. 1
      src/web/RestApi.h
  5. 6
      src/web/html/setup.html
  6. 1
      src/web/web.h

1
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];

1
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

28
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));

1
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("");

6
src/web/html/setup.html

@ -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]];
}

1
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);

Loading…
Cancel
Save