@@ -43,6 +43,68 @@ bool oappend(const char* txt)
4343 return true ;
4444}
4545
46+ void prepareHostname (char * hostname)
47+ {
48+ const char *pC = serverDescription;
49+ uint8_t pos = 5 ;
50+
51+ while (*pC && pos < 24 ) { // while !null and not over length
52+ if (isalnum (*pC)) { // if the current char is alpha-numeric append it to the hostname
53+ hostname[pos] = *pC;
54+ pos++;
55+ } else if (*pC == ' ' || *pC == ' _' || *pC == ' -' || *pC == ' +' || *pC == ' !' || *pC == ' ?' || *pC == ' *' ) {
56+ hostname[pos] = ' -' ;
57+ pos++;
58+ }
59+ // else do nothing - no leading hyphens and do not include hyphens for all other characters.
60+ pC++;
61+ }
62+ // if the hostname is left blank, use the mac address/default mdns name
63+ if (pos < 6 ) {
64+ sprintf (hostname + 5 , " %*s" , 6 , escapedMac.c_str () + 6 );
65+ } else { // last character must not be hyphen
66+ while (pos > 0 && hostname[pos -1 ] == ' -' ) {
67+ hostname[pos -1 ] = 0 ;
68+ pos--;
69+ }
70+ }
71+ }
72+
73+ // handle Ethernet connection event
74+ void WiFiEvent (WiFiEvent_t event)
75+ {
76+ char hostname[25 ] = " wled-" ;
77+
78+ switch (event) {
79+ #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET)
80+ case SYSTEM_EVENT_ETH_START:
81+ DEBUG_PRINT (" ETH Started" );
82+ break ;
83+ case SYSTEM_EVENT_ETH_CONNECTED:
84+ DEBUG_PRINT (" ETH Connected" );
85+ if (!apActive) {
86+ WiFi.disconnect (true );
87+ }
88+ if (staticIP != (uint32_t )0x00000000 && staticGateway != (uint32_t )0x00000000 ) {
89+ ETH.config (staticIP, staticGateway, staticSubnet, IPAddress (8 , 8 , 8 , 8 ));
90+ } else {
91+ ETH.config (INADDR_NONE, INADDR_NONE, INADDR_NONE);
92+ }
93+ // convert the "serverDescription" into a valid DNS hostname (alphanumeric)
94+ prepareHostname (hostname);
95+ ETH.setHostname (hostname);
96+ showWelcomePage = false ;
97+ break ;
98+ case SYSTEM_EVENT_ETH_DISCONNECTED:
99+ DEBUG_PRINT (" ETH Disconnected" );
100+ forceReconnect = true ;
101+ break ;
102+ #endif
103+ default :
104+ break ;
105+ }
106+ }
107+
46108void WLED::loop ()
47109{
48110 handleIR (); // 2nd call to function needed for ESP32 to return valid results -- should be good for ESP8266, too
@@ -120,7 +182,7 @@ void WLED::loop()
120182 lastWifiState = WiFi.status ();
121183 DEBUG_PRINT (" State time: " ); DEBUG_PRINTLN (wifiStateChangedTime);
122184 DEBUG_PRINT (" NTP last sync: " ); DEBUG_PRINTLN (ntpLastSyncTime);
123- DEBUG_PRINT (" Client IP: " ); DEBUG_PRINTLN (WiFi .localIP ());
185+ DEBUG_PRINT (" Client IP: " ); DEBUG_PRINTLN (Network .localIP ());
124186 DEBUG_PRINT (" Loops/sec: " ); DEBUG_PRINTLN (loops / 10 );
125187 loops = 0 ;
126188 debugTime = millis ();
@@ -187,6 +249,7 @@ void WLED::setup()
187249 if (strcmp (clientSSID, DEFAULT_CLIENT_SSID) == 0 )
188250 showWelcomePage = true ;
189251 WiFi.persistent (false );
252+ WiFi.onEvent (WiFiEvent);
190253
191254 if (macroBoot > 0 )
192255 applyMacro (macroBoot);
@@ -303,6 +366,10 @@ void WLED::initConnection()
303366 ws.onEvent (wsEvent);
304367 #endif
305368
369+ #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET)
370+ ETH.begin ();
371+ #endif
372+
306373 WiFi.disconnect (true ); // close old connections
307374#ifdef ESP8266
308375 WiFi.setPhyMode (WIFI_PHY_MODE_11N);
@@ -338,29 +405,7 @@ void WLED::initConnection()
338405
339406 // convert the "serverDescription" into a valid DNS hostname (alphanumeric)
340407 char hostname[25 ] = " wled-" ;
341- const char *pC = serverDescription;
342- uint8_t pos = 5 ;
343-
344- while (*pC && pos < 24 ) { // while !null and not over length
345- if (isalnum (*pC)) { // if the current char is alpha-numeric append it to the hostname
346- hostname[pos] = *pC;
347- pos++;
348- } else if (*pC == ' ' || *pC == ' _' || *pC == ' -' || *pC == ' +' || *pC == ' !' || *pC == ' ?' || *pC == ' *' ) {
349- hostname[pos] = ' -' ;
350- pos++;
351- }
352- // else do nothing - no leading hyphens and do not include hyphens for all other characters.
353- pC++;
354- }
355- // if the hostname is left blank, use the mac address/default mdns name
356- if (pos < 6 ) {
357- sprintf (hostname + 5 , " %*s" , 6 , escapedMac.c_str () + 6 );
358- } else { // last character must not be hyphen
359- while (pos > 0 && hostname[pos -1 ] == ' -' ) {
360- hostname[pos -1 ] = 0 ;
361- pos--;
362- }
363- }
408+ prepareHostname (hostname);
364409
365410#ifdef ESP8266
366411 WiFi.hostname (hostname);
@@ -381,9 +426,9 @@ void WLED::initInterfaces()
381426 DEBUG_PRINTLN (F (" Init STA interfaces" ));
382427
383428 if (hueIP[0 ] == 0 ) {
384- hueIP[0 ] = WiFi .localIP ()[0 ];
385- hueIP[1 ] = WiFi .localIP ()[1 ];
386- hueIP[2 ] = WiFi .localIP ()[2 ];
429+ hueIP[0 ] = Network .localIP ()[0 ];
430+ hueIP[1 ] = Network .localIP ()[1 ];
431+ hueIP[2 ] = Network .localIP ()[2 ];
387432 }
388433
389434 // init Alexa hue emulation
@@ -482,7 +527,7 @@ void WLED::handleConnection()
482527 wasConnected = false ;
483528 return ;
484529 }
485- if (!WLED_CONNECTED ) {
530+ if (!Network. isConnected () ) {
486531 if (interfacesInited) {
487532 DEBUG_PRINTLN (F (" Disconnected!" ));
488533 interfacesInited = false ;
@@ -495,7 +540,7 @@ void WLED::handleConnection()
495540 } else if (!interfacesInited) { // newly connected
496541 DEBUG_PRINTLN (" " );
497542 DEBUG_PRINT (F (" Connected! IP address: " ));
498- DEBUG_PRINTLN (WiFi .localIP ());
543+ DEBUG_PRINTLN (Network .localIP ());
499544 initInterfaces ();
500545 userConnected ();
501546 usermods.connected ();
0 commit comments