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/<device_id>/sensor/<sensor_id>/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://www.hurl.it/
服务器应答
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
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实例回顾
GET与POST方法实例:
GET实例 | |
GET /books/?name=Professional%20Ajax HTTP/1.1 Host: www.wrox.com 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 / HTTP /1.1 Host: www.wrox.com 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> <title>Wrox Homepage</title> </head> <body> <!-- body goes here --> </body> </html> |
实际应用:
1,arduino实现 http://blog.yeelink.net/?p=34
// send the HTTP PUT request: client.print("POST /v1.0/device/"); client.print(DEVICEID); client.print("/sensor/"); client.print(SENSORID); client.print("/datapoints"); client.println(" HTTP/1.1"); client.println("Host: api.yeelink.net"); client.print("Accept: *"); client.print("/"); client.println("*"); client.print("U-ApiKey: "); client.println(APIKEY); client.print("Content-Length: "); // calculate the length of the sensor reading in bytes: // 8 bytes for {"value":} + number of digits of the data: int thisLength = 10 + getLength(thisData); client.println(thisLength); client.println("Content-Type: application/x-www-form-urlencoded"); client.println("Connection: close"); client.println(); // here's the actual content of the PUT request: client.print("{\"value\":"); client.print(thisData); client.println("}");
2,使用LWIP向yeelink的传感器添加数据点(经测试成功)
LM3S9B92 LWIP 无OS
tcp发送内容
POST /v1.0/device/1847/sensor/2326/datapoints HTTP/1.0 Host: api.yeelink.net Accept: */* U-ApiKey: c3b3d0671f3d962ee2b8aaa1cece81 Content-Length: 12 Content-type: application/json;charset=utf-8 Connection: close {"value":55}
//***************************************************************************** // //tcp发送数据函数 // //***************************************************************************** void TcpSend(uint8 *data,uint8 len) { uint16 buf_size; s8_t write_errno; buf_size=tcp_sndbuf(tcp_client_pcb); do{ buf_size=tcp_sndbuf(tcp_client_pcb); }while(buf_size<len); write_errno=tcp_write(tcp_client_pcb,data,len,0); //0不需要开辟新内存1需要开辟新内存保存要发的数据 switch(write_errno) { case ERR_MEM: UARTprintf("TCP write mem err"); break; case ERR_OK: UARTprintf("TCP write ok"); break; } tcp_output(tcp_client_pcb); } //***************************************************************************** // //tcp发送数据函数 // //***************************************************************************** void TcpSend1(uint8 *data,uint8 len) { uint16 buf_size; s8_t write_errno; buf_size=tcp_sndbuf(tcp_client_pcb); do{ buf_size=tcp_sndbuf(tcp_client_pcb); }while(buf_size<len); write_errno=tcp_write(tcp_client_pcb,data,len,0); switch(write_errno) { case ERR_MEM: UARTprintf("TCP write mem err"); UARTprintf("sned len:%d",len); UARTprintf("buf_size:%d",buf_size); break; case ERR_OK: UARTprintf("TCP write ok"); break; } } //***************************************************************************** // //tcp发送一个回车换行 // //***************************************************************************** void tcp_sendln(void) { uint8 *pstr="\r\n"; TcpSend1(pstr,strlen(pstr)); } //***************************************************************************** // //上传数据到yeelink // //***************************************************************************** void send_data_to_yeelink(uint8 *device_num,uint8 *sensor_num,uint8 *api_key,uint8 val) { uint16 len; uint16 len1; uint8 buf[1]; uint8 tmp[2]; char strbuf[512]; char pstr7[12]="{\"value\":"; uint8 *pstr8="}"; memset(strbuf,0x00,512); if(val>10) { tmp[0]=val/10+0x30; tmp[1]=val%10+0x30; }else { tmp[0]=0x30; tmp[1]=val+0x30; } strcat(pstr7,tmp); strcat(pstr7,pstr8); //method strcat(strbuf,"POST /v1.0/device/"); strcat(strbuf,device_num); strcat(strbuf,"/sensor/"); strcat(strbuf,sensor_num); strcat(strbuf,"/datapoints HTTP/1.0\r\n"); //head strcat(strbuf,"Host: api.yeelink.net\r\nAccept: */*\r\nU-ApiKey: "); strcat(strbuf,api_key); strcat(strbuf,"\r\n"); strcat(strbuf,"Content-Length: 12\r\n"); strcat(strbuf,"Content-type: application/json;charset=utf-8\r\n"); strcat(strbuf,"Connection: close\r\n"); len1=strlen(strbuf); TcpSend(strbuf,strlen(strbuf)); //body tcp_sendln(); SysCtlDelay(55); TcpSend(pstr7,12); // UARTprintf("%s",pstr7); tcp_sendln(); } //***************************************************************************** // //tcp发送数据函数 // //***************************************************************************** void OsalStartSystem() { uint32 sysStat; //yeelink上传数据用 char *DEVICE_NUM="1847"; //设备编号 char *SENSOR_NUM="2326"; //传感器编号 char *API_KEY="c3b3d0671f3d962ee2b8aaa1cece81"; //API-KEY sysStat=0; UARTprintf("OS start..."); //上传数据到yeelink send_data_to_yeelink(DEVICE_NUM,SENSOR_NUM,API_KEY,21); SysCtlDelay(5555555); // tcp_close(tcp_client_pcb); while(1) { sysStat++; //系统运行指示灯 if(sysStat==50000) { RUN_LIGHT_ON; //点亮LED1 } if(sysStat==100000) { sysStat=0; RUN_LIGHT_OFF; //灭LED1 } ProcessEvent(); //系统事件的处理 } }