gthrtg 发表于 2015-11-2 14:31:04

关于EtherCard的webClient代码分析

以下代码摘自EtherCard的webClient实例,主要功能是打开指定网址,
Serial.print((const char*) Ethernet::buffer + off);这一行显示服务器返回的数据。当输出一个没有任何格式的页面的时候,也返回一些服务器的信息,我的想法是过滤掉服务器的信息,看这里谁做过类似的方案。
能取得数据的话,就可以在web上面设置Arduino程序里面的参数了。
ARDUINO
01.//>>> The latest version of this code can be found at https://github.com/jcw/ !!

02.

03.// Demo using DHCP and DNS to perform a web client request.

04.// 2011-06-08 <[email protected]> http://opensource.org/licenses/mit-license.php

05.// $Id: webClient.pde 7763 2011-12-11 01:28:16Z jcw $

06.

07.#include <EtherCard.h>

08.

09.// ethernet interface mac address, must be unique on the LAN

10.static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

11.

12.byte Ethernet::buffer;

13.static uint32_t timer;

14.

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

16.

17.// called when the client request is complete

18.static void my_callback (byte status, word off, word len) {

19.Serial.println(">>>");

20.Ethernet::buffer = 0;

21.Serial.print((const char*) Ethernet::buffer + off);

22.Serial.println("...");

23.}

24.

25.void setup () {

26.Serial.begin(57600);

27.Serial.println("\n");

28.

29.if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)

30.    Serial.println( "Failed to access Ethernet controller");

31.if (!ether.dhcpSetup())

32.    Serial.println("DHCP failed");

33.

34.ether.printIp("IP:", ether.myip);

35.ether.printIp("GW:", ether.gwip);

36.ether.printIp("DNS: ", ether.dnsip);

37.

38.if (!ether.dnsLookup(website))

39.    Serial.println("DNS failed");

40.

41.ether.printIp("SRV: ", ether.hisip);

42.}

43.

44.void loop () {

45.ether.packetLoop(ether.packetReceive());

46.

47.if (millis() > timer) {

48.    timer = millis() + 5000;

49.    Serial.println();

50.    Serial.print("<<< REQ ");

51.    ether.browseUrl(PSTR("/foo/"), "bar", website, my_callback);

52.}

53.}


页: [1]
查看完整版本: 关于EtherCard的webClient代码分析