2011-09-30 8 views
2

DHCP를 통해 IP 주소를받는이 웹 클라이언트 예가 있습니다. IP 주소가 192.168.0.1 인 라우터에 성공적으로 연결되었지만 google.com에 GET HTTP를 보내지 못했습니다. 근본적으로, 나는 Arduino에서 인터넷으로 나가는 트래픽을 허용 할 수 없다.Arduino 이더넷 및 DHCP가 인터넷에 통신 할 수 없습니다.

저는 Arduino에 연결된 Linksys/Cisco E2000 router입니다.

내 코드는 다음과 같습니다.

#include <SPI.h> 
#include <Ethernet.h> 
#include <EthernetDHCP.h> 

// Enter a MAC address and IP address for your controller below. 
// The IP address will be dependent on your local network: 
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x78, 0x0B}; 
byte ip[] = {192, 168, 0, 125}; 
byte gateway[] = {192, 168, 0, 1}; 
byte subnet[] = {255,255,255,0}; 
byte serverLocal[] = { 192,168,0,1 }; // Google 
byte serverExternal[] = { 173,194,33,104 }; // Google 
// Initialize the Ethernet client library 
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP): 
Client clientLocal(serverLocal, 80); 
Client clientExternal(serverExternal, 80); 

const char* ip_to_str(const uint8_t*); 

void setup() { 
    Serial.begin(9600); 

    Serial.println("Attempting to obtain a DHCP lease..."); 

    // Initiate a DHCP session. The argument is the MAC (hardware) address that 
    // you want your Ethernet shield to use. This call will block until a DHCP 
    // lease has been obtained. The request will be periodically resent until 
    // a lease is granted, but if there is no DHCP server on the network or if 
    // the server fails to respond, this call will block forever. 
    // Thus, you can alternatively use polling mode to check whether a DHCP 
    // lease has been obtained, so that you can react if the server does not 
    // respond (see the PollingDHCP example). 
    EthernetDHCP.begin(mac); 

    // Since we're here, it means that we now have a DHCP lease, so we print 
    // out some information. 
    const byte* ipAddr = EthernetDHCP.ipAddress(); 
    const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress(); 
    const byte* dnsAddr = EthernetDHCP.dnsIpAddress(); 

    Serial.println("A DHCP lease has been obtained."); 

    Serial.print("My IP address is "); 
    Serial.println(ip_to_str(ipAddr)); 

    Serial.print("Gateway IP address is "); 
    Serial.println(ip_to_str(gatewayAddr)); 

    Serial.print("DNS IP address is "); 
    Serial.println(ip_to_str(dnsAddr)); 

    // if you get a connection, report back via serial: 
    if (clientLocal.connect()) { 
     Serial.println("connected internally"); 
     // Make a HTTP request: 
     clientLocal.println("GET /index.html HTTP/1.0"); 
     clientLocal.println(); 
    } 
    else { 
     // kf you didn't get a connection to the server: 
     Serial.println("connection failed internally"); 
    } 

    // if you get a connection, report back via serial: 
    if (clientExternal.connect()) { 
     Serial.println("connected externally"); 
     // Make a HTTP request: 
     clientExternal.println("GET /search?q=arduino HTTP/1.0"); 
     clientExternal.println(); 
    } 
    else { 
     // kf you didn't get a connection to the server: 
     Serial.println("connection failed externally"); 
    } 
} 

void loop() 
{ 
    // if there are incoming bytes available 
    // from the server, read them and print them: 
    if (clientLocal.available()) { 
     char c = clientLocal.read(); 
     Serial.print(c); 
    } 

    // if the server's disconnected, stop the client: 
    if (!clientLocal.connected()) { 
     Serial.println(); 
     Serial.println("disconnecting."); 
     clientLocal.stop(); 
    } 

    // if there are incoming bytes available 
    // from the server, read them and print them: 
    if (clientLocal.available()) { 
     char c = clientLocal.read(); 
     Serial.print(c); 
    } 

    // if the server's disconnected, stop the client: 
    if (!clientExternal.connected()) { 
     Serial.println(); 
     Serial.println("disconnecting."); 
     clientExternal.stop(); 

     // do nothing forevermore: 
     for(;;) 
      ; 
    } 
} 

// Just a utility function to nicely format an IP address. 
const char* ip_to_str(const uint8_t* ipAddr) 
{ 
    static char buf[16]; 
    sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); 
    return buf; 
} 
+0

tcpdump 또는 wireshark 등을 사용하여 트래픽을 검사하면 어떻게됩니까? –

+0

허브를 사용하려고 시도했습니다. – Daniel

+0

전 포트 포워딩, 방화벽, 수동으로 @IP 설정을 시도했습니다. 그래도 작동하도록 만드는 것보다 더 간단한 것이 있다고 확신합니까? – Daniel

답변

1

은 여기 example of working code이 (아마도 소스로 이것을 사용?)입니다. 내가 볼 수있는 유일한 차이는 라인이다 :

if (clientLocal.connect()) { 

내가 그것을 긴 비록 추가 Serial.begin()은 (어떤 효과가 있지만, delay() 가 필요 수있다 의심 :

Serial.begin(9600); 
delay(1000); 

바로 줄 앞에 shot ...)

관련 문제