我正在使用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)
),但它也无法正常工作 .
I can't ping the shield IP address that shows in the router administrator page.
这个简单的代码有什么问题?
一切都是开箱即用的,Arduino和盾牌 . 我没有对它们做任何事情,只是将屏蔽连接到Arduino并发送代码 . 似乎一切都运转正常,两块板都闪烁着LEDs .
2 回答
这些循环没用..你可以试试这样的东西:
我不确定你的意思是“我还在代码中尝试了一个静态IP地址” . 如果您只是替换if(Ethernet.begin(MACaddress)== 0){
with if(Ethernet.begin(MACaddress,myIP)== 0){
结果可能是不可预测的,因为没有返回值 .
读
EthernetBegin
返回此函数的DHCP版本Ethernet.begin(mac)在成功的DHCP连接上返回int:1,在失败时返回0 . 其他版本不返回任何内容 .
你试过固定IP的例子吗?