Skip to content

Commit 374d906

Browse files
authored
Merge pull request #4768 from wled/fix-4268
Add mDNS resolution for network bus
2 parents bfe5cd5 + e7157e5 commit 374d906

File tree

7 files changed

+58
-8
lines changed

7 files changed

+58
-8
lines changed

wled00/bus_manager.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <Arduino.h>
66
#include <IPAddress.h>
77
#ifdef ARDUINO_ARCH_ESP32
8+
#include <ESPmDNS.h>
9+
#include "src/dependencies/network/Network.h" // for isConnected() (& WiFi)
810
#include "driver/ledc.h"
911
#include "soc/ledc_struct.h"
1012
#if !(defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3))
@@ -25,6 +27,7 @@
2527
#include "bus_wrapper.h"
2628
#include <bits/unique_ptr.h>
2729

30+
extern char cmDNS[];
2831
extern bool cctICused;
2932
extern bool useParallelI2S;
3033

@@ -102,7 +105,7 @@ void Bus::calculateCCT(uint32_t c, uint8_t &ww, uint8_t &cw) {
102105
} else {
103106
cct = (approximateKelvinFromRGB(c) - 1900) >> 5; // convert K (from RGB value) to relative format
104107
}
105-
108+
106109
//0 - linear (CCT 127 = 50% warm, 50% cold), 127 - additive CCT blending (CCT 127 = 100% warm, 100% cold)
107110
if (cct < _cctBlend) ww = 255;
108111
else ww = ((255-cct) * 255) / (255 - _cctBlend);
@@ -699,6 +702,10 @@ BusNetwork::BusNetwork(const BusConfig &bc)
699702
_hasCCT = false;
700703
_UDPchannels = _hasWhite + 3;
701704
_client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]);
705+
#ifdef ARDUINO_ARCH_ESP32
706+
_hostname = bc.text;
707+
resolveHostname(); // resolve hostname to IP address if needed
708+
#endif
702709
_data = (uint8_t*)d_calloc(_len, _UDPchannels);
703710
_valid = (_data != nullptr);
704711
DEBUGBUS_PRINTF_P(PSTR("%successfully inited virtual strip with type %u and IP %u.%u.%u.%u\n"), _valid?"S":"Uns", bc.type, bc.pins[0], bc.pins[1], bc.pins[2], bc.pins[3]);
@@ -733,6 +740,19 @@ size_t BusNetwork::getPins(uint8_t* pinArray) const {
733740
return 4;
734741
}
735742

743+
#ifdef ARDUINO_ARCH_ESP32
744+
void BusNetwork::resolveHostname() {
745+
static unsigned long nextResolve = 0;
746+
if (Network.isConnected() && millis() > nextResolve && _hostname.length() > 0) {
747+
nextResolve = millis() + 600000; // resolve only every 10 minutes
748+
IPAddress clnt;
749+
if (strlen(cmDNS) > 0) clnt = MDNS.queryHost(_hostname);
750+
else WiFi.hostByName(_hostname.c_str(), clnt);
751+
if (clnt != IPAddress()) _client = clnt;
752+
}
753+
}
754+
#endif
755+
736756
// credit @willmmiles & @netmindz https://github.com/wled/WLED/pull/4056
737757
std::vector<LEDType> BusNetwork::getLEDTypes() {
738758
return {
@@ -918,6 +938,13 @@ void BusManager::on() {
918938
}
919939
}
920940
}
941+
#else
942+
for (auto &bus : busses) if (bus->isVirtual()) {
943+
// virtual/network bus should check for IP change if hostname is specified
944+
// otherwise there are no endpoints to force DNS resolution
945+
BusNetwork &b = static_cast<BusNetwork&>(*bus);
946+
b.resolveHostname();
947+
}
921948
#endif
922949
#ifdef ESP32_DATA_IDLE_HIGH
923950
esp32RMTInvertIdle();

