|
|
本帖最后由 zcbzjx 于 2013-4-3 20:22 编辑
2013年4月3日,ethercard库作者已经更新库,稍微修改了下代码适应新库。
========================
要下班了先发代码,有问题后面问吧,库文件也做了点修改,主要是etherCard库文件的browsurl函数没法添加头文件内容。请下载附件库文件。
不过这样改库文件不是办法,有空还是重新整个库。
- #include <EtherCard.h>
- #define REQUEST_RATE 5000 // milliseconds
- // ethernet interface mac address
- static byte mymac[] = {
- 0x74,0x69,0x69,0x2D,0x30,0x31 };
- #define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip/dnsip values below)
- #if STATIC
- // ethernet interface ip address
- static byte myip[] = {
- 10,21,0,193 };
- // gateway ip address
- static byte gwip[] = {
- 10,21,0,1 };
- // remote dns ip address
- static byte dnsip[] = {
- 10,11,5,25 };
- #endif
- // remote website name
- char website[] PROGMEM = "api.yeelink.net";
- char urlBuf[] PROGMEM = "/v1.0/device/xxx/sensor/xxx/";
- char apiKey[] PROGMEM = "U-ApiKey: xxxxxxxxxxxxxxx";
- byte Ethernet::buffer[500]; // a very small tcp/ip buffer is enough here
- static long timer;
- String switchStatus;
- // called when the client request is complete
- static void my_result_cb (byte status, word off, word len) {
- String reply=(const char*)Ethernet::buffer + off;
- switchStatus = reply.substring(reply.length()-2,reply.length()-1);
- Serial.print("<<< reply ");
- Serial.print(millis() - timer);
- Serial.println(" ms");
- Serial.print("Switch Status:");
- Serial.println(switchStatus);
- Serial.println();
- }
- void setup () {
- Serial.begin(57600);
- Serial.println("\n[getStaticIPorDHCP]");
- if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
- Serial.println( "Failed to access Ethernet controller");
- #if STATIC
- ether.staticSetup(myip, gwip, dnsip);
- #else
- if (!ether.dhcpSetup())
- Serial.println("DHCP failed");
- #endif
- ether.printIp("IP: ", ether.myip);
- ether.printIp("GW: ", ether.gwip);
- ether.printIp("DNS: ", ether.dnsip);
- while (ether.clientWaitingGw())
- ether.packetLoop(ether.packetReceive());
- Serial.println("Gateway found");
- timer = - REQUEST_RATE; // start timing out right away
- }
- void loop () {
- ether.packetLoop(ether.packetReceive());
- if (millis() > timer + REQUEST_RATE) {
- timer = millis();
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- ether.printIp("Server: ", ether.hisip);
- Serial.println("\n>>> REQ");
- ether.browseUrl(urlBuf, "datapoints", website,apiKey, my_result_cb);
- }
- }
复制代码
同样切个串口图吧
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|