01 ESP32 Web Server
(2019/07/09)
昨天看到一篇文章是利用ESP32來架設Web Server,而且介紹的很仔細,應該照著做就可以完成,所以就照著做吧!由於這方面我也只一位入門者,只會照著做,把所做的記錄下來,這樣下次就不會忘記!參考資料內有詳細的說明,再等大家去尋寶!有興趣者再自己加深加廣吧!
一、參考資料:ESP32 Web Server – Arduino IDE
二、ESP32在Arduino IDE 的環境建置 (讓Arduino IDE認識ESP32板子)
開啟Arduino IDE
點選 檔案/偏好設定
將 https://dl.espressif.com/dl/package_esp32_index.json 放到【額外的開發板管理員網址】的空格下,由於我之前已安裝了ESP8266板子,所以空格內已有一串網址,因此按右邊的圖案,讓我們把上面網址加到第二列(如下圖),其實整個建置過程與安裝ESP8266的方式一樣!
點選 工具/開發板/開發板管理
安裝ESP32套件
先輸入ESP32來搜尋
安裝完成
三、接上 Web:bit (Bpi:bit)板子
點選 工具/開發板,找到BPI:BIT板子
點選 序列埠,找到連接BPI:BIT板子的埠 (要先安裝CH340的驅動程式)
四、認識Web:bit的腳位
到 【BPI-Bit STEAM 教育开发板】 的資料網頁,找到如下的資料
由上可知P0相當於GPIO25、P1相當於GPIO32、P0相當於GPIO33...
五、架設Web:bit的網頁伺服器(Web Server)
程式直接一字不漏的COPY參考資料的程式,如下,並修改一點點資料(紅色所示)
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// Load Wi-Fi library
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "CC1"; //更換為可連上網的SSID及密碼
const char* password = "075565940";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";
// Assign output variables to GPIO pins
const int output26 = 32; //改為 P1的腳位(GPIO32)
const int output27 = 33; //改為 P2的腳位(GPIO33)
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
將上面程式燒錄(上傳)到web:bit板子
開啟右上角的【序列埠監控視窗】
鮑速選115200
會顯示我們Web:bit的IP(待會連線用)
六、透過手機連上Web:bit的網頁伺服器(Web Server)
首先手機先透過WIFI連上同一個AP (同一個網段)
手機瀏覽器輸入 網址 (本例是192.168.2.113)
遠端控制亮滅燈
測試成功
由於我是一位入門者,只會照著做,把所做的記錄下來,這樣下次就不會忘記!參考資料內有詳細的說明,再等大家去尋寶!