270 字
1 分钟
ESP8266 创建热点并网页端配置参数
具体功能
- ESP8266 开发板创建一个 Wifi 热点
- 任意终端连接该热点并访问
http://192.168.4.1即可访问网页 - 在网页中通过表单提交需要配置的参数
完整代码
/* Create a WiFi access point and provide a web server on it. *//* Just a little test message. Go to http://192.168.4.1 in a web browser connected to this access point to see it.*/
#include <ESP8266WiFi.h>#include <ESP8266WebServer.h>
#define APSSID "ESPap"#define APPSK "thereisnospoon"
/* Set these to your desired credentials. */const char *ssid = APSSID;const char *password = APPSK;
ESP8266WebServer server(80);
const String postForms = "<html>\ <head>\ <title>ESP8266 Web Server POST handling</title>\ <style>\ body { font-family: Arial, Helvetica, Sans-Serif; }\ </style>\ </head>\ <body>\ <h2>Configuration</h2><br>\ <form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/save_parameters/\">\ <span>Wifi client SSID</span><br>\ <input type=\"text\" name=\"ssid\" value=\"ssid\"><br>\ <span>Wifi client password</span><br>\ <input type=\"text\" name=\"passowrd\" value=\"password\"><br>\ <input type=\"submit\" value=\"Save parameters\">\ </form>\ </body>\</html>";
void handleRoot() { server.send(200, "text/html", postForms);}
void handleSaveParameters() { if (server.method() != HTTP_POST) { return; }
String message = "POST form was:\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(200, "text/plain", message);}
void setup() { delay(1000); Serial.begin(115200); // Serial.println(); Serial.println("Configuring access point..."); /* You can remove the password parameter if you want the AP to be open. */ WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP);
server.on("/", handleRoot); server.on("/save_parameters/", handleSaveParameters);
server.begin(); Serial.println("HTTP server started");}
void loop() { server.handleClient();} ESP8266 创建热点并网页端配置参数
https://fuwari.vercel.app/posts/嵌入式/esp/esp8266-创建热点并网页端配置参数/