Arduino DHCP错误
我正在用Arduino Uno测试以太网,仅使用示例草图就出现了一个错误Arduino DHCP错误,arduino,ethernet,dhcp,Arduino,Ethernet,Dhcp,我正在用Arduino Uno测试以太网,仅使用示例草图就出现了一个错误 #include <SPI.h> #include <Ethernet.h> byte MACaddress[] = { 0x90, 0xAD, 0xDA, 0x0D, 0x96, 0xFE }; EthernetClient client; void setup() { Serial.begin(9600); while (!Serial) { ; }
#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() {
}
#包括
#包括
字节MACaddress[]={0x90,0xAD,0xDA,0x0D,0x96,0xFE};
以太网络客户端;
无效设置(){
Serial.begin(9600);
而(!串行){
;
}
//启动以太网连接:
if(Ethernet.begin(MACaddress)==0){
Serial.println(“未能使用DHCP配置以太网”);
对于(;;)
;
}
Serial.print(“我的IP地址:”);
对于(字节thisByte=0;thisByte<4;thisByte++){
Serial.print(Ethernet.localIP()[thisByte],DEC);
连续打印(“.”);
}
Serial.println();
}
void循环(){
}
我打开了路由器管理页面,我可以看到它给了Arduino一个,与。我还在代码中尝试了一个静态IP地址(Ethernet.begin(MACaddress,IPaddress)
),但它也不起作用
我无法ping路由器管理员页面中显示的屏蔽IP地址。
这个简单的代码有什么问题
一切都是现成的,Arduino和盾牌。我没有对他们做任何事情,只是把护盾连接到Arduino并发送了代码。看起来一切正常,两个电路板都在闪烁。这些环路没有用。。你能试试这样的东西吗
#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;
}
#如果定义(ARDUINO)和&ARDUINO>18
#包括
#恩迪夫
#包括
#包括
//MAC地址
字节mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
const char*ip_to_str(const uint8_t*);
//初始化以太网服务器库
服务器(8080);
无效设置()
{
Serial.begin(9600);
Serial.println(“试图获取DHCP租约…”);
//启动DHCP会话。参数是要启动的MAC(硬件)地址
//您希望使用以太网屏蔽。此呼叫将被阻止,直到DHCP恢复
//已获得租约。将定期重新发送请求,直到
//如果网络上没有DHCP服务器,或者
//服务器无法响应,此调用将永久阻止。
//因此,您也可以使用轮询模式来检查DHCP
//已获得租约,因此,如果服务器未获得租约,您可以作出反应
//响应(请参阅轮询DHCP示例)。
EthernetDHCP.begin(mac);
//因为我们在这里,这意味着我们现在有一个DHCP租约,所以我们打印
//我提供了一些信息。
常量字节*ipAddr=EthernetDHCP.ipAddress();
常量字节*gatewayAddr=EthernetDHCP.gatewayIpAddress();
常量字节*dnsAddr=EthernetDHCP.dnsIpAddress();
Serial.println(“已获得DHCP租约”);
串行打印(“我的IP地址是”);
Serial.println(ip_to_str(ipAddr));
串行打印(“网关IP地址为”);
串行打印LN(ip_至_str(网关地址));
串行打印(“DNS IP地址为”);
序列号println(ip_to_str(dnsAddr));
//启动服务器
server.begin();
}
void循环()
{
//您应该在循环()中定期调用此方法:它将允许
//DHCP库来维护您的DHCP租约,这意味着它将
//定期续订租约,如果无法续订租约,则重新绑定。
//因此,除非您在循环中的某个地方调用它,否则您的DHCP租约可能会失败
//过期,这可能是您不想要的:-)
EthernetDHCP.maintain();
//侦听传入的客户端
Client=server.available();
如果(客户){
//http请求以一个空行结束
布尔currentLineIsBlank=true;
while(client.connected()){
if(client.available()){
char c=client.read();
//如果你已经到了这一行的末尾(收到了一条新行)
//字符)且该行为空,则http请求已结束,
//所以你可以发送回复
如果(c='\n'&¤tLineIsBlank){
//发送标准http响应头
client.println(“HTTP/1.1200ok”);
client.println(“内容类型:text/html”);
client.println();
//一些杂项.HTML
client.println(“Arduino控制面板”);
client.println(“控制面板”);
client.println(“”);
//输出每个模拟输入引脚的值
对于(int-analogChannel=0;analogChannel<6;analogChannel++){
客户端打印(“模拟输入”);
客户端打印(模拟频道);
客户。打印(“is”);
client.print(analogRead(analogChannel));
client.println(“
”);
}
打破
}
如果(c=='\n'){
//你在开一条新的生产线
currentLineIsBlank=true;
}
如果(c!='\r'),则为else{
//当前行中有一个角色
currentLineIsBlank=false;
}
}
}
//为web浏览器提供接收数据的时间
延迟(1);
//关闭连接:
client.stop();
}
}
//只是一个很好地格式化IP地址的实用函数。
const char*ip_to_str(const uint8\u t*ipAddr)
{
静态字符buf[16];
sprintf(buf,“%d.%d.%d.%d\0”、ipAddr[0]、ipAddr[1]、ipAddr[2]、ipAddr[3]);
返回buf;
}
我不知道你说的“我在代码中也尝试了静态IP地址”是什么意思。如果你只是换了
if(Ethernet.begin(MACaddress)==0){
与
if(Ethernet.begin(MACaddress,myIP)==0){
结果可能不可预测,因为没有返回值
阅读
返回
此函数的DHCP版本Ethernet.begin(mac)在DHCP连接成功时返回int:1,在失败时返回0。其他版本不返回任何内容
您是否使用固定IP尝试过其中一个示例?有许多以太网屏蔽,请编辑问题以包括型号。示例代码针对一种类型,可能不兼容。如果不知道硬件是什么,人们在这方面无法提供帮助。