OpenHarmony开源社区
直播中

小伍

8年用户 4725经验值
擅长:电源/新能源 嵌入式技术
私信 关注
[经验]

【OpenHarmony开发样例】智能加湿器

一、 介绍
智能加湿器具有实时监控其所处环境温度、湿度,并通过数字管家设置日程,自动打开加湿器控制湿度功能。显示界面使用DevEco Studio 编写的js应用,具有很好的兼容和移植特性。硬件上采用了带有HDF框架的驱动模型,通过GPIO和IIC分别来控制电机开关和采集湿度信息,还可以通过语音模块识别语音命令,再通过串口跟主板通信来控制加湿器电机开关。

1.png

1. 交互流程
2.png
如下图所示,智能加湿器整体方案原理图可以大致分成:智能加湿器设备、数字管家应用、云平台三部分。智能加湿器通过MQTT协议连接华为IOT物联网平台,从而实现命令的接收和属性上报。 关于智能设备接入华为云IoT平台的细节可以参考 连接IOT云平台指南;智能设备同数字管家应用之间的设备模型定义可以参考profile 。

2.实物简介
3.jpg
如上图,我们使用V200Z-R蓝牙WIFI语音AIoT模组恒玄BES2600WM开发板来模拟智能加湿器设备。

首先,通过移动端APP给智能加湿器设置若干日程,当打开智能加湿器设备时,能迅速获取房间的温湿度,并显示在屏幕上。

然后,可以通过中间“温度调节”模块调节湿度的最大阈值,还可以通过语音来控制加湿器开关。

3.实物操作体验
4.gif

二、快速上手
1、硬件准备

V200Z-R蓝牙WIFI语音AIoT模组恒玄BES2600WM开发板
BearPi-HM Nano套件中的E53_IA1拓展板
预装HarmonyOS手机一台
机芯智能语音模块
硬件连线图:
5.jpg
2、Linux编译服务器基础环境准备开发基础环境由 windows 工作台和 Linux  编译服务器组成。windows 工作台可以通过 samba 服务或 ssh 方式访问 Linux编译服务器。其中 windows 工作台用来烧录和代码编辑,Linux 编译服务器用来编译 OpenHarmony 代码,为了简化步骤,Linux 编译服务器推荐安装 Ubuntu20.04 64位。

编译环境搭建包含如下几步:
  • 安装的库和工具
  • 安装python3
  • 安装arm-none-eabi-gcc
  • 获取源码&文件拷贝和修改
  • 编译流程
  • 设备配网

2.1 安装的库和工具
使用如下apt-get命令安装下面的库和工具,Ubuntu20.04 64位系统需要安装以下依赖:
安装编译依赖基础软件
  1. sudo apt-get install -y build-essential gcc g++ make zlib* libffi-dev git git-lfs
2.2 安装和配置Python
  • 打开Linux终端。
  • 输入如下命令,查看python版本号,需要使用python3.7以上版本,否则参考 系统基础环境搭建。
    1. python3 --version

  • 安装并升级Python包管理工具(pip3)。
  1. sudo apt-get install python3-setuptools python3-pip -y
  2. sudo pip3 install --upgrade pip
  3. pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
4.安装python模块
  1. sudo pip3 install setuptools kconfiglib pycryptodome ecdsa six --upgrade --ignore-installed six
2.3  安装arm-none-eabi-gcc
  • 打开Linux编译服务器终端。
  • 下载arm-none-eabi-gcc 编译工具,下载链接地址。
  • 解压 gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2安装包至~/toolchain/路径下。
    1. mkdir -p ~/toolchain/
    2. tar -jxvf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2 -C ~/toolchain/

  • 设置环境变量。
    1. vim ~/.bashrc
    将以下命令拷贝到.bashrc文件的最后一行,保存并退出。
    1. export PATH=~/toolchain/gcc-arm-none-eabi-10.3-2021.10/bin:$PATH

  • 生效环境变量。
    1. source ~/.bashrc

