• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HiSpark WiFi-IoT 套件样例开发--wifihotspot(AP模式)
2
3![hihope_illustration](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/hihope_illustration.png)
4
5[HiSpark WiFi-IoT开发套件](https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop) 首发于HDC 2020,是首批支持OpenHarmony 2.0的开发套件,亦是官方推荐套件,由润和软件HiHope量身打造,已在OpenHarmony社区和广大OpenHarmony开发者中得到广泛应用。
6
7![wifi_iot](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/2.png)
8
9##  一、Wifi STA API
10
11使用原始WiFI API接口进行编程,AP模式需要使用原始接口以及一些DHCP客户端接口。
12
13#### WiFi AP模式相关的API接口文件路径
14
15**foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot_config.h**
16
17**foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot.h**
18
19所使用的API接口有:
20
21| API                                                          | 接口说明                 |
22| ------------------------------------------------------------ | ------------------------ |
23| WifiErrorCode EnableHotspot(void);                           | 打开Wifi AP 模式         |
24| WifiErrorCode DisableHotspot(void);                          | 关闭Wifi AP 模式         |
25| WifiErrorCode SetHotspotConfig(const HotspotConfig* config); | 设置当前AP热点的配置参数 |
26| WifiErrorCode GetHotspotConfig(HotspotConfig* result);       | 获取当前AP热点的配置参数 |
27| int IsHotspotActive(void);                                   | 查询AP是否已经开启       |
28| WifiErrorCode GetStationList(StationInfo* result, unsigned int* size); | 获取接入的设备列表       |
29| int GetSignalLevel(int rssi, int band);                      | 获取信号强度等级         |
30| WifiErrorCode SetBand(int band);                             | 设置当前频段             |
31| WifiErrorCode GetBand(int* result);                          | 获取当前频段             |
32
33#### Hi3861 SDK的DHCP客户端接口:
34
35| API                 | 描述               |
36| ------------------- | ------------------ |
37| netifapi_netif_find | 按名称查找网络接口 |
38| netifapi_dhcp_start | 启动DHCP客户端     |
39| netifapi_dhcp_stop  | 停止DHCP客户端     |
40
41## 二、代码分析
42
43```c
44//wifi ap task
45static void WifiHotspotTask(void *arg)
46{
47    (void)arg;
48    WifiErrorCode errCode;
49    HotspotConfig config = {0};
50
51    // 配置作为AP热点的ssid和key
52    strcpy(config.ssid, "HiSpark-AP");
53    strcpy(config.preSharedKey, "12345678");
54    config.securityType = WIFI_SEC_TYPE_PSK;
55    config.band = HOTSPOT_BAND_TYPE_2G;
56    config.channelNum = 7;
57
58    osDelay(10);
59
60    printf("starting AP ...\r\n");
61    //开启热点
62    errCode = StartHotspot(&config);
63    printf("StartHotspot: %d\r\n", errCode);
64    //热点将开启1分钟
65    int timeout = 60;
66    while (timeout--) {
67        printf("After %d seconds Ap will turn off!\r\n", timeout);
68        osDelay(100);
69    }
70    // 可以通过串口工具发送 AT+PING=192.168.xxx.xxx(如手机连接到该热点后的IP) 去ping连接到该热点的设备的IP地址
71
72    printf("stop AP ...\r\n");
73    //关闭热点
74    StopHotspot();
75    printf("stop AP ...\r\n");
76}
77
78```
79
80
81
82## 三、如何编译
83
841. 将此目录下的 `wifi_hotspot_demo.c` 和 `BUILD.gn` 复制到openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下,
852. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
86
87```
88    features = [
89        "iothardware:wifi_demo",
90    ]
91```
92
933. 在openharmony源码顶层目录执行:`python build.py wifiiot`
94
95## 四、运行结果
96
97```
98ready to OS start
99sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
100FileSystem mount ok.
101wifi init success!
102
10300 00:00:00 0 132 D 0/HIVIEW: hilog init success.
10400 00:00:00 0 132 D 0/HIVIEW: log limit init success.
10500 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
10600 00:00:00 0 132 I 1/SAMGR: Init service:0x4af278 TaskPool:0xe4b38
10700 00:00:00 0 132 I 1/SAMGR: Init service:0x4af284 TaskPool:0xe4b58
10800 00:00:00 0 132 I 1/SAMGR: Init service:0x4af3ac TaskPool:0xe4b78
10900 00:00:00 0 164 I 1/SAMGR: Init service 0x4af284 <time: 0ms> success!
11000 00:00:00 0 64 I 1/SAMGR: Init service 0x4af278 <time: 0ms> success!
11100 00:00:00 0 8 D 0/HIVIEW: hiview init success.
11200 00:00:00 0 8 I 1/SAMGR: Init service 0x4af3ac <time: 0ms> success!
11300 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
11400 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
11500 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
11600 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
117starting AP ...
118RegisterWifiEvent: 0
119SetHotspotConfig: 0
120OnHotspotStateChanged: 1.
121EnableHotspot: 0
122g_hotspotStarted = 1.
123netifapi_netif_set_addr: 0
124netifapi_dhcp_start: 0
125StartHotspot: 0
126After 59 seconds Ap will turn off!
127After 58 seconds Ap will turn off!
128After 57 seconds Ap will turn off!
129+NOTICE:STA CONNECTED
130 PrintStationInfo: mac=54:19:C8:D9:A7:35, reason=0.
131+OnHotspotStaJoin: active stations = 1.
132After 56 seconds Ap will turn off!
133After 55 seconds Ap will turn off!
134
135AT+PING=192.168.1.2
136
137+PING:
138[0]Reply from 192.168.1.2:time=90ms TTL=64
139After 49 seconds Ap will turn off!
140[1]Reply from 192.168.1.2:time=25ms TTL=64
141After 48 seconds Ap will turn off!
142[2]Reply from 192.168.1.2:time=28ms TTL=64
143After 47 seconds Ap will turn off!
144[3]Reply from 192.168.1.2:time=4ms TTL=64
1454 packets transmitted, 4 received, 0 loss, rtt min/avg/max = 4/36/90 ms
146
147OK
148...
149...
150...
151After 3 seconds Ap will turn off!
152After 2 seconds Ap will turn off!
153After 1 seconds Ap will turn off!
154After 0 seconds Ap will turn off!
155stop AP ...
156netifapi_dhcps_stop: 0
157UnRegisterWifiEvent: 0
158+NOTICE:STA DISCONNECTED
159EnableHotspot: 0
160stop AP ...
161
162
163```
164
165
166
167
168
169### 【套件支持】
170
171##### 1. 套件介绍  http://www.hihope.org/pro/pro1.aspx?mtt=8
172
173##### 2. 套件购买  https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop
174
175##### 3. 技术资料
176
177- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg**
178
179- HiHope官网-资源中心(SDK包、技术文档下载)**http://www.hihope.org/**
180
181##### 4. 互动交流
182
183- 润和HiHope技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方群#,予以邀请入群)
184- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429**
185- 润和HiHope售后服务群(QQ:980599547)
186- 售后服务电话(025-52668590)
187