WiFi怎么从AP模式切换到STA?
打算程序开始的时候先使用AP模式,然后通过手机链接到ESP8266 WIFI,在通过web来配置STA要链接的账户密码,配置完成后,在重新mode的过程中好像出错了。发现一直连不上。
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <string.h>
#define AP_SSID"esp8266-nl9001"
#define AP_PASS"85913323"
#define ROOT_HTML"<!DOCTYPE html><html><head><title>WIFI Config</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><style type=\"text/css\">.input{display: block; margin-top: 10px;}.input span{width: 100px; float: left; float: left; height: 36px; line-height: 36px;}.input input{height: 30px;width: 200px;}.btn{width: 120px; height: 35px; background-color: #000000; border:0px; color:#ffffff; margin-top:15px; margin-left:100px;}</style><body><form method=\"GET\" action=\"connect\"><label class=\"input\"><span>WiFi SSID</span><input type=\"text\" name=\"ssid\"></label><label class=\"input\"><span>WiFi PASS</span><input type=\"text\"name=\"pass\"></label><input class=\"btn\" type=\"submit\" name=\"submit\" value=\"Submie\"></form></body></html>"
ESP8266WebServer server(80);
void setup() {
WiFi.mode(WIFI_AP);
Serial.begin(115200);
boolean result = WiFi.softAP(AP_SSID,AP_PASS);
if(result)
{
IPAddress myIP = WiFi.softAPIP();
Serial.println("");
Serial.print("Soft-AP IP address = ");
Serial.println(myIP);
Serial.println(String("MAC address = ")+ WiFi.softAPmacAddress().c_str());
}else{
Serial.println("WiFiAP Failed");
}
//首页
server.on("/", [](){
server.send(200,"text/html",ROOT_HTML);
});
//链接
server.on("/connect", [](){
WiFi.softAPdisconnect(true);
String ssid = server.arg("ssid");
String pass = server.arg("pass");
Serial.println("WiFi Connect SSID:"+ssid+"PASS:"+pass);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.send(200,"text/html","successd,conning...");
});
server.begin();
// put your setup code here, to run once:
}
void loop() {
server.handleClient();
// put your main code here, to run repeatedly:
}
你要把wifi的账号密码写在EEPROM里面去。因为当前状态只会有一个模式,要重启才能切换模式,可是你现在的这样写,wifi的账号密码重启就会丢失。所以要写去EEPROM里
页:
[1]