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?
// 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