Merge pull request #202 from timothybrown/mqttauth

MQTT Authentication Support
This commit is contained in:
Aircoookie 2019-08-19 23:20:35 +02:00 committed by GitHub
commit b12b031fdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 203 additions and 160 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_4_2 = espressif8266@1.8.0
arduino_core_2_5_0 = espressif8266@2.0.4 arduino_core_2_5_0 = espressif8266@2.0.4
arduino_core_stage = https://github.com/platformio/platform-espressif8266.git#feature/stage 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 = build_flags =
-D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH -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 -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} ${common.lib_deps_external}
lib_ignore = lib_ignore =
IRremoteESP8266 IRremoteESP8266

Binary file not shown.

View File

@ -252,7 +252,13 @@ For best results, only use one of these services at a time.<br>
Device Auth token: <input name="BK" maxlength="33"><br> 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> <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> <h3>MQTT</h3>
Broker: <input name="MS" maxlength="32"><br> Broker: <input name="MS" maxlength="32">
Port: <input name="MQPORT" type="number" min="1" max="65535" required><br>
<b>The MQTT credentials are sent over an unsecured connection.<br>
Never use the MQTT password for another service!</b><br>
Username: <input name="MQUSER" maxlength="40"><br>
Password: <input type="password" input name="MQPASS" maxlength="40"><br>
Client ID: <input name="MQCID" maxlength="40"><br>
Device Topic: <input name="MD" maxlength="32"><br> Device Topic: <input name="MD" maxlength="32"><br>
Group Topic: <input name="MG" 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> <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 * @title WLED project sketch
* @version 0.8.4 * @version 0.8.5-dev
* @author Christian Schwinne * @author Christian Schwinne
*/ */
@ -98,7 +98,7 @@
//version code in format yymmddb (b = daily build) //version code in format yymmddb (b = daily build)
#define VERSION 1906201 #define VERSION 1908181
char versionString[] = "0.8.5-dev"; char versionString[] = "0.8.5-dev";
@ -204,6 +204,10 @@ bool e131Multicast = false;
char mqttDeviceTopic[33] = ""; //main MQTT topic (individual per device, default is wled/mac) 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 mqttGroupTopic[33] = "wled/all"; //second MQTT topic (for example to group devices)
char mqttServer[33] = ""; //both domains and IPs should work (no SSL) char mqttServer[33] = ""; //both domains and IPs should work (no SSL)
char mqttUser[41] = ""; //optional: username for MQTT auth
char mqttPass[41] = ""; //optional: password for MQTT auth
char mqttClientID[41] = ""; //override the client ID
uint16_t mqttPort = 1883;
bool huePollingEnabled = false; //poll hue bridge for light state bool huePollingEnabled = false; //poll hue bridge for light state
uint16_t huePollIntervalMs = 2500; //low values (< 1sec) may cause lag but offer quicker response uint16_t huePollIntervalMs = 2500; //low values (< 1sec) may cause lag but offer quicker response

View File

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

View File

@ -308,6 +308,15 @@ void getSettingsJS(byte subPage, char* dest)
sappend('c',"SA",notifyAlexa); sappend('c',"SA",notifyAlexa);
sappends('s',"BK",(char*)((blynkEnabled)?"Hidden":"")); sappends('s',"BK",(char*)((blynkEnabled)?"Hidden":""));
sappends('s',"MS",mqttServer); sappends('s',"MS",mqttServer);
sappend('v',"MQPORT",mqttPort);
sappends('s',"MQUSER",mqttUser);
sappends('s',"MQPASS",mqttPass);
byte l = strlen(mqttPass);
char fpass[l+1]; //fill password field with ***
fpass[l] = 0;
memset(fpass,'*',l);
sappends('s',"MQPASS",fpass);
sappends('s',"MQCID",mqttClientID);
sappends('s',"MD",mqttDeviceTopic); sappends('s',"MD",mqttDeviceTopic);
sappends('s',"MG",mqttGroupTopic); sappends('s',"MG",mqttGroupTopic);
sappend('v',"H0",hueIP[0]); sappend('v',"H0",hueIP[0]);

View File

