http://www.javashuo.com/article/p-oozllcrf-hp.htmlhtml
文件系統git
https://blog.csdn.net/solar_Lan/article/details/74231360web
學習的網絡知識ajax
http://www.runoob.com/ajax/ajax-examples.html網絡
#include <ESP8266WiFi.h> /*** 該工程能夠在2.4.0版本esp8266庫中運行,沒在更高版本庫中進行測試 ***/ const char *ssid = "HUAWEI-H3VBKZ"; const char *password = "13991320168"; WiFiServer server(80); String readString = ""; //創建一個字符串對象用來接收存放來自客戶的數據 //響應頭 String responseHeaders = String("") + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Connection: close\r\n" + "\r\n"; //網頁 String myhtmlPage = String("") + "<html>" + "<head>" + " <title>ESP8266 Web Server Test</title>" + " <script defer=\"defer\">" + " function ledSwitch() {" + " var xmlhttp;" + " if (window.XMLHttpRequest) {" + " xmlhttp = new XMLHttpRequest();" + " } else {" + " xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");" + " }" + " xmlhttp.onreadystatechange = function () {" + " if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {" + " document.getElementById(\"txtState\").innerHTML = xmlhttp.responseText;" + " }" + " }," + " xmlhttp.open(\"GET\", \"Switch\", true);" + " xmlhttp.send(); " + " }" + " </script>" + "</head>" + "<body>" + " <div id=\"txtState\">Unkwon</div>" + " <input type=\"button\" value=\"Switch\" onclick=\"ledSwitch()\">" + "</body>" + "</html>"; bool isLedTurnOpen = false; // 記錄LED狀態 void setup() { pinMode(D4, OUTPUT); digitalWrite(D4, HIGH); // 熄滅LED Serial.begin(115200); Serial.println(); Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" connected"); server.begin(); Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); } void loop() { WiFiClient client = server.available(); //嘗試創建客戶對象 if (client) //若是當前有客戶可用 { boolean currentLineIsBlank = true; Serial.println("[Client connected]"); while (client.connected()) //若是客戶端創建鏈接 { if (client.available()) //等待有可讀數據 { char c = client.read(); //讀取一字節數據 readString += c; //拼接數據 /************************************************/ if (c == '\n' && currentLineIsBlank) //等待請求頭接收完成(接收到空行) { //比較接收到的請求數據 if (readString.startsWith("GET / HTTP/1.1")) //若是是網頁請求 { client.print(responseHeaders); //向客戶端輸出網頁響應 client.print(myhtmlPage); //向客戶端輸出網頁內容 client.print("\r\n"); } else if (readString.startsWith("GET /Switch")) //若是是改變LED狀態請求 { if (isLedTurnOpen == false) { digitalWrite(D4, LOW); // 點亮LED client.print("LED has been turn on"); isLedTurnOpen = true; } else { digitalWrite(D4, HIGH); // 熄滅LED client.print("LED has been turn off"); isLedTurnOpen = false; } } else { client.print("\r\n"); } break; } if (c == '\n') { currentLineIsBlank = true; //開始新行 } else if (c != '\r') { currentLineIsBlank = false; //正在接收某行中 } /************************************************/ } } delay(1); //等待客戶完成接收 client.stop(); //結束當前鏈接: Serial.println("[Client disconnected]"); Serial.println(readString); //打印輸出來自客戶的數據 readString = ""; } }
改進密碼登陸模式oop
#include <ESP8266WiFi.h> /*** 該工程能夠在2.4.0版本esp8266庫中運行,沒在更高版本庫中進行測試 ***/ const char *ssid = "HUAWEI-H3VBKZ"; const char *password = "13991320168"; WiFiServer server(80); String readString = ""; //創建一個字符串對象用來接收存放來自客戶的數據 //響應頭 String responseHeaders = String("") + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Connection: close\r\n" + "\r\n"; //網頁 String myhtmlPage= String("") + "<html>" + "<head>" + "<meta charset=\"utf-8\">"+ " <title>ESP8266 配置信息</title>" + " <script defer=\"defer\">" + " function ledSwitch() {" + " var name = document.getElementById(\"wifiname\").value;"+ " var psw = document.getElementById(\"wifipwd\").value;"+ " var xmlhttp;" + " if (window.XMLHttpRequest) {" + " xmlhttp = new XMLHttpRequest();" + " } else {" + " xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");" + " }" + " xmlhttp.onreadystatechange = function () {" + " if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {" + " document.getElementById(\"txtState\").innerHTML = xmlhttp.responseText;" + " }" + " }," + " xmlhttp.open(\"GET\", \"Switch\"+name+psw, true);" + " xmlhttp.send(); " + " }" + " </script>" + "</head>" + "<body>" "<h3>鏈接WIFI:</h3>"+ "<form action=\"\"> "+ "WIFI帳號: <input type=\"text\" id=\"wifiname\" />"+ "</form>"+ "<form action=\"\"> "+ "WIFI密碼: <input type=\"text\" id=\"wifipwd\" />"+ "</form>"+ "<button type=\"button\" onclick=\"ledSwitch()\"> 鏈接 </button>"+ "<p>狀態消息: <span id=\"txtState\"></span></p> "+ "</body>" + "</html>"; bool isLedTurnOpen = false; // 記錄LED狀態 void setup() { pinMode(D4, OUTPUT); digitalWrite(D4, HIGH); // 熄滅LED Serial.begin(115200); Serial.println(); Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" connected"); server.begin(); Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); } void loop() { WiFiClient client = server.available(); //嘗試創建客戶對象 if (client) //若是當前有客戶可用 { boolean currentLineIsBlank = true; Serial.println("[Client connected]"); while (client.connected()) //若是客戶端創建鏈接 { if (client.available()) //等待有可讀數據 { char c = client.read(); //讀取一字節數據 readString += c; //拼接數據 /************************************************/ if (c == '\n' && currentLineIsBlank) //等待請求頭接收完成(接收到空行) { //比較接收到的請求數據 if (readString.startsWith("GET / HTTP/1.1")) //若是是網頁請求 { client.print(responseHeaders); //向客戶端輸出網頁響應 client.print(myhtmlPage); //向客戶端輸出網頁內容 client.print("\r\n"); } else if (readString.startsWith("GET /Switch")) //若是是改變LED狀態請求 { if (isLedTurnOpen == false) { digitalWrite(D4, LOW); // 點亮LED client.print("LED has been turn on"); isLedTurnOpen = true; } else { digitalWrite(D4, HIGH); // 熄滅LED client.print("LED has been turn off"); isLedTurnOpen = false; } } else { client.print("\r\n"); } break; } if (c == '\n') { currentLineIsBlank = true; //開始新行 } else if (c != '\r') { currentLineIsBlank = false; //正在接收某行中 } /************************************************/ } } delay(1); //等待客戶完成接收 client.stop(); //結束當前鏈接: Serial.println("[Client disconnected]"); Serial.println(readString); //打印輸出來自客戶的數據 readString = ""; } }