Toggle main menu visibility
臺南市教育局科技教育網
線上書籍
電子相簿
PM2.5監測
IOT實作
OpenID登入
:::
登入
登入
帳號
密碼
登入
使用 臺南市 OpenID 快速登入
:::
所有書籍
「Play ESP-01 with Arduino」目錄
MarkDown
使用環境
使用環境
1. 牛刀小試~點亮LED
2. ESP-01+Line打造簡易保全系統
3. 搭配LCD1602_I2C製作網路時鐘
4. PM2.5感測器精簡版
4-1 為PM2.5感測器增加液晶顯示
5. 手機APP [Blynk] 操控ESP-01
7. 參考資料
2. ESP-01+Line打造簡易保全系統
Play ESP-01 with Arduino ======================== ### 器材準備  ### 開工了 先將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
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\r\n\r\nGPIO is now "; s += (val)?"high":"low"; s += "\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改成繼電器,那您就可以透過網路來開關家裡的電扇電燈等電器了