@ -177,6 +177,11 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
} }
strcpy(mqttServer, request->arg("MS").c_str()); strcpy(mqttServer, request->arg("MS").c_str());
t = request->arg("MQPORT").toInt();
if (t > 0) mqttPort = t;
strcpy(mqttUser, request->arg("MQUSER").c_str());
if (request->arg("MQPASS").charAt(0) != '*') strcpy(mqttPass, request->arg("MQPASS").c_str());
strcpy(mqttClientID, request->arg("MQCID").c_str());
strcpy(mqttDeviceTopic, request->arg("MD").c_str()); strcpy(mqttDeviceTopic, request->arg("MD").c_str());
strcpy(mqttGroupTopic, request->arg("MG").c_str()); strcpy(mqttGroupTopic, request->arg("MG").c_str());

View File

@ -69,8 +69,8 @@ void wledInit()
//start captive portal if AP active //start captive portal if AP active
if (onlyAP || strlen(apSSID) > 0) if (onlyAP || strlen(apSSID) > 0)
{ {
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure); dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "wled.me", WiFi.softAPIP()); dnsServer.start(53, "*", WiFi.softAPIP());
dnsActive = true; dnsActive = true;
} }
@ -78,12 +78,17 @@ void wledInit()
if (strcmp(cmDNS,"x") == 0) //fill in unique mdns default if (strcmp(cmDNS,"x") == 0) //fill in unique mdns default
{ {
strcpy(cmDNS, "wled-"); strcpy(cmDNS, "wled-");
strcat(cmDNS, escapedMac.c_str()); sprintf(cmDNS+5, "%*s", 6, escapedMac.c_str()+6);
} }
if (mqttDeviceTopic[0] == 0) if (mqttDeviceTopic[0] == 0)
{ {
strcpy(mqttDeviceTopic, "wled/"); strcpy(mqttDeviceTopic, "wled/");
strcat(mqttDeviceTopic, escapedMac.c_str()); sprintf(mqttDeviceTopic+5, "%*s", 6, escapedMac.c_str()+6);
}
if (mqttClientID[0] == 0)
{
strcpy(mqttClientID, "WLED-");
sprintf(mqttClientID+5, "%*s", 6, escapedMac.c_str()+6);
} }
strip.service(); strip.service();

View File

@ -2,7 +2,7 @@
* MQTT communication protocol for home automation * MQTT communication protocol for home automation
*/ */
#define WLED_MQTT_PORT 1883 //#define WLED_MQTT_PORT 1883
void parseMQTTBriPayload(char* payload) void parseMQTTBriPayload(char* payload)
{ {
@ -47,13 +47,13 @@ void onMqttConnect(bool sessionPresent)
sendHADiscoveryMQTT(); sendHADiscoveryMQTT();
publishMqtt(); publishMqtt();
DEBUG_PRINTLN("MQTT ready"); DEBUG_PRINTLN("MQ ready");
} }
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
DEBUG_PRINT("MQTT callb rec: "); DEBUG_PRINT("MQ callb rec: ");
DEBUG_PRINTLN(topic); DEBUG_PRINTLN(topic);
DEBUG_PRINTLN(payload); DEBUG_PRINTLN(payload);
@ -223,11 +223,12 @@ bool initMqtt()
IPAddress mqttIP; IPAddress mqttIP;
if (mqttIP.fromString(mqttServer)) //see if server is IP or domain if (mqttIP.fromString(mqttServer)) //see if server is IP or domain
{ {
mqtt->setServer(mqttIP, WLED_MQTT_PORT); mqtt->setServer(mqttIP, mqttPort);
} else { } else {
mqtt->setServer(mqttServer, WLED_MQTT_PORT); mqtt->setServer(mqttServer, mqttPort);
} }
mqtt->setClientId(escapedMac.c_str()); mqtt->setClientId(mqttClientID);
if (mqttUser[0] && mqttPass[0] != 0) mqtt->setCredentials(mqttUser, mqttPass);
mqtt->onMessage(onMqttMessage); mqtt->onMessage(onMqttMessage);
mqtt->onConnect(onMqttConnect); mqtt->onConnect(onMqttConnect);
mqtt->connect(); mqtt->connect();