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

Wifi.softAPConfig() sometimes set the wrong IP address #985

Closed
henricazottes opened this issue Jan 10, 2018 · 7 comments
Closed

Wifi.softAPConfig() sometimes set the wrong IP address #985

henricazottes opened this issue Jan 10, 2018 · 7 comments

Comments

@henricazottes
Copy link

Hardware:

Board: Lolin32 Lite
Core Installation/update date: ?11/jul/2017?
IDE name: Platform.Io
Flash Frequency: ?
Upload Speed: ?

Description:

I'm using my esp as an AP to serve a little webserver. To do so, I start the ESP in AP mode like this:

int startAP() {
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("Running config server");
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);
  WiFi.mode(WIFI_OFF);
  delay(1000);
  WiFi.mode(WIFI_AP);
  if(!WiFi.softAPConfig(IPAddress(192, 168, 5, 1), IPAddress(192, 168, 5, 1), IPAddress(255, 255, 255, 0))){
      Serial.println("AP Config Failed");
  }
  if(WiFi.softAP(gw_ssid, gw_pwd)){
    Serial.println("");
    IPAddress myIP = WiFi.softAPIP();
    Serial.println("Network " + String(gw_ssid) + " running");
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    return 0;
  } else {
      Serial.println("Starting AP failed.");
      return 1;
  }
}

As you can see I'm giving a static IP to the ESP which is supposed to be 192.168.5.1. Moreover, the sub network mask is set to /24. However despite this given configuration I sometimes get my ESP adressed as 192.168.4.1 (which is confirmed when I try to access the webserver).

Am I missing a configuration option ? Thanks :)

@henricazottes
Copy link
Author

Anyone experiencing the same ?
(ping @me-no-dev ?)

@everslick
Copy link
Contributor

everslick commented Jan 20, 2018

you have to wait until event SYSTEM_EVENT_AP_START has fired, before you can set its configuration. and the AP is started by WiFi.softAP(gw_ssid, gw_pwd); so the order is:

WiFi.mode(WIFI_AP);
WiFi.softAP(...);
wait for SYSTEM_EVENT_AP_START
WiFi.softAPConfig(...)

a crude hack would be to put delay(100); after WiFi.softAP(...);

@kharar
Copy link

kharar commented Jan 24, 2018

I can confirm that the following is working for me, running Arduino with the latest ESP32 library

#include <WiFi.h>

const char *ssid = "ESP32ap";
const char *password = "12345678";

void setup() {
  Serial.begin(115200);
  Serial.println("Configuring access point...");

  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);
  Serial.println("Wait 100 ms for AP_START...");
  delay(100);
  
  Serial.println("Set softAPConfig");
  IPAddress Ip(192, 168, 1, 1);
  IPAddress NMask(255, 255, 255, 0);
  WiFi.softAPConfig(Ip, Ip, NMask);
  
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
}

void loop() {
}

@henricazottes
Copy link
Author

I also tried today and so far it has been working correctly. I'll report it if it behaves weird again. Thanks guys !

@williamesp2015
Copy link

I have problem for one week using WIFI_AP_STA specially AP mode. I am using ESP32 and PlatformIo and after connect to AP I can not connect to websocket. I had no problem before.

WiFi.disconnect(true); //Disable STA
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_AP_STA);
while (WiFi.getMode() != WIFI_AP_STA) {
delay(50);
}
Serial.println("Configuring access point...");
WiFi.softAP(apssid, appassword);
Serial.println("Wait 100 ms for AP_START...");
delay(100);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());

@thEpisode
Copy link

thEpisode commented Jan 28, 2020

you have to wait until event SYSTEM_EVENT_AP_START has fired, before you can set its configuration. and the AP is started by WiFi.softAP(gw_ssid, gw_pwd); so the order is:

WiFi.mode(WIFI_AP);
WiFi.softAP(...);
wait for SYSTEM_EVENT_AP_START
WiFi.softAPConfig(...)

a crude hack would be to put delay(100); after WiFi.softAP(...);

@everslick Awesome! It works!

@sanfx
Copy link

sanfx commented Jun 4, 2020

I can confirm that the following is working for me, running Arduino with the latest ESP32 library

#include <WiFi.h>

const char *ssid = "ESP32ap";
const char *password = "12345678";

void setup() {
  Serial.begin(115200);
  Serial.println("Configuring access point...");

  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);
  Serial.println("Wait 100 ms for AP_START...");
  delay(100);
  
  Serial.println("Set softAPConfig");
  IPAddress Ip(192, 168, 1, 1);
  IPAddress NMask(255, 255, 255, 0);
  WiFi.softAPConfig(Ip, Ip, NMask);
  
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
}

void loop() {
}

what is #include <WiFi.h> I believe everyone is using ESP8266Wifi.h

khoih-prog added a commit to khoih-prog/ESP_WiFiManager that referenced this issue Aug 17, 2020
### Releases 1.0.11

1. Add optional **CORS (Cross-Origin Resource Sharing)** feature. Thanks to [AlesSt](https://github.com/AlesSt). See more in [Issue #27: CORS protection fires up with AJAX](#27) and [Cross Origin Resource Sharing](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing). To use, you must explicitly use `#define USING_CORS_FEATURE true`
2. Solve issue softAP with custom IP sometimes not working. Thanks to [AlesSt](https://github.com/AlesSt). See [Issue #26: softAP with custom IP not working](#26) and [Wifi.softAPConfig() sometimes set the wrong IP address](espressif/arduino-esp32#985).
3. Temporary fix for issue of not clearing WiFi SSID/PW from flash of ESP32. Thanks to [AlesSt](https://github.com/AlesSt). See more in [Issue #25: API call /r doesnt clear credentials](#25) and [WiFi.disconnect(true) problem](espressif/arduino-esp32#400).
4. Fix autoConnect() feature to permit autoConnect() to use STA static IP or DHCP IP. Remove from deprecated functi0n list.
5. Enhance README.md with more instructions and illustrations.
maxbbraun added a commit to maxbbraun/accent that referenced this issue Jun 26, 2021
knyar pushed a commit to knyar/accent that referenced this issue Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants