SW교육/아두이노

아두이노 인터넷 연결하기(Lan, enc28j60) - 3. 소스 살펴보기

GrayrabbiT 2015. 8. 12. 23:34
반응형

일단 인터넷을 연결 했으니! IP를 받아야 겠죠!


시작합시다.



  1. DHCP 받아오기

예제에 나와있는 소스는 다음과 같습니다.


#include <EtherCard.h>

//먼저 라이브러리를 불러오겠습니다.

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

//맥 주소를 넣어주어야 하는데 이때 주소는 유니크하게 주셔야 합니다. 16진수 6자리로 되어 있습니다.


byte Ethernet::buffer[700];

//Ethernet 는  ENC28J60을 typedef을 이용해서 정의한 녀석입니다., ENC28J60.h를 확인하세요.

//이때의 버퍼는 transmit과 recieve를 같이 사용하게 되는 버퍼가 됩니다.


void setup () {

 Serial.begin(57600);

 Serial.println(F("\n[testDHCP]"));

//f 는 매크로 입니다. 일단 패스하겠습니다.


 Serial.print("MAC: ");

 for (byte i = 0; i < 6; ++i) {

   Serial.print(mymac[i], HEX);//mac address 출력

   if (i < 5)

     Serial.print(':');

 }

 Serial.println();

 

 if (ether.begin(sizeof Ethernet::buffer, mymac,2) == 0)

//ether 시작 메소드 입니다. 이 때, 필요한 인자는 총 3가지 이며, 버퍼, 맥주소, CS핀 번호입니다. 이때 cs핀을 전달하지 않으면 8번이 default 설정으로 진행됩니다.

   Serial.println(F("Failed to access Ethernet controller"));


 Serial.println(F("Setting up DHCP"));

 if (!ether.dhcpSetup())

   Serial.println(F("DHCP failed"));

 //하단부는 EtherCard.h에 정의된 내용입니다.

 ether.printIp("My IP: ", ether.myip);  

 ether.printIp("Netmask: ", ether.netmask);

 ether.printIp("GW IP: ", ether.gwip);

 ether.printIp("DNS IP: ", ether.dnsip);

}


void loop () {}


아.. 먼가 개어렵게 느껴집니다.


일단 해보죠.. 넵넵..


업로드 하고 시리얼 모니터를 살펴보면 다음과 같은 화면을 확인할 수 있습니다.



제 컴퓨터에서는 85에 연결되었군요. 일단 IP를 받아왔어요. 우왕 ㅋ 굳 ㅋ!

이렇게 쉽다니!





  1. PING 날리기!


이제 핑을 날려보도록 하겠습니다.



#include <EtherCard.h>


static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };


byte Ethernet::buffer[700];

static uint32_t timer; //타이머를 설정합니다.


//핑이 오면 자동으로 실행

static void gotPinged (byte* ptr) {

 ether.printIp(">>> ping from: ", ptr);

}


void setup () {

 Serial.begin(57600);

 Serial.println("\n[pings]");

 //begin 에서 저는 2번 핀이기 때문에 2가 들어있으며 그냥 8에 꼽고 하세요.

 if (ether.begin(sizeof Ethernet::buffer, mymac,2) == 0)

   Serial.println(F("Failed to access Ethernet controller"));

 if (!ether.dhcpSetup())

   Serial.println(F("DHCP failed"));


 ether.printIp("IP:  ", ether.myip);

 ether.printIp("GW:  ", ether.gwip);


#if 1

 // use DNS to locate the IP address we want to ping

 if (!ether.dnsLookup(PSTR("www.google.com")))

   Serial.println("DNS failed");

#else

 ether.parseIp(ether.hisip, "74.125.77.99");

#endif

 ether.printIp("SRV: ", ether.hisip);

   

//아까 핑이오면 자동으로 실행하는 녀석을 이곳에서 등록합니다.

 ether.registerPingCallback(gotPinged);

 

 timer = -9999999; // start timing out right away

 Serial.println();

}


void loop () {

 word len = ether.packetReceive(); // go receive new packets

 word pos = ether.packetLoop(len); // respond to incoming pings

 

 // report whenever a reply to our outgoing ping comes back

 if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {

   Serial.print("  ");

   Serial.print((micros() - timer) * 0.001, 3);

   Serial.println(" ms");

 }

 

 // ping a remote server once every few seconds

 if (micros() - timer >= 5000000) {

   ether.printIp("Pinging: ", ether.hisip);

   timer = micros();

   ether.clientIcmpRequest(ether.hisip);

 }

}



네 요기까지구요.



이젠 한 번 웹으로 반응하게 해보겠습니다.


  1. Web


웹으로 반응하게 하기 위해서는 다음과 같이 갑니다.


// Present a "Will be back soon web page", as stand-in webserver.

// 2011-01-30 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>


#define STATIC 0  // set to 1 to disable DHCP (adjust myip/gwip values below)

//0이면 dhcp ,1 이면 static으로 동작합니다.

#if STATIC

// ethernet interface ip address

static byte myip[] = { 192,168,1,200 };

// gateway ip address

static byte gwip[] = { 192,168,1,1 };

#endif


// ethernet mac address - must be unique on your network

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };


byte Ethernet::buffer[500]; // tcp/ip send and receive buffer


//이미 프로그램 된 녀석을 내보내게 합니다.

const char page[] PROGMEM =

"HTTP/1.0 503 Service Unavailable\r\n"

"Content-Type: text/html\r\n"

"Retry-After: 600\r\n"

"\r\n"

"<html>"

 "<head><title>"

   "Service Temporarily Unavailable"

 "</title></head>"

 "<body>"

   "<h3>This service is currently unavailable</h3>"

   "<p><em>"

     "The main server is currently off-line.<br />"

     "Please try again later."

   "</em></p>"

 "</body>"

"</html>"

;


void setup(){

 Serial.begin(57600);

 Serial.println("\n[backSoon]");

 

 if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)

   Serial.println( "Failed to access Ethernet controller");

#if STATIC

 ether.staticSetup(myip, gwip);

#else

 if (!ether.dhcpSetup())

   Serial.println("DHCP failed");

#endif


 ether.printIp("IP:  ", ether.myip);

 ether.printIp("GW:  ", ether.gwip);  

 ether.printIp("DNS: ", ether.dnsip);  

}


void loop(){

 // wait for an incoming TCP packet, but ignore its contents

 if (ether.packetLoop(ether.packetReceive())) {

   memcpy_P(ether.tcpOffset(), page, sizeof page);

   ether.httpServerReply(sizeof page - 1);

 }

}


반응형