Hướng dẫn này cho bạn biết cách kết nối ESP8266 với Internet hoặc mạng cục bộ của bạn bằng mô-đun Ethernet W5500. Dưới đây là những gì chúng ta sẽ thảo luận:
Kết nối ESP8266 với mô-đun Ethernet W5500
Lập trình ESP8266 cho các yêu cầu web qua Ethernet
Xây dựng một máy chủ web cơ bản trên ESP8266 với Ethernet
Mã ESP8266 cho mô-đun Ethernet - Thực hiện yêu cầu HTTP qua Ethernet
Đoạn mã này hoạt động như một khách hàng web. Nó gửi các yêu cầu HTTP đến máy chủ web tại http://example.com/.
/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp8266/esp8266-ethernet */#include <SPI.h>#include <Ethernet.h>// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };EthernetClient client;int HTTP_PORT = 80;String HTTP_METHOD = "GET"; // or POSTchar HOST_NAME[] = "example.com";String PATH_NAME = "/";voidsetup() {Serial.begin(9600);delay(1000);Serial.println("ESP8266 - Ethernet Tutorial");// initialize the Ethernet shield using DHCP:if (Ethernet.begin(mac) == 0) {Serial.println("Failed to obtaining an IP address");// check for Ethernet hardware presentif (Ethernet.hardwareStatus() == EthernetNoHardware)Serial.println("Ethernet shield was not found");// check for Ethernet cableif (Ethernet.linkStatus() == LinkOFF)Serial.println("Ethernet cable is not connected.");while (true) ; }// connect to web server on port 80:if (client.connect(HOST_NAME, HTTP_PORT)) {// if connected:Serial.println("Connected to server");// make a HTTP request:// send HTTP header client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP headerwhile (client.connected()) {if (client.available()) {// read an incoming byte from the server and print it to serial monitor:char c = client.read();Serial.print(c); } }// the server's disconnected, stop the client: client.stop();Serial.println();Serial.println("disconnected"); } else { // if not connected:Serial.println("connection failed"); }}voidloop() {}
Hướng dẫn từng bước
Để bắt đầu với ESP8266 trên Arduino IDE, hãy làm theo các bước sau:
Kết nối module Ethernet với bảng ESP8266 như hiển thị trong sơ đồ nối.
Sử dụng cáp Ethernet để kết nối module Ethernet với bộ định tuyến hoặc switch của bạn.
Kết nối bảng ESP8266 với máy tính của bạn bằng cáp USB.
Mở Arduino IDE trên máy tính của bạn.
Chọn bảng ESP8266 phù hợp, ví dụ NodeMCU 1.0 (ESP-12E Module), và cổng COM tương ứng của nó.
Nhấp vào biểu tượng Libraries ở thanh bên trái của Arduino IDE.
Tìm kiếm Ethernet, sau đó tìm thư viện Ethernet của Various.
Nhấp nút Install để cài đặt thư viện Ethernet.
Mở Serial Monitor trong Arduino IDE.
Sao chép mã được cung cấp và dán vào Arduino IDE.
Nhấn nút Upload trong Arduino IDE để gửi mã tới ESP25.
Kiểm tra kết quả trên Serial Monitor; kết quả sẽ hiển thị như dưới đây.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
ESP8266 - Ethernet Tutorial
Connected to server
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 208425
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Fri, 12 Jul 2024 07:08:42 GMT
Etag: "3147526947"
Expires: Fri, 19 Jul 2024 07:08:42 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECAcc (lac/55B8)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256
Connection: close
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
disconnected
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2
※ Lưu ý:
Nếu một thiết bị khác trên cùng một mạng có cùng địa chỉ MAC, nó có thể hoạt động không đúng cách.
Mã ESP8266 cho mô-đun Ethernet - Máy chủ Web
Đoạn mã dưới đây biến ESP8266 thành một máy chủ web. Máy chủ này phục vụ một trang web cơ bản cho các trình duyệt web.
/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp8266/esp8266-ethernet */#include <SPI.h>#include <Ethernet.h>// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };EthernetServer server(80);voidsetup() {Serial.begin(9600);delay(1000);Serial.println("ESP8266 - Ethernet Tutorial");// initialize the Ethernet shield using DHCP:if (Ethernet.begin(mac) == 0) {Serial.println("Failed to obtaining an IP address");// check for Ethernet hardware presentif (Ethernet.hardwareStatus() == EthernetNoHardware)Serial.println("Ethernet shield was not found");// check for Ethernet cableif (Ethernet.linkStatus() == LinkOFF)Serial.println("Ethernet cable is not connected.");while (true) ; } server.begin();Serial.print("ESP8266 - Web Server IP Address: ");Serial.println(Ethernet.localIP());}voidloop() {// listen for incoming clientsEthernetClient client = server.available();if (client) {Serial.println("new client");// an HTTP request ends with a blank linebool currentLineIsBlank = true;while (client.connected()) {if (client.available()) {char c = client.read();Serial.write(c);// if you've gotten to the end of the line (received a newline// character) and the line is blank, the HTTP request has ended,// so you can send a replyif (c == '\n' && currentLineIsBlank) {// send a standard HTTP response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<body>"); client.println("<h1>ESP8266 - Web Server with Ethernet</h1>"); client.println("</body>"); client.println("</html>");break; }if (c == '\n') {// you're starting a new line currentLineIsBlank = true; } elseif (c != '\r') {// you've gotten a character on the current line currentLineIsBlank = false; } } }// give the web browser time to receive the datadelay(1);// close the connection: client.stop();Serial.println("client disconnected"); }}
Hướng dẫn từng bước
Sao chép mã ở trên và dán vào Arduino IDE.
Nhấp vào nút Tải lên trong Arduino IDE để chuyển mã sang ESP8266.
Kiểm tra kết quả trên Serial Monitor; các kết quả sẽ hiển thị như đã mô tả.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
ESP8266 - Ethernet Tutorial
ESP8266 - Web Server IP Address: 192.168.0.2
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2
Sao chép địa chỉ IP được cung cấp ở trên và nhập nó vào thanh địa chỉ của trình duyệt web của bạn. Bạn sẽ thấy một trang web đơn giản được ESP8266 hiển thị.