2.4 获取源码&文件拷贝和修改
1.码云工具下载

1)下载repo工具
  1. mkdir ~/bin
  2. curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > ~/bin/repo
  3. chmod a+x ~/bin/repo
2)设置环境变量并生效
  1. vim ~/.bashrc
  2. export PATH=~/bin:$PATH
  3. source ~/.bashrc
2.代码下载

OpenHarmony代码下载
  1. #特别注意:请下载OpenHarmony 3.1 Beta版本
  2. mkdir ~/OpenHarmony_3.1_Beta
  3. cd ~/OpenHarmony_3.1_Beta
  4. repo init -u git@gitee.com:openharmony/manifest.git -b refs/tags/OpenHarmony-v3.1-Beta --no-repo-verify
  5. repo sync -c
  6. repo forall -c 'git lfs pull'
3.安装 hb
  • 进入代码工程目录
  1. cd ~/OpenHarmony_3.1_Beta
  • 输入hb -v
​        a.  若提示如下提示,则表示未安装可以从第2步开始操作。
  1. hb: command not found
​       b.若有如下提示(版本小于0.4.4),需要先卸载该版本,然后再执行第2步操作步骤。
  1. [OHOS INFO] hb version 0.4.3
​      卸载命令:
  1. pip3 uninstall ohos-build
  • 运行如下命令安装hb
  1. pip3 install build/lite
  • 设置环境变量
  1. vim ~/.bashrc
将以下命令拷贝到.bashrc文件的最后一行,保存并退出。
  1. export PATH=~/.local/bin:$PATH
执行如下命令更新环境变量。
  1. source ~/.bashrc
  • 再次执行”hb -v“,有以下版本显示则表示安装的hb版本正确。
  1. [OHOS INFO] hb version 0.4.4
4.设备侧代码下载

使用git 命令下载。
  1. git clone git@gitee.com:openharmony-sig/knowledge_demo_smart_home.git
5.文件拷贝和修改
1. 代码拷贝
  1. mkdir ~/OpenHarmony_3.1_Beta/vendor/team_x/
  2. 主代码拷贝:
  3. cp -rfa  ~/knowledge_demo_smart_home/dev/team_x/smart_humidifier  ~/OpenHarmony_3.1_Beta/vendor/team_x/
  4. iot_link三方库拷贝:
  5. cp -rfa  ~/knowledge_demo_smart_home/dev/third_party/iot_link ~/OpenHarmony_3.1_Beta/third_party/
  6. common库拷贝:
  7. cp -rfa ~/knowledge_demo_smart_home/dev/team_x/common ~/OpenHarmony_3.1_Beta/vendor/team_x
  8. iot_link库拷贝:
  9. cp -rfa ~/knowledge_demo_smart_home/dev/third_party/iot_link ~/OpenHarmony_3.1_Beta/third_party/
2. kernel/liteos_m 修改

a. 下载patch 地址
​        点击上述链接进入浏览器,将该网页中内容全部复制。
​        本地创建一个名为***.patch文件,并将已经复制的内容粘贴到该文件中。
b.  打上步骤1中的patch
  1. cd ~/OpenHarmony_3.1_Beta/kernel/liteos_m
  2. patch -p1 < ***.patch
3. device/soc/bestechnic 修改

a. 下载patch 地址
​        点击上述链接进入浏览器,将该网页中内容全部复制。
​        本地创建一个名为***.patch文件,并将已经复制的内容粘贴到该文件中。
b.  打上步骤1中的patch
  1. cd ~/OpenHarmony_3.1_Beta/device/soc/bestechnic
  2. patch -p1 < ***.patch
