前言
在OneNET平台的HTTP协议可以说是最简单的一种协议,使用的是HTTP的RESTful风格接口,可以作为新手用来入门OneNET各类协议的协议。在OneNET它是一种短连接协议,只能用于数据上报,不能下发命令,如果你想要实现命令下发可以用EDP协议或者MQTT协议。本文向将用工具调试方法,再用C语言代码讲解,用socket来通信,不贴出socket代码,代码在另外一篇博客OneNET的EDP协议讲解与应用有贴出。本文只POST、GET一个数据流或一个数据点,读者若想实现多个数据流或数据点的POST、GET,可参考本文代码,并参考OneNET HTTP协议开发文档。
一、用TCP&UDP测试工具调试
1、工具下载
可以到TCPUDP测试工具下载,或者直接百度搜索“TCP&UDP测试工具”下载。
2、连接服务器
(1)打开工具,输入服务器IP地址和端口,OneNET HTTP协议 对应的服务器IP为:183.230.40.33,端口为:80。
(2)点击连接
3、POST数据流
-
POST /devices/
505619290/datapoints HTTP/
1.1
-
api-key:SlxhH3MCLvuuvXJ0N=a14Yo6EAQ=
-
Host:api.heclouds.com
-
Content-Length:
66
-
-
{
"datastreams":[{
"id":
"test_stream",
"datapoints":[{
"value":
28}]}]}
-
设备ID:505619290
api-key:SlxhH3MCLvuuvXJ0N=a14Yo6EAQ=
Content-Length:66,66的长度为:{"datastreams":[{"id":"test_stream","datapoints":[{"value":28}]}]} 的长度
Content-Length:66 下面一定要换行。
数据流名称:test_stream
数据流值:28
最后结束也需要换行。
成功之后,在对应设备可以看到:
4、GET数据流
-
GET
http:/
/api.heclouds.com/devices
/505619290/datapoints?datastream_id=test_stream HTTP/
1.1
-
api-
key:SlxhH3MCLvuuvXJ0N=a14Yo6EAQ=
-
Host:api.heclouds.com
-
-
设备ID:505619290
api-key:SlxhH3MCLvuuvXJ0N=a14Yo6EAQ=
Host:api.heclouds.com之后要有两个换行\r\n\r\n
由下图可在成功GET数据:
二、用C语言讲解OneNET HTTP协议的使用
1、连接OneNET平台
OneNET HTTP协议 对应的服务器IP为:183.230.40.33,端口为:80。代码如下:
-
typedef
enum
-
{
-
HTTP_SRV_CONNECT_OK =
0,
-
HTTP_SRV_CONNECT_ERROR =
1
-
}
http_srv_connect_t;
-
-
char *g_onenet_http_srv_ip_addr =
"183.230.40.33";
-
int g_onenet_http_srv_ip_port =
80;
-
-
/* socket id */
-
int g_onenet_http_socket_id =
-1;
-
-
/**************************************************************
-
函数名称 : onenet_http_service_connect
-
函数功能 : 连接OneNET平台
-
输入参数 : ip_addr:ip地址,ip_port:端口
-
返回值 : HTTP_SRV_CONNECT_OK:连接成功,
-
HTTP_SRV_CONNECT_ERROR:连接失败
-
备注 : 无
-
**************************************************************/
-
http_srv_connect_t onenet_http_service_connect(
char *ip_addr,
int ip_port)
-
{
-
g_onenet_http_socket_id = socket_create();
-
-
if(g_onenet_http_socket_id <
0)
-
{
-
ONENET_HTTP_LOG(
"connect failed, g_onenet_mqtt_socket_id < 0");
-
return HTTP_SRV_CONNECT_ERROR;
-
}
-
-
if(CONNECT_ERROR == socket_connect_service(g_onenet_http_socket_id, ip_addr, ip_port))
-
{
-
ONENET_HTTP_LOG(
"connect failed, connect error");
-
return HTTP_SRV_CONNECT_ERROR;
-
}
-
else
-
{
-
ONENET_HTTP_LOG(
"connect success");
-
return HTTP_SRV_CONNECT_OK;
-
}
-
-
}
-
-
/* 连接OneNET平台 */
-
if(HTTP_SRV_CONNECT_OK == onenet_http_service_connect(g_onenet_http_srv_ip_addr, g_onenet_http_srv_ip_port))
-
{
-
ONENET_HTTP_LOG(
"connect success");
-
}
-
else
-
{
-
ONENET_HTTP_LOG(
"connect failed");
-
}
2、POST数据流到设备
代码如下:
-
typedef
enum
-
{
-
HTTP_POST_OK =
0,
-
HTTP_POST_ERROR =
1
-
}
http_post_t;
-
-
char *g_onenet_http_pro_api_key =
"SlxhH3MCLvuuvXJ0N=a14Yo6EAQ=";
-
char *g_onenet_http_dev_id =
"505619290";
-
-
/* 数据流 */
-
int data_point =
18;
-
char *data_stream_name =
"temperature";
-
-
/**************************************************************
-
函数名称 : onenet_http_post_data_stream
-
函数功能 : post OneNET平台
-
输入参数 : 无
-
返回值 : HTTP_POST_OK:发送POST成功,
-
HTTP_POST_ERROR:发送POST失败
-
备注 : 无
-
**************************************************************/
-
http_post_t onenet_http_post_data_stream(
char *dev_id,
char *api_key)
-
{
-
char post_buf[ONENET_HTTP_POST_MAX_LEN];
-
char post_content[ONENET_HTTP_POST_CONTENT_MAX_LEN];
-
char post_content_len[
4];
-
-
memset(post_content,
0,
sizeof(post_content));
-
memset(post_buf,
0,
sizeof(post_buf));
-
-
sprintf(post_content,
"{\"datastreams\":[{\"id\":\"%s\",\"datapoints\":[{\"value\":%d}]}]}",data_stream_name, data_point);
-
sprintf(post_content_len,
"%d",
strlen(post_content));
-
-
strcat(post_buf,
"POST /devices/");
-
strcat(post_buf, dev_id);
-
strcat(post_buf,
"/datapoints HTTP/1.1\r\n");
-
strcat(post_buf,
"api-key:");
-
strcat(post_buf, api_key);
-
strcat(post_buf,
"\r\n");
-
strcat(post_buf,
"Host:api.heclouds.com\r\n");
-
strcat(post_buf,
"Content-Length:");
-
strcat(post_buf, post_content_len);
-
strcat(post_buf,
"\r\n\r\n");
-
strcat(post_buf, post_content);
-
strcat(post_buf,
"\r\n\r\n");
-
-
if(socket_send(g_onenet_http_socket_id, post_buf,
strlen(post_buf)) >
0)
-
{
-
return HTTP_POST_OK;
-
}
-
else
-
{
-
return HTTP_POST_ERROR;
-
}
-
}
-
-
/* POST数据流 */
-
if(HTTP_POST_OK == onenet_http_post_data_stream(g_onenet_http_dev_id, g_onenet_http_pro_api_key))
-
{
-
ONENET_HTTP_LOG(
"post success");
-
}
-
else
-
{
-
ONENET_HTTP_LOG(
"post failed");
-
}
POST成功之后,在拼平台对应设备可以查看到:
3、获取平台下发的结果
代码如下:
-
TaskHandle_t g_onenet_http_device_recv_pro_task_handle =
NULL;
-
-
/**************************************************************
-
函数名称 : onenet_http_device_recv_pro
-
函数功能 : 平台返回数据检测处理
-
输入参数 : data:平台返回的数据
-
返回值 : 无
-
备注 : 无
-
**************************************************************/
-
void onenet_http_device_recv_pro(unsigned char *dataPtr)
-
{
-
uart_printf(
"%s", dataPtr);
/* 将平台下发的结果通过串口打印出来 */
-
}
-
-
#define ONENET_MQTT_RECV_MAX_SIZE 1024
-
/**************************************************************
-
函数名称 : onenet_http_device_recv_pro_task
-
函数功能 : 接收onenet下发数据处理任务函数
-
输入参数 : pvParameter:任务入口参数
-
返回值 : 无
-
备注 : 无
-
**************************************************************/
-
void onenet_http_device_recv_pro_task(void *pvParameter)
-
{
-
unsigned
char data_ptr[ONENET_MQTT_RECV_MAX_SIZE];
-
-
memset(data_ptr,
0,
sizeof(data_ptr));
-
-
while(
1)
-
{
-
if(socket_receive(g_onenet_http_socket_id, data_ptr, ONENET_MQTT_RECV_MAX_SIZE) >
0)
//使用MSG_DONTWAIT会比较稳定
-
{
-
onenet_http_device_recv_pro(data_ptr);
//集中处理
-
memset(data_ptr,
0,
sizeof(data_ptr));
-
}
-
vTaskDelay(
1);
-
}
-
}
-
/**************************************************************
-
函数名称 : onenet_http_device_recv_pro_task_init
-
函数功能 : 创建接收onenet下发数据处理任务
-
输入参数 : 无
-
返回值 : 无
-
备注 : 无
-
**************************************************************/
-
void onenet_http_device_recv_pro_task_init(void)
-
{
-
if(g_onenet_http_device_recv_pro_task_handle ==
NULL) {
-
xTaskCreate(onenet_http_device_recv_pro_task,
-
"onenet_http_device_recv_pro_task",
-
1024 *
4 /
sizeof(portSTACK_TYPE),
-
NULL,
-
TASK_PRIORITY_NORMAL,
-
&g_onenet_http_device_recv_pro_task_handle);
-
ONENET_HTTP_LOG(
"onenet_http_device_recv_pro_task");
-
}
-
-
}
串口打印POST的结果如下:
4、GET数据流
代码如下:
-
char *g_onenet_http_pro_api_key =
"SlxhH3MCLvuuvXJ0N=a14Yo6EAQ=";
-
char *g_onenet_http_dev_id =
"505619290";
-
-
char *get_data_stream_name =
"test_stream";
-
-
/**************************************************************
-
函数名称 : onenet_http_get_data_stream
-
函数功能 : GET OneNET平台数据流
-
输入参数 : 无
-
返回值 : HTTP_GET_OK:发送GET成功
-
HTTP_GET_ERROR:发送GET失败
-
备注 : 无
-
**************************************************************/
-
http_get_t onenet_http_get_data_stream(
char *dev_id,
char *api_key)
-
{
-
char get_buf[ONENET_HTTP_GET_MAX_LEN];
-
-
memset(get_buf,
0,
sizeof(get_buf));
-
-
strcat(get_buf,
"GET http://api.heclouds.com/devices/");
-
strcat(get_buf, dev_id);
-
strcat(get_buf,
"/datapoints?datastream_id=");
-
strcat(get_buf, get_data_stream_name);
-
strcat(get_buf,
" HTTP/1.1\r\n");
-
strcat(get_buf,
"api-key:");
-
strcat(get_buf, api_key);
-
strcat(get_buf,
"\r\n");
-
strcat(get_buf,
"Host:api.heclouds.com\r\n");
-
strcat(get_buf,
"\r\n\r\n");
-
-
if(socket_send(g_onenet_http_socket_id, get_buf,
strlen(get_buf)) >
0)
-
{
-
return HTTP_GET_OK;
-
}
-
else
-
{
-
return HTTP_GET_ERROR;
-
}
-
}
-
-
/* GET数据流 */
-
if(HTTP_GET_OK == onenet_http_get_data_stream(g_onenet_http_dev_id, g_onenet_http_pro_api_key))
-
{
-
ONENET_HTTP_LOG(
"get success");
-
}
-
else
-
{
-
ONENET_HTTP_LOG(
"get failed");
-
}
串口打印GET的结果如下: