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的腳位

  • 由上可知P0相當於GPIO25、P1相當於GPIO32、P0相當於GPIO33...

五、架設Web:bit的網頁伺服器(Web Server)

  • 程式直接一字不漏的COPY參考資料的程式,如下,並修改一點點資料(紅色所示)

      1. /*********

      2. Rui Santos

      3. Complete project details at https://randomnerdtutorials.com

      4. *********/

      5. // Load Wi-Fi library

      6. #include <WiFi.h>

      7. // Replace with your network credentials

    1. const char* ssid = "CC1"; //更換為可連上網的SSID及密碼

    2. const char* password = "075565940";

      1. // Set web server port number to 80

      2. WiFiServer server(80);

      3. // Variable to store the HTTP request

      4. String header;

      5. // Auxiliar variables to store the current output state

      6. String output26State = "off";

      7. String output27State = "off";

      8. // Assign output variables to GPIO pins

    3. const int output26 = 32; //改為 P1的腳位(GPIO32)

    4. const int output27 = 33; //改為 P2的腳位(GPIO33)

      1. void setup() {

      2. Serial.begin(115200);

      3. // Initialize the output variables as outputs

      4. pinMode(output26, OUTPUT);

      5. pinMode(output27, OUTPUT);

      6. // Set outputs to LOW

      7. digitalWrite(output26, LOW);

      8. digitalWrite(output27, LOW);

      9. // Connect to Wi-Fi network with SSID and password

      10. Serial.print("Connecting to ");

      11. Serial.println(ssid);

      12. WiFi.begin(ssid, password);

      13. while (WiFi.status() != WL_CONNECTED) {

      14. delay(500);

      15. Serial.print(".");

      16. }

      17. // Print local IP address and start web server

      18. Serial.println("");

      19. Serial.println("WiFi connected.");

      20. Serial.println("IP address: ");

      21. Serial.println(WiFi.localIP());

      22. server.begin();

      23. }

      24. void loop(){

      25. WiFiClient client = server.available(); // Listen for incoming clients

      26. if (client) { // If a new client connects,

      27. Serial.println("New Client."); // print a message out in the serial port

      28. String currentLine = ""; // make a String to hold incoming data from the client

      29. while (client.connected()) { // loop while the client's connected

      30. if (client.available()) { // if there's bytes to read from the client,

      31. char c = client.read(); // read a byte, then

      32. Serial.write(c); // print it out the serial monitor

      33. header += c;

      34. if (c == '\n') { // if the byte is a newline character

      35. // if the current line is blank, you got two newline characters in a row.

      36. // that's the end of the client HTTP request, so send a response:

      37. if (currentLine.length() == 0) {

      38. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)

      39. // and a content-type so the client knows what's coming, then a blank line:

      40. client.println("HTTP/1.1 200 OK");

      41. client.println("Content-type:text/html");

      42. client.println("Connection: close");

      43. client.println();

      44. // turns the GPIOs on and off

      45. if (header.indexOf("GET /26/on") >= 0) {

      46. Serial.println("GPIO 26 on");

      47. output26State = "on";

      48. digitalWrite(output26, HIGH);

      49. } else if (header.indexOf("GET /26/off") >= 0) {

      50. Serial.println("GPIO 26 off");

      51. output26State = "off";

      52. digitalWrite(output26, LOW);

      53. } else if (header.indexOf("GET /27/on") >= 0) {

      54. Serial.println("GPIO 27 on");

      55. output27State = "on";

      56. digitalWrite(output27, HIGH);

      57. } else if (header.indexOf("GET /27/off") >= 0) {

      58. Serial.println("GPIO 27 off");

      59. output27State = "off";

      60. digitalWrite(output27, LOW);

      61. }

      62. // Display the HTML web page

      63. client.println("<!DOCTYPE html><html>");

      64. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");

      65. client.println("<link rel=\"icon\" href=\"data:,\">");

      66. // CSS to style the on/off buttons

      67. // Feel free to change the background-color and font-size attributes to fit your preferences

      68. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");

      69. client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");

      70. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");

      71. client.println(".button2 {background-color: #555555;}</style></head>");

      72. // Web Page Heading

      73. client.println("<body><h1>ESP32 Web Server</h1>");

      74. // Display current state, and ON/OFF buttons for GPIO 26

      75. client.println("<p>GPIO 26 - State " + output26State + "</p>");

      76. // If the output26State is off, it displays the ON button

      77. if (output26State=="off") {

      78. client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");

      79. } else {

      80. client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");

      81. }

      82. // Display current state, and ON/OFF buttons for GPIO 27

      83. client.println("<p>GPIO 27 - State " + output27State + "</p>");

      84. // If the output27State is off, it displays the ON button

      85. if (output27State=="off") {

      86. client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");

      87. } else {

      88. client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");

      89. }

      90. client.println("</body></html>");

      91. // The HTTP response ends with another blank line

      92. client.println();

      93. // Break out of the while loop

      94. break;

      95. } else { // if you got a newline, then clear currentLine

      96. currentLine = "";

      97. }

      98. } else if (c != '\r') { // if you got anything else but a carriage return character,

      99. currentLine += c; // add it to the end of the currentLine

      100. }

      101. }

      102. }

      103. // Clear the header variable

      104. header = "";

      105. // Close the connection

      106. client.stop();

      107. Serial.println("Client disconnected.");

      108. Serial.println("");

      109. }

      110. }

  • 將上面程式燒錄(上傳)到web:bit板子

  • 開啟右上角的【序列埠監控視窗】

    • 鮑速選115200

    • 會顯示我們Web:bit的IP(待會連線用)

六、透過手機連上Web:bit的網頁伺服器(Web Server)

  • 首先手機先透過WIFI連上同一個AP (同一個網段)

  • 手機瀏覽器輸入 網址 (本例是192.168.2.113)

    • 遠端控制亮滅燈

    • 測試成功

    • 由於我是一位入門者,只會照著做,把所做的記錄下來,這樣下次就不會忘記!參考資料內有詳細的說明,再等大家去尋寶!