ESP8266-NodeMCU网服务器 建立基本网络服务器 建立基本网络服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266WebServer.h> ESP8266WiFiMulti wifiMulti; ESP8266WebServer esp8266_server (80 ) ; void setup (void ) { Serial.begin(9600 ); wifiMulti.addAP("taichi-maker" , "12345678" ); wifiMulti.addAP("taichi-maker2" , "87654321" ); wifiMulti.addAP("taichi-maker3" , "13572468" ); int i = 0 ; while (wifiMulti.run() != WL_CONNECTED) { delay(1000 ); Serial.print(i++); Serial.print(' ' ); } Serial.println('\n' ); Serial.print("Connected to " ); Serial.println(WiFi.SSID()); Serial.print("IP address:\t" ); Serial.println(WiFi.localIP()); esp8266_server.begin(); esp8266_server.on("/" , handleRoot); esp8266_server.onNotFound(handleNotFound); Serial.println("HTTP esp8266_server started" ); } void loop (void ) { esp8266_server.handleClient(); } void handleRoot () { esp8266_server.send(200 , "text/plain" , "Hello from ESP8266" ); } void handleNotFound () { esp8266_server.send(404 , "text/plain" , "404: Not found" ); }
当这段示例程序上传给NodeMCU以后,启动NodeMCU并且确保它已经成功连接WiFi。接下来请打开浏览器,并且在地址栏中输入NodeMCU的IP地址并按下回车。假如将在浏览器中看到“Hello from ESP8266”,那么恭喜你已经成功的让NodeMCU实现了网络服务功能,因为你所看到的这条文字信息正是来自于NodeMCU。换句话说,NodeMCU为你建立了一个超级迷你的小网站。这个网站只有一个网页。这个网页只有一行文字“Hello from ESP8266”。
通过网络服务实现NodeMCU开发板基本控制 通过网络服务实现NodeMCU开发板基本控制
在网页中控制nodemcu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266WebServer.h> ESP8266WiFiMulti wifiMulti; ESP8266WebServer esp8266_server (80 ) ; void setup (void ) { Serial.begin(9600 ); pinMode(LED_BUILTIN, OUTPUT); wifiMulti.addAP("ssid_from_AP_1" , "your_password_for_AP_1" ); wifiMulti.addAP("ssid_from_AP_2" , "your_password_for_AP_2" ); wifiMulti.addAP("ssid_from_AP_3" , "your_password_for_AP_3" ); Serial.println("Connecting ..." ); int i = 0 ; while (wifiMulti.run() != WL_CONNECTED) { delay(1000 ); Serial.print(i++); Serial.print(' ' ); } Serial.println('\n' ); Serial.print("Connected to " ); Serial.println(WiFi.SSID()); Serial.print("IP address:\t" ); Serial.println(WiFi.localIP()); esp8266_server.begin(); esp8266_server.on("/" , HTTP_GET, handleRoot); esp8266_server.on("/LED" , HTTP_POST, handleLED); esp8266_server.onNotFound(handleNotFound); Serial.println("HTTP esp8266_server started" ); } void loop (void ) { esp8266_server.handleClient(); } void handleRoot () { esp8266_server.send(200 , "text/html" , "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>" ); } void handleLED () { digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN)); esp8266_server.sendHeader("Location" ,"/" ); esp8266_server.send(303 ); } void handleNotFound () { esp8266_server.send(404 , "text/plain" , "404: Not found" ); }
通过网络服务实现NodeMCU开发板基本控制 我们可以通过NodeMCU开发板上的FLASH按键控制D3引脚的电平。当我们没有按下该按键时,D3引脚将会保持高电平状态。当按下该按键后,D3引脚会变为低电平。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 #include <ESP8266WiFi.h > #include <ESP8266WiFiMulti.h > #include <ESP8266WebServer.h > #define buttonPin D3 ESP8266WiFiMulti wifiMulti; ESP8266WebServer esp8266_server(80 ); bool pinState; void setup(){ Serial.begin (9600 ); pinMode(buttonPin, INPUT_PULLUP); wifiMulti.addAP ("ssid_from_AP_1" , "your_password_for_AP_1" ); wifiMulti.addAP ("ssid_from_AP_2" , "your_password_for_AP_2" ); wifiMulti.addAP ("ssid_from_AP_3" , "your_password_for_AP_3" ); Serial.println ("Connecting ..." ); int i = 0 ; while (wifiMulti.run () != WL_CONNECTED) { delay(1000 ); Serial.print (i++); Serial.print (' '); } Serial.println ('\n'); Serial.print ("Connected to " ); Serial.println (WiFi.SSID ()); Serial.print ("IP address:\t" ); Serial.println (WiFi.localIP ()); esp8266_server.begin (); esp8266_server.on ("/" , handleRoot); esp8266_server.onNotFound (handleNotFound); Serial.println ("HTTP esp8266_server started" ); } void loop(){ esp8266_server.handleClient (); pinState = digitalRead(buttonPin); } void handleRoot() { String displayPinState; if (pinState == HIGH){ displayPinState = "Button State: HIGH" ; } else { displayPinState = "Button State: LOW" ; } esp8266_server.send (200 , "text/plain" , displayPinState); } void handleNotFound(){ esp8266_server.send (404 , "text/plain" , "404: Not found" ); }
当然。也可以不用一种刷新网页,让其自动刷新。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266WebServer.h> #define buttonPin D3 ESP8266WiFiMulti wifiMulti; ESP8266WebServer esp8266_server (80 ) ; bool pinState; void setup () { Serial.begin(9600 ); delay(10 ); Serial.println("" ); pinMode(buttonPin, INPUT_PULLUP); wifiMulti.addAP("ssid_from_AP_1" , "your_password_for_AP_1" ); wifiMulti.addAP("ssid_from_AP_2" , "your_password_for_AP_2" ); wifiMulti.addAP("ssid_from_AP_3" , "your_password_for_AP_3" ); Serial.println("Connecting ..." ); int i = 0 ; while (wifiMulti.run() != WL_CONNECTED) { delay(1000 ); Serial.print(i++); Serial.print(' ' ); } Serial.println('\n' ); Serial.print("Connected to " ); Serial.println(WiFi.SSID()); Serial.print("IP address:\t" ); Serial.println(WiFi.localIP()); esp8266_server.begin(); esp8266_server.on("/" , handleRoot); esp8266_server.onNotFound(handleNotFound); Serial.println("HTTP esp8266_server started" ); } void loop () { esp8266_server.handleClient(); pinState = digitalRead(buttonPin); } void handleRoot () { esp8266_server.send(200 , "text/html" , sendHTML(pinState)); } String sendHTML (bool buttonState) { String htmlCode = "<!DOCTYPE html> <html>\n" ; htmlCode +="<head><meta http-equiv='refresh' content='5'/>\n" ; htmlCode +="<title>ESP8266 Butoon State</title>\n" ; htmlCode +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n" ; htmlCode +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n" ; htmlCode +="</style>\n" ; htmlCode +="</head>\n" ; htmlCode +="<body>\n" ; htmlCode +="<h1>ESP8266 BUTTON STATE</h1>\n" ; if (buttonState) {htmlCode +="<p>Button Status: HIGH</p>\n" ;} else {htmlCode +="<p>Button Status: LOW</p>\n" ;} htmlCode +="</body>\n" ; htmlCode +="</html>\n" ; return htmlCode; } void handleNotFound () { esp8266_server.send(404 , "text/plain" , "404: Not found" ); }