이더넷쉴드2를 사용해 인터넷에 접속해보자

이더넷쉴드2를 사용해 인터넷에 접속해보자
ORIGINAL POST
By ZETA7
components
Hardware Components
Ethernet Shield2
X 1
Arduino Uno
X 1
Arduino Mega 2560
X 1
Software Apps and online services
Arduino IDE
details

1518044751926.jpg

1. 준비물

아두이노 메가 2560 또는 아두이노 우노 R3

위즈넷 이더넷 실드2

네트워크 케이블

공유기

위즈넷에서 만든 실드 2

2. 하드웨어 연결

2.1 아두이노 메가 2560

쉘드 중간에 있는 SPI 6핀이 아두이노와 연결되면 제대로 연결한 것이다.

아두이노 메가에 장착

아래 사진에 잘 보이는지 모르겠지만 왼쪽 두 핀이 아두이노에 접촉되지 않고 남는다.

반대편도 마찬가지로 두 핀이 남는다.

아두이노 메가 2560 R3과 연결하면 핀이 남지 않을 것이다. 왜냐하면 R3는 아래, 위에 핀이 두 개씩 더 있기 때문이다.

아래 사진이 현재 정품 이더넷 실드 2이다. 역시 두 핀씩 많은 게 보인다. SCL, SDA, RESERVED, RESERVED 오로카에서 구매한 것은 SCL, SDA, IOREF, RESERVED 라서 한 핀이 다르다.

https://store.arduino.cc/usa/arduino-ethernet-shield-2

2.2 아두이노 우노 R3

쉘드 중간에 있는 SPI 6핀이 아두이노와 연결되면 제대로 연결한 것이다.

우노가 R3 버전이라 쉴드2 핀이 꼭 맞다.

우노가 R3 버전이라 쉴드2 핀이 꼭 맞다.

3. 아두이노 코드

기존 Webserver 코드에서 아래만 변경했다.

#include <Ethernet.h> > #include <Ethernet2.h>
* Static IP Web Server */ #include <SPI.h> #include <Ethernet2.h> //#include <Ethernet.h> // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 35, 100); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); Serial.println(“Static IP Web Server”); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print(“server is at “); 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 boolean 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(“Refresh: 5”); // refresh the page automatically every 5 sec client.println(); client.println(“<!DOCTYPE HTML>”); client.println(“<html>”); // output the value of each analog input pin for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print(“analog input “); client.print(analogChannel); client.print(” is “); client.print(sensorReading); client.println(“<br />”); } 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”); } }

4. 실험결과

클라언트 접속 시 시리얼 모니터

웹 서버

아주 쉽게 동작해서 좋다.

1518044751926.jpg

1. 준비물

아두이노 메가 2560 또는 아두이노 우노 R3

위즈넷 이더넷 실드2

네트워크 케이블

공유기

위즈넷에서 만든 실드 2

2. 하드웨어 연결

2.1 아두이노 메가 2560

쉘드 중간에 있는 SPI 6핀이 아두이노와 연결되면 제대로 연결한 것이다.

아두이노 메가에 장착

아래 사진에 잘 보이는지 모르겠지만 왼쪽 두 핀이 아두이노에 접촉되지 않고 남는다.

반대편도 마찬가지로 두 핀이 남는다.

아두이노 메가 2560 R3과 연결하면 핀이 남지 않을 것이다. 왜냐하면 R3는 아래, 위에 핀이 두 개씩 더 있기 때문이다.

아래 사진이 현재 정품 이더넷 실드 2이다. 역시 두 핀씩 많은 게 보인다. SCL, SDA, RESERVED, RESERVED 오로카에서 구매한 것은 SCL, SDA, IOREF, RESERVED 라서 한 핀이 다르다.

https://store.arduino.cc/usa/arduino-ethernet-shield-2

2.2 아두이노 우노 R3

쉘드 중간에 있는 SPI 6핀이 아두이노와 연결되면 제대로 연결한 것이다.

우노가 R3 버전이라 쉴드2 핀이 꼭 맞다.

우노가 R3 버전이라 쉴드2 핀이 꼭 맞다.

3. 아두이노 코드

기존 Webserver 코드에서 아래만 변경했다.

#include <Ethernet.h> > #include <Ethernet2.h>
* Static IP Web Server */ #include <SPI.h> #include <Ethernet2.h> //#include <Ethernet.h> // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 35, 100); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); Serial.println(“Static IP Web Server”); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print(“server is at “); 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 boolean 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(“Refresh: 5”); // refresh the page automatically every 5 sec client.println(); client.println(“<!DOCTYPE HTML>”); client.println(“<html>”); // output the value of each analog input pin for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print(“analog input “); client.print(analogChannel); client.print(” is “); client.print(sensorReading); client.println(“<br />”); } 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”); } }

4. 실험결과

클라언트 접속 시 시리얼 모니터

웹 서버

아주 쉽게 동작해서 좋다.

COMMENTS

Please Login to comment
  Subscribe  
Notify of