README.md
1# Coap协议的通信实验<a name="ZH-CN_TOPIC_0000001130176841"></a>
2## 硬件环境搭建
3- 硬件要求:Hi3861V100核心板、扩展板;硬件搭建及组网图如下图所示。
4- [Hi3861V100核心板参考:HiSpark_WiFi_IoT智能开发套件_原理图硬件资料\原理图\HiSpark_WiFi-IoT_Hi3861_CH340G_VER.B.pdf](http://gitee.com/hihope_iot/embedded-race-hisilicon-track-2022/blob/master/%E7%A1%AC%E4%BB%B6%E8%B5%84%E6%96%99/HiSpark_WiFi_IoT%E6%99%BA%E8%83%BD%E5%AE%B6%E5%B1%85%E5%BC%80%E5%8F%91%E5%A5%97%E4%BB%B6_%E5%8E%9F%E7%90%86%E5%9B%BE.rar)
5- [扩展板参考:HiSpark_WiFi_IoT智能开发套件_原理图硬件资料\原理图\HiSpark_WiFi-IoT_Hi3861_CH340G_VER.B.pdf](http://gitee.com/hihope_iot/embedded-race-hisilicon-track-2022/blob/master/%E7%A1%AC%E4%BB%B6%E8%B5%84%E6%96%99/HiSpark_WiFi_IoT%E6%99%BA%E8%83%BD%E5%AE%B6%E5%B1%85%E5%BC%80%E5%8F%91%E5%A5%97%E4%BB%B6_%E5%8E%9F%E7%90%86%E5%9B%BE.rar)
6
7![输入图片说明](../doc/figures/coap_demo/002coap.png)
8
9## 软件介绍
10
11- 1.代码目录结构及相应接口功能介绍
12```
13vendor_hisilicon/hispark_pegasus/demo/coap_demo
14├── app_demo_iot.c #
15├── BUILD.gn # BUILD.gn文件由三部分内容(目标、源文件、头文件路径)构成,开发者根据需要填写,static_library中指定业务模块的编译结果,为静态库文件led_example,开发者根据实际情况完成填写。
16| sources中指定静态库.a所依赖的.c文件及其路径,若路径中包含"//"则表示绝对路径(此处为代码根路径),若不包含"//"则表示相对路径。include_dirs中指定source所需要依赖的.h文件路径。
17├── cjson_init.c #
18├── coap_client.c #
19├── coap_service.c #
20├── iot_config.h #
21├── iot_hmac.c #
22├── iot_hmac.h #
23├── iot_log.c #
24├── iot_log.h #
25├── iot_main.c #
26├── iot_main.h #
27├── iot_profile.c #
28├── iot_profile.h #
29└── iot_sta.c #
30```
31- 2.工程编译
32 - 将源码./vendor/hisilicon/hispark_pegasus/demo/coap_demo整个文件夹及内容复制到源码./applications/sample/wifi-iot/app/下。
33 ```
34 .
35 └── applications
36 └── sample
37 └── wifi-iot
38 └── app
39 └──coap_demo
40 └── 代码
41 ```
42 - 修改./applications/sample/wifi-iot/app/coapdemo/iot_config.h中CONFIG_AP_SSID,CONFIG_AP_PWD为WiFi名称和WiFi密码。
43 ```
44 #define CONFIG_AP_SSID "xxx" // WIFI SSID
45 #define CONFIG_AP_PWD "xxxxxx" // WIFI PWD
46 ```
47 - 如果编译coap_service服务端,修改./applications/sample/wifi-iot/app/coapdemo/BUILD.gn文件中,在sources = [ "coap_service.c" ]字段中添加。
48 ```
49 static_library("appDemoIot") {
50 sources = [
51 "app_demo_iot.c",
52 "cjson_init.c",
53 "coap_service.c",
54 "iot_hmac.c",
55 "iot_log.c",
56 "iot_main.c",
57 "iot_profile.c",
58 "iot_sta.c",
59 #"coap_client.c",
60 ]
61
62 include_dirs = [
63 "./",
64 "//utils/native/lite/include",
65 "//kernel/liteos_m/kal/cmsis",
66 "//base/iothardware/peripheral/interfaces/inner_api",
67 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/lwip_sack/include/lwip",
68 "//third_party/cJSON",
69 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/mbedtls/include/mbedtls",
70 "//foundation/communication/wifi_lite/interfaces/wifiservice",
71 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/paho.mqtt.c/include/mqtt",
72 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/libcoap/include/coap2",
73 ]
74 defines = [ "WITH_LWIP" ]
75 }
76 ```
77
78 - 修改源码./applications/sample/wifi-iot/app下的BUILD.gn文件,在features字段中增加索引,使目标模块参与编译。features字段指定业务模块的路径和目标,features字段配置如下。
79 ```
80 import("//build/lite/config/component/lite_component.gni")
81
82 lite_component("app") {
83 features = [
84 "coap_demo:appDemoIot",
85 ]
86 }
87 ```
88
89 - 修改完成后编译rebuild,烧录到Hi3861V100开发板上,烧录成功后,再次点击Hi3861核心板上的“RST”复位键,在串口工具栏可以看到server服务端IP地址。
90
91 ![输入图片说明](../doc/figures/coap_demo/002coap.png)
92
93 - 配置./applications/sample/wifi-iot/app/coap_demo/iot_config.h中字段PARAM_SERVER_ADDR里面主机IP地址。
94 ```
95 #define CONFIG_AP_SSID "xxx" // WIFI SSID
96 #define CONFIG_AP_PWD "xxxxxx" // WIFI PWD
97 #define PARAM_SERVER_ADDR "xxxxxxxx"
98 ```
99 - 如果编译coap_client客户端,修改./applications/sample/wifi-iot/app/lwip_demo/BUILD.gn文件中,在sources = [ "coap_client.c" ]字段中添加。
100 ```
101 static_library("appDemoIot") {
102 sources = [
103 "app_demo_iot.c",
104 "cjson_init.c",
105 #"coap_service.c",
106 "iot_hmac.c",
107 "iot_log.c",
108 "iot_main.c",
109 "iot_profile.c",
110 "iot_sta.c",
111 "coap_client.c",
112 ]
113
114 include_dirs = [
115 "./",
116 "//utils/native/lite/include",
117 "//kernel/liteos_m/kal/cmsis",
118 "//base/iothardware/peripheral/interfaces/inner_api",
119 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/lwip_sack/include/lwip",
120 "//third_party/cJSON",
121 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/mbedtls/include/mbedtls",
122 "//foundation/communication/wifi_lite/interfaces/wifiservice",
123 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/paho.mqtt.c/include/mqtt",
124 "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/libcoap/include/coap2",
125 ]
126 defines = [ "WITH_LWIP" ]
127 }
128 ```
129
130 - 修改源码./applications/sample/wifi-iot/app下的BUILD.gn文件,在features字段中增加索引,使目标模块参与编译。features字段指定业务模块的路径和目标,features字段配置如下。
131 ```
132 import("//build/lite/config/component/lite_component.gni")
133
134 lite_component("app") {
135 features = [
136 "coap_demo:appDemoIot",
137 ]
138 }
139 ```
140
141 - 工程相关配置完成后,然后rebuild编译。
142- 3.烧录
143 - 编译成功后,点击DevEco Home->配置工程->hi3861->upload_port->选择对应串口端口号->选择upload_protocol->选择hiburn-serial->点击save保存,在保存成功后点击upload进行烧录,出现connecting, please reset device..字样复位开发板等待烧录完成。
144 - 烧录成功后,再次点击Hi3861核心板上的“RST”复位键,此时开发板的系统会运行起来。运行结果:服务端设备在串口工具显示Hello coap,客户端在串口工具显示scheduling for xxxx ticks。