配网功能简化

This commit is contained in:
kerwincui
2022-07-22 23:26:50 +08:00
parent a7b9b1e0c4
commit 1ee43b0a87

View File

@@ -2,7 +2,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
const char *ap_ssid = "wumei_smart"; const char *ap_ssid = "wumei-device6";
const char *ap_password = ""; //开放式网络 const char *ap_password = ""; //开放式网络
char sta_ssid[32] = {0}; char sta_ssid[32] = {0};
@@ -15,8 +15,8 @@ IPAddress subnet(255, 255, 255, 0);
void initApConfig(); void initApConfig();
void initWebServer(); void initWebServer();
void handleGet(); void handleConfig();
void handlePost(); void handleStatus();
void handleNotFound(); void handleNotFound();
ESP8266WebServer server(80); ESP8266WebServer server(80);
@@ -53,8 +53,8 @@ void initApConfig()
*/ */
void initWebServer() void initWebServer()
{ {
server.on("/", HTTP_GET, handleGet); server.on("/status", HTTP_GET, handleStatus);
server.on("/", HTTP_POST, handlePost); server.on("/config", HTTP_POST, handleConfig);
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
server.enableCORS(true); server.enableCORS(true);
server.begin(); server.begin();
@@ -62,15 +62,10 @@ void initWebServer()
} }
/** /**
* 处理配网状态请求 * 连接WIFI
*/ */
void handleGet() void connectWifi()
{ {
printMsg("获取网络状态");
if (server.hasArg("deviceNum"))
{
printMsg("收到设备编号:" + server.arg("deviceNum"));
}
printMsg("连接WIFI"); printMsg("连接WIFI");
WiFi.begin(sta_ssid, sta_password); WiFi.begin(sta_ssid, sta_password);
int cnt = 0; int cnt = 0;
@@ -81,24 +76,26 @@ void handleGet()
Serial.print("."); Serial.print(".");
if (cnt >= 30) if (cnt >= 30)
{ {
printMsg("设备连接WIFI超时进入到AP配网模式!"); printMsg("设备连接WIFI超时请重新配网");
server.send(500, "text/plain;charset=utf-8", "设备连接WIFI超时配网失败"); return;
initApConfig();
break;
} }
} }
if(WiFi.status() == WL_CONNECTED){
server.send(200, "text/plain;charset=utf-8", "设备已连接WIFI配网成功");
server.close(); server.close();
WiFi.softAPdisconnect(false); WiFi.softAPdisconnect(false);
printMsg("Http服务和热点已关闭设备已连接WIFI,配网成功"); printMsg("Http服务和热点已关闭设备已连接WIFI");
}
} }
/** /**
* 处理配网Post请求 * 检测设备状态
*/ */
void handlePost() void handleStatus(){
server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪");
}
/**
* 配网:下发配置信息
*/
void handleConfig()
{ {
printMsg("进入配网......"); printMsg("进入配网......");
// wifi名称、wifi密码、用户名 // wifi名称、wifi密码、用户名
@@ -122,16 +119,14 @@ void handlePost()
{ {
printMsg("收到设备编号:" + server.arg("deviceNum")); printMsg("收到设备编号:" + server.arg("deviceNum"));
} }
if (server.hasArg("deviceName"))
{
printMsg("收到设备名称:" + server.arg("deviceName"));
}
if (server.hasArg("extra")) if (server.hasArg("extra"))
{ {
printMsg("收到补充信息:" + server.arg("extra")); printMsg("收到补充信息:" + server.arg("extra"));
} }
// TODO 可增加设备连接WIFI测试
server.send(200, "text/plain;charset=utf-8", "设备已完成配置连接Wifi中..."); server.send(200, "text/plain;charset=utf-8", "设备已更新WIFI配置,开始连接WIFI...");
connectWifi();
} }
void handleNotFound() void handleNotFound()