极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 34521|回复: 13

以太网W5500上传多路传感器数据到Yeelink~

[复制链接]
发表于 2014-5-29 11:18:43 | 显示全部楼层 |阅读模式
本帖最后由 wasdpkj 于 2014-5-29 11:45 编辑





采用了Microduino W5500以太网模块~功能是读取三个模拟口值,通过序列依次传到Yeelink

继W5100、W5200和W5300之后一款全新的全硬件TCP/IP协议栈网络芯片,这款芯片具有更低功耗与工作温度,及改良工艺,是嵌入式以太网的最佳选择方案;

规格
  • 通信协议
    • 支持硬件TCP/IP协议:TCP, UDP, ICMP, IPv4, ARP, IGMP, PPPoE
    • 支持8个独立端口(Socket)同时通讯
    • 内嵌10BaseT/100BaseTX 以太网物理层(PHY)
    • 支持自动协商(10/100-Based全双工/半双工)

  • 工作特性
    • 支持掉电模式
    • 支持网络唤醒
    • 支持自动应答(全双工/半双工模式)

  • 更新速率
    • 支持高速串行外设接口
    • 内部32K字节收发缓存

  • 接口特性
    • TTL 电平输入
    • 单电源供电: 3.3V;
    • 不支持IP分片

状态指示

    • 两个用来表示连接、发送、接收、冲突和全/ 半双工状态的可编程LED 输出;


堆叠之后是这样:



下面是程序部分:
  1. #define SENSOR_NUM 3

  2. const int DEVICEID= xx;  // 输入你的设备ID
  3. const int SENSORID[SENSOR_NUM]={
  4. xx,xx,xx};  // 输入你的传感器ID

  5. #define APIKEY    "xx" // replace your pachube api key here
  6. char server[] = "api.yeelink.net";   // yeelink API的地址

  7. #include <SPI.h>
  8. #include <Ethernet.h>

  9. // fill in an available IP address on your network here,
  10. // for manual configuration:
  11. IPAddress ip(192,168,8,177);
  12. IPAddress gw(0,0,0,0);
  13. IPAddress snip(0,0,0,0);
  14. IPAddress dnsip(0,0,0,0);
  15. // initialize the library instance:
  16. EthernetClient client;

  17. // if you don't want to use DNS (and reduce your sketch size)
  18. // use the numeric IP instead of the name for the server:
  19. //IPAddress server(173,203,98,29);      // In cases where it is not possible to use DNS, you can use the following bare-IP address alternative

  20. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  21. boolean lastConnected = false;                 // state of the connection last time through the main loop
  22. const unsigned long postingInterval = 4*1000; //delay between updates to cosm.com

  23. int sensorReading[3];
  24. int SENSORID_NUM=0;

  25. void setup() {
  26.   // start serial port:
  27.   Serial.begin(115200);

  28.   // start the Ethernet connection:
  29.   if (Ethernet.begin() == 0) {
  30.     Serial.println("Failed to configure Ethernet using DHCP");
  31.     Ethernet.begin(ip, dnsip, gw,snip);
  32.   }
  33. }

  34. void loop() {
  35.   // read the analog sensor:
  36.   sensorReading[0]= analogRead(A0);
  37.   sensorReading[1]= analogRead(A1);
  38.   sensorReading[2]= analogRead(A2);

  39.   // if there's incoming data from the net connection.
  40.   // send it out the serial port.  This is for debugging
  41.   // purposes only:
  42.   if (client.available()) {
  43.     char c = client.read();
  44.     Serial.print(c);
  45.   }

  46.   // if there's no net connection, but there was one last time
  47.   // through the loop, then stop the client:
  48.   if (!client.connected() && lastConnected) {
  49.     Serial.println();
  50.     Serial.println("disconnecting.");
  51.     client.stop();
  52.   }

  53.   // if you're not connected, and ten seconds have passed since
  54.   // your last connection, then connect again and send data:
  55.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  56.     SENSORID_NUM++;
  57.     if(SENSORID_NUM>=SENSOR_NUM) SENSORID_NUM=0;
  58.     Serial.print("======NUM:");
  59.     Serial.println(SENSORID_NUM);
  60.     sendData(DEVICEID,SENSORID[SENSORID_NUM],sensorReading[SENSORID_NUM]);
  61.   }
  62.   // store the state of the connection for next time through
  63.   // the loop:
  64.   lastConnected = client.connected();
  65. }

  66. // this method makes a HTTP connection to the server:
  67. void sendData(int DEV_Data, int SEN_Data,int thisData) {
  68.   // if there's a successful connection:
  69.   if (client.connect(server, 80)) {
  70.     Serial.println("connecting...");
  71.     // 发送HTTP PUT请求
  72.     client.print("POST /v1.0/device/");
  73.     client.print(DEV_Data);
  74.     client.print("/sensor/");
  75.     client.print(SEN_Data);
  76.     client.print("/datapoints");
  77.     client.println(" HTTP/1.1");
  78.     client.println("Host: api.yeelink.net");
  79.     client.print("Accept: *");
  80.     client.print("/");
  81.     client.println("*");
  82.     client.print("U-ApiKey: ");
  83.     client.println(APIKEY);
  84.     client.print("Content-Length: ");

  85.     // 计算http包里面内容部分的长度,即content-length长度
  86.     int thisLength = 10 + getLength(thisData);
  87.     client.println(thisLength);

  88.     client.println("Content-Type: application/x-www-form-urlencoded");
  89.     client.println("Connection: close");
  90.     client.println();

  91.     // PUT回复内容
  92.     client.print("{"value":");
  93.     client.print(thisData);
  94.     client.println("}");  
  95.   }
  96.   else {
  97.     // if you couldn't make a connection:
  98.     Serial.println("connection failed");
  99.     Serial.println();
  100.     Serial.println("disconnecting.");
  101.     client.stop();
  102.   }
  103.   // note the time that the connection was made or attempted:
  104.   lastConnectionTime = millis();
  105. }


  106. // This method calculates the number of digits in the
  107. // sensor reading.  Since each digit of the ASCII decimal
  108. // representation is a byte, the number of digits equals
  109. // the number of bytes:

  110. int getLength(int someValue) {
  111.   // there's at least one byte:
  112.   int digits = 1;
  113.   // continually divide the value by ten,
  114.   // adding one to the digit count for each
  115.   // time you divide, until you're at 0:
  116.   int dividend = someValue /10;
  117.   while (dividend > 0) {
  118.     dividend = dividend /10;
  119.     digits++;
  120.   }
  121.   // return the number of digits:
  122.   return digits;
  123. }
