Skip to content

Commit f692a41

Browse files
kumymathieucarbou
andauthored
Enable ipv6 support (#43)
* Enable ipv6 support * Update defaults * Add IPAddress getIPv6Address() * Fix ESP8266 support * Fix ARDUINO_EVENT_WIFI_STA_GOT_IP6 event to support ipv6 only networks --------- Co-authored-by: Mathieu Carbou <[email protected]>
1 parent 136a71e commit f692a41

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/MycilaESPConnect.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,26 @@ IPAddress Mycila::ESPConnect::getIPAddress(Mycila::ESPConnect::Mode mode) const
219219
}
220220
}
221221

222+
IPAddress Mycila::ESPConnect::getIPv6Address(Mycila::ESPConnect::Mode mode) const {
223+
#ifndef ESP8266
224+
const wifi_mode_t wifiMode = WiFi.getMode();
225+
switch (mode) {
226+
case Mycila::ESPConnect::Mode::AP:
227+
return IN6ADDR_ANY;
228+
case Mycila::ESPConnect::Mode::STA:
229+
return wifiMode == WIFI_MODE_STA ? WiFi.globalIPv6() : IN6ADDR_ANY;
230+
#ifdef ESPCONNECT_ETH_SUPPORT
231+
case Mycila::ESPConnect::Mode::ETH:
232+
return ETH.linkUp() ? ETH.globalIPv6() : IN6ADDR_ANY;
233+
#endif
234+
default:
235+
return IN6ADDR_ANY;
236+
}
237+
#else
238+
return IPAddress();
239+
#endif
240+
}
241+
222242
ESPCONNECT_STRING Mycila::ESPConnect::getWiFiSSID() const {
223243
switch (WiFi.getMode()) {
224244
case WIFI_MODE_AP:
@@ -558,6 +578,9 @@ void Mycila::ESPConnect::_startSTA() {
558578
WiFi.setAutoReconnect(true);
559579

560580
WiFi.mode(WIFI_STA);
581+
#ifndef ESP8266
582+
WiFi.enableIPv6();
583+
#endif
561584

562585
#ifndef ESPCONNECT_ETH_SUPPORT
563586
if (_config.ipConfig.ip) {
@@ -812,16 +835,26 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
812835
#endif
813836

814837
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
815-
if (_state == Mycila::ESPConnect::State::NETWORK_CONNECTING || _state == Mycila::ESPConnect::State::NETWORK_RECONNECTING) {
816-
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP", getStateName());
817-
_lastTime = -1;
838+
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP: %s", getStateName(), WiFi.localIP().toString().c_str());
818839
#ifndef ESPCONNECT_NO_MDNS
819-
MDNS.begin(_config.hostname.c_str());
840+
MDNS.begin(_config.hostname.c_str());
820841
#endif
842+
if (_state == Mycila::ESPConnect::State::NETWORK_CONNECTING || _state == Mycila::ESPConnect::State::NETWORK_RECONNECTING) {
843+
_lastTime = -1;
821844
_setState(Mycila::ESPConnect::State::NETWORK_CONNECTED);
822845
}
823846
break;
824847

848+
#ifndef ESP8266
849+
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
850+
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP6: %s", getStateName(), WiFi.globalIPv6().toString().c_str());
851+
if (_state == Mycila::ESPConnect::State::NETWORK_CONNECTING || _state == Mycila::ESPConnect::State::NETWORK_RECONNECTING) {
852+
_lastTime = -1;
853+
_setState(Mycila::ESPConnect::State::NETWORK_CONNECTED);
854+
}
855+
break;
856+
#endif
857+
825858
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
826859
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
827860
if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {

src/MycilaESPConnect.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ namespace Mycila {
167167
IPAddress getIPAddress() const { return getIPAddress(getMode()); }
168168
IPAddress getIPAddress(Mode mode) const;
169169

170+
IPAddress getIPv6Address() const { return getIPv6Address(getMode()); }
171+
IPAddress getIPv6Address(Mode mode) const;
172+
170173
// Returns the configured WiFi SSID or the configured SSID of the AP or captive portal, or empty if not available, depending on the current mode
171174
ESPCONNECT_STRING getWiFiSSID() const;
172175
// Returns the BSSID of the current WiFi, or BSSID of the AP or captive portal, or empty if not available

0 commit comments

Comments
 (0)