OpenHarmony继续上云之腾讯云平台

系统
说实话,腾讯云物联网平台一直是我比较喜欢的物联网云太平,除了有腾讯大厂作为品质背书之外,提供的sdk也是相当好用,更有腾讯连连小程序可以帮助做界面,调试功能.

[[442860]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

一.前面的话

说实话,腾讯云物联网平台一直是我比较喜欢的物联网云太平,除了有腾讯大厂作为品质背书之外,提供的sdk也是相当好用,更有腾讯连连小程序可以帮助做界面,调试功能,后端的API有java,c++,Golang,js,python等等语言版本,非常方便,简直是上云首选.

二.首先下载sdk

我们首先来找官方sdk,茫茫文档中给我看到了那熟悉的身影:

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

文档地址在这,拿走不谢:

https://cloud.tencent.com/document/product/1081/48356

把sdk下载之后,熟练的放进thirdparty文件夹,

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

不得不提一下,2.x版本的OpenHarmony比1.x版本的代码结构清晰多了.

1.实现几个重要接口

这个时候依然要看文档,因为有些函数需要自己实现,具体是哪些呢,在这里:

https://cloud.tencent.com/document/product/1081/48389

这篇文档写了,我们要实现里面的这些接口,此处列举一二:

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区
#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

仔细一看,霍,好家伙还不少呢,但是不怕,都是打工人,谁怕谁啊,二话不说我就写,结果就给我给写出来了:

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

写出来这些后就可以准备编译了吗?

nonono,我们还没做BUILD.gn文件呢,话不多说,直接教你写。

  1. # Copyright (c) 2020 Huawei Device Co., Ltd. 
  2. # Licensed under the Apache License, Version 2.0 (the "License"); 
  3. # you may not use this file except in compliance with the License. 
  4. # You may obtain a copy of the License at 
  5. #     http://www.apache.org/licenses/LICENSE-2.0 
  6. # Unless required by applicable law or agreed to in writing, software 
  7. # distributed under the License is distributed on an "AS IS" BASIS, 
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  9. # See the License for the specific language governing permissions and 
  10. # limitations under the License. 
  11.  
  12. import("//build/lite/config/component/lite_component.gni"
  13. import("//build/lite/ndk/ndk.gni"
  14.  
  15. config("qcloud_sdk_config") { 
  16.      
  17.        include_dirs = [ 
  18.         "sdk_src/internal_inc"
  19.         "sdk_src/library"
  20.         "include"
  21.         "include/exports"
  22.  
  23.         "//kernel/liteos_m/kernel/include"
  24.        
  25.         # "//third_party/cmsis"
  26.         "//third_party/mbedtls/include"
  27.         "//third_party/mbedtls/include/mbedtls"
  28.      
  29.     ] 
  30.  
  31.  
  32.     cflags = [ "-Wno-unused-variable" ] 
  33.     cflags += [ "-Wno-unused-but-set-variable" ] 
  34.     cflags += [ "-Wno-unused-parameter" ] 
  35.     cflags += [ "-Wno-sign-compare" ] 
  36.     cflags += [ "-Wno-unused-function" ] 
  37.     cflags += [ "-Wno-return-type" ] 
  38.  
  39. qcloud_sdk_sources = [ 
  40.     "sdk_src/network/socket/network_socket.c"
  41.     "sdk_src/network/tls/network_tls.c"
  42.     "sdk_src/network/network_interface.c"
  43.  
  44.     "sdk_src/utils/utils_list.c"
  45.     "sdk_src/utils/utils_base64.c"
  46.     "sdk_src/utils/qcloud_iot_ca.c"
  47.     "sdk_src/utils/utils_aes.c"
  48.     "sdk_src/utils/utils_getopt.c"
  49.     "sdk_src/utils/utils_hmac.c"
  50.     "sdk_src/utils/utils_md5.c"
  51.     "sdk_src/utils/utils_sha1.c"
  52.     "sdk_src/utils/json_parser.c"
  53.     "sdk_src/utils/json_token.c"
  54.     "sdk_src/utils/string_utils.c"
  55.     "sdk_src/utils/utils_ringbuff.c"
  56.     "sdk_src/utils/qcloud_iot_log.c"
  57.     "sdk_src/utils/qcloud_iot_device.c"
  58.     "sdk_src/utils/utils_timer.c"
  59.  
  60.     "sdk_src/protocol/mqtt/mqtt_client_common.c"
  61.     "sdk_src/protocol/mqtt/mqtt_client_connect.c"
  62.     "sdk_src/protocol/mqtt/mqtt_client_net.c"
  63.     "sdk_src/protocol/mqtt/mqtt_client_publish.c"
  64.     "sdk_src/protocol/mqtt/mqtt_client_subscribe.c"
  65.     "sdk_src/protocol/mqtt/mqtt_client_unsubscribe.c"
  66.     "sdk_src/protocol/mqtt/mqtt_client_yield.c"
  67.     "sdk_src/protocol/mqtt/mqtt_client.c"
  68.  
  69.     "sdk_src/services/data_template/data_template_action.c"
  70.     "sdk_src/services/data_template/data_template_client.c"
  71.     "sdk_src/services/data_template/data_template_client_common.c"
  72.     "sdk_src/services/data_template/data_template_client_json.c"
  73.     "sdk_src/services/data_template/data_template_client_manager.c"
  74.     "sdk_src/services/data_template/data_template_event.c"
  75.  
  76.     "platform/os/liteos_m/HAL_Device_liteos_m.c"
  77.     "platform/os/liteos_m/HAL_OS_liteos_m.c"
  78.     "platform/os/liteos_m/HAL_TCP_liteos_m.c"
  79.     "platform/os/liteos_m/HAL_Timer_liteos_m.c"
  80.     # "./os/liteos_m/HAL_TLS_mbedtls_liteos_m.c"
  81.     # "./tls/mbedtls/HAL_DTLS_mbedtls.c"
  82.     "platform/tls/mbedtls/HAL_TLS_mbedtls.c"
  83.  
  84.   
  85.  
  86. lite_library("qcloud_sdk_static") { 
  87.     target_type = "static_library" 
  88.  
  89.     sources = qcloud_sdk_sources 
  90.     public_configs = [ ":qcloud_sdk_config" ] 
  91.  
  92. lite_library("qcloud_sdk_shared") { 
  93.     target_type = "shared_library" 
  94.     sources = qcloud_sdk_sources 
  95.     public_configs = [ ":qcloud_sdk_config" ] 
  96.  
  97. ndk_lib("qcloud_ndk") { 
  98.     if (board_name != "hi3861v100") { 
  99.         lib_extension = ".so" 
  100.         deps = [ 
  101.             ":qcloud_sdk_shared" 
  102.         ] 
  103.     } else { 
  104.         deps = [ 
  105.             ":qcloud_sdk_static" 
  106.         ] 
  107.     } 
  108.     head_files = [ 
  109.         "//third_party/iot_link/network/mqtt/paho_mqtt/paho" 
  110.     ] 

整完之后就可以尝试一下有没有错误了.

2.在main里面写逻辑,处理事情

我们把官方sdk里面的点灯demo拿过来,直接新建一个demo文件夹,把文件放进去:

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

自己写好BUILD.gn,跟之前华为云对接一个套路,大家这么聪明,不用我多说了:

  1. # Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. 
  2. # Licensed under the Apache License, Version 2.0 (the "License"); 
  3. # you may not use this file except in compliance with the License. 
  4. # You may obtain a copy of the License at 
  5. #     http://www.apache.org/licenses/LICENSE-2.0 
  6. # Unless required by applicable law or agreed to in writing, software 
  7. # distributed under the License is distributed on an "AS IS" BASIS, 
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  9. # See the License for the specific language governing permissions and 
  10. # limitations under the License. 
  11.  
  12. static_library("qcloud_demo") { 
  13.     sources = [ 
  14.         # "iot_thread.c"
  15.         "light.c"
  16.         "light_data_template_sample.c" 
  17.         
  18.     ] 
  19.      
  20.     cflags = [ "-Wno-unused-variable" ] 
  21.     cflags += [ "-Wno-unused-but-set-variable" ] 
  22.      
  23.     include_dirs = [ 
  24.         "."
  25.         "//foundation/communication/softbus_lite/os_adapter/include"
  26.         "//utils/native/lite/include"
  27.         "//kernel/liteos_m/components/cmsis/2.0"
  28.         "//base/iot_hardware/peripheral/interfaces/kits"
  29.         "//third_party" 
  30.  
  31.     ] 
  32.  
  33.     deps = [  
  34.         "//third_party/qcloud-iot-explorer-sdk-embedded-c-3.2.0:qcloud_sdk_static"
  35.         # "//third_party/qcloud-iot-explorer-sdk-embedded-c-3.2.0/external_libs/mbedtls:mbedtls_static"
  36.     ] 
  37.  
  38.  

然后在sample里面的sdk把这个文件夹开启编译:

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

这样就完成了代码的编写了,很快乐有木有。

三.在云平台上创建设备

其实创建设备的过程官方文档也的确挺详细的,几乎不用再另外加工,这里给出官方的智能灯接入指南:

https://cloud.tencent.com/document/product/1081/41155

官方定义了许多的物模型,其实我们的产品可能是官方物模型没有定义的,此时我们就需要自己定义产品的属性,事件和动作等,这块自己仔细阅读官方文档就能搞懂

https://cloud.tencent.com/document/product/1081/34916

搞定了物模型,创建一个设备,记录下设备的产品id,设备id和连接秘钥

这点跟华为云平台不太一样,腾讯云的产品秘钥是平台生成的,而华为云平台是你自己定义好设备的秘钥,在创建设备的时候传上去

四.编译,运行

编译的过程倒是很顺利,这里就不过多废话了.

不过在运行的时候出现一个情况,就是mutex不够用了

自己折腾了一晚上也没找不解决办法,后面经过请教猴哥才搞明白:

[[442861]]

阿不对,下面这个候哥才对,大家可以去他主页关注一下:

https://harmonyos.51cto.com/user/posts/13519852

候哥真大神也,短短一两句话就帮我搞定了,在此特别感谢侯哥的帮助:

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

然后我就把我的mutex相关代码改成posix接口,这是修改之前的:

  1. void *HAL_MutexCreate(void) 
  2.     osMutexAttr_t   attr; 
  3.     osMutexId_t     mutex; 
  4.     char            mutexName[RT_NAME_MAX]; 
  5.     static uint32_t mutex_v; 
  6.  
  7.     attr.name    = mutexName; 
  8.     attr.cb_mem  = &mutex_v; 
  9.     attr.cb_size = 4; 
  10.  
  11.     mutex = osMutexNew(&attr); 
  12.     if (NULL == mutex) { 
  13.         HAL_Printf("create mutex failed"); 
  14.     } 
  15.  
  16.     return mutex; 
  17.  
  18. void HAL_MutexDestroy(_IN_ void *mutex) 
  19.     int err_num; 
  20.  
  21.     err_num = osMutexDelete((osMutexId_t)mutex); 
  22.  
  23.     if (0 != err_num) { 
  24.         HAL_Printf("destroy mutex failed"); 
  25.     } 
  26.  
  27. void HAL_MutexLock(_IN_ void *mutex) 
  28.     int err_num; 
  29.  
  30.     err_num = osMutexAcquire((osMutexId_t)mutex, osWaitForever); 
  31.  
  32.     if (0 != err_num) { 
  33.         HAL_Printf("lock mutex failed"); 
  34.     } 
  35.  
  36. void HAL_MutexUnlock(_IN_ void *mutex) 
  37.     int err_num; 
  38.  
  39.     err_num = osMutexRelease((osMutexId_t)mutex); 
  40.  
  41.     if (0 != err_num) { 
  42.         HAL_Printf("unlock mutex failed"); 
  43.     } 
  44.  
  45. int HAL_MutexTryLock(_IN_ void *mutex) 
  46.     int err_num; 
  47.  
  48.     err_num = osMutexAcquire((osMutexId_t)mutex, osNoWait); 
  49.  
  50.     if (0 != err_num) { 
  51.         HAL_Printf("trylock mutex failed"); 
  52.     } 
  53.     return err_num; 

改完之后是这样:

  1. void *HAL_MutexCreate(void) 
  2.     // osMutexAttr_t   attr; 
  3.     // osMutexId_t     mutex; 
  4.     char            mutexName[RT_NAME_MAX]; 
  5.     static uint32_t mutex_v; 
  6.  
  7.     pthread_mutex_t *   mutex = HAL_Malloc(sizeof(pthread_mutex_t)); 
  8.     pthread_mutexattr_t attr; 
  9.  
  10.     int ret = pthread_mutex_init(mutex, &attr);  // osMutexNew(&attr); 
  11.     // if (NULL == mutex) { 
  12.     if (ret != 0) { 
  13.         HAL_Printf("create mutex failed\n"); 
  14.     } 
  15.  
  16.     return mutex; 
  17.  
  18. void HAL_MutexDestroy(_IN_ void *mutex) 
  19.     int err_num; 
  20.  
  21.     err_num = pthread_mutex_destroy(mutex); 
  22.  
  23.     if (0 != err_num) { 
  24.         HAL_Printf("destroy mutex failed"); 
  25.     } 
  26.  
  27. void HAL_MutexLock(_IN_ void *mutex) 
  28.     int err_num; 
  29.  
  30.     err_num = pthread_mutex_lock(mutex); 
  31.  
  32.     if (0 != err_num) { 
  33.         HAL_Printf("lock mutex failed"); 
  34.     } 
  35.  
  36. void HAL_MutexUnlock(_IN_ void *mutex) 
  37.     int err_num; 
  38.  
  39.     err_num = pthread_mutex_unlock(mutex); 
  40.  
  41.     if (0 != err_num) { 
  42.         HAL_Printf("unlock mutex failed"); 
  43.     } 
  44.  
  45. int HAL_MutexTryLock(_IN_ void *mutex) 
  46.     int err_num; 
  47.     struct timespec absTimeout={0,0}; 
  48.  
  49.     err_num = pthread_mutex_timedlock(mutex, &absTimeout); 
  50.  
  51.     if (0 != err_num) { 
  52.         HAL_Printf("trylock mutex failed"); 
  53.     } 
  54.     return err_num; 

使用的时候记得加上头文件: #include

处理完这个异常情况,接下来就很顺利的登录腾讯云平台收发数据了

五.云平台控制展示

直接查看动图,控制还是很及时的:

#星光计划2.0#  OpenHarmony继续上云之腾讯云平台-鸿蒙HarmonyOS技术社区

六.总结

其实对接腾讯云还是蛮简单的,最新版的sdk在打印上也比以前好多了.OpenHarmony的2.x版本优化了不少东西,记得去年这时候用1.x版本对接的时候还是很费劲的,给OpenHarmony团队点赞,也给腾讯云团队点赞

大家赶紧玩起来吧

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2023-09-28 08:01:41

2013-07-18 10:10:31

2021-12-28 16:06:15

鸿蒙HarmonyOS应用

2012-06-13 11:10:59

2016-07-05 10:53:56

2021-06-08 09:54:18

云计算混合云Region架构

2015-05-05 11:20:09

腾讯云

2019-05-14 13:02:26

私有云公有云企业

2022-10-26 11:59:05

腾讯云IaaSPaaS

2016-12-27 16:13:06

亚马逊云阿里云腾讯云

2016-07-14 16:48:59

云计算

2012-07-06 15:48:59

华为服务器

2020-11-29 15:09:15

腾讯云云开发代码

2016-04-28 14:03:39

腾讯云计算CDN

2017-08-23 10:50:26

腾讯云政企转型

2017-11-14 14:21:15

甲骨文云大会SaaS

2015-09-01 15:47:00

腾讯云安全云

2021-04-23 09:50:46

腾讯云数字化大运河

2018-12-10 23:01:44

点赞
收藏

51CTO技术栈公众号