ESP8266 - 이더넷 | ESP8266 - Ethernet

이 가이드는 ESP8266을 W5500 이더넷 모듈을 사용하여 인터넷 또는 귀하의 로컬 네트워크에 연결하는 방법을 보여줍니다. 다음 내용을 다룰 것입니다:

ESP8266 NodeMCU 이더넷

Hardware Preparation

1×ESP8266 NodeMCU Amazon
1×Micro USB Cable Amazon
1×W5500 Ethernet Module Amazon
1×Ethernet Cable Amazon
1×Jumper Wires Amazon
1×Breadboard 쿠팡 | Amazon
1×(Optional) ESP8266 Screw Terminal Adapter Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

W5500 이더넷 모듈에 대해

W5500 이더넷 모듈에는 두 가지 종류의 연결이 있습니다:

  • RJ45 인터페이스: 이더넷 케이블을 사용하여 라우터 또는 스위치와 같은 네트워크 장치에 연결합니다.
  • SPI 인터페이스: 다음 연결을 사용하여 이것을 ESP8266 보드에 연결합니다:
    • NC 핀: 이 핀은 연결하지 마십시오.
    • INT 핀: 이 핀은 연결하지 마십시오.
    • RST 핀: 이것을 ESP8266의 리셋(EN) 핀에 연결합니다.
    • GND 핀: 이것을 ESP8266의 접지(GND) 핀에 연결합니다.
    • 5V 핀: 이 핀은 연결하지 마십시오.
    • 3.3V 핀: 이것을 ESP8266의 3.3V 핀에 연결합니다.
    • MISO 핀: 이것을 ESP8266의 SPI MISO 핀에 연결합니다.
    • MOSI 핀: 이것을 ESP8266의 SPI MOSI 핀에 연결합니다.
    • SCS 핀: 이것을 ESP8266의 SPI CS(Chip Select) 핀에 연결합니다.
    • SCLK 핀: 이것을 ESP8266의 SPI SCK(시계) 핀에 연결합니다.
    이더넷 모듈 핀배열
    image source: diyables.io

ESP8266과 W5500 이더넷 모듈 간의 배선도

ESP8266 NodeMCU 이더넷 모듈 배선도

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

ESP8266의 핀 배치ESP8266 및 다른 구성 요소에 전원을 공급하는 방법에 대해 더 많이 보십시오.

image source: diyables.io

ESP8266 코드 - 이더넷 모듈을 통해 HTTP 요청하기

이 코드는 웹 클라이언트로 작동합니다. http://example.com/에 있는 웹 서버로 HTTP 요청을 보냅니다.

/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; EthernetClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; // or POST char HOST_NAME[] = "example.com"; String PATH_NAME = "/"; void setup() { 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 present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (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 header while (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"); } } void loop() { }

Detailed Instructions

Arduino IDE에서 ESP8266을 시작하려면 다음 단계를 따르세요:

  • ESP8266을 처음 사용하는 경우 Arduino IDE에서 ESP8266 환경 설정하는 방법 튜토리얼을 확인하세요.
  • 연결도에 표시된 대로 이더넷 모듈을 ESP8266 보드에 연결합니다.
  • 이더넷 케이블을 사용하여 이더넷 모듈을 라우터 또는 스위치에 연결합니다.
  • USB 케이블을 사용하여 ESP8266 보드를 컴퓨터에 연결합니다.
  • 컴퓨터에서 Arduino IDE를 엽니다.
  • 올바른 ESP8266 보드(예: NodeMCU 1.0 (ESP-12E 모듈))와 해당 COM 포트를 선택합니다.
  • Arduino IDE의 왼쪽 바에 있는 Libraries 아이콘을 클릭합니다.
  • Ethernet을 검색한 후 다양한 이더넷 라이브러리를 찾습니다.
  • Install 버튼을 클릭하여 이더넷 라이브러리를 설치합니다.
ESP8266 NodeMCU 이더넷 라이브러리
  • 아두이노 IDE에서 시리얼 모니터를 엽니다.
  • 주어진 코드를 복사하여 아두이노 IDE에 붙여넣습니다.
  • 아두이노 IDE에서 업로드 버튼을 눌러 코드를 ESP25로 전송합니다.
  • 아래와 같이 결과를 표시하는 시리얼 모니터에서 결과를 확인합니다.
COM6
Send
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
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

같은 네트워크에 있는 다른 기기가 동일한 MAC 주소를 가지고 있다면 제대로 작동하지 않을 수 있습니다.

ESP8266 코드 - 이더넷 모듈용 웹 서버

아래 코드는 ESP8266을 웹 서버로 변환합니다. 이 서버는 인터넷 브라우저에 기본 웹페이지를 제공합니다.

/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; EthernetServer server(80); void setup() { 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 present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (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()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an HTTP request ends with a blank line bool 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 reply if (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; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } }

Detailed Instructions

  • 위의 코드를 복사하여 아두이노 IDE에 붙여넣으세요.
  • 아두이노 IDE에서 Upload 버튼을 클릭하여 코드를 ESP8266으로 전송하세요.
  • 결과를 시리얼 모니터에서 확인하세요; 설명된 대로 표시될 것입니다.
COM6
Send
ESP8266 - Ethernet Tutorial ESP8266 - Web Server IP Address: 192.168.0.2
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 위에 제공된 IP 주소를 복사하여 웹 브라우저의 주소 창에 입력하세요. ESP8266이 표시하는 간단한 웹페이지를 볼 수 있습니다.
ESP8266 NodeMCU 이더넷 웹 서버

관련 튜토리얼

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!