复制代码


成功后可以看到:





用到的库:(需要删除原有的Ethernet库)


W5500更多资料:
http://www.microduino.cc/wiki/index.php?title=Microduino-W5500/zh

本帖子中包含更多资源

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

x

评分

参与人数 1 +3 收起 理由
Microduino + 3 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2014-5-29 11:24:04 | 显示全部楼层
{:soso_e142:}
回复 支持 反对

使用道具 举报

发表于 2014-5-29 11:26:06 | 显示全部楼层
哈哈哈,暂暂暂
回复 支持 反对

使用道具 举报

发表于 2014-5-29 11:29:13 | 显示全部楼层
干得漂亮!
回复 支持 反对

使用道具 举报

发表于 2014-5-29 11:30:58 | 显示全部楼层
老潘出品,必属佳品。
回复 支持 反对

使用道具 举报

发表于 2014-5-29 12:27:28 | 显示全部楼层
把库名改一下,就不用删掉原来的库了吧
回复 支持 反对

使用道具 举报

发表于 2014-5-29 12:45:44 | 显示全部楼层
microduino的东西做工好、体积小。就是价格太不亲民,让人又爱又恨。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-29 14:02:24 | 显示全部楼层
futuremeng 发表于 2014-5-29 12:27
把库名改一下,就不用删掉原来的库了吧

也要删除,调用的语法会冲突
回复 支持 反对

使用道具 举报

发表于 2014-5-29 16:29:21 | 显示全部楼层
W5500+Microduino+Yeelink 绝佳组合。
回复 支持 反对

使用道具 举报

发表于 2014-5-29 19:23:43 | 显示全部楼层
学习了。。。。。谢谢
回复 支持 反对

使用道具 举报

发表于 2014-5-29 19:34:56 | 显示全部楼层
非常感谢~



回复 支持 反对

使用道具 举报

发表于 2014-5-30 09:11:49 | 显示全部楼层
void sendData,函数,正需要,谢谢!
回复 支持 反对

使用道具 举报

发表于 2014-11-17 22:25:23 | 显示全部楼层
老潘加油,学习了。
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 01:49 , Processed in 0.051424 second(s), 33 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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