电子说
智能门铃通过监控来访者信息,告诉主人门外是否有人按铃、有陌生人靠近或者无人状态。主人可以在数字管家中远程接收消息,并根据需要进行远程取消报警和一键开锁。同时,也可以通过室内屏幕获取门外状态。室内屏幕显示界面使用DevEco Studio 编写的js应用,具有很好的兼容和移植特性。硬件上采用了带有HDF框架的驱动模型,通过GPIO来获取传感器采集信息并驱动报警器进行动作。
如上图所示,智能门铃整体方案原理图可以大致分成:智能门铃设备、数字管家应用、云平台三部分。智能门铃通过MQTT协议连接华为IOT物联网平台,从而实现命令的接收和属性上报。 。
gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]开发基础环境由 windows 工作台和 Linux 编译服务器组成。windows 工作台可以通过 samba 服务或 ssh 方式访问 Linux编译服务器。其中 windows 工作台用来烧录和代码编辑,Linux 编译服务器用来编译 OpenHarmony 代码,为了简化步骤,Linux 编译服务器推荐安装 Ubuntu20.04。
sudo apt-get install -y build-essential gcc g++ make zlib* libffi-dev git git-lfs
python3 --version
sudo apt-get install python3-setuptools python3-pip -y
sudo pip3 install --upgrade pip
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
hb -v
a. 若提示如下内容,则表示未安装可以从第2步开始操作。
bash: /home/***/.local/bin/hb: No such file or directory
b.若提示如下内容,需要先卸载该版本,然后再执行第2步操作步骤。
[OHOS INFO] hb version 0.4.3
卸载命令:
pip3 uninstall ohos-build
pip3 install build/lite // 该命令需在OpenHarmony源码根目录下执行
vim ~/.bashrc
将以下命令拷贝到.bashrc文件的最后一行,保存并退出。
export PATH=~/.local/bin:$PATH
执行如下命令更新环境变量。
source ~/.bashrc
[OHOS INFO] hb version 0.4.4
在Linux编译服务器上搭建好基础开发环境后,需要安装OpenHarmony 编译V200Z-R平台特有的开发环境。
解压 [gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2]安装包至~/toolchain/路径下。
mkdir -p ~/toolchain/
tar -jxvf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2 -C ~/toolchain/
设置环境变量。
vim ~/.bashrc
将以下命令拷贝到.bashrc文件的最后一行,保存并退出。
export PATH=~/toolchain/gcc-arm-none-eabi-10.3-2021.10/bin:$PATH
生效环境变量。
source ~/.bashrc
arm-none-eabi-gcc -v
本用例采用repo的方式从码云官仓下载系统系统源码以及开发板适配代码,使用git从gitee的sig仓库拉取设备应用代码。
sudo apt install git
sudo apt install git-lfs
ssh-keygen -t ed25519 -C "xxxxx@xxxxx.com"
cat ~/.ssh/id_ed25519.pub
git config --global user.name "yourname"
git config --global user.email "your-email-address"
git config --global credential.helper store
sudo curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > /usr/local/bin/repo
chmod a+x /usr/local/bin/repo
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
#特别注意:请下载OpenHarmony 3.1 Beta版本
mkdir ~/OpenHarmony_3.1_Beta
cd ~/OpenHarmony_3.1_Beta
repo init -u git@gitee.com:openharmony/manifest.git -b refs/tags/OpenHarmony-v3.1-Beta --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
注意:
/usr/bin/env: ‘python’: No such file or directory
执行如下命令后,进行重试:
sudo ln -s /usr/bin/python3.8 /usr/bin/python
使用git 命令下载
git clone git@gitee.com:openharmony-sig/knowledge_demo_smart_home.git --depth=1
team_x源码拷贝
mkdir ~/OpenHarmony_3.1_Beta/vendor/team_x
cp -rfa ~/knowledge_demo_smart_home/dev/team_x/smart_door_bell ~/OpenHarmony_3.1_Beta/vendor/team_x
common库拷贝
cp -rfa ~/knowledge_demo_smart_home/dev/team_x/common ~/OpenHarmony_3.1_Beta/vendor/team_x
iot_link库拷贝
cp -rfa ~/knowledge_demo_smart_home/dev/third_party/iot_link ~/OpenHarmony_3.1_Beta/third_party/
步骤1、下载patch
点击上述链接进入浏览器,将该网页中内容全部复制。
本地创建一个名为bes_kernle.patch文件,并将已经复制的内容粘贴到该文件中。
步骤2、 打上步骤1中的patch
cd ~/OpenHarmony_3.1_Beta/kernel/liteos_m
patch -p1 < bes_kernle.patch
步骤1、下载patch
点击上述链接进入浏览器,将该网页中内容全部复制。
本地创建一个名为bes_device.patch文件,并将已经复制的内容粘贴到该文件中。
步骤2、 打上步骤1中的patch
cd ~/OpenHarmony_3.1_Beta/device/soc/bestechnic
patch -p1 < bes_device.patch
参考如下代码段修改 mbedtls/library/platform.c
diff --git a/library/platform.c b/library/platform.c
index c4c3fd3..214173b 100755
--- a/library/platform.c
+++ b/library/platform.c
@@ -86,9 +86,24 @@ static void platform_free_uninit( void *ptr )
static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
+#include "los_memory.h"
+
void * mbedtls_calloc( size_t nmemb, size_t size )
{
- return (*mbedtls_calloc_func)( nmemb, size );
+ //return (*mbedtls_calloc_func)( nmemb, size );
+ size_t real_size;
+ void *ptr = NULL;
+
+ if (nmemb == 0 || size == 0) {
+ return NULL;
+ }
+
+ real_size = (size_t)(nmemb * size);
+ ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size);
+ if (ptr != NULL) {
+ (void)memset_s(ptr, real_size, 0, real_size);
+ }
+ return ptr;
}
参考如下代码段修改 mbedtls/BUILD.gn
diff --git a/BUILD.gn b/BUILD.gn
index 9ecb37a..30dbb2e 100755
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -124,6 +124,7 @@ if (ohos_kernel_type == "liteos_m") {
include_dirs = [
"//kernel/liteos_m/kal/posix/include",
"//kernel/liteos_m/kernel/include",
"//kernel/liteos_m/utils",
"//third_party/musl/porting/liteos_m/kernel/include/",
+ "//kernel/liteos_m/kernel/include/",
]
}
output_name = "mbedtls"
修改src/api/netdb.c 文件
diff --git a/src/api/netdb.c b/src/api/netdb.c
index 52a6fdf..2043636 100644
--- a/src/api/netdb.c
+++ b/src/api/netdb.c
@@ -100,7 +100,7 @@ lwip_gethostbyname(const char *name)
err = netconn_gethostbyname(name, &addr);
if (err != ERR_OK) {
LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%dn", name, err));
- h_errno = HOST_NOT_FOUND;
+ //h_errno = HOST_NOT_FOUND;
return NULL;
}
参考如下代码段修改 third_party/iot_link/network/dtls/BUILD.gn
diff --git a/third_party/iot_link/network/dtls/BUILD.gn b/third_party/iot_link/network/dtls/BUILD.gn
index 035805d709075ef2bc9d9388e73edb84dc1ee943..ac7256cf2bc93a0a998388f31910f152bf306fa9 100755
--- a/third_party/iot_link/network/dtls/BUILD.gn
+++ b/third_party/iot_link/network/dtls/BUILD.gn
@@ -41,7 +41,8 @@ dtls_cflags = [
"-Wno-unused-parameter",
]
-static_library("dtls") {
+#static_library("dtls") {
+source_set("dtls") {
cflags = dtls_cflags
defines = dtls_def
sources = dtls_src
参考如下代码段修改 third_party/iot_link/network/dtls/mbedtls/mbedtls_port/dtls_interface.c
diff --git a/third_party/iot_link/network/dtls/mbedtls/mbedtls_port/dtls_interface.c b/third_party/iot_link/network/dtls/mbedtls/mbedtls_port/dtls_interface.c
index 9f87c6fae041b17af4522d5d89114b69005a00be..73e406893a33385a9888a82cf6d1b3706a81387d 100755
--- a/third_party/iot_link/network/dtls/mbedtls/mbedtls_port/dtls_interface.c
+++ b/third_party/iot_link/network/dtls/mbedtls/mbedtls_port/dtls_interface.c
@@ -56,6 +56,7 @@
#include "dtls_interface.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/ssl_internal.h"
+#include "mbedtls/platform.h"
#include "cmsis_os2.h"
#include "string.h"
@@ -157,6 +158,7 @@ mbedtls_ssl_context *dtls_ssl_new(dtls_establish_info_s *info, char plat_type)
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
if (info- >psk_or_cert == VERIFY_WITH_PSK)
{
+ #if 0
if ((ret = mbedtls_ssl_conf_psk(conf,
info- >v.p.psk,
info- >v.p.psk_len,
@@ -166,6 +168,7 @@ mbedtls_ssl_context *dtls_ssl_new(dtls_establish_info_s *info, char plat_type)
MBEDTLS_LOG("mbedtls_ssl_conf_psk failed: -0x%x", -ret);
goto exit_fail;
}
+ #endif
}
#endif
@@ -548,5 +551,5 @@ void dtls_init(void)
{
(void)mbedtls_platform_set_calloc_free(calloc, free);
(void)mbedtls_platform_set_snprintf(snprintf);
- (void)mbedtls_platform_set_printf(printf);
+ //(void)mbedtls_platform_set_printf(printf);
}
参考如下代码段修改 third_party/iot_link/network/mqtt/BUILD.gn
diff --git a/third_party/iot_link/network/mqtt/BUILD.gn b/third_party/iot_link/network/mqtt/BUILD.gn
index 5a4a8e0ded129b91ce550a5d16f04e0784fddd45..d6bc39b8ede5735d79c74a6ab562c6141bd0b377 100755
--- a/third_party/iot_link/network/mqtt/BUILD.gn
+++ b/third_party/iot_link/network/mqtt/BUILD.gn
@@ -16,6 +16,7 @@ mqtt_paho_inc = [
"paho_mqtt/paho/MQTTPacket/src",
"paho_mqtt/port",
"../../inc",
+ "//third_party/musl/porting/liteos_m/kernel/include/",
"//kernel/liteos_m/components/cmsis/2.0",
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
]
@@ -50,7 +51,8 @@ mqtt_cflags = [
"-Wno-unused-function",
]
-static_library("mqtt") {
+#static_library("mqtt") {
+source_set("mqtt") {
cflags = mqtt_cflags
defines = mqtt_paho_defs
sources = mqtt_paho_src
参考如下代码段修改 third_party/iot_link/network/mqtt/paho_mqtt/port/paho_mqtt_port.c
diff --git a/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_mqtt_port.c b/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_mqtt_port.c
index a213b74c7286c68061e3285c0c3ac6c440949f26..d0900c8d88149f08c93fe885f34f4d2affa30885 100755
--- a/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_mqtt_port.c
+++ b/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_mqtt_port.c
@@ -180,6 +180,50 @@ static int __socket_read(void *ctx, unsigned char *buf, int len, int timeout)
int fd;
int ret = 0;
+#if 1
+ int bytes = 0;
+ fd_set fdset;
+
+ struct timeval timedelay = {timeout / 1000, (timeout % 1000) * 1000};
+ if(NULL== buf)
+ {
+ return ret;
+ }
+
+ fd = (int)(intptr_t)ctx; ///< socket could be zero
+
+ if (timedelay.tv_sec < 0 || (timedelay.tv_sec == 0 && timedelay.tv_usec <= 0))
+ {
+ timedelay.tv_sec = 0;
+ timedelay.tv_usec = 100;
+ }
+ timedelay.tv_sec = 5;
+ FD_ZERO(&fdset);
+ FD_SET(fd, &fdset);
+
+ ret = select(fd + 1, &fdset, NULL, NULL, &timedelay);
+ printf("[%s|%s|%d]fd = %d, ret = %dn", __FILE__,__func__,__LINE__, fd, ret);
+ if (ret > 0) {
+ while (bytes < len) {
+ int rc = recv(fd, &buf[bytes], (size_t)(len - bytes), 0);
+ printf("[%s|%s|%d]fd = %d, rc = %dn", __FILE__,__func__,__LINE__, fd, rc);
+ if (rc == -1) {
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ bytes = -1;
+ }
+ break;
+ } else if (rc == 0) {
+ bytes = 0;
+ break;
+ } else {
+ bytes += rc;
+ }
+ }
+ }
+
+ return bytes;
+#else
+
struct timeval timedelay = {timeout / 1000, (timeout % 1000) * 1000};
if(NULL== buf)
{
@@ -216,7 +260,7 @@ static int __socket_read(void *ctx, unsigned char *buf, int len, int timeout)
}
}
return bytes;
-
+ #endif
}
@@ -586,7 +630,7 @@ static void * __connect(mqtt_al_conpara_t *conparam)
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
- attr.stack_size = 1024 * 2;
+ attr.stack_size = 1024 * 4;
attr.priority = 28;
cb- >task = osThreadNew((osThreadFunc_t)__loop_entry, cb, &attr);
if(NULL == cb- >task)
参考如下代码段修改 third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h
diff --git a/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h b/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h
index 2c6cab1b90bef7be82891637c200cdb664e9735b..38e3dce3867d2b2d538858c72b36bee7330c7b4f 100755
--- a/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h
+++ b/third_party/iot_link/network/mqtt/paho_mqtt/port/paho_osdepends.h
@@ -84,6 +84,7 @@
#include "ohos_init.h"
#include "cmsis_os2.h"
+#include "sys/time.h"
#include < mqtt_al.h >
#define MQTT_TASK 1
@@ -117,6 +118,13 @@ typedef struct Thread
int ThreadStart(Thread*, void (*fn)(void*), void* arg);
+#define timeradd(s,t,a) (void) ( (a)- >tv_sec = (s)- >tv_sec + (t)- >tv_sec,
+ ((a)- >tv_usec = (s)- >tv_usec + (t)- >tv_usec) >= 1000000 &&
+ ((a)- >tv_usec -= 1000000, (a)- >tv_sec++) )
+#define timersub(s,t,a) (void) ( (a)- >tv_sec = (s)- >tv_sec - (t)- >tv_sec,
+ ((a)- >tv_usec = (s)- >tv_usec - (t)- >tv_usec) < 0 &&
+ ((a)- >tv_usec += 1000000, (a)- >tv_sec--) )
+
typedef struct Network
{
void *ctx; ///< if it is tls, then it is tls context, else it is socket fd
参考如下代码段修改 third_party/iot_link/oc_mqtt/BUILD.gn
diff --git a/third_party/iot_link/oc_mqtt/BUILD.gn b/third_party/iot_link/oc_mqtt/BUILD.gn
index c777c5e4f2e79646a5c76471f22c5437e9d4b3ee..1e3e07ddb6c0bc52465a60c415c8550608a21e55 100755
--- a/third_party/iot_link/oc_mqtt/BUILD.gn
+++ b/third_party/iot_link/oc_mqtt/BUILD.gn
@@ -11,7 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-static_library("oc_mqtt") {
+#static_library("oc_mqtt") {
+source_set("oc_mqtt") {
sources = [
"oc_mqtt_al/oc_mqtt_al.c",
"oc_mqtt_profile_v5/oc_mqtt_profile.c",
参考下方代码修改foundation/ace/ace_engine_lite/frameworks/module_manager/ohos_module_config.h
diff --git a/frameworks/module_manager/ohos_module_config.h b/frameworks/module_manager/ohos_module_config.h
index f8eb744..3bb25d1 100644
--- a/frameworks/module_manager/ohos_module_config.h
+++ b/frameworks/module_manager/ohos_module_config.h
@@ -47,6 +47,10 @@ extern void InitNativeApiFs(JSIValue exports);
extern void InitNativeApiKv(JSIValue exports);
#endif
+#if (FEATURE_MODULE_COMMUNICATIONKIT == 1)
+extern void InitNativeApiCommunicationKit(JSIValue exports);
+#endif
+
#if (FEATURE_MODULE_DEVICE == 1)
extern void InitDeviceModule(JSIValue exports);
#endif
@@ -98,11 +102,13 @@ const Module OHOS_MODULES[] = {
#if (FEATURE_MODULE_DIALOG == 1)
{"prompt", InitDialogModule},
#endif // FEATURE_MODULE_DIALOG
-
#if (FEATURE_MODULE_STORAGE == 1)
{"file", InitNativeApiFs},
{"storage", InitNativeApiKv},
#endif
+#if (FEATURE_MODULE_COMMUNICATIONKIT == 1)
+ {"CommunicationKit", InitNativeApiCommunicationKit},
+#endif
#if (FEATURE_MODULE_DEVICE == 1)
{"device", InitDeviceModule},
#endif
根据开发板的硬件版本不同,还需更改配置文件中flash_size的值。具体路径为/device/board/fnlink/v200zr/liteos_m/config.gni。其中v1.0版本对应16,2.0版本对应32,硬件版本打印在了开发板正面。
diff --git a/v200zr/liteos_m/config.gni b/v200zr/liteos_m/config.gni
index b288101..6489dd2 100644
--- a/v200zr/liteos_m/config.gni
+++ b/v200zr/liteos_m/config.gni
@@ -47,7 +47,7 @@ if (product_path != "") {
bsp_bin_list = product_conf.bsp_bin_list
pack_burn = product_conf.pack_burn
}
-flash_size = 16
+flash_size = 32
参考下方代码修改device/board/fnlink/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
diff --git a/shields/v200zr-evb-t1/v200zr-evb-t1.hcs b/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
index 44212eb..4fc99da 100644
--- a/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
+++ b/shields/v200zr-evb-t1/v200zr-evb-t1.hcs
@@ -17,12 +17,17 @@ root {
platform {
gpio_config {
match_attr = "gpio_config";
- pin = [0, 1];
+ pin = [0, 1, 2, 3, 4, 5, 6, 7];
// touch_ztw523: TSP_RST - GPIO12, TSP_INT-GPIO27
// touch_fts: TSP_RST - GPIO05, TSP_INT-GPIO27
- realPin = [5, 27];
- config = [5, 2];
- pinNum = 2;
+ realPin = [5, 27, 1, 11, 15, 10, 14, 13];
+ config = [5, 2, 2, 4, 5, 5, 5, 2];
+ pinNum = 8;
+ }
+ pwm0_config{
+ match_attr = "pwm0_config";
+ pwmId = 0;
+ pwmPin = 20;
}
i2c_config {
i2c0 {
根据下方代码修改drivers/adapter/platform/gpio/gpio_bes.c
diff --git a/platform/gpio/gpio_bes.c b/platform/gpio/gpio_bes.c
index ed4d18b..890d528 100755
--- a/platform/gpio/gpio_bes.c
+++ b/platform/gpio/gpio_bes.c
@@ -257,7 +257,7 @@ static int32_t GpioDriverBind(struct HdfDeviceObject *device)
}
gpioCntlr.device.hdfDev = device;
- device- >service = gpioCntlr.device.service;
+ device- >service = &(gpioCntlr.device);
return HDF_SUCCESS;
}
参考下方代码修改driversadapterkhdfliteos_mplatformBUILD.gn
diff --git a/khdf/liteos_m/platform/BUILD.gn b/khdf/liteos_m/platform/BUILD.gn
index 84c9152..8a4016a 100755
--- a/khdf/liteos_m/platform/BUILD.gn
+++ b/khdf/liteos_m/platform/BUILD.gn
@@ -63,7 +63,10 @@ hdf_driver("hdf_platform_lite") {
}
if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_PWM)) {
- sources += [ "$HDF_FRAMEWORKS_PATH/support/platform/src/pwm/pwm_core.c" ]
+ sources += [
+ "$HDF_FRAMEWORKS_PATH/support/platform/src/pwm/pwm_core.c",
+ "$HDF_FRAMEWORKS_PATH/support/platform/src/pwm/pwm_if.c",
+ ]
}
if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_RTC)) {
关于JS应用开发请参考[从零开始学习L0: JS开发系列]。本节为可选章节,忽略本节内容不影响智能门铃项目的展示和运行。但若想要在本项目的基础上进行涉及到显示的内容修改与新规开发,例如增加天气预报的图标,则需完整的阅读本节及链接内容。
工程目录为:knowledge_demo_smart_home/dev/team_x/smart_door_bell/FA
将@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)中查找。
依次选择构建 -> Build Hap(s)/APP(s) -> Build Hap(s)进行hap包编译,编译后的hap包目录为:FAentrybuildoutputshapdebugentry-debug-lite-unsigned.hap。
将entry-debug-lite-unsigned.hap修改后缀为zip并解压。在解压后的目录:entry-debug-lite-unsigned/assets/js/default目录中除app.js.map外的的数据全部拷贝到OpenHarmonySDK中的vendor/team_x/smart_door_bell/fs/data/data/js目录下。
整合并修改完成后的代码目录结构如下图:
编译命令:
hb set // 如果是第一次编译,Input code path 命令行中键入"./" 指定OpenHarmony工程编译根目录后回车。
如下图所示,使用键盘上下键选中智能门铃 “smart_door_bell”(注:工程名字根据实际要编译的工程来):
hb build // 如果需要全量编译,可以添加-f 选项
编译通过,生成固件成功,如图:
注意:固件烧录完成后需将烧录窗口关闭,再按下设备Reset键设备才能启动。如果未将烧录窗口关闭,设备会再次进行烧写状态。
####操作体验
![](//file1.elecfans.com/web2/M00/CF/C7/wKgZomYiHdKAGL0OAA_n0GFunYg951.jpg)
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !