【问题标题】:Arduino DHCP errorArduino DHCP 错误
【发布时间】:2013-01-18 16:32:02
【问题描述】:

我正在使用 Arduino Uno 测试以太网 shield,但仅使用示例草图就会出现 DHCP 错误。

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

byte MACaddress[] = { 0x90, 0xAD, 0xDA, 0x0D, 0x96, 0xFE };

EthernetClient client;

void setup() {
    Serial.begin(9600);
    while (!Serial) {
        ;
    }

    // Start the Ethernet connection:
    if (Ethernet.begin(MACaddress) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        for(;;)
            ;
    }
    Serial.print("My IP address: ");
    for (byte thisByte = 0; thisByte < 4; thisByte++) {
        Serial.print(Ethernet.localIP()[thisByte], DEC);
        Serial.print(".");
    }
    Serial.println();
}

void loop() {
}

我打开了路由器管理页面,我可以看到它给了 Arduino 一个IP address,与MAC address 相关联。我也在代码中尝试了静态 IP 地址 (Ethernet.begin(MACaddress, IPaddress)),但它也不起作用。

我无法ping通路由器管理员页面中显示的屏蔽IP地址。

这个简单的代码有什么问题?

一切都是开箱即用的,Arduino 和盾牌。我没有对它们做任何事情,只是将屏蔽连接到 Arduino 并发送代码。似乎一切正常,LEDs 两块板都在闪烁。

【问题讨论】:

  • 有很多以太网屏蔽,编辑问题包含模型。示例代码适用于一种类型,可能不兼容。如果人们不知道硬件是什么,他们就无法在这方面提供帮助。

标签: arduino ethernet dhcp


【解决方案1】:

这些循环没用..你能试试类似的东西吗:

#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDHCP.h>

// MAC Address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

const char* ip_to_str(const uint8_t*);

// Initialize the Ethernet server library
Server server(8080);

void setup()
{
  Serial.begin(9600);

  Serial.println("Attempting to obtain a DHCP lease...");

  // Initiate a DHCP session. The argument is the MAC (hardware) address that
  // you want your Ethernet shield to use. This call will block until a DHCP
  // lease has been obtained. The request will be periodically resent until
  // a lease is granted, but if there is no DHCP server on the network or if
  // the server fails to respond, this call will block forever.
  // Thus, you can alternatively use polling mode to check whether a DHCP
  // lease has been obtained, so that you can react if the server does not
  // respond (see the PollingDHCP example).
  EthernetDHCP.begin(mac);

  // Since we're here, it means that we now have a DHCP lease, so we print
  // out some information.
  const byte* ipAddr = EthernetDHCP.ipAddress();
  const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  const byte* dnsAddr = EthernetDHCP.dnsIpAddress();

  Serial.println("A DHCP lease has been obtained.");

  Serial.print("My IP address is ");
  Serial.println(ip_to_str(ipAddr));

  Serial.print("Gateway IP address is ");
  Serial.println(ip_to_str(gatewayAddr));

  Serial.print("DNS IP address is ");
  Serial.println(ip_to_str(dnsAddr));

  // Start the server
   server.begin();
}

void loop()
{
  // You should periodically call this method in your loop(): It will allow
  // the DHCP library to maintain your DHCP lease, which means that it will
  // periodically renew the lease and rebind if the lease cannot be renewed.
  // Thus, unless you call this somewhere in your loop, your DHCP lease might
  // expire, which you probably do not want :-)
  EthernetDHCP.maintain();

  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // Some misc. HTML 
          client.println("<title>Arduino Control Panel</title>");
          client.println("<center><h1>Control Panel</h1></center>");
          client.println("<p></p>");

          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("Analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}

【讨论】:

    【解决方案2】:

    我不确定您所说的“我还在代码中尝试了静态 IP 地址”是什么意思。如果你只是更换 if (Ethernet.begin(MACaddress) == 0) {

    与 if (Ethernet.begin(MACaddress, myIP) == 0) {

    结果可能无法预料,因为没有返回值。

    阅读

    EthernetBegin

    返回 此函数的 DHCP 版本 Ethernet.begin(mac) 返回一个 int: 1 表示成功的 DHCP 连接,0 表示失败。其他版本不返回任何内容。

    您是否尝试过使用固定 IP 的示例之一?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      相关资源
      最近更新 更多