4. third_party/mbedtls 修改
参考如下代码段修改 platform.c 和BUILD.gn
  1. diff --git a/library/platform.c b/library/platform.c
  2. index c4c3fd3..214173b 100755
  3. --- a/library/platform.c
  4. +++ b/library/platform.c
  5. [url=home.php?mod=space&uid=1999721]@@[/url] -86,9 +86,24 @@ static void platform_free_uninit( void *ptr )
  6. static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  7. static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;

  8. +#include "los_memory.h"
  9. +
  10. void * mbedtls_calloc( size_t nmemb, size_t size )
  11. {
  12. -    return (*mbedtls_calloc_func)( nmemb, size );
  13. +    //return (*mbedtls_calloc_func)( nmemb, size );
  14. +    size_t real_size;
  15. +    void *ptr = NULL;
  16. +
  17. +    if (nmemb == 0 || size == 0) {
  18. +        return NULL;
  19. +    }
  20. +
  21. +    real_size = (size_t)(nmemb * size);
  22. +    ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size);
  23. +    if (ptr != NULL) {
  24. +        (void)memset_s(ptr, real_size, 0, real_size);
  25. +    }
  26. +    return ptr;
  27. }
  28. diff --git a/BUILD.gn b/BUILD.gn
  29. index 9ecb37a..30dbb2e 100755
  30. --- a/BUILD.gn
  31. +++ b/BUILD.gn
  32. @@ -124,6 +124,7 @@ if (defined(ohos_lite)) {
  33.          "//kernel/liteos_m/kernel/include",
  34.          "//kernel/liteos_m/utils",
  35.          "//third_party/musl/porting/liteos_m/kernel/include/",
  36. +        "//kernel/liteos_m/kernel/include/",
  37.        ]
  38.      }
  39.      output_name = "mbedtls"<span id="LC41" class="line"></span>
5.third_party/lwip 修改

修改src/api/netdb.c 文件
  1. diff --git a/src/api/netdb.c b/src/api/netdb.c
  2. index 52a6fdf..2043636 100644
  3. --- a/src/api/netdb.c
  4. +++ b/src/api/netdb.c
  5. @@ -100,7 +100,7 @@ lwip_gethostbyname(const char *name)
  6.    err = netconn_gethostbyname(name, &addr);
  7.    if (err != ERR_OK) {
  8.      LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d
  9. ", name, err));
  10. -    h_errno = HOST_NOT_FOUND;
  11. +    //h_errno = HOST_NOT_FOUND;
  12.      return NULL;
  13.    }<span id="LC13" class="line"></span>
6.修改iot_link中的部分文件
a. third_party/iot_link/network/mqtt/paho_mqtt/port/paho_mqtt_port.c
测试发现,当fd为0的时候,在执行recv时会立马返回-1,因此做下面规避操作。
  1. static int __socket_connect(Network *n, const char *host, int port)
  2. {
  3.         ...
  4.         int tmpfd = socket(AF_INET,SOCK_STREAM,0); // to skip fd = 0;
  5.         fd = socket(AF_INET,SOCK_STREAM,0);
  6.         if(fd == -1) {
  7.                 return ret;
  8.         }
  9.         close(tmpfd);       // to skip fd = 0;
  10.         ...
  11. }
