极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 114369|回复: 50

【翻译教程】enc28J60 和 Arduino (3)——作为客户端

[复制链接]
发表于 2012-10-9 08:38:48 | 显示全部楼层 |阅读模式
本帖最后由 zcbzjx 于 2013-2-22 19:50 编辑

    国庆节参加完小舅子的婚礼终于回北京了,继续。。。。



    第三篇文章,我会介绍如何连接一个网站并获取返回的信息。

    当你学会了如何(使用静态ip地址或者通过DHCP服务器自动获取ip地址)把你的Arduino连接到网络后,你需要学习如何向一个网站发送数据和接收该网站的返回数据。
    为了使下面的例子更实际,我编写了一个简单的php网页,每次连接将随机返回一句格言,地址如下http://www.lucadentella.it/demo/aphorisms.php

    我们来编写一个Arduino代码来获得这些格言并输出到串口。

DNS

    通常我们连接一个网站是键入他的域名(如www.sina.com.cn),我们的电脑,会通过一个dns服务器将他的域名解析为相应ip来进行连接。
   
    在arduino中配置你网络对应的dns服务器地址是非常重要的:

    如果你的arduino是采用DHCP服务器来进行配置,通常DNS服务器地址也是自动获取的。
    如果是静态配置,那么你调用 staticSetup()方法时需要包含DNS服务器地址。
  1. #include <EtherCard.h>
  2. static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
  3. static byte myip[] = {192,168,1,10};
  4. static byte gatewayip[] = {192,168,1,1};
  5. static byte dnsip[] = {151,99,125,2};
  6. ...
  7. ether.staticSetup(myip, gatewayip, dnsip);
复制代码
你可以用dnsLookup()方法验证域名服务器是否正常。
  1. char website[] PROGMEM = "www.lucadentella.it";
  2. ...
  3. if (!ether.dnsLookup(website))
  4.   Serial.println("DNS failed");
  5. else
  6.   Serial.println("DNS resolution done");
  7. ether.printIp("SRV IP:\t", ether.hisip);
复制代码
连接

    EtherCard库提供了一个非常方便的方法browseUrl()来连接一个网站,This method prepares the connection; which is completed in following steps performed(不知道咋个翻译) – during the loop - 通过我们已经学了的2个指令:
  1. ether.packetLoop(ether.packetReceive());
复制代码
非常重要的是需要保持这两个指令运行,才能使代码正常工作。
   
    browseUrl()方法需要一些参数:

    prog_char urlbuf,地址的 固定部分;
    const char * urlbuf_varpart,地址的变量部分;
    prog_char hoststr,网站的名称;
    void (*callback)(byte,word,word)),这是一个回调函数名称,连接结束时调用的函数。

    地址分为两部分,以优化内存的利用率:静态部分存储在flash中(PSTR()命令),以节省RAM空间。
   
回调函数

    回调函数包含连接结束时执行的一个动作,在这个例子,是在串口打印出网站返回的格言。
    你可以选择任意的函数名称,但必须声明如下的参数:
  1. static void response_callback (byte status, word off, word len)
复制代码
第一个参数为连接的状态,第二个参数为响应存储在buffer中的偏移量,第三个参数为响应的长度。

    要理解偏移量(OFF)参数的含义,你必须要注意的以太网连接发送和接收的所有数据存储在我们定义的缓冲区中:
  1. byte Ethernet::buffer[700];
复制代码
你收到数据,整个数据包(包含头文件及校验字段等),都存储在这个缓冲区中,偏移量值告诉我们从那个字段开始存储这个数据包,及哪儿是我们收到的网页响应。
    下面是一个回调函数在串口打印网页的响应:
  1. static void response_callback (byte status, word off, word len) {

  2.   Serial.print((const char*) Ethernet::buffer + off);
  3. }
复制代码
如果你运行这个代码(你可以在这个链接找到)(注意要把回调函数改成以上回调函数),你就可以看到如下信息:

    arduino正确配置了DHCP,它解析“www.lucadentella.it”这个域名,连接网站,打印网站响应。

    响应不仅包含格言,还包含了HTTP头文件。你需要打印所有的响应,然后通过文本编辑器(推荐notepad++),找到它们有多少个字符:

    这些行位于接收缓冲器中的格言之前。所以我们需要改变回调函数,打印207个字符之后的缓冲区的内容:
  1. static void response_callback (byte status, word off, word len) {

  2.   Serial.print((const char*) Ethernet::buffer + off + 207);
  3. }
复制代码
经过以上更改,你就可以接收到正确的数据:


返回目录

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2012-10-9 09:06:52 | 显示全部楼层
好贴。。。。
回复 支持 反对

使用道具 举报

发表于 2012-10-9 09:21:50 | 显示全部楼层
好帖,求外网连接内网教程
回复 支持 反对

使用道具 举报

发表于 2012-10-9 09:40:49 | 显示全部楼层
等了几天了,谢谢老师分享~!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-9 10:40:30 | 显示全部楼层
清水 发表于 2012-10-9 09:21
好帖,求外网连接内网教程

能具体点么。
回复 支持 反对

使用道具 举报

发表于 2012-10-9 12:19:34 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2012-10-9 12:26:24 | 显示全部楼层
zcbzjx 发表于 2012-10-9 10:40
能具体点么。

