Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30107871
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: 云计算

2017-04-21 17:29:22

yeelink是一个物联网平台 国内类似的平台还有乐为物联网 觉得挺好玩最近就试了试

特点

1,它是免费平台 任何人可以注册然后在上面新建设备和传感器

2,支持数据双向交互 你可以随时上传传感器的数据也可以读取平台上的传感器数据

具体应用

1,把自己的设备数据通过串口或tcp上传到网络 然后绘制曲线或做其他用途

2,在平台上新建一个传感器然后读取传感器的值或长连接来控制本地的设备(无论是开关器件,马达或其他设备) 



我在yeelink上新建了个设备和传感器正在测试用过post上传传感器数据

教程可参考

yeelink入门   http://www.yeelink.net/develop/quickstart

yeelink  api   http://www.yeelink.net/develop/api#create_datapoint



Yeelink增加一个数据点的方法

 数据点datapoints


一个datapoint是由key和value组成的键值对.

创建数据点

URL http://api.yeelink.net/v1.0/device//sensor//datapoints
数据格式 JSON
Method POST
返回 HTTP Headers only

对该URL的一个HTTP POST请求会为指定的传感器创建一个新的数据点, 使用此API来为传感器存储历史数据

发送创建数据点的请求(注意两点).

1,  向传感器的url 通过post方法发送一个数据点(数据点放在body中)

数据点格式为{"value":349}(时间戳可省去由服务器添加)
单个上传数据例子(JSON): 
数值型传感器格式如下:
{
  "timestamp":"2012-03-15T16:13:14",
  "value":294.34
}
批量上传数据例子(JSON):
数值型传感器格式如下:
[
  {"timestamp": "2012-06-15T14:00:00", "value":315.01},
  {"timestamp": "2012-06-15T14:00:10", "value":316.23},
  {"timestamp": "2012-06-15T14:00:20", "value":317.26},
  {"timestamp": "2012-06-15T14:00:30", "value":318},
  {"timestamp": "2012-06-15T14:00:40", "value":317}
]

2,在header中增加U-ApiKey: 您申请的API_KEY"
举例1:
请求实例 (运用curl): 
curl –request POST --data '{"value":349}' --header U-ApiKey: 您申请的API_KEY" --verbose http://api.yeelink.net/v1.0/device/8/sensor/12/datapoints


举例2:
使用hurl测试 工具地址



 

 

 

 

 

 

 

 

服务器应答

HTTP/1.1200 OK

Server:nginx/1.0.14

Date: Thu, 07Feb 2013 01:37:26 GMT

Content-Type: text/html

Connection:keep-alive

X-Powered-By:PHP/5.3.10

Set-Cookie:CAKEPHP=gc4hv756u2u64n3rcs1fsutsj5; expires=Fri, 15-Feb-2013 09:37:26 GMT;path=/

P3P: CP="NOIADM DEV PSAi COM NAV OUR OTRo STP IND DEM"

Vary:Accept-Encoding

Content-Length: 0




整个请求与回复流程(数据上传与服务器应答流程)

 


