我正在尝试使用 ESP8226 (ESP01) 创建一个网页让用户输入他的新 IP 地址、网关和子网。但是在我按下保存后,它确实正确解析了数据,但它并没有将新的 IP 数据保存到闪存中。
我希望有人能帮助我。非常感谢。
下面是我的esp01代码:
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#define WEBSERVER_H //this line need before ESPAsyncWebServer.h else compile error
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
IPAddress ip;
IPAddress gateway;
IPAddress subnet;
WiFiManager wifiManager;
WiFiManagerParameter custom_ip("ip", "IP Address", ip.toString().c_str(), 15);
WiFiManagerParameter custom_gateway("gateway", "Gateway", gateway.toString().c_str(), 15);
WiFiManagerParameter custom_subnet("subnet", "Subnet Mask", subnet.toString().c_str(), 15);
void setup() {
ESP.wdtEnable(WDTO_4S);
Serial.begin(115200);
// Add the parameters to the WiFiManager object
wifiManager.addParameter(&custom_ip);
wifiManager.addParameter(&custom_gateway);
wifiManager.addParameter(&custom_subnet);
// Get the custom IP, gateway, and subnet values entered by the user
ip.fromString(custom_ip.getValue());
gateway.fromString(custom_gateway.getValue());
subnet.fromString(custom_subnet.getValue());
Serial.println("Boot up IP:");
Serial.println(ip);
Serial.println(gateway);
Serial.println(subnet);
// Try to connect to WiFi with saved credentials, if unsuccessful, start Access Point
wifiManager.autoConnect();
// Set the static IP configuration for the module
WiFi.config(ip, gateway, subnet);
// Set network configuration when user submits the form
// Serve a web page for the user to input network configuration
server.on("/", HTTP_GET, [&ip, &gateway, &subnet](AsyncWebServerRequest *request){
String html = "<html><body><h1>Network Configuration</h1>";
html += "<form method='POST' action='/save'>";
html += "IP Address: <input type='text' name='ip' value='" + WiFi.localIP().toString() + "'><br>";
html += "Gateway: <input type='text' name='gateway' value='" + WiFi.gatewayIP().toString() + "'><br>";
html += "Subnet Mask: <input type='text' name='subnet' value='" + WiFi.subnetMask().toString() + "'><br>";
html += "<input type='submit' value='Submit'>";
html += "</form></body></html>";
request->send(200, "text/html", html);
Serial.println("Network IP Configuration Page");
});
// Set network configuration when user submits the form
server.on("/save", HTTP_POST, [&ip, &gateway, &subnet](AsyncWebServerRequest *request){
Serial.println("Changing new IP");
String ipString = request->arg("ip");
String gatewayString = request->arg("gateway");
String subnetString = request->arg("subnet");
// Update the network configuration
Serial.println(ipString);
Serial.println(gatewayString);
Serial.println (subnetString);
IPAddress new_ip=IPAddress();
IPAddress new_gw=IPAddress();
IPAddress new_sub=IPAddress();
new_ip = ip.fromString(ipString);
new_gw = gateway.fromString(gatewayString);
new_sub = subnet.fromString(subnetString);
if (!new_ip ||
!new_gw ||
!new_sub) {
request->send(400, "text/plain", "Invalid input");
return;
}
custom_ip.setValue(ipString.c_str(),15);
custom_gateway.setValue(gatewayString.c_str(),15);
custom_subnet.setValue(subnetString.c_str(),15);
wifiManager.addParameter(&custom_ip);
wifiManager.addParameter(&custom_gateway);
wifiManager.addParameter(&custom_subnet);
String newIP = custom_ip.getValue();
String newGW = custom_gateway.getValue();
String newSub = custom_subnet.getValue();
Serial.println("new IP saved");
Serial.println(newIP);
Serial.println(newGW);
Serial.println (newSub);
IPAddress newIP2;
IPAddress newGW2;
IPAddress newSub2;
newIP2.fromString(newIP);
newGW2.fromString(newGW);
newSub2.fromString(newSub);
// Send a response to the client
request->send(200, "text/plain", "Network configuration updated. Restarting...");
delay(1000);
ESP.restart();
});
// Print the assigned IP address to the Serial Monitor
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start server
server.begin();
Serial.println("Connected.");
}
void loop() {
}
我尝试过其他方法,如 eeprom 仿真,以及 preferences.h 但效果不佳。
所有的moethod数据似乎都正确解析到“保存”页面,但在esp8266(esp01)重置后,它仍然坚持使用旧IP。