zcbzjx 发表于 2012-9-26 16:03:52

enc28j60+yeelink利用browseUrl()函数l获取开关状态

本帖最后由 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;   // 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");

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);
}
}


同样切个串口图吧


zhangdeyue1 发表于 2012-9-26 17:52:57

呵呵~!
谢谢张老师了~!正好在找这方面的资料:lol

zcbzjx 发表于 2012-9-26 18:00:56

不客气,共同学习,这个是静态地址和dhcp通用的代码,只需更改#define STATIC的值, 1为静态,0为动态获取ip地址

zhangdeyue1 发表于 2012-9-26 18:04:14

>>> REQ
<<< reply 78 ms
Switch Status:


Server: 202.136.56.203

>>> REQ
<<< reply 79 ms
Switch Status:


Server: 202.136.56.203

>>> REQ
<<< reply 79 ms
Switch Status:

为什么我返回的值是空的?:funk:

zcbzjx 发表于 2012-9-26 18:10:21

本帖最后由 zcbzjx 于 2012-9-26 18:11 编辑

奇了个怪了。。你在String reply=(const char*)Ethernet::buffer + off;后加个Serila.println(reply);看看返回那些东西。

darkorigin 发表于 2012-9-26 22:10:46

学习了~~~谢谢无私贡献

zhangdeyue1 发表于 2012-9-27 11:25:57

我设置20秒发送一个数据,10秒读取一个数据
貌似会产生冲突
既发送不出去又读取不到数据:Q

erjiang 发表于 2012-9-27 12:14:30

张老师威武

zcbzjx 发表于 2012-9-27 14:06:49

zhangdeyue1 发表于 2012-9-27 11:25 static/image/common/back.gif
我设置20秒发送一个数据,10秒读取一个数据
貌似会产生冲突
既发送不出去又读取不到数据

不会的,你代码的问题

Pizzro 发表于 2012-10-9 09:26:48

返回404 是怎么回事 。说我的迷药错误。 但是我是复制过去的

xl_1120 发表于 2012-10-13 09:39:58

DNS一定要配置吗

zcbzjx 发表于 2012-10-13 13:43:50

xl_1120 发表于 2012-10-13 09:39 static/image/common/back.gif
DNS一定要配置吗

如果访问ip地址的话不需要,访问域名就要配置dns

ahthhxl 发表于 2013-1-1 15:36:13



用了你的附件,串口打印出 Serial.println( "Failed to access Ethernet controller");就不动了,连里面的实例都一样,还回原来的库就好的,请教是什么问题呀

zcbzjx 发表于 2013-4-3 18:58:38

应某人要求,更新了下代码。

幻生幻灭 发表于 2013-4-3 20:50:54

zcbzjx 发表于 2013-4-3 18:58 static/image/common/back.gif
应某人要求,更新了下代码。

很荣幸成为某人: ) 。 测试成功!
页: [1] 2
查看完整版本: enc28j60+yeelink利用browseUrl()函数l获取开关状态