POST /v1.0/device/1847/sensor/2326/datapoints HTTP/1.1 Host: api.yeelink.net Accept: */* U-ApiKey: 44c3b3d067 Content-Length: 11 Content-Type: application/x-www-form-urlencoded  
{"value":6}




HTTP/1.1 200 OK Server: nginx/1.0.14 Date: Thu, 07 Feb 2013 01:52:49 GMT Content-Type: text/html Connection: keep-alive X-Powered-By: PHP/5.3.10 Set-Cookie: CAKEPHP=35obvmnt9sahvubkq4; expires=Fri, 15-Feb-2013 09:52:49 GMT; path=/ P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM" Vary: Accept-Encoding Content-Length: 0





http中  get  post实例回顾


GETPOST方法实例:

GET实例  

GET /books/?name=Professional%20Ajax HTTP/1.1

Host:

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)

Gecko/20050225 Firefox/1.0.1

Connection: Keep-Alive



POST实例

POST / HTTP/1.1

Host:

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)

Gecko/20050225 Firefox/1.0.1

Content-Type: application/x-www-form-urlencoded

Content-Length: 40

Connection: Keep-Alive

     (此处空一行)

name=Professional%20Ajax&publisher=Wiley


 






http响应实例


   HTTP响应实例:

HTTP/1.1 200 OK

Date: Sat, 31 Dec 2005 23:59:59 GMT

Content-Type: text/html;charset=ISO-8859-1

Content-Length: 122

html

head

titleWrox Homepage/title

/head

body

!-- body goes here --

/body

/html







实际应用:

1,arduino实现   http://blog.yeelink.net/?p=34


[cpp] view plain copy
 print?
  1. // send the HTTP PUT request:  
  2.     client.print("POST /v1.0/device/");  
  3.     client.print(DEVICEID);  
  4.     client.print("/sensor/");  
  5.     client.print(SENSORID);  
  6.     client.print("/datapoints");  
  7.     client.println(" HTTP/1.1");  
  8.     client.println("Host: api.yeelink.net");  
  9.     client.print("Accept: *");  
  10.     client.print("/");  
  11.     client.println("*");  
  12.     client.print("U-ApiKey: ");  
  13.     client.println(APIKEY);  
  14.     client.print("Content-Length: ");  
  15.   
  16.     // calculate the length of the sensor reading in bytes:  
  17.     // 8 bytes for {"value":} + number of digits of the data:  
  18.     int thisLength = 10 + getLength(thisData);  
  19.     client.println(thisLength);  
  20.   
  21.     client.println("Content-Type: application/x-www-form-urlencoded");  
  22.     client.println("Connection: close");  
  23.     client.println();  
  24.   
  25.     // here's the actual content of the PUT request:  
  26.     client.print("{\"value\":");  
  27.     client.print(thisData);  
  28.     client.println("}");  



2,使用LWIP向yeelink的传感器添加数据点(经测试成功)


[cpp] view plain copy
 print?
  1. LM3S9B92  LWIP  无OS  
[cpp] view plain copy
 print?
  1.   
[cpp] view plain copy
 print?
  1. tcp发送内容  
[cpp] view plain copy
 print?
  1. POST /v1.0/device/1847/sensor/2326/datapoints HTTP/1.0  
  2.  Host: api.yeelink.net  
  3.  Accept: */*  
  4.  U-ApiKey: c3b3d0671f3d962ee2b8aaa1cece81  
  5.  Content-Length: 12  
  6.  Content-type: application/json;charset=utf-8  
  7.  Connection: close  
  8.    
  9.  {"value":55}  




