c++ - 当禁用路由器上的 DHCP 时,以太网模块 ENC28C60 无法连接到互联网

标签 c++ networking arduino

使用 this库 我尝试连接静态 IP 但它没有连接到互联网。如果在路由器上启用了 DHCP 并删除 ether.staticSetup(myip, gwip, dnsip, netmask); 它会起作用。我使用 arduino MEGA。静态 IP 已经过测试并可在其他设备上运行。

问题:如何正确设置静态ip?

注意:我在这个社区发布这个问题是因为它是关于代码而不是电子产品。

连接:

VCC -   3.3V
GND -    GND 
SCK - Pin 52
SO  - Pin 50
SI  - Pin 51
CS  - Pin 53 # Selectable with the ether.begin() function

代码:

// This is a demo of the RBBB running as webserver with the Ether Card
// 2010-05-28 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = { xxx, xxx, 216, 203 };
static byte gwip[] = { xxx, xxx, 216, 126 };
static byte dnsip[] = { 8, 8, 8, 8};
static byte netmask[] = { 255, 255, 255, 0};
byte Ethernet::buffer[500];
BufferFiller bfill;

void setup () {
  Serial.begin(9600);
  if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
    Serial.println(F("Failed to access Ethernet controller"));
  ether.staticSetup(myip, gwip, dnsip, netmask);

  while (ether.clientWaitingGw())
    ether.packetLoop(ether.packetReceive());
  Serial.println("\nGateway found");
  Serial.print("ether.clientWaitingGw=");// just to test
  Serial.print(ether.clientWaitingGw());
}


void loop () {

  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (pos) { // check if valid tcp data is received
    ether.httpServerReply(homePage());// send web page data
    Serial.println("\nSend web data");
  }
delay(1000);
}


static word homePage() {
  long t = millis() / 1000;
  word h = t / 3600;
  byte m = (t / 60) % 60;
  byte s = t % 60;
  bfill = ether.tcpOffset();
  bfill.emit_p(PSTR(
                 "HTTP/1.0 200 OK\r\n"
                 "Content-Type: text/html\r\n"
                 "Pragma: no-cache\r\n"
                 "\r\n"
                 "<meta http-equiv='refresh' content='1'/>"
                 "<title>RBBB server</title>"
                 "<h1>$D$D:$D$D:$D$D</h1>"),
               h / 10, h % 10, m / 10, m % 10, s / 10, s % 10);
  return bfill.position();
}

输出:

XXX.XXX.216.203
XXX.XXX.216.126
8.8.8.8
255.255.255.0

Gateway found
Fail to send web data

最佳答案

经过研究,循环功能的延迟导致请求超时。通过删除 delay(1000); 它起作用了

用静态ip更正代码:

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = { xxx, xxx, 216, 203 };
static byte gwip[] = { xxx, xxx, 216, 126 };
static byte dnsip[] = { 8, 8, 8, 8};
static byte netmask[] = { 255, 255, 255, 0};
byte Ethernet::buffer[500];
BufferFiller bfill;

void setup () {
  Serial.begin(9600);
  if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)// 53 only on MEGA
    Serial.println(F("Failed to access Ethernet controller"));
  ether.staticSetup(myip, gwip, dnsip, netmask);

  while (ether.clientWaitingGw())
    ether.packetLoop(ether.packetReceive());
  Serial.println("\nGateway found");
  Serial.print("ether.clientWaitingGw=");// just to test
  Serial.print(ether.clientWaitingGw());
}


void loop () {

  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (pos) { // check if valid tcp data is received
    ether.httpServerReply(homePage());// send web page data
    Serial.println("\nSend web data");
  }

关于c++ - 当禁用路由器上的 DHCP 时,以太网模块 ENC28C60 无法连接到互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454320/

相关文章:

c++ - 如何从 C++ 中的另一个主函数调用 Google Tests

windows - 查找网络驱动器的 UNC 路径?

javascript - Websocket base64,二进制类型

Arduino LCD仅在底行显示黑框

c++ - 贪吃蛇游戏 - 食物 block 的随机数生成器

c++ - 重载函数调用 double 是不明确的

c++ - getline:找不到标识符

c++ - Qt4中如何使用libavcodec?

c++ - VertexBuffer 混合顶点

android - 从 Android 访问信标帧信息