Arduino Ethernet Shield Client.connect返回错误代码-5

时间:2012-09-08 01:10:53

标签: arduino

我在shield上有一个全新的以太网Arduino Uno,并且在尝试使用以太网屏蔽之前,已经完成了许多(非以太网)示例,没有任何问题。

使用提供的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.

3 个答案:

答案 0 :(得分:1)

不确定为什么这会有所帮助,但是在初始化Serial之后,在开始以太网之前添加延迟,并且在使用以太网之前增加延迟似乎也有效。

/*
  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 :(得分:0)

尝试定义Arduino Reference Page上显示的服务器IP地址:

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

尝试几个不同的示例程序。迁移到IDE 1.0可能会影响兼容性。

答案 2 :(得分: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