系统setsockopt函数未适配,因此需要做下面的修改:
  1. static int __socket_read(void *ctx, unsigned char *buf, int len, int timeout)
  2. {
  3.         int fd;
  4.     int ret = 0;
  5. #if 0
  6.         struct timeval timedelay = {timeout / 1000, (timeout % 1000) * 1000};
  7.     if(NULL== buf)
  8.     {
  9.         return ret;
  10.     }
  11.    
  12.     fd = (int)(intptr_t)ctx;  ///< socket could be zero

  13.     if (timedelay.tv_sec < 0 || (timedelay.tv_sec == 0 && timedelay.tv_usec <= 0))
  14.     {
  15.         timedelay.tv_sec = 0;
  16.         timedelay.tv_usec = 100;
  17.     }
  18.    
  19.     if(0 != setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,&timedelay,sizeof(struct timeval)))
  20.     {
  21.         return ret;  //could not support the rcv timeout
  22.     }
  23.     int bytes = 0;
  24.     while (bytes < len) {
  25.         int rc = recv(fd, &buf[bytes], (size_t)(len - bytes), 0);
  26.         printf("[%s|%s|%d]fd = %d, rc = %d
  27. ", __FILE__,__func__,__LINE__, fd, rc);
  28.         if (rc == -1) {
  29.             if (errno != EAGAIN && errno != EWOULDBLOCK) {
  30.                 bytes = -1;
  31.             }
  32.             break;
  33.         } else if (rc == 0) {
  34.             bytes = 0;
  35.             break;
  36.         } else {
  37.             bytes += rc;
  38.         }
  39.     }
  40.     return bytes;
  41. #else
  42.         int bytes = 0;
  43.     fd_set fdset;

  44.     struct timeval timedelay = {timeout / 1000, (timeout % 1000) * 1000};
  45.     if(NULL== buf)
  46.     {
  47.         return ret;
  48.     }
  49.    
  50.     fd = (int)(intptr_t)ctx;  ///< socket could be zero

  51.     if (timedelay.tv_sec < 0 || (timedelay.tv_sec == 0 && timedelay.tv_usec <= 0))
  52.     {
  53.         timedelay.tv_sec = 0;
  54.         timedelay.tv_usec = 100;
  55.     }
  56.     timedelay.tv_sec = 2;
  57.     FD_ZERO(&fdset);
  58.     FD_SET(fd, &fdset);

  59.     ret = select(fd + 1, &fdset, NULL, NULL, &timedelay);
  60.     if (ret > 0) {
  61.         while (bytes < len) {
  62.             int rc = recv(fd, &buf[bytes], (size_t)(len - bytes), 0);
  63. //         printf("[%s|%s|%d]fd = %d, rc = %d, errno=%d(%s)
  64. ", __FILE__,__func__,__LINE__, fd, rc,errno, strerror(errno));
  65.             if (rc == -1) {
  66.                 if (errno != EAGAIN && errno != EWOULDBLOCK) {
  67.                     bytes = -1;
  68.                 }
  69.                 break;
  70.             } else if (rc == 0) {
  71.                 bytes = 0;
  72.                 break;
  73.             } else {
  74.                 bytes += rc;
  75.             }
  76.         }
  77.     }

  78.     return bytes;
  79. #endif
  80. }
b. third_party/iot_link/network/dtls/mbedtls/mbedtls_port/dtls_interface.c
系统部分mbedtls接口不一致,固需要注释部分接口代码:
  1. mbedtls_ssl_context dtls_ssl_new(dtls_establish_info_s *info, char plat_type)
  2. {
  3.         ...
  4.         if (info->psk_or_cert == VERIFY_WITH_PSK)
  5.     {
  6. /*
  7.         if ((ret = mbedtls_ssl_conf_psk(conf,
  8.                                         info->v.p.psk,
  9.                                         info->v.p.psk_len,
  10.                                         info->v.p.psk_identity,
  11.                                         strlen((const char *)info->v.p.psk_identity))) != 0)
  12.         {
  13.             MBEDTLS_LOG("mbedtls_ssl_conf_psk failed: -0x%x", -ret);
  14.             goto exit_fail;
  15.         }
  16. */
  17.     }
  18.     ...
  19. }

  20. int dtls_shakehand(mbedtls_ssl_context *ssl, const dtls_shakehand_info_s *info)
  21. {
  22.         ...
  23.         if (MBEDTLS_SSL_IS_CLIENT == info->client_or_server)
  24.     {
  25.         ret = mbedtls_net_connect(server_fd, info->u.c.host, info->u.c.port, info->udp_or_tcp);
  26.         if( 0 != ret)
  27.         {
  28.             ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
  29.             goto exit_fail;
  30.         }
  31.     }
  32.     else
  33.     {
  34.         //server_fd = (mbedtls_net_context*)atiny_net_bind(NULL, info->u.s.local_port, MBEDTLS_NET_PROTO_UDP);
  35.         ///< --TODO ,not implement yet
  36.     }
  37.         ...
  38. }

  39. void dtls_init(void)
  40. {
  41.     (void)mbedtls_platform_set_calloc_free(calloc, free);
  42.     (void)mbedtls_platform_set_snprintf(snprintf);
  43. //    (void)mbedtls_platform_set_printf(printf);
  44. }
