Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting DHCP IP-range start for softAP #6271

Closed
1 task done
ch-fb opened this issue Feb 12, 2022 · 9 comments · Fixed by #6731
Closed
1 task done

Setting DHCP IP-range start for softAP #6271

ch-fb opened this issue Feb 12, 2022 · 9 comments · Fixed by #6731
Assignees
Labels
Status: Solved Type: Feature request Feature request for Arduino ESP32
Milestone

Comments

@ch-fb
Copy link

ch-fb commented Feb 12, 2022

Related area

WiFi softAP DHCP

Hardware specification

ESP32

Is your feature request related to a problem?

When several Wi-Fi stations are connected to an ESP's softAP and the ESP reboots, it can happen that some stations hold on to their old DHCP lease. However, the DHCP server does not know and re-assigns such still-in-use IP addresses. As a workaround for the problem, the DHCP server could use a different IP range after each restart (i.e. cycling through several ranges), but this requires the possibility of configuring the DHCP server.

Describe the solution you'd like

Possibility of configuring the DHCP server:

*) Adding IPAddress argument to softAPConfig:
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcpstart);

(can be an optional argument using a default value, or the current 3-argument version softAPConfig(local_ip,gateway,subnet) calls the extended 4-argument version)

*) This requires adding an argument to set_esp_interface_ip:
esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPAddress(), IPAddress gateway=IPAddress(), IPAddress subnet=IPAddress(), IPAddress dhcpstart=IPAddress());

An implementation could look like this:

dhcps_lease_t lease;
lease.enable = true;
lease.start_ip.addr = static_cast<uint32_t>(dhcpstart);
lease.end_ip.addr = static_cast<uint32_t>(dhcpstart) + (10 << 24);

instead of
lease.start_ip.addr = static_cast<uint32_t>(local_ip) + (1 << 24);
lease.end_ip.addr = static_cast<uint32_t>(local_ip) + (11 << 24);

This extension would help me (and probably some others) a lot.
Thanks for all your great work,
Christian

Describe alternatives you've considered

No response

Additional context

No response

I have checked existing list of Feature requests and the Contribution Guide

  • I confirm I have checked existing list of Feature requests and Contribution Guide.
@ch-fb ch-fb added the Type: Feature request Feature request for Arduino ESP32 label Feb 12, 2022
@me-no-dev
Copy link
Member

This does not look like something that should be fixed on the ESP32 SoftAP side. Why did the clients hold their lease if the AP was rebooted and they had to reconnect again? This does not sound at all OK and not how networking works. To "hold" a lease, a client must first tell the DHCP server that it intends to do so and the DHCP server needs to respond that it has accepted the same lease.

@SuGlider SuGlider self-assigned this Feb 18, 2022
@SuGlider
Copy link
Collaborator

SuGlider commented Feb 19, 2022

@ch-fb @me-no-dev
I can confirm that I could reproduce the issue.

How to reproduce:
I used C3 as SoftAP (IP x.x.x.1), ESP32 as STA, and PC Win10 as STA.
When C3 starts, DCHP is Ready. Then I connected ESP32 with IP x.x.x.2 followed by Win10 with IP Addr x.x.x.3.
Everything fine.

I disconnect Win10 and reset AP (C3), at the same time. Keep ESP32 STA on.
Start reconnection of Win10 and Turn on AP (C3), in a very short time.
Result is that Win10 now acquired IP x.x.x.2 and ESP32 STA after loosing connection to AP tries to acquire an IP and fail.

This is not easy to reproduce. I had to try it several times to see the issue in action.

ESP32 STA Log (set Menu Option Core Debug Level: "Verbose")

[  4343][D][WiFiGeneric.cpp:842] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[  4365][V][WiFiGeneric.cpp:305] _arduino_event_cb(): STA Got New IP:192.168.1.2
[  4365][D][WiFiGeneric.cpp:842] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[  4368][D][WiFiGeneric.cpp:896] _eventCallback(): STA IP: 192.168.1.2, MASK: 255.255.255.0, GW: 192.168.1.1
.
Connected to WiFi network with IP Address: 192.168.1.2
[120038][V][WiFiGeneric.cpp:300] _arduino_event_cb(): STA Disconnected: SSID: ESP32-Access-Point, BSSID: 84:f7:03:60:17:61, Reason: 6
[120039][D][WiFiGeneric.cpp:842] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[120047][W][WiFiGeneric.cpp:863] _eventCallback(): Reason: 6 - NOT_AUTHED
[240040][V][WiFiGeneric.cpp:309] _arduino_event_cb(): STA IP Lost
[240041][D][WiFiGeneric.cpp:842] _eventCallback(): Arduino Event: 9 - STA_LOST_IP

@SuGlider SuGlider added Resolution: More info needed More info must be provided in this issue Status: Test needed Issue needs testing labels Feb 19, 2022
@SuGlider
Copy link
Collaborator

SuGlider commented Feb 19, 2022

A possible solution to this issue is to detect the STA_LOST_IP and restart WiFi STA.
Try using ARDUINO_EVENT_WIFI_STA_LOST_IP.

Example:

// call back function when IP is Lost by Station -- needs to reconnect to AP
void WiFiStationLostIP(arduino_event_id_t event)
{
  log_d("Enter into LOST IP Event Call Back...");
  while (! WiFi.reconnect()) {
    log_d("Trying to Reconnect due to STA__LOST_IP...");
    delay(500); // wait a bit for the necessary processing
  }
}

void setup()
{
  Serial.begin(115200);
  // Set a callback function when the event STA_LOST_IP happens
  WiFi.onEvent(WiFiStationLostIP, ARDUINO_EVENT_WIFI_STA_LOST_IP);
  Serial.println("STA_LOST_IP event callback is set.");

  // Start WiFi in STA Mode
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

}

