在這篇教學中,我們將擴展 Arduino Web Server 範例 ,以便可以通過瀏覽器向 Arduino 發送指令。

我們將透過存取 /on 的 URL 點亮 Arduino 上的內建 LED,並透過開啟 /off 的 URL 來關閉它。其他任何指令都將無效。

以下是來自其他教學的程式碼:

#include <SPI.h>
#include <WiFiNINA.h>

WiFiServer server(80);

void setup() {
    char ssid[] = SECRET_SSID;
    char pass[] = SECRET_PASS;

    Serial.begin(9600);
    while (!Serial);

    int status = WL_IDLE_STATUS;
    while (status != WL_CONNECTED) {
        Serial.print("Connecting to ");
        Serial.println(ssid);
        status = WiFi.begin(ssid, pass);
        delay(5000);
    }

    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    server.begin();
}

void loop() {
    WiFiClient client = server.available();
    if (client) {
        String line = "";
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);

                if (c != '\n' && c != '\r') {
                    line += c;
                }

                if (c == '\n') {
                    if (line.length() == 0) {
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-Type: text/html");
                        client.println("Connection: close");
                        client.println();
                        client.println("<!DOCTYPE HTML>");
                        client.println("<html>");
                        client.println("test");
                        client.println("</html>");
                        break;
                    } else {
                        line = "";
                    }
                }
            }
        }

        client.stop();
    }
}

在最後的 else 部分,我們可以檢查完整的一行內容,以便在清除內容之前做檢查。在這個案例中,我們可以檢查是否為 GET /onGET /off,以檢測所要執行的指令:

String command = "";

// ...

if (line.startsWith("GET /on ")) {
    command = "on";
}
if (line.startsWith("GET /off ")) {
    command = "off";
}

在準備好回覆時,我們可以檢查指令並將 LED 打開或關閉:

if (command == "on") {
    digitalWrite(LED_BUILTIN, HIGH);
} else if (command == "off") {
    digitalWrite(LED_BUILTIN, LOW);
}

我們還可以回覆一個確認訊息,例如:

client.println("Turned the LED " + command);

完成了!現在將程式碼燒錄到 Arduino 上,並呼叫 /on/off 的 URL。

我在我的本地網路路由器中為 Arduino 預留了一個靜態 IP,並且在 /etc/hosts 檔案中將其命名為 arduino.local,因此可以透過 http://arduino.local/on 打開 LED,或透過 http://arduino.local/off 關閉 LED。

以下是完整的程式碼:

#include <SPI.h>
#include <WiFiNINA.h>

WiFiServer server(80);

void setup() {
    char ssid[] = SECRET_SSID;
    char pass[] = SECRET_PASS;

    Serial.begin(9600);
    while (!Serial);

    int status = WL_IDLE_STATUS;
    while (status != WL_CONNECTED) {
        Serial.print("Connecting to ");
        Serial.println(ssid);
        status = WiFi.begin(ssid, pass);
        delay(5000);
    }

    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    server.begin();
}

void loop() {
    WiFiClient client = server.available();
    if (client) {
        String line = "";
        String command = "";
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);

                if (c != '\n' && c != '\r') {
                    line += c;
                }

                if (c == '\n') {
                    if (line.length() == 0) {
                        if (command == "on") {
                            digitalWrite(LED_BUILTIN, HIGH);
                        } else if (command == "off") {
                            digitalWrite(LED_BUILTIN, LOW);
                        }

                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-Type: text/html");
                        client.println("Connection: close");
                        client.println();
                        client.println("<!DOCTYPE HTML>");
                        client.println("<html>");
                        client.println("Turned the LED " + command);
                        client.println("</html>");
                        break;
                    } else {
                        if (line.startsWith("GET /on ")) {
                            command = "on";
                        }
                        if (line.startsWith("GET /off ")) {
                            command = "off";
                        }

                        line = "";
                    }
                }
            }
        }

        client.stop();
    }
}