极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11959|回复: 8

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

[复制链接]
发表于 2014-2-21 08:09:52 | 显示全部楼层 |阅读模式
ENC28J60模块作为客户端,为什么不能获取局域网网页的内容;
ENC28J60模块能ping的通,自己电脑能打得开,却获取不到内容;

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

  16. void setup () {
  17.   Serial.begin(57600);
  18.   Serial.println("MY C Demo");
  19.   if (!ether.begin(sizeof Ethernet::buffer, mymac, 53))
  20.     Serial.println( "Failed to access Ethernet controller");
  21.   else
  22.     Serial.println("Ethernet controller initialized");
  23. // if (!ether.dhcpSetup())
  24. //   Serial.println("DHCP failed");
  25. // else
  26. // {
  27.     ether.staticSetup(myip,gatewayip);
  28.     ether.copyIp(ether.dnsip,dnsip);
  29.     ether.copyIp(ether.mymask,mymask);
  30. // }
  31. // Serial.println("DHCP configuration done");
  32.   ether.printIp("IP Address:\t", ether.myip);
  33.   ether.printIp("Netmask:\t", ether.mymask);
  34.   ether.printIp("Gateway:\t", ether.gwip);
  35.   ether.printIp("dnsip:\t", ether.dnsip);
  36.   Serial.println();
  37.   if (!ether.dnsLookup(website))
  38.     Serial.println("DNS failed");
  39.   else
  40.     Serial.println("DNS resolution done");  
  41.   ether.printIp("SRV IP:\t", ether.hisip);
  42.   Serial.println();
  43. }

  44. void loop() {
  45.   ether.packetLoop(ether.packetReceive());
  46.   if (millis() > timer) {
  47.     timer = millis() + 5000;
  48.     ether.browseUrl(PSTR("/examples/servlets/servlet/"), "HelloWorldExample", "192.168.5.108:8080", response_callback);
  49.   }
  50. }
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-2-21 09:38:26 | 显示全部楼层
提示的很清楚,dns failed,解析服务器的ip地址错。
手工设置下试试。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-21 13:46:38 | 显示全部楼层
瘦网虫 发表于 2014-2-21 09:38
提示的很清楚,dns failed,解析服务器的ip地址错。
手工设置下试试。

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

使用道具 举报

发表于 2014-2-21 14:20:32 | 显示全部楼层
真的很高科技,还没玩过网卡这部分。 帮你出点搜主义,试试其他网页的内容,比如外网的?这样至少排除了,你网卡盾和程序的问题,如果外网也出不来那就找程序的问题呗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-21 14:38:15 | 显示全部楼层
shihaipeng04 发表于 2014-2-21 14:20
真的很高科技,还没玩过网卡这部分。 帮你出点搜主义,试试其他网页的内容,比如外网的?这样至少排除了,你 ...

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

使用道具 举报

发表于 2014-2-21 15:58:47 | 显示全部楼层
端口问题吧。可能在enc28j60上设置端口不是这样的。表追问,我不懂,猜的。可以自己去看下库。
回复 支持 反对

使用道具 举报

发表于 2014-2-21 18:33:19 | 显示全部楼层
这是因为<EtherCard.h>在以太网层,总是先向网关发数据,我的方法是内网服务器和网关设置成同一IP

  1. #include <EtherCard.h>
  2. #include <EEPROM.h>
  3. #include <avr/wdt.h>

  4. #define REQUEST_RATE 5000 // milliseconds

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

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

  20. // called when the client request is complete

  21. void Reboot()
  22. {
  23.   wdt_disable() ;
  24.   wdt_enable(WDTO_15MS);
  25.   while(1)
  26.   {
  27.   }
  28. }


  29. void setup () {

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

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

  43.   initDns();
  44.   //while (ether.clientWaitingGw()) ether.packetLoop(ether.packetReceive());
  45.   
  46.   wdt_enable(WDTO_8S);
  47.   
  48.   
  49. }

  50. void loop () {
  51.   wdt_reset();
  52.   ether.packetLoop(ether.packetReceive());

  53.   if (millis() > timer + REQUEST_RATE) {
  54.     if (millis() > 3600000) {Reboot();}
  55.     if  (errcount > 10) {Reboot();}
  56.     timer = millis();   
  57.     send2server();   
  58.   }
  59. }

  60. void send2server() {
  61.   Serial.println("\n>>> REQ");   
  62.   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));  
  63.   ether.httpPost (path, website,header ,andataBuf, my_result_cb);
  64.    Serial.println(andataBuf);
  65.   Serial.println("wait");
  66.   errcount=errcount+1;

  67. }
  68. static void my_result_cb (byte status, word off, word len) {
  69.   errcount=0;
  70.   //okcount=okcount+1;
  71.   Serial.print("<<< reply ");
  72.   Serial.print(millis() - timer);
  73.   Serial.println(" ms");
  74.   Serial.println((const char*) Ethernet::buffer + off);
  75.   /*
  76.   if(strstr((char *)Ethernet::buffer + off, "zoneuploaddataisok") != 0) {
  77.     Serial.println("Received ok command");
  78.   }  
  79.   else  Serial.println("Received no ok command");
  80. */

  81. }

  82. //dns提速
  83. void initDns(){
  84.   long timer2;
  85.   //waitting status
  86.   timer2 = millis();
  87.   while (!ether.isLinkUp() ) {
  88.     if( millis() - timer2 < 10000 )
  89.       ether.packetLoop(ether.packetReceive());
  90.     else break;
  91.   }
  92.   timer2 = millis();
  93.   while( ether.clientWaitingGw() ){
  94.     if( millis() - timer2 < 10000 )
  95.       ether.packetLoop(ether.packetReceive());
  96.     else break;
  97.   }
  98. }


  99. void getconfig() {
  100.   int  deviceID=EEPROM.read(0);
  101.   if (deviceID <= 10)  {
  102.     Serial.print( "READ EEPROMINFO:");
  103.     Serial.println(  deviceID, DEC);

  104.     myip[3] = myip[3] +deviceID;
  105.     mymac[5] = mymac[5] +deviceID;
  106.   }
  107.   switch (deviceID) {
  108.   case 1:
  109.     zone="A1";
  110.     break;
  111.   case 2:
  112.     zone="A2";
  113.     break;
  114.   case 3:
  115.     zone="B2";
  116.     break;
  117.   case 4:
  118.     zone="B11";
  119.     break;
  120.   case 5:
  121.     zone="B12";
  122.     break;      
  123.   default:
  124.     zone="A1";
  125.   }
  126.   

  127. }



