Arduino项目:用你的浏览器点亮内置LED

122 阅读2分钟

在本教程中,我想扩展Arduino Web服务器的例子,使我们能够通过浏览器向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");  // the connection will be closed after completion of the response
            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 URL,或/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();
  }
}