【问题标题】:Arduino Ethernet Shield Client.connect returns error code -5Arduino Ethernet Shield Client.connect 返回错误代码 -5
【发布时间】:2012-09-10 08:01:59
【问题描述】:

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

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

【问题讨论】:

  • 您是否尝试过在服务器上进行wireshark 捕获以查看正在到达的流量(如果有)?
  • 没有流量到达服务器,但我的路由器确实租用了一个地址。
  • 我遇到了完全相同的问题,我不知道如何解决它..
  • 我使用 Wireshark 调试了很多相同的问题。

标签: arduino


【解决方案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(;;)
      ;
  }
}

【讨论】:

  • 我很想有人告诉我为什么这样有效,我可以通过消除这些延迟来重现问题,并通过将它们重新插入来治愈它。我认为 while(!Serial) 。 ALSO while(!client) 似乎永远不会通过,所以我只是使用了很长的延迟。
【解决方案2】:

该 Google IP 地址 (173.194.33.104) 现在无效。尝试改用 74.125.226.242:

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

在您在 Arduino 上试用之前,请确保您可以在浏览器中打开此 IP 地址:

 http://74.125.226.242

【讨论】:

  • 是的,已经尝试了许多 IP,上面更新为使用主机名。
【解决方案3】:

尝试定义服务器 IP 地址,如Arduino Reference Page 所示:

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

尝试几个不同的示例程序。迁移到 IDE 1.0 后进行了一些可能影响兼容性的修订。

【讨论】:

  • 无论如何,使用主机名也不起作用,请参阅我对 Jason 的回答的评论。
  • 我不是指特定的 IP 地址。我的意思是尝试使用字节数组而不是IPAddress ....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
相关资源
最近更新 更多