[GH-ISSUE #909] Static IP on webserver not working after setting WiFi with AP #769

Closed
opened 2026-02-28 01:26:58 +03:00 by kerem · 1 comment
Owner

Originally created by @RmenaLopez on GitHub (Jul 5, 2019).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/909

Im trying to to connect my ESP8266 to WiFi using the autoConnect method and then create a server to receive requests.
I need to know both IP, AP and server, always so that I can configure and use this ESP with other ones, that's why I want to set both IPs as fixed.
Setting the AP IP works just fine, I can connect to the hotspot and pick my WiFi as usual, but setting the server IP doesn't work, the IP is set but I'm unable to access the server.
Here's the code im using:

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "WiFiManager.h"         //https://github.com/tzapu/WiFiManager

std::unique_ptr<ESP8266WebServer> server;

//default custom static IP
char static_ip[16] = "192.168.13.90";
char static_gw[16] = "192.168.13.1";
char static_sn[16] = "255.255.255.0";
char static_dns[16] = "8.8.8.8";

void handleRoot() {
  server->send(200, "text/plain", "hello from esp8266!");
}

void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server->uri();
  message += "\nMethod: ";
  message += (server->method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server->args();
  message += "\n";
  for (uint8_t i = 0; i < server->args(); i++) {
    message += " " + server->argName(i) + ": " + server->arg(i) + "\n";
  }
  server->send(404, "text/plain", message);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  //Serial.setDebugOutput(true);
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //reset saved settings
  wifiManager.resetSettings();

  //fetches ssid and pass from eeprom and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  //wifiManager.autoConnect("AutoConnectAP");
  //or use this for auto generated name ESP + ChipID

  IPAddress _ip,_gw,_sn,_dns;
  _ip.fromString(static_ip);
  _gw.fromString(static_gw);
  _sn.fromString(static_sn);
  _dns.fromString(static_dns);

  wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
  
  if (!wifiManager.autoConnect("AutoConnectAP")) {
    Serial.println("failed to connect, we should reset as see if it connects");
    delay(3000);
    ESP.reset();
    delay(5000);
  }

  
  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
  
  server.reset(new ESP8266WebServer(WiFi.localIP(), 80));

  server->on("/", handleRoot);

  server->on("/inline", []() {
    server->send(200, "text/plain", "this works as well");
  });

  server->onNotFound(handleNotFound);

  server->begin();
  Serial.println("HTTP server started");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
  server->handleClient();
}

If I remove wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); then I can connect, but I don't want the generated IP, I need a known and fixed IP.

Originally created by @RmenaLopez on GitHub (Jul 5, 2019). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/909 Im trying to to connect my ESP8266 to WiFi using the autoConnect method and then create a server to receive requests. I need to know both IP, AP and server, always so that I can configure and use this ESP with other ones, that's why I want to set both IPs as fixed. Setting the AP IP works just fine, I can connect to the hotspot and pick my WiFi as usual, but setting the server IP doesn't work, the IP is set but I'm unable to access the server. Here's the code im using: ```C++ #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino //needed for library #include <DNSServer.h> #include <ESP8266WebServer.h> #include "WiFiManager.h" //https://github.com/tzapu/WiFiManager std::unique_ptr<ESP8266WebServer> server; //default custom static IP char static_ip[16] = "192.168.13.90"; char static_gw[16] = "192.168.13.1"; char static_sn[16] = "255.255.255.0"; char static_dns[16] = "8.8.8.8"; void handleRoot() { server->send(200, "text/plain", "hello from esp8266!"); } void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server->uri(); message += "\nMethod: "; message += (server->method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server->args(); message += "\n"; for (uint8_t i = 0; i < server->args(); i++) { message += " " + server->argName(i) + ": " + server->arg(i) + "\n"; } server->send(404, "text/plain", message); } void setup() { // put your setup code here, to run once: Serial.begin(115200); //Serial.setDebugOutput(true); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //reset saved settings wifiManager.resetSettings(); //fetches ssid and pass from eeprom and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration //wifiManager.autoConnect("AutoConnectAP"); //or use this for auto generated name ESP + ChipID IPAddress _ip,_gw,_sn,_dns; _ip.fromString(static_ip); _gw.fromString(static_gw); _sn.fromString(static_sn); _dns.fromString(static_dns); wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); if (!wifiManager.autoConnect("AutoConnectAP")) { Serial.println("failed to connect, we should reset as see if it connects"); delay(3000); ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); server.reset(new ESP8266WebServer(WiFi.localIP(), 80)); server->on("/", handleRoot); server->on("/inline", []() { server->send(200, "text/plain", "this works as well"); }); server->onNotFound(handleNotFound); server->begin(); Serial.println("HTTP server started"); Serial.println(WiFi.localIP()); } void loop() { // put your main code here, to run repeatedly: server->handleClient(); } ``` If I remove `wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);` then I can connect, but I don't want the generated IP, I need a known and fixed IP.
kerem 2026-02-28 01:26:58 +03:00
Author
Owner

@RmenaLopez commented on GitHub (Jul 7, 2019):

Hi! This appears to be working correctly in the development branch.

<!-- gh-comment-id:508962802 --> @RmenaLopez commented on GitHub (Jul 7, 2019): Hi! This appears to be working correctly in the development branch.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#769
No description provided.