|
楼主 |
发表于 2015-1-29 07:51:32
|
显示全部楼层
本帖最后由 billtian 于 2015-1-29 07:57 编辑
获取传感器数据函数
yeelink_get((const char *)"设备ID",(const char *)"传感器ID",value);
char yeelink_get(const char *device_id,const char *sensors_id,char *value)
{
int ret;
char* presult;
char remote_server[] = "api.yeelink.net";
char str_tmp[128] = {0};
SlSockAddrIn_t sAddr;
int iAddrSize;
int iSockID;
int iStatus;
//long lLoopCount = 0;
long lNonBlocking = 1;
long lRetVal = -1;
sprintf(str_tmp,"/v1.0/device/%s/sensor/%s/datapoints",device_id,sensors_id);
// 确定 HTTP请求首部
// 例如POST /v1.0/device/98d19569e0474e9abf6f075b8b5876b9/1/1/datapoints/add HTTP/1.1\r\n
sprintf( http_request , "GET %s HTTP/1.1\r\n",str_tmp);
// 增加属性 例如 Host: api.machtalk.net\r\n
sprintf( str_tmp , "Host:%s\r\n" , remote_server);
strcat( http_request , str_tmp);
// 增加密码 例如 APIKey: d8a605daa5f4c8a3ad086151686dce64
sprintf( str_tmp , "U-ApiKey:%s\r\n" , "d8a605daa5f4c8a3ad086151686dce64");//需要替换为自己的APIKey
strcat( http_request , str_tmp);
//
strcat( http_request , "Accept: */*\r\n");
// 增加表单编码格式 Content-Type:application/x-www-form-urlencoded\r\n
strcat( http_request , "Content-Type: application/x-www-form-urlencoded\r\n");
strcat( http_request , "Connection: close\r\n");
// HTTP首部和HTTP内容 分隔部分
strcat( http_request , "\r\n");
// Get external host IP address
lRetVal = sl_NetAppDnsGetHostByName((signed char*)HOST_NAME, sizeof(HOST_NAME),
&g_ulDestinationIp, SL_AF_INET);
ASSERT_ON_ERROR(lRetVal);
//filling the TCP server socket address
sAddr.sin_family = SL_AF_INET;
sAddr.sin_port = sl_Htons((unsigned short)80);
sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp);
iAddrSize = sizeof(SlSockAddrIn_t);
// creating a TCP socket
iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
if( iSockID < 0 )
{
ASSERT_ON_ERROR(TCP_CLIENT_FAILED);
}
// connecting to TCP server
iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
if( iStatus < 0 )
{
// error
ASSERT_ON_ERROR(sl_Close(iSockID));
ASSERT_ON_ERROR(TCP_CLIENT_FAILED);
}
else
{
Report("connected to TCP server successfully\n\r");
Report("Wireless serial STATION configure successfully\n\r");
}
// setting socket option to make the socket as non blocking
//iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,
// &lNonBlocking, sizeof(lNonBlocking));
//发送请求
iStatus = sl_Send(iSockID,(unsigned char *)http_request,strlen(http_request),0);
if(iStatus != strlen(http_request)){
Report("%d:Socket Send Error\r\n",iStatus);
while(1);
}
//获得响应
iStatus = sl_Recv(iSockID, http_response, BUF_SIZE, 0);
if( iStatus <= 0 )
{
//Report("iStatus = %d\n\r",iStatus);
}
else
{
UART_SEND_BUF((unsigned char*)http_response,iStatus);
//iStatus = sl_Send(iSockID,g_cBsdBuf,iStatus,0);
}
http_response[iStatus] = '\0';
//判断是否收到HTTP OK
presult = strstr( (const char *)http_response , (const char *)"200 OK\r\n");
if( presult != NULL ){
sscanf(http_response,"%*[^{]{%[^}]",strTmp);
//提取返回信息
char timestamp[64]={0};
char timestampTmp[64]={0};
char valueTmp[64]={0};
sscanf(strTmp,"%[^,],%[^,]",timestampTmp,valueTmp);
strncpy(timestamp,strstr(timestampTmp,":")+2,strlen(strstr(timestampTmp,":"))-3);
strncpy(value,strstr(valueTmp,":")+1,strlen(strstr(valueTmp,":"))-1);
}else{
Report("Http Response Error\r\n");
Report("%s",http_response);
}
sl_Close(iSockID);
return 0;
} |
|