RT-Thread 軟件包-IoT-OneNET①
OneNET
中文頁 | English
1、介紹
OneNET 平臺是中國移動基于物聯(lián)網(wǎng)產(chǎn)業(yè)打造的生態(tài)平臺,具有高并發(fā)可用、多協(xié)議接入、豐富 API 支持、數(shù)據(jù)安全存儲、快速應(yīng)用孵化等特點,同時,OneNET 平臺還提供全方位支撐,加速用戶產(chǎn)品的開發(fā)速度。
OneNET 軟件包是 RT-Thread 針對 OneNET 平臺連接做的的適配,通過這個軟件包,可以讓設(shè)備在 RT-Thread 上非常方便的連接 OneNet 平臺,完成數(shù)據(jù)的發(fā)送、接收、設(shè)備的注冊和控制等功能。
軟件包具有以下優(yōu)點:
- 斷線重連
- 自動注冊
- 自定義響應(yīng)函數(shù)
- 自定義 topic 和 topic 對應(yīng)的回調(diào)函數(shù)
- 上傳二進制數(shù)據(jù)
更多介紹請查看詳細介紹
1.1 目錄結(jié)構(gòu)
OneNET
│ README.md // 軟件包使用說明
│ SConscript // RT-Thread 默認的構(gòu)建腳本
├───docs
│ └───figures // 文檔使用圖片
│ │ api.md // API 使用說明
│ │ introduction.md // 軟件包詳細介紹
│ │ principle.md // 實現(xiàn)原理
│ │ README.md // 文檔結(jié)構(gòu)說明
│ │ samples.md // 軟件包示例
│ │ user-guide.md // 使用說明
│ │ port.md // 移植說明文檔
│ └───version.md // 版本
├───ports // 移植文件
│ └───onenet_port.c // 移植文件模板
├───samples // 示例代碼
│ └───onenet_sample.c // 軟件包應(yīng)用示例代碼
├───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 軟件包的配置菜單按下圖所示配置,里面的信息依據(jù)自己的產(chǎn)品和設(shè)備的實際情況填寫
--- 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 協(xié)議連接 OneNET 支持
Enable OneNET automatic register device :開啟 OneNET 自動注冊設(shè)備功能
device id :配置云端創(chuàng)建設(shè)備時獲取的 設(shè)備ID
auth info :配置云端創(chuàng)建產(chǎn)品時 用戶自定義的鑒權(quán)信息
(每個產(chǎn)品的每個設(shè)備唯一)
api key :配置云端創(chuàng)建設(shè)備時獲取的 APIkey
product id :配置云端創(chuàng)建產(chǎn)品時獲取的 產(chǎn)品ID
master/product apikey :配置云端創(chuàng)建產(chǎn)品時獲取的 產(chǎn)品APIKey
配置完成后讓 RT-Thread 的包管理器自動更新,或者使用 pkgs --update 命令更新包到 BSP 中。
3、使用 OneNET 軟件包
-
詳細的示例介紹,請參考 示例文檔 。
-
如何從零開始使用,請參考 用戶手冊。
-
完整的 API 文檔,請參考 API 手冊。
-
OneNET 軟件包工作原理,請參考 工作原理 。
-
OneNET 軟件包移植,請參考 移植手冊 。
-
更多詳細介紹文檔位于
/docs
文件夾下,使用軟件包進行開發(fā)前請務(wù)必查看。文章來源:http://www.zghlxwxcb.cn/news/detail-824061.html
4、注意事項
- 未啟用自動注冊功能,在 menuconfig 選項中配置的
device id
、api key
、product id
、auth info
等信息需要和 OneNET 云端新建產(chǎn)品和新建設(shè)備時獲取的信息一致。 - 啟用自動注冊功能后,需要閱讀移植手冊并完成移植工作。
- 初始化 OneNET package 之前需要設(shè)備聯(lián)網(wǎng)成功。
示例代碼
…\bsp\stm32\stm32l452-st-nucleo\board\board.c文章來源地址http://www.zghlxwxcb.cn/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
到了這里,關(guān)于RT-Thread 軟件包-IoT-OneNET①的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!