@SuGlider
Copy link
Collaborator

I tested the LOST IP EVENT callback and it works as expected, reconnecting the STA again when its IP is used by another device, right after AP reset/reboot.

This is the STA log with the suggestion I made:

There are two steps in the LOG:
1- ESP32 STA connects first time with IP 192.168.4.2 to ESP32-C3 AP. Then I start Win10 connection to AP, at the same time that I reset the C3. At this time I see AP message leasing IP 192.168.4.2 to Win10, which is previuos STA IP Address.
2- STA gets diconnected and can't get previous IP Address back. Then it goes to STA_LOST_IP state and calls WiFiStationLosIP(), reconnecting with a new IP 192.168.4.4.

STA_LOST_IP event callback is set.
[    54][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 0 - WIFI_READY
[   149][V][WiFiGeneric.cpp:283] _arduino_event_cb(): STA Started
[   150][V][WiFiGeneric.cpp:96] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0
[   151][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 2 - STA_START
Connecting
....[  2248][V][WiFiGeneric.cpp:295] _arduino_event_cb(): STA Connected: SSID: ESP32-Access-Point, BSSID: 84:f7:03:60:17:61, Channel: 1, Auth: OPEN
[  2250][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[  2276][V][WiFiGeneric.cpp:305] _arduino_event_cb(): STA Got New IP:192.168.4.2
[  2276][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[  2279][D][WiFiGeneric.cpp:904] _eventCallback(): STA IP: 192.168.4.2, MASK: 255.255.255.0, GW: 192.168.4.1
.
Connected to WiFi network with IP Address: 192.168.4.2
[ 42882][V][WiFiGeneric.cpp:300] _arduino_event_cb(): STA Disconnected: SSID: ESP32-Access-Point, BSSID: 84:f7:03:60:17:61, Reason: 6
[ 42883][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 42891][W][WiFiGeneric.cpp:871] _eventCallback(): Reason: 6 - NOT_AUTHED
[162884][V][WiFiGeneric.cpp:309] _arduino_event_cb(): STA IP Lost
[162885][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 9 - STA_LOST_IP
[162886][D][STA_AP_TESTE.ino:41] WiFiStationLostIP(): Enter into LOST IP Event Call Back...
[164963][V][WiFiGeneric.cpp:295] _arduino_event_cb(): STA Connected: SSID: ESP32-Access-Point, BSSID: 84:f7:03:60:17:61, Channel: 1, Auth: OPEN
[164965][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[165005][V][WiFiGeneric.cpp:305] _arduino_event_cb(): STA Got New IP:192.168.4.4
[165005][D][WiFiGeneric.cpp:850] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[165008][D][WiFiGeneric.cpp:904] _eventCallback(): STA IP: 192.168.4.4, MASK: 255.255.255.0, GW: 192.168.4.1

@SuGlider
Copy link
Collaborator

SuGlider commented Feb 19, 2022

@ch-fb

Please let me know if this solution with WiFi Event Callback solves the issue.

@ch-fb
Copy link
Author

ch-fb commented Mar 7, 2022

Hi, sorry for replying so late.
I am afraid this does not solve the problem. My STAs are laptops (windos/linux/macOS) and phones, not other ESPs. I can reproduce the problem relatively frequently and I found discussion of the problem on the net, so others had the problem too.

Reboting an ESP32 is very fast, so it's AP is gone for just a very short time, too short for other STAs to even notice. And after the reboot, ESPs DHCP server does not know about them anymore. My workaround with cycling through different IP ranges works quite nicely in practice and would not mean much to be implemented in arduino-esp32.

I don't know whether a DHCP server has the possibility to force all STAs to renew their leases. If so, this could be the solution, but that one is part of the SDK.

@VojtechBartoska
Copy link
Contributor

@SuGlider Can you please take a look on last comment and validate if already merged PR fix this fully? Thanks!

@VojtechBartoska VojtechBartoska removed the Resolution: More info needed More info must be provided in this issue label Apr 6, 2022
@VojtechBartoska VojtechBartoska moved this from Under investigation to In Review in Arduino ESP32 Core Project Roadmap Apr 11, 2022
@VojtechBartoska VojtechBartoska moved this from In Review to Under investigation in Arduino ESP32 Core Project Roadmap Apr 11, 2022
@VojtechBartoska VojtechBartoska added the Status: To be implemented Selected for Development label Apr 20, 2022
@VojtechBartoska VojtechBartoska added this to the 2.0.4 milestone May 4, 2022
@SuGlider SuGlider moved this from Under investigation to In Progress in Arduino ESP32 Core Project Roadmap May 10, 2022
@SuGlider SuGlider removed the Status: Test needed Issue needs testing label May 10, 2022
@SuGlider
Copy link
Collaborator

@ch-fb - The PR #6731 implements the feature you have requested.
It shall be merged for the Arduino Core 2.0.4 future release, but you can apply it to your project if you want to do so.

@SuGlider SuGlider moved this from In Progress to In Review in Arduino ESP32 Core Project Roadmap May 12, 2022
Repository owner moved this from In Review to Done in Arduino ESP32 Core Project Roadmap May 16, 2022
@VojtechBartoska VojtechBartoska added Status: Solved and removed Status: To be implemented Selected for Development labels May 26, 2022
@ch-fb
Copy link
Author

ch-fb commented Jun 2, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Solved Type: Feature request Feature request for Arduino ESP32
Projects
Development

Successfully merging a pull request may close this issue.

4 participants