過去研究樹莓派時有執行過一個實驗:Raspberry Pi 筆記(4):WebIOPi讓瀏覽器控制樹莓派,讓安裝在樹莓派的WebIOPi 服務程式控制 LED燈,如果是 Arduino的系統,需自己寫Web的回應及控制 LED燈,就來研究一下,看看如何在 Arduinio系統,透過 Web 來控制 LED燈的開關。
實驗中,直接使用 NodeMCU 的ESP8266 無線網路,先從 Console 得知取得的 IP,再透過瀏覽器顯示按鍵及目前 LED 燈的狀態。
• LED x 2
• 電阻 220 ohms x 2
• 麵包板 x 1
• 連接線 x 若干條
• NodeMCU D2 (GPIO 4) 接 LED(紅) 正極;負極接220歐姆電阻
• NodeMCU GND 接電阻一端
打開瀏覽器,輸入Console 顯示的IP,如 http://192.168.1.137
使用游標按下ON,即可讓LED亮起來,這時畫面的 ON 會變成 OFF,再按一次即可讓 LED 熄滅。
實驗中,直接使用 NodeMCU 的ESP8266 無線網路,先從 Console 得知取得的 IP,再透過瀏覽器顯示按鍵及目前 LED 燈的狀態。
[材料]
• Arduino NodeMCU x 1• LED x 2
• 電阻 220 ohms x 2
• 麵包板 x 1
• 連接線 x 若干條
[接線]
• NodeMCU D1 (GPIO 5) 接 LED(黃) 正極;負極接220歐姆電阻• NodeMCU D2 (GPIO 4) 接 LED(紅) 正極;負極接220歐姆電阻
• NodeMCU GND 接電阻一端
[程式]
#include <ESP8266WiFi.h>
// 設定無線基地台SSID跟密碼
const char* ssid = "MyHome"; //改成您的SSID
const char* password = "12345678"; //改成您的密碼
// 設定 web server port number 80
WiFiServer server(80);
// 儲存 HTTP request 的變數
String header;
// Auxiliar variables to store the current output state
String output5State = "off";
String output4State = "off";
// 指定輸出的 GPIO Pins
const int output5 = 5;
const int output4 = 4;
void setup() {
Serial.begin(115200);
// 將輸出變數初始化
pinMode(output5, OUTPUT);
pinMode(output4, OUTPUT);
// 設定為低電位
digitalWrite(output5, LOW);
digitalWrite(output4, LOW);
// 使用SSID 跟 password 連線基地台
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// 使用COM Port 列出取得的IP address
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // 等待 clients 連線
if (client) { // 假使新的用戶端連線
Serial.println("New Client."); // 從序列 Port印出訊息內容
String currentLine = ""; // 清空這行的內容
while (client.connected()) { // 當 client繼續連線持續執行迴圈
if (client.available()) { // 假使從 client 有讀到字元
char c = client.read(); // 讀取這個字元
Serial.write(c); // 印出這個字元在串列視窗
header += c;
if (c == '\n') { // 假使是換行符號
// 假使目前的一行是空白且有一個新行,就結束 client HTTP 的要求
if (currentLine.length() == 0) {
// HTTP 表頭開始時,會有回應碼 response code (如: HTTP/1.1 200 OK)
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// 將 GPIOs 開或關
if (header.indexOf("GET /5/on") >= 0)
{
Serial.println("GPIO 5 on");
output5State = "on";
digitalWrite(output5, HIGH);
} else if (header.indexOf("GET /5/off") >= 0)
{
Serial.println("GPIO 5 off");
output5State = "off";
digitalWrite(output5, LOW);
} else if (header.indexOf("GET /4/on") >= 0)
{
Serial.println("GPIO 4 on");
output4State = "on";
digitalWrite(output4, HIGH);
} else if (header.indexOf("GET /4/off") >= 0)
{
Serial.println("GPIO 4 off");
output4State = "off";
digitalWrite(output4, LOW);
}
// 顯示 HTML 網頁
client.println("<html>");
client.println("<head>");
client.println("<link rel=\"icon\" href=\"data:,\">");
// 設定 on/off 按鈕的CSS
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
// 網頁表頭
client.println("<body><h1>ESP8266 Web Server</html>");
// 顯示現在GPIO 5 按鈕的狀態是 ON/OFF
client.println("<p>GPIO 5 - State " + output5State + "</p>");
// 按鈕假使狀態是 off, 就要顯示 ON
if (output5State=="off") {
client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// 顯示現在GPIO 4 按鈕的狀態是 ON/OFF
client.println("<p>GPIO 4 - State " + output4State + "</p>");
// 按鈕假使狀態是 off, 就要顯示 ON
if (output4State=="off") {
client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button/a></p>");
} else {
client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button/a></p>");
}
client.println("</body></html>");
// 使用空白行結束 HTTP回應
client.println();
break;
} else { // 假使有新的一行, 清除目前這一行
currentLine = "";
}
} else if (c != '\r') { // 讀取到的不是換行符號
currentLine += c; // 增加一個字元在本行最後
}
}
}
// 清除表頭變數
header = "";
// 關閉連線 connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
[執行結果]
從 Console 得知連線的IP:打開瀏覽器,輸入Console 顯示的IP,如 http://192.168.1.137
使用游標按下ON,即可讓LED亮起來,這時畫面的 ON 會變成 OFF,再按一次即可讓 LED 熄滅。
[參考資料]
- RandomNerdTutorials: Build an ESP8266 Web Server – Code and Schematics



按鈕顯示“OFF”的時候比顯示“ON”的時候寬...
回覆刪除if (c == '\n') {
回覆刪除// 假使目前的一行是空白且有兩個新行,就結束 client HTTP 的要求
if (currentLine.length() == 0) {
請問這邊明明只有判斷一個新行字元,但說明卻說是兩個新行,請問是筆誤還是有哪邊我還沒理解的?
您好,
刪除是筆誤, 應該是一個新行字元, 謝謝您的指正。
您好,我想請問程式問題
回覆刪除header.indexOf("GET /4/on") >= 0請問這段式甚麼意思呢?
您好,
刪除header是紀錄用戶輸入的URL,indexOf 是要找出字串中是否有某些字元,可參考以下這篇文章說明 indexOf的用法:
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringIndexOf
透過URL的字串來判斷LED是否要on或off。
謝謝
刪除張貼留言