Added MQTT authentication support

This commit is contained in:
Timothy Brown 2019-08-17 06:27:06 -04:00
parent f0f02c4ea6
commit c6d8b63e54
9 changed files with 178 additions and 149 deletions

View File

@ -57,7 +57,7 @@ arduino_core_2_4_1 = espressif8266@1.7.3
arduino_core_2_4_2 = espressif8266@1.8.0
arduino_core_2_5_0 = espressif8266@2.0.4
arduino_core_stage = https://github.com/platformio/platform-espressif8266.git#feature/stage
platform = ${common:esp8266.arduino_core_2_4_2}
platform = ${common:esp8266.arduino_core_2_5_0}
build_flags =
-D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
-Wl,-Teagle.flash.4m1m.ld ;;;; Required for core > v2.5.0 or staging version 4MB Flash 3MB SPIFFs
@ -160,4 +160,3 @@ lib_deps =
${common.lib_deps_external}
lib_ignore =
IRremoteESP8266

Binary file not shown.

View File

@ -253,6 +253,9 @@ Device Auth token: <input name="BK" maxlength="33"><br>
<i>Clear the token field to disable. </i><a href="https://github.com/Aircoookie/WLED/wiki/Blynk" target="_blank">Setup info</a>
<h3>MQTT</h3>
Broker: <input name="MS" maxlength="32"><br>
Username: <input name="MQTTUSER" maxlength="32"><br>
Password: <input type="password" input name="MQTTPASS" maxlength="32"><br>
Client ID: <input name="MQTTCID" maxlength="32"><br>
Device Topic: <input name="MD" maxlength="32"><br>
Group Topic: <input name="MG" maxlength="32"><br>
<i>Reboot required to apply changes. </i><a href="https://github.com/Aircoookie/WLED/wiki/MQTT" target="_blank">MQTT info</a>

View File

@ -3,7 +3,7 @@
*/
/*
* @title WLED project sketch
* @version 0.8.4
* @version 0.8.5-dev #mqttauth @TimothyBrown
* @author Christian Schwinne
*/
@ -204,6 +204,9 @@ bool e131Multicast = false;
char mqttDeviceTopic[33] = ""; //main MQTT topic (individual per device, default is wled/mac)
char mqttGroupTopic[33] = "wled/all"; //second MQTT topic (for example to group devices)
char mqttServer[33] = ""; //both domains and IPs should work (no SSL)
char mqttUser[33] = ""; //optional: username for MQTT auth
char mqttPass[33] = ""; //optional: password for MQTT auth
char mqttClientID[33] = ""; //override the client ID
bool huePollingEnabled = false; //poll hue bridge for light state
uint16_t huePollIntervalMs = 2500; //low values (< 1sec) may cause lag but offer quicker response

View File

@ -6,7 +6,7 @@
#define EEPSIZE 2560
//eeprom Version code, enables default settings instead of 0 init on update
#define EEPVER 10
#define EEPVER 11
//0 -> old version, default
//1 -> 0.4p 1711272 and up
//2 -> 0.4p 1711302 and up
@ -18,6 +18,7 @@
//8 -> 0.8.0-a and up
//9 -> 0.8.0
//10-> 0.8.2
//11-> 0.8.5-dev #mqttauth @TimothyBrown
/*
@ -256,6 +257,9 @@ void saveSettingsToEEPROM()
writeStringToEEPROM(2300, mqttServer, 32);
writeStringToEEPROM(2333, mqttDeviceTopic, 32);
writeStringToEEPROM(2366, mqttGroupTopic, 32);
writeStringToEEPROM(2399, mqttUser, 32);
writeStringToEEPROM(2432, mqttPass, 32);
writeStringToEEPROM(2465, mqttClientID, 32);
EEPROM.commit();
}
@ -471,6 +475,13 @@ void loadSettingsFromEEPROM(bool first)
strip.ablMilliampsMax = ABL_MILLIAMPS_DEFAULT;
}
if (lastEEPROMversion > 10)
{
readStringFromEEPROM(2399, mqttUser, 32);
readStringFromEEPROM(2432, mqttPass, 32);
readStringFromEEPROM(2465, mqttClientID, 32);
}
receiveDirect = !EEPROM.read(2200);
notifyMacro = EEPROM.read(2201);
uiConfiguration = EEPROM.read(2202);

View File

@ -308,6 +308,9 @@ void getSettingsJS(byte subPage, char* dest)
sappend('c',"SA",notifyAlexa);
sappends('s',"BK",(char*)((blynkEnabled)?"Hidden":""));
sappends('s',"MS",mqttServer);
sappends('s',"MQTTUSER",mqttUser);
sappends('s',"MQTTPASS",mqttPass);
sappends('s',"MQTTCID",mqttClientID);
sappends('s',"MD",mqttDeviceTopic);
sappends('s',"MG",mqttGroupTopic);
sappend('v',"H0",hueIP[0]);

View File

@ -177,6 +177,9 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}
strcpy(mqttServer, request->arg("MS").c_str());
strcpy(mqttUser, request->arg("MQTTUSER").c_str());
strcpy(mqttPass, request->arg("MQTTPASS").c_str());
strcpy(mqttClientID, request->arg("MQTTCID").c_str());
strcpy(mqttDeviceTopic, request->arg("MD").c_str());
strcpy(mqttGroupTopic, request->arg("MG").c_str());

View File

@ -85,6 +85,11 @@ void wledInit()
strcpy(mqttDeviceTopic, "wled/");
strcat(mqttDeviceTopic, escapedMac.c_str());
}
if (mqttClientID[0] == 0)
{
strcpy(mqttClientID, "WLED-");
sprintf(mqttClientID+5, "%*s", 6, escapedMac.c_str()+6);
}
strip.service();

View File

@ -227,7 +227,9 @@ bool initMqtt()
} else {
mqtt->setServer(mqttServer, WLED_MQTT_PORT);
}
mqtt->setClientId(escapedMac.c_str());
//mqtt->setClientId(escapedMac.c_str());
mqtt->setClientId(mqttClientID);
if (mqttUser[0] && mqttPass[0] != 0) mqtt->setCredentials(mqttUser, mqttPass);
mqtt->onMessage(onMqttMessage);
mqtt->onConnect(onMqttConnect);
mqtt->connect();