风轻云淡愿君赏,消尽烦忧心亮敞。夏去秋来添衣裳,一声问候祝安康!

Wemos D1 WIFI配网

实现步骤:1 开机创建AP_clock 192.168.1.5

     2 手机打开网页输入网络与密码

     3 联接成功,关闭AP模式

  1 /*
  2  *  This sketch demonstrates how to scan WiFi networks. 
  3  *  The API is almost the same as with the WiFi Shield library, 
  4  *  the most obvious difference being the different file you need to include:
  5  */
  6 #include "ESP8266WiFi.h"
  7 #include <ESP8266WebServer.h>
  8 #include <ESP8266mDNS.h>
  9 ESP8266WebServer server(80);
 10 const char* Mssid="AP_clock";
 11 String HTML_TITLE = "<!DOCTYPE html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>ESP8266网页配网</title>";
 12 String HTML_SCRIPT_ONE = "<script type=\"text/javascript\">function wifi(){var ssid = s.value;var password = p.value;var xmlhttp=new XMLHttpRequest();xmlhttp.open(\"GET\",\"/HandleWifi?ssid=\"+ssid+\"&password=\"+password,true);xmlhttp.send();xmlhttp.onload = function(e){alert(this.responseText);}}</script>";
 13 String HTML_SCRIPT_TWO = "<script>function c(l){document.getElementById('s').value=l.innerText||l.textContent;document.getElementById('p').focus();}</script>";
 14 String HTML_HEAD_BODY_BEGIN = "</head><body>请输入wifi信息进行配网:";
 15 String HTML_FORM_ONE = "<form>WiFi名称:<input id='s' name='s' type=\"text\" placeholder=\"请输入您WiFi的名称\"><br>WiFi密码:<input id='p' name='p' type=\"text\" placeholder=\"请输入您WiFi的密码\"><br><input type=\"button\" value=\"扫描\" onclick=\"window.location.href = '/HandleScanWifi'\"><input type=\"button\" value=\"连接\" onclick=\"wifi()\"></form>";
 16 String HTML_BODY_HTML_END = "</body></html>";
 17 
 18 void HandleScanWifi() {
 19    Serial.println("scan start");
 20   String HTML_FORM_TABLE_BEGIN = "<table><head><tr><th>序号</th><th>名称</th><th>强度</th></tr></head><body>";
 21     String HTML_FORM_TABLE_END = "</body></table>";
 22     String HTML_FORM_TABLE_CON = "";
 23     String HTML_TABLE;
 24   // WiFi.scanNetworks will return the number of networks found
 25   int n = WiFi.scanNetworks();
 26   Serial.println("scan done");
 27   if (n == 0)
 28     Serial.println("no networks found");
 29   else
 30   {
 31     Serial.print(n);
 32     Serial.println(" networks found");
 33     for (int i = 0; i < n; ++i)
 34     {
 35       // Print SSID and RSSI for each network found
 36       Serial.print(i + 1);
 37       Serial.print(": ");
 38       Serial.print(WiFi.SSID(i));
 39       Serial.print(" (");
 40       Serial.print(WiFi.RSSI(i));
 41       Serial.print(")");
 42       Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
 43       delay(10);
 44        HTML_FORM_TABLE_CON = HTML_FORM_TABLE_CON + "<tr><td align=\"center\">" + String(i+1) + "</td><td align=\"center\">" + "<a href='#p' onclick='c(this)'>" + WiFi.SSID(i) + "</a>" + "</td><td align=\"center\">" + WiFi.RSSI(i) + "</td></tr>";
 45     }
 46       HTML_TABLE = HTML_FORM_TABLE_BEGIN + HTML_FORM_TABLE_CON + HTML_FORM_TABLE_END;
 47   }
 48     String scanstr = HTML_TITLE + HTML_SCRIPT_ONE + HTML_SCRIPT_TWO + HTML_HEAD_BODY_BEGIN + HTML_FORM_ONE + HTML_TABLE + HTML_BODY_HTML_END;
 49  server.send(200, "text/html", scanstr);
 50  //Serial.println(scanstr);
 51 }
 52 void handleRoot() {
 53     Serial.println("Client is  Connected!");
 54     String str = HTML_TITLE + HTML_SCRIPT_ONE + HTML_SCRIPT_TWO + HTML_HEAD_BODY_BEGIN + HTML_FORM_ONE + HTML_BODY_HTML_END;
 55     server.send(200, "text/html", str);
 56 }
 57 void handleNotFound() { 
 58   String message = "File Not Found\n\n";
 59   message += "URI: ";
 60   message += server.uri();
 61   message += "\nMethod: ";
 62   message += (server.method() == HTTP_GET) ? "GET" : "POST";
 63   message += "\nArguments: ";
 64   message += server.args();
 65   message += "\n";
 66   for (uint8_t i = 0; i < server.args(); i++) {
 67     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
 68   }
 69   server.send(404, "text/plain", message);
 70 }
 71 void HandleWifi()
 72 {
 73     String wifis = server.arg("ssid"); //从JavaScript发送的数据中找ssid的值
 74     String wifip = server.arg("password"); //从JavaScript发送的数据中找password的值
 75     Serial.println("received:"+wifis);
 76     server.send(200, "text/html", "连接中..");
 77     const char* ssid = wifis.c_str();//String转char
 78     const char* password = wifip.c_str();
 79     WiFi.begin(ssid,password);
 80 }
 81 
 82 void htmlConfig()
 83 {
 84     WiFi.mode(WIFI_AP_STA);//设置模式为AP+STA
 85      WiFi.softAP(Mssid);
 86     IPAddress softLocal(192,168,5,1);           //IP地址,用以设置IP第4字段
 87     IPAddress softGateway(192,168,5,1);         //IP网关,用以设置IP第3字段
 88     IPAddress softSubnet(255,255,255,0);
 89     //配置自定义的IP信息
 90     WiFi.softAPConfig(softLocal,softGateway, softSubnet);  
 91     WiFi.disconnect();
 92     IPAddress myIP = WiFi.softAPIP();
 93     if (MDNS.begin("clock")) {
 94       Serial.println("MDNS responder started");
 95     }
 96     
 97     server.on("/", handleRoot);
 98     server.on("/HandleWifi", HTTP_GET, HandleWifi);
 99     server.on("/HandleScanWifi", HandleScanWifi);
100     server.onNotFound(handleNotFound);//请求失败回调函数
101     MDNS.addService("http", "tcp", 80);
102     server.begin();//开启服务器
103     Serial.print("HTTP server started IP:");
104      Serial.println(myIP);
105     int counter = 0;
106     while(1)
107     {
108         server.handleClient();
109         MDNS.update();  
110          delay(500);
111          counter++;
112         if (WiFi.status() == WL_CONNECTED)
113         {
114             Serial.println("HtmlConfig Success");
115             Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
116             //Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
117             Serial.println("HTML已经连接");
118             break;
119         }
120     }
121        WiFi.mode(WIFI_STA);//关闭AP模式
122     
123 }
124 void setup() {
125   Serial.begin(115200);
126   // Set WiFi to station mode and disconnect from an AP if it was previously connected
127   delay(100);
128   htmlConfig();
129   
130   Serial.println("Setup done");
131 }
132 void loop() {
133   delay(5000);
134 }

 参考:https://github.com/yuan910715/Esp8266_NTP_Clock_Weather

posted on 2021-08-14 12:17  荣林  阅读(408)  评论(0编辑  收藏  举报

导航