5. ÀÌ´õ³Ý ½¯µå(Ethernet Shield)
À̹ø °Á¿¡¼´Â ÀÌ´õ³Ý ½¯µå¿¡ ´ëÇؼ °£´ÜÈ÷ ¼Ò°³ÇÏ°í, ±× »ç¿ë¹ý¿¡ ´ëÇؼ ¾Ë¾Æº¸µµ·Ï ÇÑ´Ù.
°Á¿¡¼ »ç¿ëµÇ´Â ÀÌ´õ³Ý ½¯µå´Â ¾Æ·¡ »çÁø°ú °°°í, Wiznet W5100 TCP/IP Àü¿ë IC¸¦ »ç¿ëÇÏ°í ÀÖÀ¸¸ç, µ¿½Ã¿¡ 4°³ÀÇ ¼ÒÄÏÀÇ ¿¬°áÀÌ °¡´ÉÇÏ´Ù.
![](http://postfiles12.naver.net/20140606_187/rnc_ohm_1401992614238vWKA5_JPEG/5._Ethernet_Shield_img_0.jpg?type=w2)
¾ÆµÎÀ̳ë Mega2560/Uno R3 ȣȯÇüÀ̸ç, Micro-SD ¼ÒÄÏÀÌ ³»ÀåµÇ¾î ÀÖ´Ù. ¶ÇÇÑ, 1°³ÀÇ µ¶¸³µÈ PoE(Power-over-Ethernet) ¸ðµâÀ» °¡Áö°í ÀÖ´Â ÀåÁ¡ÀÌ ÀÖ´Ù.
¾ÆµÎÀ̳ë IDEÀÇ Ethernet ¶óÀ̺귯¸®¸¦ »ç¿ëÇÏ¿© ÀÛµ¿ÀÌ °¡´ÉÇϸç, SPI ¹ö½º Á¦¾î·Î ÃÖ¼ÒÇÑÀÇ Æ÷Æ®·Î Á¦¾î°¡ °¡´ÉÇÏ´Ù.
¾ÆµÎÀÌ³ë °ø½Ä »çÀÌÆ®¿¡¼ Á¦°øµÇ´Â Ethernet Library ·¹ÆÛ·±½º¿¡´Â ¾ÆµÎÀ̳ë¿Í ÀÌ´õ³Ý ½¯µå°¡ ¾î¶»°Ô ¿¬°áÀÌ µÇ¾ß µÇ´ÂÁö ¼³¸íÀÌ µÇ¾î ÀÖ´Ù.
Arduino communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53, is not used to select the W5100, but it must be kept as an output or the SPI interface won't work.
![](http://postfiles2.naver.net/20140606_209/rnc_ohm_1401992614787TKldq_JPEG/5._Ethernet_Shield_img_1.jpg?type=w2)
°Á¿¡¼´Â ¾ÆµÎÀÌ³ë ¿ì³ë·Î ÁøÇàÀ» ÇÏ°í ÀÖÀ¸´Ï, ¿ì³ë º¸µå¸¸ Âü°íÇϱ⠹ٶõ´Ù.
ÀÏ´Ü, IDE¿¡¼ Á¦°øµÇ´Â ¶óÀ̺귯¸®¿Í ¿¹Á¦¸¦ ÀÌ¿ëÇؼ ÀÌ´õ³Ý ½¯µå¸¦ »ç¿ëÇØ º¸±â·Î ÇÏÀÚ.
![](http://postfiles4.naver.net/20140606_115/rnc_ohm_14019926152899Tuxm_JPEG/5._Ethernet_Shield_img_2.jpg?type=w2)
WebServer ¿¹Á¦¸¦ ¿¾î º¸ÀÚ.
#include
#include
// 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,10,177);
// 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);
// 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("");
client.println("");
// 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(" ");
}
client.println("");
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 disonnected");
}
} |
À§ ÄÚµå´Â ÀÌ´õ³Ý ½¯µå¸¦ À¥¼¹ö·Î ÀÌ¿ëÇÏ´Â °£´ÜÇÑ ¿¹Á¦·Î, 6°³ÀÇ ¾Æ³¯·Î±× Æ÷Æ®ÀÇ °ªÀ» À¥À» ÅëÇؼ È®ÀÎ ÇÒ ¼ö ÀÖ´Â ¿¹Á¦ÀÌ´Ù.
#include
#include
À§¿¡¼ ¾ð±ÞÇßµíÀÌ, ÀÌ´õ³Ý ½¯µå´Â SPI ¹ö½º¸¦ ÅëÇؼ Á¦¾î°¡ µÇ´Ï SPI ¶óÀ̺귯¸®¿Í Á¦°øµÇ´Â Ethernet ¶óÀ̺귯¸®¸¦ Ãß°¡ÇÑ´Ù.
IPAddress ip(192,168,10,177);
IP ÁÖ¼Ò´Â º»ÀÎÀÇ È¯°æ¿¡¼ ÇÒ´çµÈ IP¸¦ ±âÀÔÇÏ¸é µÈ´Ù.
![](http://postfiles1.naver.net/20140606_96/rnc_ohm_14019926156977dIw1_JPEG/5._Ethernet_Shield_img_3.jpg?type=w2)
pingÀ» Çѹø ³¯·Á º¸¸é, Àß ¿¬°áµÇ¾î ÀÀ´äÇÏ´Â °ÍÀ» º¼ ¼ö ÀÖ´Ù.
EthernetServer server(80);
ÀÌ´õ³Ý ¼¹ö ¶óÀ̺귯¸®¸¦ ÃʱâÈÇÏ°í, httpÀÇ µðÆúÆ® Æ÷Æ®(80)¸¦ ÁöÁ¤ÇØ ÁØ´Ù.
Ethernet.begin(mac, ip);
server.begin();
ÀÌ´õ³Ý ¿¬°á°ú ¼¹ö¸¦ ½ÃÀÛÇÑ´Ù.
EthernetClient client = server.available();
Ŭ¶óÀ̾ðÆ®ÀÇ ÀÎÄ¿¹Ö(incoming)À» È®ÀÎÇÑ´Ù.
Ŭ¶óÀ̾ðÆ®ÀÇ ÀÎÄ¿¹ÖÀÌ ÀÖÀ¸¸ç, Ŭ¶óÀ̾ðÆ®¿Í ¿¬°áÀ» »ý¼ºÇÏ°í ÁöÁ¤µÈ µ¿ÀÛÀ» ¼öÇàÇÑ´Ù.
Ŭ¶óÀ̾ðÆ®¿Í ¿¬°áÀÌ µÇ¸é, ¾Æ·¡¿Í °°ÀÌ ½Ã¸®¾ó Å͹̳ο¡ ¿¬°á ¸Þ½ÃÁö°¡ ¶ß°í,
![](http://postfiles6.naver.net/20140606_293/rnc_ohm_1401992616136q3XYN_JPEG/5._Ethernet_Shield_img_4.jpg?type=w2)
ÀÎÅÍ³Ý ÀͽºÇ÷ξ ½ÇÇà ÈÄ¿¡, À¥¼¹öÀÇ IP¸¦ ÀÔ·ÂÇϸé,
![](http://postfiles1.naver.net/20140606_160/rnc_ohm_1401992616457MusYm_JPEG/5._Ethernet_Shield_img_5.jpg?type=w2)
¾Æ³¯·Î±× Æ÷Æ®ÀÇ °ªÀÌ Ãâ·ÂµÇ´Â °ÍÀ» º¼ ¼ö ÀÖ´Ù.
client.println("Refresh: 5"); // refresh the page automatically
every 5 sec
Refresh ÁֱⰡ 5ÃÊ·Î ¼³Á¤µÇ¾î ÀÖ¾î¼, 5Ãʸ¶´Ù °»½ÅÀ» ÇÏ°Ô µÇ¾î ÀÖÀ¸´Ï, ÀÓÀÇ·Î Áֱ⸦ º¯°æÇÏ¿© ¾Æ³¯·Î±× ÀÔ·Â °ªÀ» È®ÀÎ ÇÒ ¼ö ÀÖ´Ù.
À¥¼¹öÀÇ ±â´ÉÀ» È°¿ëÇÏ·Á¸é, HTML¿¡ ´ëÇؼµµ Áö½ÄÀÌ ÀÖ¾î¾ß µÇ±â ¶§¹®¿¡ »ç¿ë¿¡ ¹®Á¦°¡ ÀÖÀ» ¼öµµ ÀÖÁö¸¸, °£´ÜÇÑ Äڵ带 ÀÍÇô¼ Àû¿ë½ÃÅ°¸é, ÈǸ¢ÇÑ À¥¼¹ö·Îµµ ÃæºÐÈ÷ ÀÌ¿ëÀÌ °¡´ÉÇÏ´Ù°í »ý°¢µÈ´Ù.