Go Down

Topic: ESP32 webserver not replying when using static IP (Read 208 times) previous topic - next topic

ingdemurtas

I would like the ESP32 to have a static IP to access the webpage.

If the ESP32 is set to get the IP via DHCP it will get 109, then visiting 192.168.1.109 I get the reply from the webserver ("it works!").
When I set a static IP (like example below) the ESP connects to the wifi with the specified IP, but the webpage 192.168.1.80 would not load.
Obviously I made sure that the IP is available.
If I set the static IP equal to the IP that was assigned via DHCP, than the webpage loads.  :o

What can I do to make it work on any static IP?

Code: [Select]
// ESP32 webserver
// 17 august 2019
// Board:ESP32 WROVER module
// stranamente funziona solo con l'ip assegnato dal DHCP. Con IP statico non carica la pagina

const char *ssid     = "xxx";
const char *password = "xxx";
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);

IPAddress ip(192, 168, 1, 80);
IPAddress gateway(192, 168, 1, 1);
IPAddress dns(192, 168, 1, 1);
IPAddress subnet(255,255,255,0);



//-------------------------------------------
void setup(){
  Serial.begin(19200);                               
  Serial.println("\n boot");
}

//---------------------------------------------------
void connect_wifi(){
  WiFi.mode(WIFI_OFF);  //serve a evitare che si metta a fare l'AccessPoint, e quindi non connettere alla rete esistente
  delay(1000);
  WiFi.mode(WIFI_STA);  //serve a evitare che si metta a fare l'AccessPoint, e quindi non connettere alla rete esistente
  WiFi.config(ip, dns, gateway, subnet); //IP statico WiFi.config(ip, dns, gateway, subnet);
  WiFi.begin(ssid, password);   //WiFi connection
 
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) { //blocking code
    Serial.print(".");
    delay(500);
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");  Serial.println(ssid);
  Serial.print("IP address: ");  Serial.println(WiFi.localIP());  //IP address assigned to your ESP

  server.on("/", handleHomepage); //Associate the handler function to the path
  server.begin(); //Start the server
  Serial.println("Server listening");
}



//************************************************************************
void loop(){
  if(WiFi.status() != WL_CONNECTED){
    connect_wifi();
  }
  server.handleClient();
}
//*************************************************************


//--------------------------------------------------------------
void handleHomepage() {
  Serial.println("visit to homepage");
  String msg;
  msg="it works!";
  server.send(200, "text/html", msg); //Send web page
} //end of handleHomepage


Juraj

bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);

ingdemurtas

whoho! it works!


Code: [Select]
IPAddress ip(192, 168, 1, 90);
IPAddress gateway(192, 168, 1, 1);
IPAddress dns(192, 168, 1, 1);
IPAddress subnet(255,255,255,0);
IPAddress dns1 = (uint32_t)0x00000000;
IPAddress dns2 = (uint32_t)0x00000000;


Code: [Select]
WiFi.config(ip, gateway, subnet, dns1, dns2); //IP statico WiFi.config(ip, dns, gateway, subnet);
 


It seems that this is the wrong documentation
https://www.arduino.cc/en/Reference/WiFiConfig

Where do I find the definition of the fuction WiFi.config? (where is the file WiFi.h?)

Juraj

It seems that this is the wrong documentation
https://www.arduino.cc/en/Reference/WiFiConfig

Where do I find the definition of the fuction WiFi.config? (where is the file WiFi.h?)
esp32 arduino core doesn't have documentation.
WiFi.h is on your computer in bards packages installation folder. it is the same folder IDE shows in Preferences as the preferences file location.
and then esp32/hardware/esp32/1.0.1/libraries/WiFi/src/WiFi.h

Go Up