使用 ESP32 为无线调制解调器配置 DHCP softAP

Configure a DHCP softAP for wireless modem using ESP32

我已经为与 ESP32 连接的 wireless modem 配置了软 AP,它从无线传感器网络接收值以在 192.168.1.77 为网页提供服务。这个网页是用来保存 ipv4 设置的。在登陆页面上,我为 select DHCP 或静态 IP 设置提供了两个选项。

我能够 select 并保存静态 IP、网关和子网设置,然后将其配置为 softAP。

但是在 DHCP 模式下,它仍然采用最后配置的值 192.168.1.77,即使它必须采用 192.168.4.1。

请看一下我的代码和网络表单

#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 == '[=10=]') {
            break;                            // No more separators, exit
        }
        str++;                                // Point to next character after separator
    }
}

您对此的建议将有很大帮助。

在你的代码中,我可以看到你正在使用 passDhcpWeb 作为 softAP 密码,但它只包含 7 个字符,而根据 WiFiAP.cpp 密码长度应该是8 个字符。这可能是导致您出现问题的原因,关于断开连接,我也遇到了同样的问题。我通过使用 WiFi.disconnect(false) 解决了它; Wifi.enabeAP(假); 希望对你有帮助