就是我用webserve将传感器采集来的数据在网页上显示出来,但现在只能内网查看,如果我想通过外网看一下我内网的网页。我同学说要路由器什么端口映射什么的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-9 13:07:11 | 显示全部楼层
恩,这个和这个etherCard库没什么关系了,和路由器设置有关,建议还是通过客户端的形式,将数据发送到外网平台。可以用yeelink平台或者cosm平台。
回复 支持 反对

使用道具 举报

发表于 2012-10-9 15:17:26 | 显示全部楼层
zcbzjx 发表于 2012-10-9 13:07
恩,这个和这个etherCard库没什么关系了,和路由器设置有关,建议还是通过客户端的形式,将数据发送到外网平 ...

哦,cosm我成功了,yeelink还没试过,等会我试一下。不过我还是希望自己架设服务器,这个有难度
回复 支持 反对

使用道具 举报

发表于 2012-10-22 07:31:47 | 显示全部楼层
ha好帖,这几帖弄一块就方便了
回复 支持 反对

使用道具 举报

发表于 2012-12-15 19:26:56 | 显示全部楼层
本帖最后由 葱头同学 于 2012-12-15 19:46 编辑

有没有完整的代码
噢 看到了 原文有链接
回复 支持 反对

使用道具 举报

发表于 2013-3-12 23:50:06 | 显示全部楼层
原作者没给出代码。但是在example中有。
  1. // Demo using DHCP and DNS to perform a web client request.
  2. // 2011-06-08 <[email protected]> http://opensource.org/licenses/mit-license.php

  3. #include <EtherCard.h>

  4. // ethernet interface mac address, must be unique on the LAN
  5. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

  6. byte Ethernet::buffer[700];
  7. static uint32_t timer;

  8. char website[] PROGMEM = "www.google.com";

  9. // called when the client request is complete
  10. static void my_callback (byte status, word off, word len) {
  11.   Serial.println(">>>");
  12.   Ethernet::buffer[off+300] = 0;
  13.   Serial.print((const char*) Ethernet::buffer + off);
  14.   Serial.println("...");
  15. }

  16. void setup () {
  17.   Serial.begin(57600);
  18.   Serial.println("\n[webClient]");

  19.   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  20.     Serial.println( "Failed to access Ethernet controller");
  21.   if (!ether.dhcpSetup())
  22.     Serial.println("DHCP failed");

  23.   ether.printIp("IP:  ", ether.myip);
  24.   ether.printIp("GW:  ", ether.gwip);  
  25.   ether.printIp("DNS: ", ether.dnsip);  

  26.   if (!ether.dnsLookup(website))
  27.     Serial.println("DNS failed");
  28.    
  29.   ether.printIp("SRV: ", ether.hisip);
  30. }

  31. void loop () {
  32.   ether.packetLoop(ether.packetReceive());
  33.   
  34.   if (millis() > timer) {
  35.     timer = millis() + 5000;
  36.     Serial.println();
  37.     Serial.print("<<< REQ ");
  38.     ether.browseUrl(PSTR("/foo/"), "bar", website, my_callback);
  39.   }
  40. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-3-12 23:54:40 | 显示全部楼层
改成本例子就是:
  1. // Demo using DHCP and DNS to perform a web client request.
  2. // 2011-06-08 <[email protected]> http://opensource.org/licenses/mit-license.php

  3. #include <EtherCard.h>

  4. // ethernet interface mac address, must be unique on the LAN
  5. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

  6. byte Ethernet::buffer[700];
  7. static uint32_t timer;

  8. char website[] PROGMEM = "www.lucadentella.it";

  9. // called when the client request is complete
  10. static void response_callback (byte status, word off, word len) {

  11.   Serial.print((const char*) Ethernet::buffer + off + 207);
  12. }

  13. void setup () {
  14.   Serial.begin(57600);
  15.   Serial.println("\n[webClient]");

  16.   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  17.     Serial.println( "Failed to access Ethernet controller");
  18.   if (!ether.dhcpSetup())
  19.     Serial.println("DHCP failed");

  20.   ether.printIp("IP:  ", ether.myip);
  21.   ether.printIp("GW:  ", ether.gwip);  
  22.   ether.printIp("DNS: ", ether.dnsip);  

  23.   if (!ether.dnsLookup(website))
  24.     Serial.println("DNS failed");
  25.    
  26.   ether.printIp("SRV: ", ether.hisip);
  27. }

  28. void loop () {
  29.   ether.packetLoop(ether.packetReceive());
  30.   
  31.   if (millis() > timer) {
  32.     timer = millis() + 5000;
  33.     Serial.println();
  34.     Serial.print("<<< REQ ");
  35.     ether.browseUrl(PSTR("/demo/"), "aphorisms.php", website, response_callback);
  36.   }
  37. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-3-13 00:00:13 | 显示全部楼层
由于他服务器的变化 已经变成了 HTTP/1.1 200 OK
Date: Tue, 12 Mar 2013 15:58:01 GMT
Server: Apache
X-Powered-By: PHP/5.3.3-7+squeeze14
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 98
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/html
X-Pad: avoid browser bug

259个字符了

最好还是通过两个换行实现。
回复 支持 反对

使用道具 举报

发表于 2013-3-13 00:03:36 | 显示全部楼层
原谅我的不仔细。
https://github.com/lucadentella/enc28j60_tutorial这是作者给的代码。
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-24 13:25 , Processed in 0.044792 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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