esp8266 - 使用 ESP32 为无线调制解调器配置 DHCP softAP
问题描述
我已经为与 ESP32 连接的无线调制解调器配置了软 AP,它从无线传感器网络接收值,以在 192.168.1.77 处提供网页服务。此网页用于保存 ipv4 设置。在登录页面上,我提供了两个选项来选择 DHCP 或静态 IP 设置。
我可以选择并保存静态 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 == '\0') {
break; // No more separators, exit
}
str++; // Point to next character after separator
}
}
您对此的建议将有很大帮助。
解决方案
在您的代码中,我可以看到您正在使用 passDhcpWeb 作为 softAP 密码,但它仅包含 7 个字符,而根据WiFiAP.cpp密码长度应为 8 个字符。这可能是您的问题的原因,关于断开连接,我也面临同样的问题。我通过使用 WiFi.disconnect(false); 解决了它 Wifi.enabeAP(假); 希望这可以帮助你
推荐阅读
- windows - 如何在 Windows 的 conda 环境中使用 setuptools 安装 python 命令行脚本?
- linux - DBD-Oracle-1.76 安装问题
- jquery - 如何在 attr 中连接字符串
- python-3.x - NameError:未定义名称“setBranchCourse”
- node.js - CircleCI:$ npm 测试失败并且找不到已安装的包
- http - 永久删除 url 的两部分
- php - 使用 laravel 外部存储路径计划 SQL 备份
- python - Pandas:如何查找来自另一列的一列的唯一元素数量?
- python - 如何计算两个日期/时间之间的实例数
- flask - 如何让scrapy spider在flask app表单提交上运行?