极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 59267|回复: 36

ENC28J60利用ethercard库httpPost函数发送数据到yeelink或者乐联网

[复制链接]
发表于 2012-9-6 14:51:12 | 显示全部楼层 |阅读模式
本帖最后由 zcbzjx 于 2013-5-29 00:39 编辑

2013年5月29日更新:由于库文件更新比较多,以前的帖子有些不适用了,重新开贴又没有必要,所以就直接修改这个帖子,并争取把这个帖子修改成精华帖。




最近研究ethercard库,发觉有个httpPost函数,用这个库函数发送post数据比较省事,而且可以方便获得服务器返回数据。

实验硬件器材和这个帖子的实验器材一样,不再赘述。
ArduinoIDE版本:1.0.4
库文件为最新的ethercard库文件

Yeelink代码


  1. #include <OneWire.h>
  2. #include <EtherCard.h>
  3. #include <DallasTemperature.h>
  4. #define OUT
  5. // Data wire is plugged into port A5 on the Arduino
  6. #define ONE_WIRE_BUS A5
  7. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  8. OneWire oneWire(ONE_WIRE_BUS);
  9. // Pass our oneWire reference to Dallas Temperature.
  10. DallasTemperature sensors(&oneWire);

  11. #define REQUEST_RATE 15000 // milliseconds
  12. // ethernet interface mac address
  13. static byte mymac[] = {
  14.   0x74,0x69,0x69,0x2D,0x30,0x31 };
  15. // ethernet interface ip address
  16. static byte myip[] = {
  17.   10,21,0,193 };
  18. // gateway ip address
  19. static byte gwip[] = {
  20.   10,21,0,1 };
  21. // remote dns ip address
  22. static byte dnsip[] = {
  23.   10,11,5,25 };

  24. // remote website name
  25. char website[] PROGMEM = "api.yeelink.net";
  26. char urlBuf[] PROGMEM = "/v1.0/device/xxx/sensor/xxx/datapoints";
  27. char apiKey[] PROGMEM = "U-ApiKey: xxxxxxxxxxxxxx";
  28. byte Ethernet::buffer[400];   // a very small tcp/ip buffer is enough here
  29. static long timer;

  30. // called when the client request is complete
  31. static void my_result_cb (byte status, word off, word len) {
  32.   Serial.print("<<< reply ");
  33.   Serial.print(millis() - timer);
  34.   Serial.println(" ms");
  35.   Serial.println((const char*) Ethernet::buffer + off);
  36. }

  37. void setup () {
  38.   sensors.begin();
  39.   Serial.begin(57600);
  40.   Serial.println("\n[getStaticIP]");

  41.   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  42.     Serial.println( "Failed to access Ethernet controller");

  43.   ether.staticSetup(myip, gwip, dnsip);

  44.   while (ether.clientWaitingGw())
  45.     ether.packetLoop(ether.packetReceive());
  46.   Serial.println("Gateway found");

  47.   timer = - REQUEST_RATE; // start timing out right away
  48. }

  49. void loop () {
  50.   ether.packetLoop(ether.packetReceive());

  51.   if (millis() > timer + REQUEST_RATE) {
  52.     timer = millis();
  53.     Serial.println("\n>>> REQ");
  54.     static char buf[20];
  55.     get_send_string(buf);
  56.     if (!ether.dnsLookup(website))
  57.       Serial.println("DNS failed");
  58.     ether.printIp("Server: ", ether.hisip);
  59.     ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
  60.   }
  61. }

  62. void get_send_string(OUT char *p){
  63.   sensors.requestTemperatures();  
  64.   uint16_t Tc_100 = sensors.getTempCByIndex(0)*10;
  65.   uint8_t whole, fract;
  66.   whole = Tc_100/10 ;  // separate off the whole and fractional portions
  67.   fract = Tc_100 % 10;
  68.   sprintf(p,"{"value":%d.%d}",whole,fract);
  69. }
复制代码


串口图如下:




乐联网代码如下:

  1. #include <OneWire.h>
  2. #include <EtherCard.h>
  3. #include <DallasTemperature.h>
  4. #define OUT
  5. // Data wire is plugged into port A5 on the Arduino
  6. #define ONE_WIRE_BUS 7
  7. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  8. OneWire oneWire(ONE_WIRE_BUS);
  9. // Pass our oneWire reference to Dallas Temperature.
  10. DallasTemperature sensors(&oneWire);

  11. #define REQUEST_RATE 15000 // milliseconds
  12. // ethernet interface mac address 需要根据你的实际网络环境进行更改,我这儿用的是固定ip
  13. static byte mymac[] = {
  14.   0x74,0x69,0x69,0x2D,0x30,0x31 };
  15. // ethernet interface ip address
  16. static byte myip[] = {
  17.   10,21,0,193 };
  18. // gateway ip address
  19. static byte gwip[] = {
  20.   10,21,0,1 };
  21. // remote dns ip address
  22. static byte dnsip[] = {
  23.   10,21,0,1 };

  24. // remote website name
  25. char website[] PROGMEM = "www.lewei50.com";
  26. char urlBuf[] PROGMEM = "/api/V1/gateway/UpdateSensors/01"; // 01应该更改为你对应的网关标识
  27. char apiKey[] PROGMEM = "userkey:xxxxxxxxxxxxxxxxxxxxxxxxx"; //需要更改为你的userkey
  28. byte Ethernet::buffer[700];   // a very small tcp/ip buffer is enough here
  29. static long timer;

  30. // called when the client request is complete
  31. static void my_result_cb (byte status, word off, word len) {
  32.   Serial.print("<<< reply ");
  33.   Serial.print(millis() - timer);
  34.   Serial.println(" ms");
  35.   Serial.println((const char*) Ethernet::buffer + off);
  36. }

  37. void setup () {
  38.   sensors.begin();
  39.   Serial.begin(57600);
  40.   Serial.println("\n[getStaticIP]");

  41.   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  42.     Serial.println( "Failed to access Ethernet controller");

  43.   ether.staticSetup(myip, gwip, dnsip);

  44.   while (ether.clientWaitingGw())
  45.     ether.packetLoop(ether.packetReceive());
  46.   Serial.println("Gateway found");

  47.   timer = - REQUEST_RATE; // start timing out right away
  48. }

  49. void loop () {
  50.   ether.packetLoop(ether.packetReceive());

  51.   if (millis() > timer + REQUEST_RATE) {
  52.     timer = millis();
  53.     Serial.println("\n>>> REQ");
  54.     static char buf[20];
  55.     get_send_string(buf);
  56.     if (!ether.dnsLookup(website))
  57.       Serial.println("DNS failed");
  58.     ether.printIp("Server: ", ether.hisip);
  59.     ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
  60.   }
  61. }

  62. void get_send_string(OUT char *p){
  63.   sensors.requestTemperatures();  
  64.   uint16_t Tc_100 = sensors.getTempCByIndex(0)*10;
  65.   uint8_t whole, fract;
  66.   whole = Tc_100/10 ;  // separate off the whole and fractional portions
  67.   fract = Tc_100 % 10;
  68.   sprintf(p,"[{"Name":"T2","Value":"%d.%d"}]",whole,fract);  //T2应该更改为你对应的设备标识
  69. }
复制代码


以下为串口切图:



相信大家已经看出来来了,代码没什么大的变化。

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2012-9-6 16:49:45 | 显示全部楼层
好快。。。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-9-6 20:15:12 | 显示全部楼层
嘛意思。。。。
回复 支持 反对

使用道具 举报

发表于 2012-9-6 20:43:14 | 显示全部楼层
张老师给力
回复 支持 反对

使用道具 举报

发表于 2012-9-12 14:15:46 | 显示全部楼层
请问这个函数可不可以放到wifi模块上去用?
求指点
回复 支持 反对

使用道具 举报

发表于 2012-9-12 20:03:07 | 显示全部楼层
感谢分享!
回复 支持 反对

使用道具 举报

发表于 2012-9-23 17:05:30 | 显示全部楼层
请问MAC地址是如何获取的呢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-9-24 01:52:53 | 显示全部楼层
mac地址可以自己随便编,只要本地没重复的就行。
回复 支持 反对

使用道具 举报

发表于 2012-10-28 23:55:16 | 显示全部楼层
可以向自己的服务器发送数据吗?比如向192.168.1.222发送数据。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-29 07:49:49 | 显示全部楼层
all_heart 发表于 2012-10-28 23:55
可以向自己的服务器发送数据吗?比如向192.168.1.222发送数据。

可以,192.168.1.222作为服务器,应该有相应的脚本将数据存储到数据库或者文本文件。
回复 支持 反对

使用道具 举报

发表于 2012-10-29 09:56:09 | 显示全部楼层
张老师能不能将这个ecard库直接改动一下,封装成直连yeelink的库?提供几个简单的参数就能完成上传?
回复 支持 反对

使用道具 举报

发表于 2012-10-29 22:48:24 | 显示全部楼层
zcbzjx 发表于 2012-10-29 07:49
可以,192.168.1.222作为服务器,应该有相应的脚本将数据存储到数据库或者文本文件。

能具体点吗,asp.net我知道该如何做,但arduino我还是比较茫然。望指点。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-30 07:52:43 | 显示全部楼层
本帖最后由 zcbzjx 于 2012-10-30 07:55 编辑
all_heart 发表于 2012-10-29 22:48
能具体点吗,asp.net我知道该如何做,但arduino我还是比较茫然。望指点。


ardino端用httppost函数,把数据发送到服务器相应页面,asp.net服务器端脚本处理这个post请求就行了。再详细我就要架服务器编服务器代码调试了。。太麻烦,建议你试试yeelink。然后再自己编写服务器端代码。
回复 支持 反对

使用道具 举报

发表于 2013-5-15 17:08:10 | 显示全部楼层
对照这个研究了数天后终于成功了,感谢楼主的教程
回复 支持 反对

使用道具 举报

发表于 2013-5-16 16:10:09 | 显示全部楼层
好帖子,再次谢过楼主
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 22:10 , Processed in 0.044206 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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