@@ -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
@@ -131,7 +193,7 @@ void WLED::loop()
131193 lastWifiState = WiFi.status ();
132194 DEBUG_PRINT (" State time: " ); DEBUG_PRINTLN (wifiStateChangedTime);
133195 DEBUG_PRINT (" NTP last sync: " ); DEBUG_PRINTLN (ntpLastSyncTime);
134- DEBUG_PRINT (" Client IP: " ); DEBUG_PRINTLN (WiFi .localIP ());
196+ DEBUG_PRINT (" Client IP: " ); DEBUG_PRINTLN (Network .localIP ());
135197 DEBUG_PRINT (" Loops/sec: " ); DEBUG_PRINTLN (loops / 10 );
136198 loops = 0 ;
137199 debugTime = millis ();
@@ -195,6 +257,7 @@ void WLED::setup()
195257 if (strcmp (clientSSID, DEFAULT_CLIENT_SSID) == 0 )
196258 showWelcomePage = true ;
197259 WiFi.persistent (false );
260+ WiFi.onEvent (WiFiEvent);
198261
199262 Serial.println (F (" Ada" ));
200263
@@ -322,6 +385,10 @@ void WLED::initConnection()
322385 ws.onEvent (wsEvent);
323386 #endif
324387
388+ #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET)
389+ ETH.begin ();
390+ #endif
391+
325392 WiFi.disconnect (true ); // close old connections
326393#ifdef ESP8266
327394 WiFi.setPhyMode (WIFI_PHY_MODE_11N);
@@ -357,29 +424,7 @@ void WLED::initConnection()
357424
358425 // convert the "serverDescription" into a valid DNS hostname (alphanumeric)
359426 char hostname[25 ] = " wled-" ;
360- const char *pC = serverDescription;
361- uint8_t pos = 5 ;
362-
363- while (*pC && pos < 24 ) { // while !null and not over length
364- if (isalnum (*pC)) { // if the current char is alpha-numeric append it to the hostname
365- hostname[pos] = *pC;
366- pos++;
367- } else if (*pC == ' ' || *pC == ' _' || *pC == ' -' || *pC == ' +' || *pC == ' !' || *pC == ' ?' || *pC == ' *' ) {
368- hostname[pos] = ' -' ;
369- pos++;
370- }
371- // else do nothing - no leading hyphens and do not include hyphens for all other characters.
372- pC++;
373- }
374- // if the hostname is left blank, use the mac address/default mdns name
375- if (pos < 6 ) {
376- sprintf (hostname + 5 , " %*s" , 6 , escapedMac.c_str () + 6 );
377- } else { // last character must not be hyphen
378- while (pos > 0 && hostname[pos -1 ] == ' -' ) {
379- hostname[pos -1 ] = 0 ;
380- pos--;
381- }
382- }
427+ prepareHostname (hostname);
383428
384429#ifdef ESP8266
385430 WiFi.hostname (hostname);
@@ -400,9 +445,9 @@ void WLED::initInterfaces()
400445 DEBUG_PRINTLN (F (" Init STA interfaces" ));
401446
402447 if (hueIP[0 ] == 0 ) {
403- hueIP[0 ] = WiFi .localIP ()[0 ];
404- hueIP[1 ] = WiFi .localIP ()[1 ];
405- hueIP[2 ] = WiFi .localIP ()[2 ];
448+ hueIP[0 ] = Network .localIP ()[0 ];
449+ hueIP[1 ] = Network .localIP ()[1 ];
450+ hueIP[2 ] = Network .localIP ()[2 ];
406451 }
407452
408453 // init Alexa hue emulation
@@ -501,7 +546,7 @@ void WLED::handleConnection()
501546 wasConnected = false ;
502547 return ;
503548 }
504- if (!WLED_CONNECTED ) {
549+ if (!Network. isConnected () ) {
505550 if (interfacesInited) {
506551 DEBUG_PRINTLN (F (" Disconnected!" ));
507552 interfacesInited = false ;
@@ -514,7 +559,7 @@ void WLED::handleConnection()
514559 } else if (!interfacesInited) { // newly connected
515560 DEBUG_PRINTLN (" " );
516561 DEBUG_PRINT (F (" Connected! IP address: " ));
517- DEBUG_PRINTLN (WiFi .localIP ());
562+ DEBUG_PRINTLN (Network .localIP ());
518563 initInterfaces ();
519564 userConnected ();
520565 usermods.connected ();
0 commit comments