Play ESP-01 with Arduino

1. 牛刀小試~點亮LED

器材準備

開工了

先將ESP-01接上USB燒錄器,記得切到燒錄模式,插上電腦

打開ARDUINO軟體,複製下列程式碼貼上

void setup() {
pinMode(2,OUTPUT);
}

void loop() {
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
delay(1000);
}

程式裡2的意思是使用GPIO2,因為不需與UNO連接,所以TX.RX這兩個IO也可以拿來使用,這樣板上一共有4個GPIO可用(0、1、2、3),如下圖:

程式寫好後,記得先檢查一下USB的序列埠有沒有選對,再按上傳

程式上傳成功後,開始接線,請注意ESP-01上面的VCC與CH_PD要接到電源板的3.3V,若接到5V可能會導致晶片燒燬!!

線路接好,再把電源板插上變壓器,就可以看到LED燈開始一閃一閃了

搭上網路

ESP-01本身是有連網能力的,接著我們用內建的範例來看看它強大的功能,

開啟Arduino軟體,從檔案>>範例>>ESP8266WiFi>>WiFiWebServer

程式內容如下,這裡我們只需要修改你所在環境的無線網路SSID與密碼(第12、13行),再上傳即可,有夠簡單吧~

/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO2 low,
 *    http://server_ip/gpio/1 will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be 
 *  printed to Serial when the module is connected.
 */

#include <ESP8266WiFi.h>

const char* ssid = "MY_SSID";
const char* password = "MY_PASSWORD";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(2, val);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

上傳完成後,接好線路(電路圖同上面blink範例的接法),接著找出你的ESP-01配發到的IP是多少(進無線AP管理介面查看)

以本例取得的IP是192.168.0.104

只要用在瀏覽器的網址列輸入 http://192.168.0.104/gpio/1 就可以點亮LED燈

輸入 http://192.168.0.104/gpio/0 關閉LED燈

把LED改成繼電器,那您就可以透過網路來開關家裡的電扇電燈等電器了

全部 錯誤訊息 (7) 已棄用 (0) 資料庫語法 (13) 區塊 (0) 額外資訊 (2) 計時(3)
錯誤訊息
未知: Array and string offset access syntax with curly braces is deprecated 在檔案中的第 /include/functions.encoding.php 列 40
未知: Array and string offset access syntax with curly braces is deprecated 在檔案中的第 /include/functions.encoding.php 列 40
未知: Array and string offset access syntax with curly braces is deprecated 在檔案中的第 /include/functions.encoding.php 列 73
未知: Function get_magic_quotes_gpc() is deprecated 在檔案中的第 /class/libraries/vendor/xoops/xmf/src/Request.php 列 505
未知: Function get_magic_quotes_gpc() is deprecated 在檔案中的第 /class/libraries/vendor/xoops/xmf/src/Request.php 列 119
未知: Function get_magic_quotes_gpc() is deprecated 在檔案中的第 /class/libraries/vendor/xoops/xmf/src/Request.php 列 505
通知: Undefined index: bootstrap 在檔案中的第 /modules/tadtools/class/Utility.php 列 409
已棄用
資料庫語法
0.000080 - SET SQL_BIG_SELECTS = 1
0.000618 - SELECT * FROM config WHERE (`conf_modid` = '0' AND `conf_catid` = '1') ORDER BY conf_order ASC
0.000236 - SELECT sess_data, sess_ip FROM session WHERE sess_id = 'stojqu8q6fq06jas61d0ohk7sd'
0.000219 - SELECT * FROM modules WHERE dirname = 'tad_book3'
0.000310 - SELECT COUNT(*) FROM group_permission WHERE (`gperm_modid` = '1' AND (`gperm_groupid` = '3') AND `gperm_name` = 'module_read' AND `gperm_itemid` = '9')
0.000248 - SELECT * FROM config WHERE (`conf_modid` = '9') ORDER BY conf_order ASC
0.000394 - select a.tbsn,a.title,b.author,a.category,a.page,a.paragraph,a.sort from tad_book3_docs as a left join tad_book3 as b on a.tbsn=b.tbsn where a.tbdsn='39'
0.000149 - SELECT tbsn,title FROM tad_book3 ORDER BY sort
0.000160 - select * from tad_book3_docs where tbdsn='39'
0.000141 - select * from tad_book3 where tbsn='11'
0.000214 - SELECT * FROM modules WHERE dirname = 'tadtools'
0.000230 - SELECT * FROM config WHERE (`conf_modid` = '4') ORDER BY conf_order ASC
0.000125 - select `tt_theme_kind` from `tadtools_setup` where `tt_theme`='school2015'
總計: 13
區塊
總計: 0
額外資訊
包含檔案: 103 檔案
使用記憶體: 3630080 bytes
計時
XOOPS 使用 0.034 秒來載入。
XOOPS Boot 使用 0.023 秒來載入。
Module init 使用 0.011 秒來載入。