[cpp] view plain copy
 print?
  1. //*****************************************************************************  
  2.  //  
  3.  //tcp发送数据函数  
  4.  //  
  5.  //*****************************************************************************  
  6.  void TcpSend(uint8 *data,uint8 len)  
  7.  {  
  8.     uint16  buf_size;  
  9.     s8_t write_errno;  
  10.    
  11.       buf_size=tcp_sndbuf(tcp_client_pcb);  
  12.      do{  
  13.         buf_size=tcp_sndbuf(tcp_client_pcb);  
  14.      }while(buf_size
  15.    
  16.      write_errno=tcp_write(tcp_client_pcb,data,len,0);      //0不需要开辟新内存1需要开辟新内存保存要发的数据  
  17.          
  18.    switch(write_errno)  
  19.    {  
  20.     case ERR_MEM:                 
  21.          UARTprintf("TCP write mem err");     
  22.          break;  
  23.     case ERR_OK:  
  24.          UARTprintf("TCP write ok");  
  25.          break;  
  26.    }  
  27.      tcp_output(tcp_client_pcb);  
  28.    
  29.  }  
  30.  //*****************************************************************************  
  31.  //  
  32.  //tcp发送数据函数  
  33.  //  
  34.  //*****************************************************************************  
  35.  void TcpSend1(uint8 *data,uint8 len)  
  36.  {  
  37.     uint16  buf_size;  
  38.     s8_t write_errno;  
  39.    
  40.    
  41.       buf_size=tcp_sndbuf(tcp_client_pcb);  
  42.      do{  
  43.         buf_size=tcp_sndbuf(tcp_client_pcb);  
  44.      }while(buf_size
  45.    
  46.      write_errno=tcp_write(tcp_client_pcb,data,len,0);  
  47.        switch(write_errno)  
  48.    {  
  49.     case ERR_MEM:                 
  50.          UARTprintf("TCP write mem err");  
  51.          UARTprintf("sned len:%d",len);  
  52.          UARTprintf("buf_size:%d",buf_size);      
  53.          break;  
  54.     case ERR_OK:  
  55.          UARTprintf("TCP write ok");  
  56.          break;  
  57.    }  
  58.  }  
  59.  //*****************************************************************************  
  60.  //  
  61.  //tcp发送一个回车换行  
  62.  //  
  63.  //*****************************************************************************  
  64.  void tcp_sendln(void)  
  65.  {  
  66.  uint8 *pstr="\r\n";      
  67.  TcpSend1(pstr,strlen(pstr));  
  68.  }  
  69.  //*****************************************************************************  
  70.  //  
  71.  //上传数据到yeelink  
  72.  //  
  73.  //*****************************************************************************  
  74.  void send_data_to_yeelink(uint8 *device_num,uint8 *sensor_num,uint8 *api_key,uint8 val)  
  75.  {  
  76.  uint16 len;  
  77.  uint16 len1;  
  78.  uint8 buf[1];  
  79.  uint8 tmp[2];  
  80.    
  81.  char strbuf[512];  
  82.  char pstr7[12]="{\"value\":";  
  83.  uint8 *pstr8="}";  
  84.    
  85.  memset(strbuf,0x00,512);  
  86.   if(val>10)  
  87.  {  
  88.     tmp[0]=val/10+0x30;  
  89.     tmp[1]=val%10+0x30;  
  90.  }else  
  91.  {  
  92.     tmp[0]=0x30;  
  93.     tmp[1]=val+0x30;  
  94.  }  
  95.  strcat(pstr7,tmp);  
  96.  strcat(pstr7,pstr8);  
  97.    
  98.  //method  
  99.  strcat(strbuf,"POST /v1.0/device/");  
  100.  strcat(strbuf,device_num);  
  101.  strcat(strbuf,"/sensor/");  
  102.  strcat(strbuf,sensor_num);  
  103.  strcat(strbuf,"/datapoints HTTP/1.0\r\n");    
  104.    
  105.  //head  
  106.  strcat(strbuf,"Host: api.yeelink.net\r\nAccept: */*\r\nU-ApiKey: ");  
  107.  strcat(strbuf,api_key);  
  108.  strcat(strbuf,"\r\n");  
  109.  strcat(strbuf,"Content-Length: 12\r\n");  
  110.  strcat(strbuf,"Content-type: application/json;charset=utf-8\r\n");  
  111.  strcat(strbuf,"Connection: close\r\n");  
  112.  len1=strlen(strbuf);  
  113.  TcpSend(strbuf,strlen(strbuf));  
  114.    
  115.  //body  
  116.     tcp_sendln();  
  117.     SysCtlDelay(55);  
  118.     TcpSend(pstr7,12);  
  119.  // UARTprintf("%s",pstr7);  
  120.     tcp_sendln();  
  121.  }  
  122.  //*****************************************************************************  
  123.  //  
  124.  //tcp发送数据函数  
  125.  //  
  126.  //*****************************************************************************  
  127.  void OsalStartSystem()  
  128.  {  
  129.     uint32 sysStat;  
  130.    
  131.      //yeelink上传数据用  
  132.     char *DEVICE_NUM="1847";   //设备编号  
  133.     char *SENSOR_NUM="2326";   //传感器编号  
  134.     char *API_KEY="c3b3d0671f3d962ee2b8aaa1cece81";   //API-KEY  
  135.    
  136.     sysStat=0;  
  137.      UARTprintf("OS start...");  
  138.    //上传数据到yeelink  
  139.     send_data_to_yeelink(DEVICE_NUM,SENSOR_NUM,API_KEY,21);  
  140.    
  141.    
  142.     SysCtlDelay(5555555);  
  143.  //   tcp_close(tcp_client_pcb);  
  144.     while(1)  
  145.     {     
  146.         sysStat++;              //系统运行指示灯  
  147.         if(sysStat==50000)  
  148.         {  
  149.             RUN_LIGHT_ON;   //点亮LED1  
  150.         }                     
  151.         if(sysStat==100000)  
  152.         {  
  153.             sysStat=0;  
  154.             RUN_LIGHT_OFF;   //灭LED1  
  155.         }        
  156.        ProcessEvent();      //系统事件的处理  
  157.     }  
  158.  }  
阅读(1999) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~