复制代码
回复 支持 反对

使用道具 举报

发表于 2014-2-21 18:47:04 | 显示全部楼层
7楼的ip地址和远程服务器设成同一地址了,现重发一遍

  1. #include <EtherCard.h>
  2. #include <EEPROM.h>
  3. #include <avr/wdt.h>

  4. #define REQUEST_RATE 5000 // milliseconds

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

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

  20.   
  21. void Reboot()
  22. {
  23.   wdt_disable() ;
  24.   wdt_enable(WDTO_15MS);
  25.   while(1)
  26.   {
  27.   }
  28. }


  29. void setup () {

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

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

  43.   initDns();
  44.   //while (ether.clientWaitingGw()) ether.packetLoop(ether.packetReceive());
  45.   
  46.   wdt_enable(WDTO_8S);
  47.   
  48.   
  49. }

  50. void loop () {
  51.   wdt_reset();
  52.   ether.packetLoop(ether.packetReceive());

  53.   if (millis() > timer + REQUEST_RATE) {
  54.     if (millis() > 3600000) {Reboot();}
  55.     if  (errcount > 10) {Reboot();}
  56.     timer = millis();   
  57.     send2server();   
  58.   }
  59. }

  60. void send2server() {
  61.   Serial.println("\n>>> REQ");   
  62.   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));  
  63.   ether.httpPost (path, website,header ,andataBuf, my_result_cb);
  64.    Serial.println(andataBuf);
  65.   Serial.println("wait");
  66.   errcount=errcount+1;

  67. }
  68. static void my_result_cb (byte status, word off, word len) {
  69.   errcount=0;
  70.   //okcount=okcount+1;
  71.   Serial.print("<<< reply ");
  72.   Serial.print(millis() - timer);
  73.   Serial.println(" ms");
  74.   Serial.println((const char*) Ethernet::buffer + off);
  75.   /*
  76.   if(strstr((char *)Ethernet::buffer + off, "zoneuploaddataisok") != 0) {
  77.     Serial.println("Received ok command");
  78.   }  
  79.   else  Serial.println("Received no ok command");
  80. */

  81. }

  82. //dns提速
  83. void initDns(){
  84.   long timer2;
  85.   //waitting status
  86.   timer2 = millis();
  87.   while (!ether.isLinkUp() ) {
  88.     if( millis() - timer2 < 10000 )
  89.       ether.packetLoop(ether.packetReceive());
  90.     else break;
  91.   }
  92.   timer2 = millis();
  93.   while( ether.clientWaitingGw() ){
  94.     if( millis() - timer2 < 10000 )
  95.       ether.packetLoop(ether.packetReceive());
  96.     else break;
  97.   }
  98. }


  99. void getconfig() {
  100.   int  deviceID=EEPROM.read(0);
  101.   if (deviceID <= 10)  {
  102.     Serial.print( "READ EEPROMINFO:");
  103.     Serial.println(  deviceID, DEC);

  104.     myip[3] = myip[3] +deviceID;
  105.     mymac[5] = mymac[5] +deviceID;
  106.   }
  107.   switch (deviceID) {
  108.   case 1:
  109.     zone="A1";
  110.     break;
  111.   case 2:
  112.     zone="A2";
  113.     break;
  114.   case 3:
  115.     zone="B2";
  116.     break;
  117.   case 4:
  118.     zone="B11";
  119.     break;
  120.   case 5:
  121.     zone="B12";
  122.     break;      
  123.   default:
  124.     zone="A1";
  125.   }
  126.   

  127. }



复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-22 08:55:44 | 显示全部楼层
托马斯 发表于 2014-2-21 18:47
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
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 14:48 , Processed in 0.079678 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表