Arduino以太网盾2无法正常工作

时间:2015-06-05 12:38:22

标签: arduino ethernet

我目前正试图让以太网盾在我的Mega上工作。我试图运行Webserver示例,但程序似乎停留在某一点,所以我试图从头开始。

这是我的测试代码:

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

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0F, 0xF6, 0x3D
};
byte subnet[] = { 255,0,0,0 };
byte gateway[] = { 2,0,0,1 };
IPAddress ip(2, 0, 0, 1);

EthernetServer server(80);

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.println("Ethernet started");
  server.begin();
  Serial.println("Server started");
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Loop");
}

我从串行控制台获得的输出是:

Etrted
Ethernet started

所以我认为该程序卡在了EthernetServer :: begin()函数中。 我知道有早期版本的以太网屏蔽与大型电脑不兼容,但我盾的供应商说它是。

我也不明白,为什么输出第一行。

感谢您的提示!

3 个答案:

答案 0 :(得分:1)

Arduino.cc和Arduino.org不一样......销售以太网盾2的Arduino.org有自己的IDE和正确的库!您可以在http://www.arduino.org/downloads下载该来源,可以在https://github.com/arduino-org/Arduino/tree/1.7.4/libraries

找到来源

答案 1 :(得分:0)

试试此代码表单(http://www.arduino.cc/en/Tutorial/DhcpAddressPrinter):

#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[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// 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);
  // this check is only needed on the Leonardo:
   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(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
}

void loop() {

}

并发布串口监视器输出的内容。

答案 2 :(得分:0)

试试这个:

NB。您可以重用为Arduino Ethernet Shield编写的代码,只需替换

即可
#include <Ethernet.h>  -->  #include <Ethernet2.h>
#include <EthernetUdp.h>  -->  #include <EthernetUdp2.h>

请参阅:http://labs.arduino.org/Arduino+Ethernet+Shield+2