设备配网改进

This commit is contained in:
kerwincui
2022-07-20 00:19:05 +08:00
parent bd9beba488
commit a7b9b1e0c4

View File

@@ -2,176 +2,160 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#define DEBUG const char *ap_ssid = "wumei_smart";
const char *ap_password = ""; //开放式网络
#ifdef DEBUG
//以下三个定义为调试定义
#define DebugBegin(baud_rate) Serial.begin(baud_rate)
#define DebugPrintln(message) Serial.println(message)
#define DebugPrint(message) Serial.print(message)
#else
//以下三个定义为调试定义
#define DebugBegin(baud_rate)
#define DebugPrintln(message)
#define DebugPrint(message)
#endif
const char* ap_ssid = "esp_webconfig";
const char* ap_password = "";//开放式网络
char sta_ssid[32] = {0}; char sta_ssid[32] = {0};
char sta_password[64] = {0}; char sta_password[64] = {0};
char sta_user_name[64] = {0};
const char* webpage_html = "\ IPAddress local_IP(192, 168, 4, 1);
<!DOCTYPE html>\r\n\ IPAddress gateway(192, 168, 4, 1);
<html lang='en'>\r\n\ IPAddress subnet(255, 255, 255, 0);
<head>\r\n\
<meta charset='UTF-8'>\r\n\
<title>Document</title>\r\n\
</head>\r\n\
<body>\r\n\
<form name='input' action='/' method='POST'>\r\n\
wifi名称: <br>\r\n\
<input type='text' name='ssid'><br>\r\n\
wifi密码:<br>\r\n\
<input type='text' name='password'><br>\r\n\
<input type='submit' value='保存'>\r\n\
</form>\r\n\
</body>\r\n\
</html>\r\n\
";
IPAddress local_IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
void initApConfig(); void initApConfig();
void initWebServer(); void initWebServer();
void connectToWifi(); void handleGet();
void handleRootPost(); void handlePost();
void handleRoot();
void handleNotFound(); void handleNotFound();
ESP8266WebServer server(80); ESP8266WebServer server(80);
void setup(void) { void setup(void)
DebugBegin(115200); {
DebugPrintln(""); //打开串行端口:
DebugPrint("connect ap: "); Serial.begin(115200);
DebugPrintln(ap_ssid); // AP模式
initApConfig(); initApConfig();
// web服务
DebugPrint("IP address: ");
DebugPrintln(WiFi.softAPIP());
initWebServer(); initWebServer();
DebugPrintln("HTTP server started");
Serial.printf("Ready! Open http://%s in your browser\n",
WiFi.softAPIP().toString().c_str());
} }
void loop(void) { void loop(void)
{
// Web服务端
server.handleClient(); server.handleClient();
} }
/** /**
* 初始化AP配置 * AP模式
*/ */
void initApConfig(){ void initApConfig()
WiFi.mode(WIFI_AP); {
WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(local_IP, gateway, subnet); WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(ap_ssid, ap_password); WiFi.softAP(ap_ssid, ap_password);
printMsg("已启动AP配网IP地址" + WiFi.softAPIP().toString());
} }
/** /**
* 初始化webserver配置 * 初始化webserver配置
*/ */
void initWebServer(){ void initWebServer()
server.on("/", HTTP_GET, handleRoot); {
server.on("/", HTTP_POST, handleRootPost); server.on("/", HTTP_GET, handleGet);
server.on("/", HTTP_POST, handlePost);
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
server.enableCORS(true); server.enableCORS(true);
server.begin(); server.begin();
printMsg("HTTP服务已启动");
} }
/** /**
* 连接到WiFi * 处理配网状态请求
*/ */
void connectToWifi(){ void handleGet()
DebugPrintln("connectToWifi"); {
WiFi.disconnect(); printMsg("获取网络状态");
WiFi.mode(WIFI_STA); if (server.hasArg("deviceNum"))
{
printMsg("收到设备编号:" + server.arg("deviceNum"));
}
printMsg("连接WIFI");
WiFi.begin(sta_ssid, sta_password); WiFi.begin(sta_ssid, sta_password);
int cnt = 0; int cnt = 0;
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED)
delay(500); {
cnt++; delay(500);
Serial.print("."); cnt++;
if(cnt>=40){ Serial.print(".");
cnt = 0; if (cnt >= 30)
//重启系统 {
DebugPrintln("\r\nRestart now!"); printMsg("设备连接WIFI超时进入到AP配网模式!");
ESP.restart(); server.send(500, "text/plain;charset=utf-8", "设备连接WIFI超时配网失败");
} initApConfig();
break;
}
}
if(WiFi.status() == WL_CONNECTED){
server.send(200, "text/plain;charset=utf-8", "设备已连接WIFI配网成功");
server.close();
WiFi.softAPdisconnect(false);
printMsg("Http服务和热点已关闭设备已连接WIFI配网成功");
} }
DebugPrintln("connectToWifi Success!");
} }
/** /**
* 处理web post请求 * 处理配网Post请求
*/ */
void handleRootPost() { void handlePost()
DebugPrintln("handleRootPost"); {
if (server.hasArg("ssid")) { printMsg("进入配网......");
DebugPrint("got ssid:"); // wifi名称、wifi密码、用户名
strcpy(sta_ssid, server.arg("ssid").c_str()); if (server.hasArg("SSID") && server.hasArg("password") && server.hasArg("userName"))
DebugPrintln(sta_ssid); {
} else { strcpy(sta_ssid, server.arg("SSID").c_str());
DebugPrintln("error, not found ssid");
server.send(200, "text/plain", "error, not found ssid");
return;
}
if (server.hasArg("password")) {
DebugPrint("got password:");
strcpy(sta_password, server.arg("password").c_str()); strcpy(sta_password, server.arg("password").c_str());
DebugPrintln(sta_password); strcpy(sta_user_name, server.arg("userName").c_str());
} else { printMsg("收到WIFI名称" + (String)sta_ssid);
DebugPrintln("error, not found password"); printMsg("收到WIFI密码" + (String)sta_password);
server.send(200, "text/plain", "error, not found password"); printMsg("收到用户名称:" + (String)sta_user_name);
}
else
{
printMsg("配网必须传递用户名、WIFI名称和WIFI密码,配网失败");
server.send(500, "text/plain;charset=utf-8", "配网必须传递用户名、WIFI名称和WIFI密码配网失败");
return; return;
} }
// 可选字段
if (server.hasArg("deviceNum"))
{
printMsg("收到设备编号:" + server.arg("deviceNum"));
}
if (server.hasArg("deviceName"))
{
printMsg("收到设备名称:" + server.arg("deviceName"));
}
if (server.hasArg("extra"))
{
printMsg("收到补充信息:" + server.arg("extra"));
}
server.send(200, "text/plain", "保存成功"); server.send(200, "text/plain;charset=utf-8", "设备已完成配置连接Wifi中...");
delay(2000);
//连接wifi
connectToWifi();
} }
/** void handleNotFound()
* 处理web get请求 {
*/ printMsg("进入预检请求或请求地址找不到");
void handleRoot() {
DebugPrintln("handleRoot");
server.send(200, "text/html", webpage_html);
}
void handleNotFound() {
if (server.method() == HTTP_OPTIONS) if (server.method() == HTTP_OPTIONS)
{ {
server.sendHeader("Access-Control-Max-Age", "10000"); // 处理浏览器跨域问题
server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS"); server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Headers", "*"); server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
server.send(204); server.sendHeader("Access-Control-Allow-Headers", "*");
} server.send(204);
else }
{ else
server.send(404, "text/plain", ""); {
} server.send(404, "text/plain;charset=utf-8", "请求的地址找不到或无法访问");
String message = "File Not Found\n\n"; }
server.send(404, "text/plain", message); }
//打印提示信息
void printMsg(String msg)
{
Serial.print("\r\n[");
Serial.print(millis());
Serial.print("ms]");
Serial.print(msg);
} }