2012-09-08 80 views
2

我有一個全新的以太網shieldArduino Uno並已經通過許多(非以太網)示例沒有任何問題,直到我試圖使用以太網盾。Arduino以太網盾客戶端連接返回錯誤代碼-5

使用提供的EthernetClient示例,我得到連接失敗。返回碼是-5(我只能找到-4到1的答案)。

/* 
    Web client 

This sketch connects to a website (http://www.google.com) 
using an Arduino Wiznet Ethernet shield. 

Circuit: 
* Ethernet shield attached to pins 10, 11, 12, 13 

Created 18 Dec 2009 
Modified 9 Apr 2012 
by David A. Mellis 

*/ 

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

// Enter a MAC address for your controller below. 
// Newer Ethernet shields have a MAC address printed on a sticker on the shield 

byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0x71 };; 
char server[] = "google.com"; // 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): 
EthernetClient client; 

void setup() { 
// Open serial communications and wait for port to open: 
    Serial.begin(9600); 
    while (!Serial) { 
    ; // wait for serial port to connect. Needed for Leonardo only 
    } 

    // Start the Ethernet connection: 
    if (Ethernet.begin(mac) == 0) { 
    Serial.println("Failed to configure Ethernet using DHCP"); 
    // no point in carrying on, so do nothing forevermore: 
    for(;;) 
     ; 
    } 
    // Give the Ethernet shield a second to initialize: 
    delay(1000); 
    Serial.println("connecting..."); 

    Serial.println("Obtaining local IP address"); 
    IPAddress myIPAddress = Ethernet.localIP(); 
    Serial.println(myIPAddress); 
    // if you get a connection, report back via serial: 
    int ret = client.connect(server, 80); 
    if (ret == 1) { 
    Serial.println("connected"); 
    // Make a HTTP request: 
    client.println("GET /search?q=arduino HTTP/1.0"); 
    client.println(); 
    } 
    else { 
    // kf you didn't get a connection to the server: 
    Serial.println("Connection failed"); 
    Serial.println(ret); 
    Serial.println(client.status()); 
    } 
} 

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

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

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

的結果總是:

Connecting... 
Obtaining local IP address 
192.168.0.7 
Connection failed 
-5 
0 

disconnecting. 
+0

您是否嘗試過在服務器上執行wireshark捕獲以查看到達的流量 - 如果有的話? – ZnArK

+0

沒有流量進入服務器,但我的路由器確實租用了一個地址。 – Eddie

+0

我有完全相同的問題,我不知道如何解決它.. – andrepcg

回答

1

不知道爲什麼這幫助,但加入了串行初始化之後的延遲,在開始以太網之前,並且在使用以太網之前增加延遲似乎工作。

/* 
    Web client 

This sketch connects to a website (http://www.google.com) 
using an Arduino Wiznet Ethernet shield. 

Circuit: 
* Ethernet shield attached to pins 10, 11, 12, 13 

created 18 Dec 2009 
modified 9 Apr 2012 
by David A. Mellis 

*/ 

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

// Enter a MAC address for your controller below. 
// Newer Ethernet shields have a MAC address printed on a sticker on the shield 

byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0x71 };; 
char server[] = "google.com"; // 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): 
EthernetClient client; 

void setup() { 
// Open serial communications and wait for port to open: 
    Serial.begin(9600); 
    while (!Serial) { 
    ; // wait for serial port to connect. Needed for Leonardo only 
    } 
delay(5000); 
    // start the Ethernet connection: 
    if (Ethernet.begin(mac) == 0) { 
    Serial.println("Failed to configure Ethernet using DHCP"); 
    // no point in carrying on, so do nothing forevermore: 
    for(;;) 
     ; 
    } 
    // give the Ethernet shield a second to initialize: 
    delay(5000); 
    Serial.println("connecting..."); 

    Serial.println("Obtaining local IP"); 
    IPAddress myIPAddress = Ethernet.localIP(); 
    Serial.println(myIPAddress); 
    // if you get a connection, report back via serial: 
    int ret = client.connect(server, 80); 
    if (ret == 1) { 
    Serial.println("connected"); 
    // Make a HTTP request: 
    client.println("GET /search?q=arduino HTTP/1.0"); 
    client.println(); 
    } 
    else { 
    // kf you didn't get a connection to the server: 
    Serial.println("connection failed"); 
    Serial.println(ret); 
    Serial.println(client.status()); 
    } 
} 

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

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

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

我會喜歡有人告訴我爲什麼這會起作用,我可以通過消除這些延遲來重新創建問題,並通過重新插入來恢復它。我會認爲while(!Serial)。儘管(!客戶端)似乎永遠不會通過,所以我只是使用很長的延遲。 – Eddie

+1

我與OP有完全相同的問題,但延遲並沒有解決它... – andrepcg

+0

@Eddie:也許接受此答案? – angelatlarge

0

嘗試定義服務器的IP地址,因爲它是在Arduino Reference Page顯示:

byte server[] = { 64, 233, 187, 99 }; // Google 

嘗試了幾個不同的示例程序。對於可能影響兼容性的IDE 1.0進行了一些修改。

+0

64.233.187.99在我這裏不起作用... – xiaobai

+0

無論如何,使用主機名也不起作用,請參閱我對Jason的回答的評論。 – Eddie

+0

我並不是指特定的IP地址。我的意思是嘗試使用一個字節數組而不是'IPAddress ....' – ZnArK

0

Google IP地址(173.194.33.104)現在無效。嘗試使用74.125.226.242代替:

IPAddress server(74,125,226,242); // Google 

而且你試一下的Arduino之前,請確保您可以在瀏覽器中打開此IP地址:

http://74.125.226.242 
+0

是的,已經嘗試了許多IP,上面更新使用主機名。 – Eddie

相關問題