c. 修改dtls下的BUILD.gn因为弱引用导致无法链接相关符号
  1. diff --git a/dev/third_party/iot_link/network/dtls/BUILD.gn b/dev/third_party/iot_link/network/dtls/BUILD.gn
  2. index 035805d7..05188295 100755
  3. --- a/dev/third_party/iot_link/network/dtls/BUILD.gn
  4. +++ b/dev/third_party/iot_link/network/dtls/BUILD.gn
  5. @@ -17,6 +17,7 @@ dtls_inc = [
  6.      "../../link_misc",
  7.      "//kernel/liteos_m/components/cmsis/2.0",
  8.      "//third_party/mbedtls/include/",
  9. +    "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/",^M
  10. ]


  11. @@ -41,7 +42,8 @@ dtls_cflags = [
  12.      "-Wno-unused-parameter",
  13. ]

  14. -static_library("dtls") {
  15. +#static_library("dtls") {^M
  16. +source_set("dtls") {^M
  17.          cflags = dtls_cflags
  18.          defines = dtls_def
  19.          sources = dtls_src
d. 修改mqtt下的BUILD.gn因为弱引用导致无法链接相关符号
  1. diff --git a/dev/third_party/iot_link/network/mqtt/BUILD.gn b/dev/third_party/iot_link/network/mqtt/BUILD.gn
  2. index 5a4a8e0d..f56f4ae6 100755
  3. --- a/dev/third_party/iot_link/network/mqtt/BUILD.gn
  4. +++ b/dev/third_party/iot_link/network/mqtt/BUILD.gn
  5. @@ -16,8 +16,10 @@ mqtt_paho_inc = [
  6.      "paho_mqtt/paho/MQTTPacket/src",
  7.      "paho_mqtt/port",
  8.      "../../inc",
  9. +    "//third_party/musl/porting/liteos_m/kernel/include/",^M
  10.      "//kernel/liteos_m/components/cmsis/2.0",
  11.      "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
  12. +    "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/",^M
  13. ]


  14. @@ -50,7 +52,8 @@ mqtt_cflags = [
  15.      "-Wno-unused-function",
  16. ]

  17. -static_library("mqtt") {
  18. +#static_library("mqtt") {^M
  19. +source_set("mqtt") {^M
  20.          cflags = mqtt_cflags
  21.          defines = mqtt_paho_defs
  22.          sources = mqtt_paho_src<span id="LC26" class="line"></span>
e. 修改时间编译问题:
  1. iff --git a/dev/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h b/dev/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h
  2. index 2c6cab1b..38e3dce3 100755
  3. --- a/dev/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h
  4. +++ b/dev/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h
  5. @@ -84,6 +84,7 @@

  6. #include "ohos_init.h"
  7. #include "cmsis_os2.h"
  8. +#include "sys/time.h"
  9. #include  <mqtt_al.h>

  10. #define MQTT_TASK 1
  11. @@ -117,6 +118,13 @@ typedef struct Thread
  12. int ThreadStart(Thread*, void (*fn)(void*), void* arg);


  13. +#define timeradd(s,t,a) (void) ( (a)->tv_sec = (s)->tv_sec + (t)->tv_sec, ^M
  14. +        ((a)->tv_usec = (s)->tv_usec + (t)->tv_usec) >= 1000000 && ^M
  15. +        ((a)->tv_usec -= 1000000, (a)->tv_sec++) )^M
  16. +#define timersub(s,t,a) (void) ( (a)->tv_sec = (s)->tv_sec - (t)->tv_sec, ^M
  17. +        ((a)->tv_usec = (s)->tv_usec - (t)->tv_usec) < 0 && ^M
  18. +        ((a)->tv_usec += 1000000, (a)->tv_sec--) )^M
  19. +^M
  20. typedef struct Network
  21. {
  22.      void *ctx;                      ///< if it is tls, then it is tls context, else it is socket fd<span id="LC27" class="line"></span>
7.配置相关的修改
a. gpio hdf框架相关修改。
根据下方代码修改drivers/adapter/platform/gpio/gpio_bes.c
  1. diff --git a/platform/gpio/gpio_bes.c b/platform/gpio/gpio_bes.c
  2. index ed4d18b..890d528 100755
  3. --- a/platform/gpio/gpio_bes.c
  4. +++ b/platform/gpio/gpio_bes.c
  5. @@ -257,7 +257,7 @@ static int32_t GpioDriverBind(struct HdfDeviceObject *device)
  6.      }

  7.      gpioCntlr.device.hdfDev = device;
  8. -    device->service = gpioCntlr.device.service;
  9. +    device->service = &(gpioCntlr.device);

  10.      return HDF_SUCCESS;
  11. }
b . uart相关配置,本实例使用uart1接口,相对应的GPIO为gpio20,gpio21。电机控制使用的gpio11,相关的配置如下所示,文件路径:/home/water/OpenHarmony_3.1_Beta/device/board/fnlink/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
  1. diff --git a/shields/v200zr-evb-t1/v200zr-evb-t1.hcs b/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
  2. index 44212eb..f8d0985 100644
  3. --- a/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
  4. +++ b/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
  5. @@ -1,6 +1,6 @@
  6. #include "../../hcs/v200zr.hcs"
  7. root {
  8. -    /*device_info {
  9. +    device_info {
  10.          platform :: host {
  11.              device_uart :: device {
  12.                  uart2 :: deviceNode {
  13. @@ -8,21 +8,21 @@ root {
  14.                      priority = 40;                     
  15.                      permission = 0644;                  
  16.                      moduleName = "BES_UART_MODULE_HDF";   
  17. -                    serviceName = "HDF_PLATFORM_UART_2";
  18. +                    serviceName = "HDF_PLATFORM_UART_1";
  19.                      deviceMatchAttr = "uart2_config";
  20.                  }
  21.              }
  22.          }
  23. -    }*/
  24. +    }
  25.      platform {
  26.          gpio_config {
  27.              match_attr = "gpio_config";
  28. -            pin = [0, 1];
  29. +            pin = [0, 1, 2];
  30.              // touch_ztw523: TSP_RST - GPIO12, TSP_INT-GPIO27
  31.              // touch_fts: TSP_RST - GPIO05, TSP_INT-GPIO27
  32. -            realPin = [5, 27];
  33. -            config = [5, 2];
  34. -            pinNum = 2;
  35. +            realPin = [5, 27, 11];
  36. +            config = [5, 2, 2];
  37. +            pinNum = 3;
  38.          }
  39.          i2c_config {
  40.              i2c0 {
  41. @@ -52,7 +52,7 @@ root {
  42.                  mode = 0;
  43.              }
  44.          }
  45. -        /*uart_config {
  46. +        uart_config {
  47.              template uart_controller {
  48.                  match_attr = "";
  49.                  num = 0;
  50. @@ -65,9 +65,9 @@ root {
  51.              }
  52.              uart2 :: uart_controller {
  53.                  match_attr = "uart2_config";
  54. -                num = 2;
  55. +                num = 1;
  56.              }
  57. -        }*/
  58. +        }
  59.      }
  60.      display {
  61.          panel_config {
  62. (END)
c. iic相关的配置,使i2c1通道和芯片管脚gpio6,gpio7相关的配置如下:
  1.         i2c_config {
  2.             i2c1 {
  3.                 match_attr = "i2c1_config";
  4.                 port = 1;
  5.                 speed = 200000;
  6.                 // TSP_SCL/SDA - I2C1 = GPIO06/GPIO07
  7.                 sclPin = 6;
  8.                 sdaPin = 7;
  9.                 useDma = 0;
  10.                 useSync = 1;
  11.                 asMaster = 1;
  12.                 address_width = 8;
  13.                 mode = 0;
  14.             }
  15.         }
8.将JS应用合入工程。

关于JS应用开发请参考从零开始学习L0: JS开发系列。本节为可选章节,忽略本节内容不影响本项目的展示和运行。但若想要在本项目的基础上进行涉及到显示的内容修改与新规开发,则需完整的阅读本节及链接内容。
1.下载并安装DevEco Studio。
2.新建一个js工程。
工程名为:smart_humidifier/FA

3.在DevEco Studio的SDK中添加@system.communicationkit.d.ts文件。
将@system.communicationkit.d.ts文件(源文件目录:~/knowledge_demo_smart_home/dev/interface/sdk-js/api/common/@system.communicationkit.d.ts)拷贝到DevEco Studio的SDK中(目标目录:HarmonyOS Legacy SDK/js/3.0.0.0/api/common)。HarmonyOS Legacy SDK目录在DevEco Studio安装时,由用户配置,该目录位置可在设置(ctrl+alt+s)中查找。
6.png
4.编译hap包。
依次选择构建 -> Build Hap(s)/APP(s) -> Build Hap(s)进行hap包编译。
7.png
5.使用预览功能得到 js 包: entry.previewintermediates
esdebugliteassetsjsdefault
8.png
将 js 包放到文件系统里面:
default目录中除app.js.map外的的数据全部拷贝到OpenHarmonySDK中的//vendor/team_x/smart_humidifier/fs/data/data/js目录下:

需要注意:FA的bundleName必须和工程代码//vendor/team_x/smart_humidifier/demo_smart_humidifier/ability/ability_device.cpp文件里面定义的JS_BUNDLE_NAME一致。
  1. #define JS_BUNDLE_NAME "com.example.control_panel2.hmservice"
FA中的bundleName:
9.png
整合并修改完成后的代码目录结构如下图:
10.png


5、编译&烧录
编译命令:
  1. hb set  // 如果是第一次编译,Input code path 命令行中键入"./" 指定OpenHarmony工程编译根目录后 回车
如下图所示,使用键盘上下键选中智能加湿器 “smart_humidifier”,(注:工程名字根据实际要编译的工程来)如图:
11.png
  1. hb build // 如果需要全量编译,可以添加-f 选项
编译通过,生成固件成功,如图:
12.png
固件烧录步骤
1.安装CP2102驱动
2.固件编译完成以后拷贝./out/v200zr/smart_humidifier/write_flash_gui文件夹到windows下,并点击Wifi_download_main.exe
13.jpg
3.点击工具上的文件夹图标
14.jpg
4.选择List按钮
15.jpg
5.在显示出来的串口列表中选择需要烧录的串口,并点击开始按钮。
16.jpg
6.在开发板上点击reset按键,或者重新上电。
17.png
7.进入烧录状态
18.jpg
8.烧录成功
19.jpg

6、设备配网
  • 在设备上电前需准备好安装了数字管家应用的HarmonyOS手机,详情见数字管家应用开发, 并在设置中开启手机的NFC功能;
  • 写设备NFC标签,详细操作见设备NFC标签指导文档;
  • 烧录完成后,上电,将手机上半部靠近NFC标签;
  • 无需任何操作手机将自动拉起数字管家应用并进入配网界面,输入热点密码。
20.gif

更多回帖

发帖
×
20
完善资料,
赚取积分