M5stack Core2 as an AP and webserver - WiFi dhcp is inconsistent serving IP add to clients
-
I have tried soooo many things to try and work around this, but continue to have the same issue. The code below is the bare minimum to recreate the issue. When I run this on my core2 and connect PC, Mac, or iPhone, it will usually not give the first client that connects an address. You can see it trying in the debug of console port, but the client doesn't get the address..... then, here's the crazy part. If you try and connect another client (iPhone), the first client gets an address, and the second works too. Any assistance would be greatly appreciated.... been banging my head on this one. Thank you.
#include <M5Stack.h> #include "WiFi.h" // Include WiFi library char ssid[] = "M5Stack"; char password[] = "yourpassword"; WiFiServer server(80); IPAddress ip(192, 168, 0, 1); IPAddress gateway(192, 168, 0, 1); IPAddress subnet(255, 255, 255, 0); void setup() { m5.begin(); Serial.begin(115200); WiFi.mode(WIFI_AP); WiFi.softAP(ssid, password); WiFi.softAPConfig(ip, gateway, subnet); WiFi.begin(); } void loop() { // Example of getting the number of connected clients int clientCount = WiFi.softAPgetStationNum(); Serial.print("Connected clients: "); Serial.println(clientCount); delay(5000); // Check every 5 seconds }
-
Hello @Krmiller
I am not able to reproduce the issue you are describing. Tested with Linux and MacOS client. That said I recommend not using
delay()
in the loop as it blocks the WiFi background process.Try replacing loop with below code. (You should see a much quicker response too.)
void loop() { static uint32_t t = millis(); if(millis() > t + 5000) { t = millis(); int clientCount = WiFi.softAPgetStationNum(); Serial.print("Connected clients: "); Serial.println(clientCount); } }
Thanks
Felix -
Hi @felmue,
Thanks for taking a look. Tried your code change - same issue. Not only can I repeat with different core2s, I can reproduce with 2 different Macs and 2 different PCs. I'm Starting to wonder if it has something to do with CN (China) country code. Any other thoughts? ver of M5Stack.h or WiFi.h?
thanks again,
Kris -
As an example: Here the first time I try to connect.... it works and gives my Mac 192.168.0.2.
*** SNIP***
OK
[ 1295][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 0 - WIFI_READY
[ 1363][V][WiFiGeneric.cpp:392] _arduino_event_cb(): AP Started
[ 1363][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring SoftAP static IP: 192.168.0.1, MASK: 255.255.255.0, GW: 192.168.0.1
[ 1364][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 10 - AP_START
[ 1378][V][WiFiGeneric.cpp:143] set_esp_interface_ip(): SoftAP: 192.168.0.1 | Gateway: 192.168.0.1 | DHCP Start: 0.0.0.0 | Netmask: 255.255.255.0
[ 1390][V][WiFiGeneric.cpp:190] set_esp_interface_ip(): DHCP Server Range: 192.168.0.2 to 192.168.0.12
[ 1400][V][WiFiGeneric.cpp:340] _arduino_event_cb(): STA Started
[ 1404][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0
[ 1405][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 2 - STA_START
[ 1428][E][WiFiSTA.cpp:317] begin(): connect failed! 0x300a
Connected clients: 0
Connected clients: 0
Connected clients: 0
Connected clients: 0
Connected clients: 0
Connected clients: 0
[ 32004][V][WiFiGeneric.cpp:407] _arduino_event_cb(): AP Station Connected: MAC: 6c:40:08:99:7b:c0, AID: 1
[ 32005][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 12 - AP_STACONNECTED
[ 32035][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[ 32035][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 1
Connected clients: 1
Connected clients: 1
**** SNIP ****Then I disconnect and connect to another network (my home WiFi) .... although I'm no longer connected to the core2, It continues to show 1 client connected. When they I try to reconnect to the core2 SSID, it first show me disconnected, then tries to serve an address over and over. The Mac never accepts the address.
**** SNIP ****
Connected clients: 1
Connected clients: 1
Connected clients: 1
Connected clients: 1
Connected clients: 1
[208861][V][WiFiGeneric.cpp:414] _arduino_event_cb(): AP Station Disconnected: MAC: 6c:40:08:99:7b:c0, AID: 1
[208862][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 13 - AP_STADISCONNECTED
Connected clients: 1
[216440][V][WiFiGeneric.cpp:407] _arduino_event_cb(): AP Station Connected: MAC: 6c:40:08:99:7b:c0, AID: 1
[216440][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 12 - AP_STACONNECTED
Connected clients: 1
[216552][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[216552][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[217890][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[217891][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[220363][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[220364][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 1
[226304][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[226305][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 1
[227309][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[227309][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[229339][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[229339][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 1
[233314][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[233315][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 1
Connected clients: 1
[242335][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[242336][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[243334][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[243335][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[245337][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[245337][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 1
[249339][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[249339][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 1
Connected clients: 1
[258361][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[258361][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[259365][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[259365][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[261365][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.2
[261365][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
**** SNIP ****This goes on for sever minutes. Then as soon as i connect with my iPhone... both clients get an address.
**** SNIP ****
Connected clients: 1
Connected clients: 1
[942052][V][WiFiGeneric.cpp:407] _arduino_event_cb(): AP Station Connected: MAC: 2e:da:8a:d8:c5:64, AID: 2
[942053][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 12 - AP_STACONNECTED
[943452][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.3
[943453][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[944350][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.0.3
[944351][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
Connected clients: 2
Connected clients: 2
Connected clients: 2
Connected clients: 2
Connected clients: 2