今天依舊接續之前沒講完程式碼,這次主要是講loop裡面的程式碼,那此次主要介紹較為主要的部份,但由於這部分稍長,所以會挑一些講,另外補充些額外的內容,剩下就會丟給後天那篇文章。
狀態碼是在說明當手機或各種裝置(可上網頁),當連接至網頁伺服器時所回覆狀態,包含連接正常,或伺服器離線等等,我這樣講大家可能聽不太懂,那我說個大家容易聽見的404,許多人都會在連接網路時,如果連接不到或者網路不好時都會發生404 Not Found,那通常404 Not Found是伺服器不想給使用者知道為何連線被拒絕,那下面就稍微介紹一些常見狀態碼。
代表伺服器接受使用者的請求,但通常各位看不見,因為接受就直接進網頁拉,不會像常見的4xx狀態碼一樣會顯示在網頁上。
這類的狀態碼代表了客戶端看起來可能發生了錯誤,妨礙了伺服器的處理。典型的就像403、404等等。
那這邊就不多介紹拉,因為主要就是要說Esp32s開啟網頁時,也是會有狀態碼,那如果要看詳細的可以到 HTTP狀態碼 wiki 查看。
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 /23/on") >= 0) {
              Serial.println("GPIO 23 on");
              output0State = "on";
              digitalWrite(output0, HIGH);
            } else if (header.indexOf("GET /23/off") >= 0) {
              Serial.println("GPIO 23 off");
              output0State = "off";
              digitalWrite(output0, LOW);
            } else if (header.indexOf("GET /22/on") >= 0) {
              Serial.println("GPIO 22 on");
              output2State = "on";
              digitalWrite(output2, HIGH);
            } else if (header.indexOf("GET /22/off") >= 0) {
              Serial.println("GPIO 22 off");
              output2State = "off";
              digitalWrite(output2, 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 23  
            client.println("<p>GPIO 23 - State " + output0State + "</p>");
            // If the output0State is off, it displays the ON button       
            if (output0State=="off") {
              client.println("<p><a href=\"/23/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/23/off\"><button class=\"button button2\">OFF</button></a></p>");
            } 
               
            // Display current state, and ON/OFF buttons for GPIO 22  
            client.println("<p>GPIO 22 - State " + output2State + "</p>");
            // If the output2State is off, it displays the ON button       
            if (output2State=="off") {
              client.println("<p><a href=\"/22/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/22/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("");
  }
}
從程式碼部分開始看,從43行開始會偵測是否有使用者連接,並抓取使用者資訊,而45行會判斷是否有使用者連接,如果有則往46行走,沒有結束(直接到loop結尾)
好了,這次講解內容還希望大家聽得懂,因為loop的部分有許多牽扯到網頁狀態及網頁寫法...等,所以可能無法一次就理解全部,下次講解程式碼時會稍微講述html的寫法,那麼明天再見囉~