[GH-ISSUE #1269] Static IP changes when turning esp01 back on #1086

Open
opened 2026-02-28 01:28:27 +03:00 by kerem · 0 comments
Owner

Originally created by @FelipeCasaes on GitHub (Jul 18, 2021).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1269

Esp8266/Esp32: Esp8266
Hardware: esp01,

Problem description

Hi, I need help.
I've already searched here and didn't understand the answers given to the same problem.

When reconnecting my esp, the ip defined in the configuration screen changes to the default static of the code, how can I solve this problem and make it continue with the IP configured in the AP screen?

Sketch

#include <FS.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>

char static_ip [16] = "192.168.0.90";
char static_gw [16] = "192.168.0.1";
char static_sn [16] = "255.255.255.0";
bool shouldSaveConfig = false;

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}

void setup() {
Serial.begin(115200);

WiFiManager wifiManager;

wifiManager.setSaveConfigCallback(saveConfigCallback);
IPAddress _ip, _gw, _sn;
_ip.fromString (static_ip);
_gw.fromString (static_gw);
_sn.fromString (static_sn);
wifiManager.setSTAStaticIPConfig (_ip, _gw, _sn);

wifiManager.autoConnect("ESP-01", "12345678");

// prepare LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(static_ip);

WiFi.mode(WIFI_STA);

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

// Start the server
server.begin();
Serial.println(F("Server started"));

// Print the IP address
Serial.println(WiFi.localIP());
}

void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println(F("new client"));

client.setTimeout(5000); // default is 1000

// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(F("request: "));
Serial.println(req);

// Match the request
int val;
if (req.indexOf(F("/gpio/0")) != -1) {
val = 0;
} else if (req.indexOf(F("/gpio/1")) != -1) {
val = 1;
} else {
Serial.println(F("invalid request"));
val = digitalRead(LED_BUILTIN);
}

// Set LED according to the request
digitalWrite(LED_BUILTIN, val);

// read/ignore the rest of the request
// do not client.flush(): it is for output only, see below
while (client.available()) {
// byte by byte is not very efficient
client.read();
}

// Send the response to the client
// it is OK for multiple small client.print/write,
// because nagle algorithm will group them into one single packet
client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n<html>\r\nGPIO is now "));
client.print((val) ? F("high") : F("low"));
client.print(F("

Click here to switch LED GPIO on, or here to switch LED GPIO off.</html>"));

// The client will actually be flushed then disconnected
// when the function returns and 'client' object is destroyed (out-of-scope)
// flush = ensure written data are received by the other side
Serial.println(F("Disconnecting from client"));
}

Originally created by @FelipeCasaes on GitHub (Jul 18, 2021). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1269 Esp8266/Esp32: Esp8266 Hardware: esp01, Problem description Hi, I need help. I've already searched here and didn't understand the answers given to the same problem. When reconnecting my esp, the ip defined in the configuration screen changes to the default static of the code, how can I solve this problem and make it continue with the IP configured in the AP screen? ### Sketch #include <FS.h> #include <ESP8266WiFi.h> #include <WiFiManager.h> #include <DNSServer.h> #include <ESP8266WebServer.h> char static_ip [16] = "192.168.0.90"; char static_gw [16] = "192.168.0.1"; char static_sn [16] = "255.255.255.0"; bool shouldSaveConfig = false; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); void saveConfigCallback () { Serial.println("Should save config"); shouldSaveConfig = true; } void setup() { Serial.begin(115200); WiFiManager wifiManager; wifiManager.setSaveConfigCallback(saveConfigCallback); IPAddress _ip, _gw, _sn; _ip.fromString (static_ip); _gw.fromString (static_gw); _sn.fromString (static_sn); wifiManager.setSTAStaticIPConfig (_ip, _gw, _sn); wifiManager.autoConnect("ESP-01", "12345678"); // prepare LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(static_ip); WiFi.mode(WIFI_STA); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } Serial.println(); Serial.println(F("WiFi connected")); // Start the server server.begin(); Serial.println(F("Server started")); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } Serial.println(F("new client")); client.setTimeout(5000); // default is 1000 // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(F("request: ")); Serial.println(req); // Match the request int val; if (req.indexOf(F("/gpio/0")) != -1) { val = 0; } else if (req.indexOf(F("/gpio/1")) != -1) { val = 1; } else { Serial.println(F("invalid request")); val = digitalRead(LED_BUILTIN); } // Set LED according to the request digitalWrite(LED_BUILTIN, val); // read/ignore the rest of the request // do not client.flush(): it is for output only, see below while (client.available()) { // byte by byte is not very efficient client.read(); } // Send the response to the client // it is OK for multiple small client.print/write, // because nagle algorithm will group them into one single packet client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ")); client.print((val) ? F("high") : F("low")); client.print(F("<br><br>Click <a href='http://")); client.print(WiFi.localIP()); client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://")); client.print(WiFi.localIP()); client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>")); // The client will actually be *flushed* then disconnected // when the function returns and 'client' object is destroyed (out-of-scope) // flush = ensure written data are received by the other side Serial.println(F("Disconnecting from client")); }
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#1086
No description provided.