wled00/bus_manager.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class Bus {
133133
virtual uint16_t getUsedCurrent() const { return 0; }
134134
virtual uint16_t getMaxCurrent() const { return 0; }
135135
virtual size_t getBusSize() const { return sizeof(Bus); }
136+
virtual const String getCustomText() const { return String(); }
136137

137138
inline bool hasRGB() const { return _hasRgb; }
138139
inline bool hasWhite() const { return _hasWhite; }
@@ -215,7 +216,7 @@ class Bus {
215216
uint8_t _autoWhiteMode;
216217
// global Auto White Calculation override
217218
static uint8_t _gAWM;
218-
// _cct has the following menaings (see calculateCCT() & BusManager::setSegmentCCT()):
219+
// _cct has the following meanings (see calculateCCT() & BusManager::setSegmentCCT()):
219220
// -1 means to extract approximate CCT value in K from RGB (in calcualteCCT())
220221
// [0,255] is the exact CCT value where 0 means warm and 255 cold
221222
// [1900,10060] only for color correction expressed in K (colorBalanceFromKelvin())
@@ -342,6 +343,10 @@ class BusNetwork : public Bus {
342343
size_t getBusSize() const override { return sizeof(BusNetwork) + (isOk() ? _len * _UDPchannels : 0); }
343344
void show() override;
344345
void cleanup();
346+
#ifdef ARDUINO_ARCH_ESP32
347+
void resolveHostname();
348+
const String getCustomText() const override { return _hostname; }
349+
#endif
345350

346351
static std::vector<LEDType> getLEDTypes();
347352

@@ -351,6 +356,9 @@ class BusNetwork : public Bus {
351356
uint8_t _UDPchannels;
352357
bool _broadcastLock;
353358
uint8_t *_data;
359+
#ifdef ARDUINO_ARCH_ESP32
360+
String _hostname;
361+
#endif
354362
};
355363

356364

@@ -368,8 +376,9 @@ struct BusConfig {
368376
uint16_t frequency;
369377
uint8_t milliAmpsPerLed;
370378
uint16_t milliAmpsMax;
379+
String text;
371380

372-
BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, uint8_t maPerLed=LED_MILLIAMPS_DEFAULT, uint16_t maMax=ABL_MILLIAMPS_DEFAULT)
381+
BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, uint8_t maPerLed=LED_MILLIAMPS_DEFAULT, uint16_t maMax=ABL_MILLIAMPS_DEFAULT, String sometext = "")
373382
: count(std::max(len,(uint16_t)1))
374383
, start(pstart)
375384
, colorOrder(pcolorOrder)
@@ -379,6 +388,7 @@ struct BusConfig {
379388
, frequency(clock_kHz)
380389
, milliAmpsPerLed(maPerLed)
381390
, milliAmpsMax(maMax)
391+
, text(sometext)
382392
{
383393
refreshReq = (bool) GET_BIT(busType,7);
384394
type = busType & 0x7F; // bit 7 may be/is hacked to include refresh info (1=refresh in off state, 0=no refresh)

wled00/cfg.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
235235
}
236236
ledType |= refresh << 7; // hack bit 7 to indicate strip requires off refresh
237237

238-
busConfigs.emplace_back(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz, maPerLed, maMax);
238+
String host = elm[F("text")] | String();
239+
busConfigs.emplace_back(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz, maPerLed, maMax, host);
239240
doInitBusses = true; // finalization done in beginStrip()
240241
if (!Bus::isVirtual(ledType)) s++; // have as many virtual buses as you want
241242
}
@@ -379,7 +380,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
379380
DEBUG_PRINTF_P(PSTR("PIN ALLOC error: GPIO%d for touch button #%d is not a touch pin!\n"), btnPin[s], s);
380381
btnPin[s] = -1;
381382
PinManager::deallocatePin(pin,PinOwner::Button);
382-
}
383+
}
383384
//if touch pin, enable the touch interrupt on ESP32 S2 & S3
384385
#ifdef SOC_TOUCH_VERSION_2 // ESP32 S2 and S3 have a function to check touch state but need to attach an interrupt to do so
385386
else
@@ -976,6 +977,7 @@ void serializeConfig(JsonObject root) {
976977
ins[F("freq")] = bus->getFrequency();
977978
ins[F("maxpwr")] = bus->getMaxCurrent();
978979
ins[F("ledma")] = bus->getLEDCurrent();
980+
ins[F("text")] = bus->getCustomText();
979981
}
980982

981983
JsonArray hw_com = hw.createNestedArray(F("com"));
@@ -1340,4 +1342,4 @@ void serializeConfigSec() {
13401342
if (f) serializeJson(root, f);
13411343
f.close();
13421344
releaseJSONBufferLock();
1343-
}
1345+
}

