我已经为连接到ESP32的wireless modem配置了软AP,它从无线传感器网络接收值,以服务于192.168.1.77的网页。此网页用于保存ipv4设置。在登录页面的此处,我给出了两个选择DHCP或静态IP设置的选项。
我能够选择并保存静态IP,网关和子网设置,然后将其配置为softAP。
但是在DHCP模式下,即使必须使用192.168.4.1,它仍将采用最后配置的值192.168.1.77。
请看看我的代码和网络表单
#include <WiFi.h>
#include "FS.h"
#include "SPIFFS.h"
#include <WebServer.h>
WebServer server(80);
//**********check for connection*************/
bool isConnected = true;
bool isDisconnect = false;
//**********softAPconfig Timer*************/
unsigned long APTimer = 0;
unsigned long APInterval = 120000;
//*********SSID and Pass for AP**************/
const char *ssidAP = "ESPuser";
const char *ssidAPWeb = "ESPWebUser";
const char *ssidDhcpWeb = "ESPDHCPUser";
const char *passDhcpWeb = "1234567";
//*********Static IP Config**************/
IPAddress ap_local_IP(192,168,1,77);
IPAddress ap_gateway(192,168,1,254);
IPAddress ap_subnet(255,255,255,0);
//*********Static IP WebConfig**************/
IPAddress ap_localWeb_IP;
IPAddress ap_Webgateway;
IPAddress ap_Websubnet;
IPAddress ap_dhcpWeb_IP;
uint8_t ip0;
uint8_t ip1;
uint8_t ip2;
uint8_t ip3;
//*********IP Char Array**************/
char ipv4Arr[20];
char gatewayArr[20];
char subnetArr[20];
char ipv4dhcpArr[20];
void setup()
{
Serial.begin(9600);
while(!Serial);
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
Serial.println(WiFi.softAP(ssidAP) ? "soft-AP setup": "Failed to connect");
delay(100);
Serial.println(WiFi.softAPConfig(ap_local_IP, ap_gateway, ap_subnet)? "Configuring Soft AP" : "Error in Configuration");
Serial.println(WiFi.softAPIP());
SPIFFS.begin();
server.begin();
server.on("/", handleRoot);
server.on("/dhcp", handleDHCP);
server.on("/static", handleStatic);
server.onNotFound(handleNotFound);
APTimer = millis();
while(isConnected && millis()-APTimer<= APInterval) {
server.handleClient(); }
}
void loop()
{
Serial.println(WiFi.softAPIP());
delay(500);
}
//****************************HANDLE ROOT***************************//
void handleRoot() {
//Redisplay the form
if(server.args()>0){
for(int i=0; i<=server.args();i++){
Serial.println(String(server.argName(i))+'\t' + String(server.arg(i)));
}
if(server.hasArg("ipv4static") && server.hasArg("gateway") && server.hasArg("subnet")){
staticSet();
}else if(server.arg("ipv4")!= ""){
dhcpSetManual();
}else{
dhcpSetDefault();
}
}else{
File file = SPIFFS.open("/Select_Settings.html", "r");
server.streamFile(file,"text/html");
file.close();
}
}
void handleDHCP(){
File file = SPIFFS.open("/page_dhcp.html", "r");
server.streamFile(file,"text/html");
file.close();}
void handleStatic(){
File file = SPIFFS.open("/page_static.html", "r");
server.streamFile(file,"text/html");
file.close();}
void staticSet(){
String response="<p>The static ip is ";
response += server.arg("ipv4static");
response +="<br>";
response +="The gateway ip is ";
response +=server.arg("gateway");
response +="<br>";
response +="The subnet Mask is ";
response +=server.arg("subnet");
response +="</P><BR>";
response +="<H2><a href=\"/\">go home</a></H2><br>";
response += "<script> alert(\"Settings Saved\"); </script>";
server.send(200, "text/html", response);
String ipv4static = String(server.arg("ipv4static"));
String gateway = String(server.arg("gateway"));
String subnet = String(server.arg("subnet"));
ipv4static.toCharArray(ipv4Arr,sizeof(ipv4static)+2);
gateway.toCharArray(gatewayArr,sizeof(gateway)+2);
subnet.toCharArray(subnetArr,sizeof(subnet)+2);
byte ip[4];
parseBytes(ipv4Arr,'.', ip, 4, 10);
ip0 = (uint8_t)ip[0];
ip1 = (uint8_t)ip[1];
ip2 = (uint8_t)ip[2];
ip3 = (uint8_t)ip[3];
IPAddress ap_local(ip0,ip1,ip2,ip3);
ap_localWeb_IP = ap_local;
parseBytes(gatewayArr,'.', ip, 4, 10);
ip0 = (uint8_t)ip[0];
ip1 = (uint8_t)ip[1];
ip2 = (uint8_t)ip[2];
ip3 = (uint8_t)ip[3];
IPAddress ap_gate(ip0,ip1,ip2,ip3);
ap_Webgateway = ap_gate;
parseBytes(subnetArr,'.', ip, 4, 10);
ip0 = (uint8_t)ip[0];
ip1 = (uint8_t)ip[1];
ip2 = (uint8_t)ip[2];
ip3 = (uint8_t)ip[3];
IPAddress ap_net(ip0,ip1,ip2,ip3);
ap_Websubnet= ap_net;
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
Serial.println(WiFi.softAP(ssidAPWeb) ? "Setting up SoftAP" : "error setting up");
delay(100);
while(!(WiFi.softAPIP()== ap_localWeb_IP)){
Serial.println(WiFi.softAPConfig(ap_localWeb_IP, ap_gate, ap_net) ? "Configuring softAP" : "kya yaar not connected");
}
isConnected = false;
}
void dhcpSetManual(){
String response="<p>The dhcp IPv4 address is ";
response += server.arg("ipv4");
response +="</P><BR>";
response +="<H2><a href=\"/\">go home</a></H2><br>";
response += "<script> alert(\"Settings Saved\"); </script>";
server.send(200, "text/html", response);
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
Serial.println(WiFi.softAP(ssidDhcpWeb, passDhcpWeb) ? "Setting up SoftAP" : "error setting up");
delay(100);
while(!(WiFi.softAPIP()== ap_dhcp)){
Serial.println(WiFi.softAPConfig(ap_dhcp, ap_gateway,ap_subnet) ? "Configuring DHCP" : "error configuring");
}
isConnected = false;
}
void dhcpSetDefault(){
String response="<p>The dhcp IPv4 address is ";
response += server.arg("configure");
response +="</P><BR>";
response +="<H2><a href=\"/\">go home</a></H2><br>";
response += "<script> alert(\"Settings Saved\"); </script>";
server.send(200, "text/html", response);
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
Serial.println(WiFi.softAP(ssidDhcpWeb, passDhcpWeb) ? "Setting up SoftAP" : "error setting up");
delay(100);
while(!(WiFi.softAPIP()== ap_dhcp)){
Serial.println(WiFi.softAPConfig(ap_dhcp, ap_gateway,ap_subnet) ? "Configuring DHCP" : "error configuring");
}
isConnected = false;
}
//****************HANDLE NOT FOUND*********************//
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";
}
message +="<H2><a href=\"/\">go home</a></H2><br>";
server.send(404, "text/plain", message);
}
void parseBytes(const char* str, char sep, byte* bytes, int maxBytes, int base) {
for (int i = 0; i < maxBytes; i++) {
bytes[i] = strtoul(str, NULL, base); // Convert byte
str = strchr(str, sep); // Find next separator
if (str == NULL || *str == '\0') {
break; // No more separators, exit
}
str++; // Point to next character after separator
}
}
您对此的建议将有很大帮助。
答案 0 :(得分:1)
在您的代码中,我可以看到您正在使用passDhcpWeb作为softAP密码,但是它仅包含7个字符,而根据 WiFiAP.cpp ,密码长度应为8个字符。这可能是造成您的问题的原因,关于断开连接,我也遇到了同样的问题。我通过使用WiFi.disconnect(false);解决了它Wifi.enabeAP(false); 希望这对您有帮助