2

我在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.
4

3 回答 3

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(;;)
      ;
  }
}
于 2012-09-08T02:04:15.213 回答
0

尝试定义服务器 IP 地址,如Arduino 参考页所示:

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

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

于 2012-09-08T01:23:50.070 回答
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
于 2012-09-08T01:25:10.557 回答