wled00/data/settings_leds.htm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@
284284
gId("dig"+n+"l").style.display = (isD2P(t) || isPWM(t)) ? "inline":"none"; // bus clock speed / PWM speed (relative) (not On/Off)
285285
gId("rev"+n).innerHTML = isAna(t) ? "Inverted output":"Reversed"; // change reverse text for analog else (rotated 180°)
286286
//gId("psd"+n).innerHTML = isAna(t) ? "Index:":"Start:"; // change analog start description
287+
gId("net"+n+"h").style.display = isNet(t) && !is8266() ? "block" : "none"; // show host field for network types except on ESP8266
288+
if (!isNet(t) || is8266()) d.Sf["HS"+n].value = ""; // cleart host field if not network type or ESP8266
287289
});
288290
// display global white channel overrides
289291
gId("wc").style.display = (gRGBW) ? 'inline':'none';
@@ -466,6 +468,7 @@
466468
<span id="p2d${s}"></span><input type="number" name="L2${s}" class="s" onchange="UI();pinUpd(this);"/>
467469
<span id="p3d${s}"></span><input type="number" name="L3${s}" class="s" onchange="UI();pinUpd(this);"/>
468470
<span id="p4d${s}"></span><input type="number" name="L4${s}" class="s" onchange="UI();pinUpd(this);"/>
471+
<div id="net${s}h" class="hide">Host: <input type="text" name="HS${s}" maxlength="32" pattern="[a-zA-Z0-9_\\-]*" onchange="UI()"/>.local</div>
469472
<div id="dig${s}r" style="display:inline"><br><span id="rev${s}">Reversed</span>: <input type="checkbox" name="CV${s}"></div>
470473
<div id="dig${s}s" style="display:inline"><br>Skip first LEDs: <input type="number" name="SL${s}" min="0" max="255" value="0" oninput="UI()"></div>
471474
<div id="dig${s}f" style="display:inline"><br><span id="off${s}">Off Refresh</span>: <input id="rf${s}" type="checkbox" name="RF${s}"></div>

wled00/data/style.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ input {
7979
input:disabled {
8080
color: #888;
8181
}
82+
input:invalid {
83+
color: #f00;
84+
}
8285
input[type="text"],
8386
input[type="number"],
8487
input[type="password"],
@@ -202,4 +205,4 @@ td {
202205
#btns select {
203206
width: 144px;
204207
}
205-
}
208+
}

wled00/set.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
141141
unsigned colorOrder, type, skip, awmode, channelSwap, maPerLed;
142142
unsigned length, start, maMax;
143143
uint8_t pins[5] = {255, 255, 255, 255, 255};
144+
String text;
144145

145146
// this will set global ABL max current used when per-port ABL is not used
146147
unsigned ablMilliampsMax = request->arg(F("MA")).toInt();
@@ -174,6 +175,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
174175
char sp[4] = "SP"; sp[2] = offset+s; sp[3] = 0; //bus clock speed (DotStar & PWM)
175176
char la[4] = "LA"; la[2] = offset+s; la[3] = 0; //LED mA
176177
char ma[4] = "MA"; ma[2] = offset+s; ma[3] = 0; //max mA
178+
char hs[4] = "HS"; hs[2] = offset+s; hs[3] = 0; //hostname (for network types, custom text for others)
177179
if (!request->hasArg(lp)) {
178180
DEBUG_PRINTF_P(PSTR("# of buses: %d\n"), s+1);
179181
break;
@@ -224,9 +226,10 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
224226
maMax = request->arg(ma).toInt() * request->hasArg(F("PPL")); // if PP-ABL is disabled maMax (per bus) must be 0
225227
}
226228
type |= request->hasArg(rf) << 7; // off refresh override
229+
text = request->arg(hs).substring(0,31);
227230
// actual finalization is done in WLED::loop() (removing old busses and adding new)
228231
// this may happen even before this loop is finished so we do "doInitBusses" after the loop
229-
busConfigs.emplace_back(type, pins, start, length, colorOrder | (channelSwap<<4), request->hasArg(cv), skip, awmode, freq, maPerLed, maMax);
232+
busConfigs.emplace_back(type, pins, start, length, colorOrder | (channelSwap<<4), request->hasArg(cv), skip, awmode, freq, maPerLed, maMax, text);
230233
busesChanged = true;
231234
}
232235
//doInitBusses = busesChanged; // we will do that below to ensure all input data is processed

wled00/xml.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ void getSettingsJS(byte subPage, Print& settingsScript)
311311
char sp[4] = "SP"; sp[2] = offset+s; sp[3] = 0; //bus clock speed
312312
char la[4] = "LA"; la[2] = offset+s; la[3] = 0; //LED current
313313
char ma[4] = "MA"; ma[2] = offset+s; ma[3] = 0; //max per-port PSU current
314+
char hs[4] = "HS"; hs[2] = offset+s; hs[3] = 0; //hostname (for network types, custom text for others)
314315
settingsScript.print(F("addLEDs(1);"));
315316
uint8_t pins[5];
316317
int nPins = bus->getPins(pins);
@@ -350,6 +351,7 @@ void getSettingsJS(byte subPage, Print& settingsScript)
350351
printSetFormValue(settingsScript,sp,speed);
351352
printSetFormValue(settingsScript,la,bus->getLEDCurrent());
352353
printSetFormValue(settingsScript,ma,bus->getMaxCurrent());
354+
printSetFormValue(settingsScript,hs,bus->getCustomText().c_str());
353355
sumMa += bus->getMaxCurrent();
354356
}
355357
printSetFormValue(settingsScript,PSTR("MA"),BusManager::ablMilliampsMax() ? BusManager::ablMilliampsMax() : sumMa);

0 commit comments

Comments
 (0)