繼前一篇 Arduino筆記(33):透過 ESP8266 無線網路讀取DHT-11的溫濕度,今天要實作的是一起在拍賣網站購買的 ESP-01S繼電器模組。前實作每次要上傳程式到 ESP-01S時,就要先將 ESP-01S接六條線到 USB to TTL及麵包板,上傳完成後,拔下電線,再將 ESP-01S插到 DHT-11模組,非常麻煩。這次就使用可以直接插 ESP-01S的 USB to TTL模組,快速在上傳程式跟繼電器模組間轉換,以下紀錄一下我實作的過程。
• ESP8266 ESP-01S 無線模組
• 12V LED燈 x 1個
• 麵包板 x 1個
• 麵包板電源 x 1個
• 繼電器無線網路 ESP-01S模組 x 1個
選好開發板 [Generic ESP8266 Module]後,上傳程式到 ESP-01S,上傳完成,出現以下畫面:
取下ESP-01S,插在繼電器模組後,開啟電源開關。接著使用瀏覽器,連線至 ESP-01S的 Web 伺服器,可以看到網頁上的控制按鈕。
以下是執行結果的影片:
Instructables: ESP01/01S Relay module tutorial
[材料]
• 模組下載器 ESP USB轉TTL (CP2102 晶片)• ESP8266 ESP-01S 無線模組
• 12V LED燈 x 1個
• 麵包板 x 1個
• 麵包板電源 x 1個
• 繼電器無線網路 ESP-01S模組 x 1個
[線路圖]
上傳程式只要將 ESP-01S插在下載器上,再將下載器插入電腦的 USB即可,非常方便。[程式]
#include <ESP8266WiFi.h>
#define RELAY 0 //定義GPIO0
// 設定無線基地台SSID跟密碼
const char* ssid = "MyHome"; //改成您的SSID
const char* password = "12345678"; //改成您的密碼
// 設定 web server port number 80
WiFiServer server(80);
// 儲存 HTTP request 的變數
String header;
// 儲存目前輸出的狀態
String output0State = "off";
void setup() {
Serial.begin(115200);
// 將輸出變數初始化
pinMode(RELAY, OUTPUT);
// 設定為低電位
digitalWrite(RELAY, 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 /0/on") >= 0)
{
Serial.println("GPIO 0 on");
output0State = "on";
digitalWrite(RELAY, HIGH);
} else if (header.indexOf("GET /0/off") >= 0)
{
Serial.println("GPIO 0 off");
output0State = "off";
digitalWrite(RELAY, 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 0 按鈕的狀態是 ON/OFF
client.println("<p>GPIO 0 - State " + output0State + "</p>");
// 按鈕假使狀態是 off, 就要顯示 ON
if (output0State=="off") {
client.println("<p><a href=\"/0/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/0/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("");
}
}
[執行結果]
重新啟動 ESP01S,打開 Console,得知目前 無線裝置的 IP。取下ESP-01S,插在繼電器模組後,開啟電源開關。接著使用瀏覽器,連線至 ESP-01S的 Web 伺服器,可以看到網頁上的控制按鈕。
以下是執行結果的影片:
[參考資料]
RandomNerdTutorials: Build an ESP8266 Web Server – Code and SchematicsInstructables: ESP01/01S Relay module tutorial



版主你好
回覆刪除想請問如果我想要將GPIOs訊號送出後1000毫秒後關閉 只送出一次訊號 不要讓他持續送出的話
我應該要怎麼設定
張貼留言