From bd9beba4887655904390d8da2d44e1b6c6da5f76 Mon Sep 17 00:00:00 2001 From: kerwincui <164770707@qq.com> Date: Fri, 15 Jul 2022 23:20:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=91sdk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/Arduino/ApConfig/ApConfig.ino | 177 ++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 sdk/Arduino/ApConfig/ApConfig.ino diff --git a/sdk/Arduino/ApConfig/ApConfig.ino b/sdk/Arduino/ApConfig/ApConfig.ino new file mode 100644 index 00000000..c05e6805 --- /dev/null +++ b/sdk/Arduino/ApConfig/ApConfig.ino @@ -0,0 +1,177 @@ + +#include +#include + +#define DEBUG + +#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_password[64] = {0}; + +const char* webpage_html = "\ +\r\n\ +\r\n\ +\r\n\ + \r\n\ + Document\r\n\ +\r\n\ +\r\n\ +
\r\n\ + wifi名称:
\r\n\ +
\r\n\ + wifi密码:
\r\n\ +
\r\n\ + \r\n\ +
\r\n\ +\r\n\ +\r\n\ +"; + +IPAddress local_IP(192,168,4,1); +IPAddress gateway(192,168,4,1); +IPAddress subnet(255,255,255,0); + +void initApConfig(); +void initWebServer(); +void connectToWifi(); +void handleRootPost(); +void handleRoot(); +void handleNotFound(); + +ESP8266WebServer server(80); + +void setup(void) { + DebugBegin(115200); + DebugPrintln(""); + DebugPrint("connect ap: "); + DebugPrintln(ap_ssid); + + initApConfig(); + + DebugPrint("IP address: "); + DebugPrintln(WiFi.softAPIP()); + + initWebServer(); + + DebugPrintln("HTTP server started"); + + Serial.printf("Ready! Open http://%s in your browser\n", + WiFi.softAPIP().toString().c_str()); +} + +void loop(void) { + server.handleClient(); +} + +/** + * 初始化AP配置 + */ +void initApConfig(){ + WiFi.mode(WIFI_AP); + WiFi.softAPConfig(local_IP, gateway, subnet); + WiFi.softAP(ap_ssid, ap_password); +} + +/** + * 初始化webserver配置 + */ +void initWebServer(){ + server.on("/", HTTP_GET, handleRoot); + server.on("/", HTTP_POST, handleRootPost); + server.onNotFound(handleNotFound); + server.enableCORS(true); + server.begin(); +} + +/** + * 连接到WiFi + */ +void connectToWifi(){ + DebugPrintln("connectToWifi"); + WiFi.disconnect(); + WiFi.mode(WIFI_STA); + WiFi.begin(sta_ssid, sta_password); + + int cnt = 0; + while (WiFi.status() != WL_CONNECTED) { + delay(500); + cnt++; + Serial.print("."); + if(cnt>=40){ + cnt = 0; + //重启系统 + DebugPrintln("\r\nRestart now!"); + ESP.restart(); + } + } + DebugPrintln("connectToWifi Success!"); +} + +/** + * 处理web post请求 + */ +void handleRootPost() { + DebugPrintln("handleRootPost"); + if (server.hasArg("ssid")) { + DebugPrint("got ssid:"); + strcpy(sta_ssid, server.arg("ssid").c_str()); + DebugPrintln(sta_ssid); + } else { + 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()); + DebugPrintln(sta_password); + } else { + DebugPrintln("error, not found password"); + server.send(200, "text/plain", "error, not found password"); + return; + } + + server.send(200, "text/plain", "保存成功"); + delay(2000); + //连接wifi + connectToWifi(); +} + +/** + * 处理web get请求 + */ +void handleRoot() { + DebugPrintln("handleRoot"); + server.send(200, "text/html", webpage_html); +} + +void handleNotFound() { + 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-Allow-Headers", "*"); + server.send(204); + } + else + { + server.send(404, "text/plain", ""); + } + String message = "File Not Found\n\n"; + server.send(404, "text/plain", message); +}