前言
尝试使用esp8266去创建一个服务器以及实现对开发版的基本控制
本篇文章参考自太极创客的相关文章,作为博主笔记使用
其他教程
3-2-1 建立基本网络服务器
3-2-2 通过网络服务实现NodeMCU开发板基本控制
3-2-3 通过网络服务将开发板引脚状态显示在网页中
【ESP8266教程】零基础入门学用物联网-基础知识篇(太极创客团队)
内容
建立网络基本服务器的示例代码
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
|
#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"); }
|
通过网络服务器控制Esp8266示例代码
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
|
#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"); }
|
代码示例讲解
esp8266_server.on("/", HTTP_GET, handleRoot)
当服务器访问/
目录时,且访问方式为GET模式,则使用handleRoot
函数对其请求进行处理
esp8266_server.sendHeader("Location","/")
发送一个头,位置是/
通过网络服务显示端口状态示例代码
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
|
#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"); }
|
代码讲解
digitalRead(buttonPin)
获取buttonPin
的当前引脚状态