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

ESP32 does not accept Static IP #1262

Closed
williamesp2015 opened this issue Mar 26, 2018 · 11 comments
Closed

ESP32 does not accept Static IP #1262

williamesp2015 opened this issue Mar 26, 2018 · 11 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@williamesp2015
Copy link

Hardware:

Board: ESP32 Dev Module
Core Installation/update date: Latest
IDE name: Platform.io
Flash Frequency: 40Mhz
Upload Speed: 115200?

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.mode(WIFI_AP);
WiFi.softAP("ADSL1", "AbbasMeamarbashi2017***");
Serial.println("Wait 100 ms for AP_START...");
delay(300);
WiFi.onEvent(wifi_event);
for(int rrr=0;rrr<20;rrr++)
{
Serial.println("Set softAPConfig");
IPAddress Ip(192, 168, 1, 11);
IPAddress NMask(255, 255, 255, 0);
WiFi.softAPConfig(Ip, Ip, NMask);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
}
void loop()
{
}

@JacoFourie
Copy link

I seem to have the same issue. If I use the sample program it will not connect.

@JacoFourie
Copy link

I have tested both methods.

 //if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  //  Serial.println("STA Failed to configure");
 // }

  Serial.print("Connecting to ");
  Serial.println(ssid);

  //WiFi.begin(ssid, password);

  if(!WiFi.config(IPAddress(10, 64, 9, 38), IPAddress(10, 64, 9, 20), IPAddress(255, 255, 255, 0), IPAddress(10, 64, 9, 20))){
        Serial.println("STA Failed to configure");
    }
    if(WiFi.begin(ssid, password) == WL_CONNECT_FAILED){
        Serial.println("STA Failed to start");
    }

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

The output I get is this

Connecting to Pandora-studio
..............................................................................................................................................................................................................................................

If I use DHCP by removing this line

WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)

Then it connects without any problem.

Connecting to Pandora-studio
...
WiFi connected!
IP address: 10.64.9.119
ESP Mac Address: 24:0A:C4:0D:84:40
Subnet Mask: 255.255.255.0
Gateway IP: 10.64.9.20
DNS: 10.64.9.20

@JacoFourie
Copy link

OK I found this. #1081

TThe error is still there. If I remove

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

and just add a delay of 15 seconds then all works fine.

@JacoFourie
Copy link

OK so this is the fix.

1444806

comment out the if

} else if(event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
        //if(WiFiSTAClass::status() == WL_IDLE_STATUS) {
            WiFiSTAClass::_setStatus(WL_CONNECTED);
        //}
    }

@williamesp2015
Copy link
Author

Thanks JacoFourie. Could please simplify your correct changes? Thanks

@JacoFourie
Copy link

Go to the file in the libraries folder of the Arduino-Core files

My home path is

C:\Users\Jaco\Documents\Arduino\hardware\espressif\esp32\
C:\Users\Jaco\Documents\Arduino\hardware\espressif\esp32\libraries\WiFi\src\WiFiGeneric.cpp

and comment out the if as shown.

} else if(event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
        //if(WiFiSTAClass::status() == WL_IDLE_STATUS) {
            WiFiSTAClass::_setStatus(WL_CONNECTED);
        //}
    }

@Duske
Copy link

Duske commented Mar 28, 2018

@JacoFourie can confirm that this did the trick for me as well. In my case running Atom with platform.io,
the WifiGeneric.cpp file is located here: .platformio\packages\framework-arduinoespressif32\libraries\WiFi\src\WiFiGeneric.cpp

@sreenumalae
Copy link

thank you so much for your answers.
But I'm confused and I'm not able to figure out the sketch to assign a static IP for esp32.
so @JacoFourie can you please provide me the exact sketch?

@JacoFourie
Copy link

JacoFourie commented May 3, 2018

@sreenumalae I am posting the part you need for a static IP


#include <WiFi.h>

int wificounter = 0;
const char* ssid     = "Pandora";
const char* password = "**********";

IPAddress local_IP(10, 64, 9, 38);
IPAddress gateway(10, 64, 9, 20);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(10, 64, 9, 20); //optional
IPAddress secondaryDNS(8, 8, 8, 8); //optional

void setup_wifi() {

  

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  if (WiFi.status() == WL_CONNECTED) {
    WiFi.disconnect();
  }


  if (WiFi.status() != WL_CONNECTED) {
    WiFi.persistent(false);
    WiFi.mode(WIFI_OFF);
    WiFi.mode(WIFI_STA);
    
    if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
      }
    WiFi.begin(ssid, password);
  }


  wificounter = 0;
  while (WiFi.status() != WL_CONNECTED && wificounter < 10) {
    for (int i = 0; i < 500; i++) {
      delay(1);
    }
    Serial.print(".");
    wificounter++;
  }

  if (wificounter >= 10) {
    Serial.println("Restarting ...");
    ESP.restart();
  }

  delay(10);
  // We start by connecting to a WiFi network

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

}


void setup() {
  
  Serial.begin(115200);
          
  // We start by connecting to a WiFi network
  setup_wifi();


}

@stale
Copy link

stale bot commented Aug 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Aug 1, 2019
@stale
Copy link

stale bot commented Aug 15, 2019

This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

4 participants