Arduino 項目:使用瀏覽器點亮內建LED 在這篇教學中,我們將擴展 Arduino Web Server 範例 ,以便可以通過瀏覽器向 Arduino 發送指令。
我們將透過存取 /on
的 URL 點亮 Arduino 上的內建 LED,並透過開啟 /off
的 URL 來關閉它。其他任何指令都將無效。
以下是來自其他教學的程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #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 /on
或 GET /off
,以檢測所要執行的指令:
1 2 3 4 5 6 7 8 9 10 String command = "" ; if (line.startsWith ("GET /on " )) { command = "on" ; } if (line.startsWith ("GET /off " )) { command = "off" ; }
在準備好回覆時,我們可以檢查指令並將 LED 打開或關閉:
1 2 3 4 5 if (command == "on" ) { digitalWrite (LED_BUILTIN, HIGH); } else if (command == "off" ) { digitalWrite (LED_BUILTIN, LOW); }
我們還可以回覆一個確認訊息,例如:
1 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。
以下是完整的程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #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 (); } }
tags: [“Arduino”, “Web Server”, “LED”]