RT-Thread 软件包-IoT-OneNET①
OneNET
中文页 | English
1、介绍
OneNET 平台是中国移动基于物联网产业打造的生态平台,具有高并发可用、多协议接入、丰富 API 支持、数据安全存储、快速应用孵化等特点,同时,OneNET 平台还提供全方位支撑,加速用户产品的开发速度。
OneNET 软件包是 RT-Thread 针对 OneNET 平台连接做的的适配,通过这个软件包,可以让设备在 RT-Thread 上非常方便的连接 OneNet 平台,完成数据的发送、接收、设备的注册和控制等功能。
软件包具有以下优点:
- 断线重连
- 自动注册
- 自定义响应函数
- 自定义 topic 和 topic 对应的回调函数
- 上传二进制数据
更多介绍请查看详细介绍
1.1 目录结构
OneNET
│ README.md // 软件包使用说明
│ SConscript // RT-Thread 默认的构建脚本
├───docs
│ └───figures // 文档使用图片
│ │ api.md // API 使用说明
│ │ introduction.md // 软件包详细介绍
│ │ principle.md // 实现原理
│ │ README.md // 文档结构说明
│ │ samples.md // 软件包示例
│ │ user-guide.md // 使用说明
│ │ port.md // 移植说明文档
│ └───version.md // 版本
├───ports // 移植文件
│ └───onenet_port.c // 移植文件模板
├───samples // 示例代码
│ └───onenet_sample.c // 软件包应用示例代码
├───inc // 头文件
└───src // 源文件
1.2 许可证
OneNET package 遵循 GUN GPL 许可,详见 LICENSE
文件。
1.3 依赖
- RT_Thread 3.0+
- paho-mqtt
- webclient
- cJSON
2、获取方式
使用 OneNET package
需要在 RT-Thread 的包管理中选中它,具体路径如下:
RT-Thread online packages
IoT - internet of things --->
IoT Cloud --->
[*] OneNET: China Mobile OneNet cloud SDK for RT-Thread
进入 onenet 软件包的配置菜单按下图所示配置,里面的信息依据自己的产品和设备的实际情况填写
--- OneNET: China Mobile OneNet cloud SDK for RT-Thread
[ ] Enable OneNET sample
[*] Enable support MQTT protocol
[ ] Enable OneNET automatic register device (NEW)
(35936966) device id
(201807171718) auth info
(H3ak5Bbl0NxpW3QVVe33InnPxOg=) api key
(156418) product id
(dVZ=ZjVJvGjXIUDsbropzg1a8Dw=) master/product apikey (NEW)
version (latest) --->
Enable OneNET sample :开启 OneNET 示例代码
Enable support MQTT protocol :开启 MQTT 协议连接 OneNET 支持
Enable OneNET automatic register device :开启 OneNET 自动注册设备功能
device id :配置云端创建设备时获取的 设备ID
auth info :配置云端创建产品时 用户自定义的鉴权信息
(每个产品的每个设备唯一)
api key :配置云端创建设备时获取的 APIkey
product id :配置云端创建产品时获取的 产品ID
master/product apikey :配置云端创建产品时获取的 产品APIKey
配置完成后让 RT-Thread 的包管理器自动更新,或者使用 pkgs --update 命令更新包到 BSP 中。
3、使用 OneNET 软件包
-
详细的示例介绍,请参考 示例文档 。
-
如何从零开始使用,请参考 用户手册。
-
完整的 API 文档,请参考 API 手册。
-
OneNET 软件包工作原理,请参考 工作原理 。
-
OneNET 软件包移植,请参考 移植手册 。
-
更多详细介绍文档位于
/docs
文件夹下,使用软件包进行开发前请务必查看。文章来源:https://www.toymoban.com/news/detail-824061.html
4、注意事项
- 未启用自动注册功能,在 menuconfig 选项中配置的
device id
、api key
、product id
、auth info
等信息需要和 OneNET 云端新建产品和新建设备时获取的信息一致。 - 启用自动注册功能后,需要阅读移植手册并完成移植工作。
- 初始化 OneNET package 之前需要设备联网成功。
示例代码
…\bsp\stm32\stm32l452-st-nucleo\board\board.c文章来源地址https://www.toymoban.com/news/detail-824061.html
/*
* File : onenet_http.c
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-04-24 chenyong first version
*/
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <cJSON_util.h>
#include <webclient.h>
#include <onenet.h>
#define DBG_ENABLE
#define DBG_COLOR
#define DBG_SECTION_NAME "onenet.http"
#if ONENET_DEBUG
#define DBG_LEVEL DBG_LOG
#else
#define DBG_LEVEL DBG_INFO
#endif /* ONENET_DEBUG */
#include <rtdbg.h>
#define ONENET_SEND_DATA_LEN 1024
#define ONENET_HEAD_DATA_LEN 256
#define ONENET_CON_URI_LEN 256
#define ONENET_RECV_RESP_LEN 1024
#define ONENET_TIME_BUF_LEN 24
#define ONENET_HTTP_HEAD_LEN 1024
#if WEBCLIENT_SW_VERSION_NUM < 0x20000
#error "Please upgrade the webclient version "
#endif
#define WEBCLIENT_HEADER_ADD(session, fmt, ...) \
do \
{ \
if (webclient_header_fields_add(session, fmt, ##__VA_ARGS__) < 0) \
{ \
LOG_E("webclient add header failed!"); \
goto __exit; \
} \
} while(0); \
extern struct rt_onenet_info onenet_info;
static rt_err_t onenet_upload_data(char *send_buffer)
{
struct webclient_session *session = RT_NULL;
char *buffer = send_buffer;
char *URI = RT_NULL;
rt_err_t result = RT_EOK;
RT_ASSERT(send_buffer);
URI = ONENET_CALLOC(1, ONENET_CON_URI_LEN);
if (URI == RT_NULL)
{
LOG_E("OneNet Send data failed! No memory for URI buffer!");
result = -RT_ENOMEM;
goto __exit;
}
rt_snprintf(URI, ONENET_CON_URI_LEN, "http://api.heclouds.com/devices/%s/datapoints?type=3", onenet_info.device_id);
session = webclient_session_create(ONENET_HTTP_HEAD_LEN);
if (session == RT_NULL)
{
result = -RT_ERROR;
goto __exit;
}
WEBCLIENT_HEADER_ADD(session, "api-key: %s\r\n", onenet_info.api_key);
WEBCLIENT_HEADER_ADD(session, "Content-Length: %d\r\n", strlen(buffer));
WEBCLIENT_HEADER_ADD(session, "Content-Type: application/octet-stream\r\n");
if (webclient_post(session, URI, buffer, strlen(buffer)) != 200)
{
result = -RT_ERROR;
goto __exit;
}
LOG_D("buffer : %.*s", strlen(buffer), buffer);
__exit:
if (session)
{
webclient_close(session);
}
if (URI)
{
ONENET_FREE(URI);
}
return result;
}
static rt_err_t onenet_get_string_data(const char *ds_name, const char *str, char **out_buff)
{
rt_err_t result = RT_EOK;
cJSON *root = RT_NULL;
RT_ASSERT(ds_name);
RT_ASSERT(str);
RT_ASSERT(out_buff);
root = cJSON_CreateObject();
if (!root)
{
LOG_E("onenet publish string data failed! cJSON create object error return NULL!");
return -RT_ENOMEM;
}
cJSON_AddStringToObject(root, ds_name, str);
/* render a cJSON structure to buffer */
*out_buff = cJSON_PrintUnformatted(root);
if (!(*out_buff))
{
LOG_E("onenet publish string data failed! cJSON print unformatted error return NULL!");
result = -RT_ENOMEM;
goto __exit;
}
__exit:
if (root)
{
cJSON_Delete(root);
}
return result;
}
static rt_err_t onenet_get_digit_data(const char *ds_name, const double digit, char **out_buff)
{
rt_err_t result = RT_EOK;
cJSON *root = RT_NULL;
RT_ASSERT(ds_name);
RT_ASSERT(out_buff);
root = cJSON_CreateObject();
if (!root)
{
LOG_E("onenet publish digit data failed! cJSON create object error return NULL!");
return -RT_ENOMEM;
}
cJSON_AddNumberToObject(root, ds_name, digit);
/* render a cJSON structure to buffer */
*out_buff = cJSON_PrintUnformatted(root);
if (!(*out_buff))
{
LOG_E("onenet publish digit data failed! cJSON print unformatted error return NULL!");
result = -RT_ENOMEM;
goto __exit;
}
__exit:
if (root)
{
cJSON_Delete(root);
}
return result;
}
/**
* upload digit data to OneNET cloud.
*
* @param ds_name datastream name
* @param digit digit data
*
* @return 0 : upload data success
* -5 : no memory
*/
rt_err_t onenet_http_upload_digit(const char *ds_name, const double digit)
{
char *send_buffer = RT_NULL;
rt_err_t result = RT_EOK;
RT_ASSERT(ds_name);
/* get JSON format data */
result = onenet_get_digit_data(ds_name, digit, &send_buffer);
if (result < 0)
{
goto __exit;
}
/* send data to cloud by HTTP */
result = onenet_upload_data(send_buffer);
if (result < 0)
{
goto __exit;
}
__exit:
if (send_buffer)
{
cJSON_free(send_buffer);
}
return result;
}
/**
* upload string data to OneNET cloud.
*
* @param ds_name datastream name
* @param str string data
*
* @return 0 : upload data success
* -5 : no memory
*/
rt_err_t onenet_http_upload_string(const char *ds_name, const char *str)
{
char *send_buffer = RT_NULL;
rt_err_t result = RT_EOK;
RT_ASSERT(ds_name);
RT_ASSERT(str);
/* get JSON format data */
result = onenet_get_string_data(ds_name, str, &send_buffer);
if (result < 0)
{
goto __exit;
}
/* send data to cloud by HTTP */
result = onenet_upload_data(send_buffer);
if (result < 0)
{
goto __exit;
}
__exit:
if (send_buffer)
{
cJSON_free(send_buffer);
}
return result;
}
#ifdef ONENET_USING_AUTO_REGISTER
static rt_err_t response_register_handlers(const unsigned char *rec_buf, const size_t length)
{
cJSON *root = RT_NULL;
cJSON *item = RT_NULL;
cJSON *itemid = RT_NULL;
cJSON *itemapikey = RT_NULL;
RT_ASSERT(rec_buf);
LOG_D("response is %.*s", length, rec_buf);
root = cJSON_Parse((char *)rec_buf);
if (!root)
{
LOG_E("onenet register device failed! cJSON Parse data error return NULL!");
return -RT_ENOMEM;
}
item = cJSON_GetObjectItem(root, "errno");
if (item->valueint == 0)
{
itemid = cJSON_GetObjectItem(root->child->next, "device_id");
itemapikey = cJSON_GetObjectItem(root->child->next, "key");
onenet_port_save_device_info(itemid->valuestring, itemapikey->valuestring);
}
else
{
LOG_E("onenet register device failed! errno is %d", item->valueint);
return -RT_ERROR;
}
return RT_EOK;
}
#endif /* ONENET_USING_AUTO_REGISTER */
维护人:
- 华为奋斗者精神, 邮箱:1992152446@qq.com
到了这里,关于RT-Thread 软件包-IoT-OneNET①的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!