303931362 发表于 2014-2-21 08:09:52

请大神们赐教ENC28J60模块的问题

ENC28J60模块作为客户端,为什么不能获取局域网网页的内容;
ENC28J60模块能ping的通,自己电脑能打得开,却获取不到内容;

#include <EtherCard.h>
static byte mymac[] = {0x00,0x1A,0x4B,0x38,0x0C,0x5C};
static byte myip[] = {192,168,5,106};
static byte gatewayip[] = {192,168,5,1};
static byte dnsip[] ={192,168,5,1};
static byte mymask[]={255,255,255,0};
static byte targetmac[] = {0x00,0x1A,0x4B,0x38,0x0F,0x5C};
byte Ethernet::buffer;
char website[] PROGMEM = "192.168.5.108:8080";//www.lucadentella.it
static uint32_t timer;
static void response_callback (byte status, word off, word len) {
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Serial.println((const char*) Ethernet::buffer + off );
Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}

void setup () {
Serial.begin(57600);
Serial.println("MY C Demo");
if (!ether.begin(sizeof Ethernet::buffer, mymac, 53))
    Serial.println( "Failed to access Ethernet controller");
else
    Serial.println("Ethernet controller initialized");
// if (!ether.dhcpSetup())
//   Serial.println("DHCP failed");
// else
// {
    ether.staticSetup(myip,gatewayip);
    ether.copyIp(ether.dnsip,dnsip);
    ether.copyIp(ether.mymask,mymask);
// }
// Serial.println("DHCP configuration done");
ether.printIp("IP Address:\t", ether.myip);
ether.printIp("Netmask:\t", ether.mymask);
ether.printIp("Gateway:\t", ether.gwip);
ether.printIp("dnsip:\t", ether.dnsip);
Serial.println();
if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
else
    Serial.println("DNS resolution done");
ether.printIp("SRV IP:\t", ether.hisip);
Serial.println();
}

void loop() {
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
    timer = millis() + 5000;
    ether.browseUrl(PSTR("/examples/servlets/servlet/"), "HelloWorldExample", "192.168.5.108:8080", response_callback);
}
}



瘦网虫 发表于 2014-2-21 09:38:26

提示的很清楚,dns failed,解析服务器的ip地址错。
手工设置下试试。

303931362 发表于 2014-2-21 13:46:38

瘦网虫 发表于 2014-2-21 09:38 static/image/common/back.gif
提示的很清楚,dns failed,解析服务器的ip地址错。
手工设置下试试。

我只是访问局域网里的web,应该不用设置DNS也可以访问的啊,为何response_callback没输出信息呢?

shihaipeng04 发表于 2014-2-21 14:20:32

真的很高科技,还没玩过网卡这部分。 帮你出点搜主义,试试其他网页的内容,比如外网的?这样至少排除了,你网卡盾和程序的问题,如果外网也出不来那就找程序的问题呗?

303931362 发表于 2014-2-21 14:38:15

shihaipeng04 发表于 2014-2-21 14:20 static/image/common/back.gif
真的很高科技,还没玩过网卡这部分。 帮你出点搜主义,试试其他网页的内容,比如外网的?这样至少排除了,你 ...

如果网线接在路由器上(路由器可以连接外网),这样可以输出web内容的,但是我需要没有外网的情况下输出局域网web的内容,好像不行,不知道能不能解决呢

瘦网虫 发表于 2014-2-21 15:58:47

端口问题吧。可能在enc28j60上设置端口不是这样的。表追问,我不懂,猜的。可以自己去看下库。

托马斯 发表于 2014-2-21 18:33:19

这是因为<EtherCard.h>在以太网层,总是先向网关发数据,我的方法是内网服务器和网关设置成同一IP
#include <EtherCard.h>
#include <EEPROM.h>
#include <avr/wdt.h>

#define REQUEST_RATE 5000 // milliseconds

static byte mymac[] = {0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = {192,168,10,100 };
static byte gwip[] = {192,168,10,100 };
static byte dnsip[] = {   192,168,10,100};

// remote website name
char website[] PROGMEM = "192.168.10.100";
static byte hisip[] = {   192,168,10,100};
char path[] PROGMEM = "/thcpu/uploaddata.jsp";
char header[] PROGMEM = "U-ApiKey: a";
staticchar andataBuf;
char*zone = "A1";
byte Ethernet::buffer;   // a very small tcp/ip buffer is enough here
static long timer;
static int errcount = 0;   //
static int okcount = 0;   //

// called when the client request is complete

void Reboot()
{
wdt_disable() ;
wdt_enable(WDTO_15MS);
while(1)
{
}
}


void setup () {

Serial.begin(115200);
Serial.println("WebClient linjingmi");
getconfig();
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");   
ether.staticSetup(myip, gwip,dnsip);
ether.copyIp(ether.hisip,hisip );
ether.hisport = 8080;

ether.printIp("IP Address:\t", ether.myip);
ether.printIp("Gateway:\t", ether.gwip);
   
timer = - REQUEST_RATE+2000; // start timing out right away
//ether.persistTcpConnection(true);   // if notset, only 512bytes return in callback

initDns();
//while (ether.clientWaitingGw()) ether.packetLoop(ether.packetReceive());

wdt_enable(WDTO_8S);


}

void loop () {
wdt_reset();
ether.packetLoop(ether.packetReceive());

if (millis() > timer + REQUEST_RATE) {
    if (millis() > 3600000) {Reboot();}
    if(errcount > 10) {Reboot();}
    timer = millis();   
    send2server();   
}
}

void send2server() {
Serial.println("\n>>> REQ");   
sprintf(andataBuf,"zone=%s&signal=%dA%dB%dC%dD%dE%dF%dG%d",zone,analogRead(0),analogRead(1),analogRead(2),analogRead(3),analogRead(4),analogRead(5),analogRead(6),analogRead(7));
ether.httpPost (path, website,header ,andataBuf, my_result_cb);
   Serial.println(andataBuf);
Serial.println("wait");
errcount=errcount+1;

}
static void my_result_cb (byte status, word off, word len) {
errcount=0;
//okcount=okcount+1;
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
/*
if(strstr((char *)Ethernet::buffer + off, "zoneuploaddataisok") != 0) {
    Serial.println("Received ok command");
}
elseSerial.println("Received no ok command");
*/

}

//dns提速
void initDns(){
long timer2;
//waitting status
timer2 = millis();
while (!ether.isLinkUp() ) {
    if( millis() - timer2 < 10000 )
      ether.packetLoop(ether.packetReceive());
    else break;
}
timer2 = millis();
while( ether.clientWaitingGw() ){
    if( millis() - timer2 < 10000 )
      ether.packetLoop(ether.packetReceive());
    else break;
}
}


void getconfig() {
intdeviceID=EEPROM.read(0);
if (deviceID <= 10){
    Serial.print( "READ EEPROMINFO:");
    Serial.println(deviceID, DEC);

    myip = myip +deviceID;
    mymac = mymac +deviceID;
}
switch (deviceID) {
case 1:
    zone="A1";
    break;
case 2:
    zone="A2";
    break;
case 3:
    zone="B2";
    break;
case 4:
    zone="B11";
    break;
case 5:
    zone="B12";
    break;      
default:
    zone="A1";
}


}



托马斯 发表于 2014-2-21 18:47:04

7楼的ip地址和远程服务器设成同一地址了,现重发一遍
#include <EtherCard.h>
#include <EEPROM.h>
#include <avr/wdt.h>

#define REQUEST_RATE 5000 // milliseconds

static byte mymac[] = {0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = {192,168,10,99};
static byte gwip[] = {192,168,10,100 };
static byte dnsip[] = {   192,168,10,100 };

// remote website name
char website[] PROGMEM = "192.168.10.100 ";
static byte hisip[] = {   192,168,10,100 };
char path[] PROGMEM = "/thcpu/uploaddata.jsp";
char header[] PROGMEM = "U-ApiKey: a";
staticchar andataBuf;
char*zone = "A1";
byte Ethernet::buffer;   // a very small tcp/ip buffer is enough here
static long timer;
static int errcount = 0;   //
static int okcount = 0;   //


void Reboot()
{
wdt_disable() ;
wdt_enable(WDTO_15MS);
while(1)
{
}
}


void setup () {

Serial.begin(115200);
Serial.println("WebClient");
getconfig();
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");   
ether.staticSetup(myip, gwip,dnsip);
ether.copyIp(ether.hisip,hisip );
ether.hisport = 8080;

ether.printIp("IP Address:\t", ether.myip);
ether.printIp("Gateway:\t", ether.gwip);
   
timer = - REQUEST_RATE+2000; // start timing out right away
//ether.persistTcpConnection(true);   // if notset, only 512bytes return in callback

initDns();
//while (ether.clientWaitingGw()) ether.packetLoop(ether.packetReceive());

wdt_enable(WDTO_8S);


}

void loop () {
wdt_reset();
ether.packetLoop(ether.packetReceive());

if (millis() > timer + REQUEST_RATE) {
    if (millis() > 3600000) {Reboot();}
    if(errcount > 10) {Reboot();}
    timer = millis();   
    send2server();   
}
}

void send2server() {
Serial.println("\n>>> REQ");   
sprintf(andataBuf,"zone=%s&signal=%dA%dB%dC%dD%dE%dF%dG%d",zone,analogRead(0),analogRead(1),analogRead(2),analogRead(3),analogRead(4),analogRead(5),analogRead(6),analogRead(7));
ether.httpPost (path, website,header ,andataBuf, my_result_cb);
   Serial.println(andataBuf);
Serial.println("wait");
errcount=errcount+1;

}
static void my_result_cb (byte status, word off, word len) {
errcount=0;
//okcount=okcount+1;
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
/*
if(strstr((char *)Ethernet::buffer + off, "zoneuploaddataisok") != 0) {
    Serial.println("Received ok command");
}
elseSerial.println("Received no ok command");
*/

}

//dns提速
void initDns(){
long timer2;
//waitting status
timer2 = millis();
while (!ether.isLinkUp() ) {
    if( millis() - timer2 < 10000 )
      ether.packetLoop(ether.packetReceive());
    else break;
}
timer2 = millis();
while( ether.clientWaitingGw() ){
    if( millis() - timer2 < 10000 )
      ether.packetLoop(ether.packetReceive());
    else break;
}
}


void getconfig() {
intdeviceID=EEPROM.read(0);
if (deviceID <= 10){
    Serial.print( "READ EEPROMINFO:");
    Serial.println(deviceID, DEC);

    myip = myip +deviceID;
    mymac = mymac +deviceID;
}
switch (deviceID) {
case 1:
    zone="A1";
    break;
case 2:
    zone="A2";
    break;
case 3:
    zone="B2";
    break;
case 4:
    zone="B11";
    break;
case 5:
    zone="B12";
    break;      
default:
    zone="A1";
}


}



303931362 发表于 2014-2-22 08:55:44

托马斯 发表于 2014-2-21 18:47 static/image/common/back.gif
7楼的ip地址和远程服务器设成同一地址了,现重发一遍

非常感谢,这个代码可以用,不过为什么输出的只是信息,而不是内容呢?

<<< reply 7 ms
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"55-1393030446205"
Last-Modified: Sat, 22 Feb 2014 00:54:06 GMT
Content-Type: text/html
Content-Length: 55
Date: Sat, 22 Feb 2014 00:56:01 GMT
Connection: close
页: [1]
查看完整版本: 请大神们赐教ENC28J60模块的问题