10a7ce71fSopenharmony_ci# HiSpark WiFi-IoT 套件样例开发--wifihotspot(AP模式)
20a7ce71fSopenharmony_ci
30a7ce71fSopenharmony_ci![hihope_illustration](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/hihope_illustration.png)
40a7ce71fSopenharmony_ci
50a7ce71fSopenharmony_ci[HiSpark WiFi-IoT开发套件](https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop) 首发于HDC 2020,是首批支持OpenHarmony 2.0的开发套件,亦是官方推荐套件,由润和软件HiHope量身打造,已在OpenHarmony社区和广大OpenHarmony开发者中得到广泛应用。 
60a7ce71fSopenharmony_ci
70a7ce71fSopenharmony_ci![wifi_iot](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/2.png)
80a7ce71fSopenharmony_ci
90a7ce71fSopenharmony_ci##  一、Wifi STA API
100a7ce71fSopenharmony_ci
110a7ce71fSopenharmony_ci使用原始WiFI API接口进行编程,AP模式需要使用原始接口以及一些DHCP客户端接口。
120a7ce71fSopenharmony_ci
130a7ce71fSopenharmony_ci#### WiFi AP模式相关的API接口文件路径
140a7ce71fSopenharmony_ci
150a7ce71fSopenharmony_ci**foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot_config.h**
160a7ce71fSopenharmony_ci
170a7ce71fSopenharmony_ci**foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot.h**
180a7ce71fSopenharmony_ci
190a7ce71fSopenharmony_ci所使用的API接口有:
200a7ce71fSopenharmony_ci
210a7ce71fSopenharmony_ci| API                                                          | 接口说明                 |
220a7ce71fSopenharmony_ci| ------------------------------------------------------------ | ------------------------ |
230a7ce71fSopenharmony_ci| WifiErrorCode EnableHotspot(void);                           | 打开Wifi AP 模式         |
240a7ce71fSopenharmony_ci| WifiErrorCode DisableHotspot(void);                          | 关闭Wifi AP 模式         |
250a7ce71fSopenharmony_ci| WifiErrorCode SetHotspotConfig(const HotspotConfig* config); | 设置当前AP热点的配置参数 |
260a7ce71fSopenharmony_ci| WifiErrorCode GetHotspotConfig(HotspotConfig* result);       | 获取当前AP热点的配置参数 |
270a7ce71fSopenharmony_ci| int IsHotspotActive(void);                                   | 查询AP是否已经开启       |
280a7ce71fSopenharmony_ci| WifiErrorCode GetStationList(StationInfo* result, unsigned int* size); | 获取接入的设备列表       |
290a7ce71fSopenharmony_ci| int GetSignalLevel(int rssi, int band);                      | 获取信号强度等级         |
300a7ce71fSopenharmony_ci| WifiErrorCode SetBand(int band);                             | 设置当前频段             |
310a7ce71fSopenharmony_ci| WifiErrorCode GetBand(int* result);                          | 获取当前频段             |
320a7ce71fSopenharmony_ci
330a7ce71fSopenharmony_ci#### Hi3861 SDK的DHCP客户端接口:
340a7ce71fSopenharmony_ci
350a7ce71fSopenharmony_ci| API                 | 描述               |
360a7ce71fSopenharmony_ci| ------------------- | ------------------ |
370a7ce71fSopenharmony_ci| netifapi_netif_find | 按名称查找网络接口 |
380a7ce71fSopenharmony_ci| netifapi_dhcp_start | 启动DHCP客户端     |
390a7ce71fSopenharmony_ci| netifapi_dhcp_stop  | 停止DHCP客户端     |
400a7ce71fSopenharmony_ci
410a7ce71fSopenharmony_ci## 二、代码分析
420a7ce71fSopenharmony_ci
430a7ce71fSopenharmony_ci```c
440a7ce71fSopenharmony_ci//wifi ap task
450a7ce71fSopenharmony_cistatic void WifiHotspotTask(void *arg)
460a7ce71fSopenharmony_ci{
470a7ce71fSopenharmony_ci    (void)arg;
480a7ce71fSopenharmony_ci    WifiErrorCode errCode;
490a7ce71fSopenharmony_ci    HotspotConfig config = {0};
500a7ce71fSopenharmony_ci
510a7ce71fSopenharmony_ci    // 配置作为AP热点的ssid和key
520a7ce71fSopenharmony_ci    strcpy(config.ssid, "HiSpark-AP");
530a7ce71fSopenharmony_ci    strcpy(config.preSharedKey, "12345678");
540a7ce71fSopenharmony_ci    config.securityType = WIFI_SEC_TYPE_PSK;
550a7ce71fSopenharmony_ci    config.band = HOTSPOT_BAND_TYPE_2G;
560a7ce71fSopenharmony_ci    config.channelNum = 7;
570a7ce71fSopenharmony_ci
580a7ce71fSopenharmony_ci    osDelay(10);
590a7ce71fSopenharmony_ci
600a7ce71fSopenharmony_ci    printf("starting AP ...\r\n");
610a7ce71fSopenharmony_ci    //开启热点
620a7ce71fSopenharmony_ci    errCode = StartHotspot(&config);
630a7ce71fSopenharmony_ci    printf("StartHotspot: %d\r\n", errCode);
640a7ce71fSopenharmony_ci    //热点将开启1分钟
650a7ce71fSopenharmony_ci    int timeout = 60;
660a7ce71fSopenharmony_ci    while (timeout--) {
670a7ce71fSopenharmony_ci        printf("After %d seconds Ap will turn off!\r\n", timeout);
680a7ce71fSopenharmony_ci        osDelay(100);
690a7ce71fSopenharmony_ci    }
700a7ce71fSopenharmony_ci    // 可以通过串口工具发送 AT+PING=192.168.xxx.xxx(如手机连接到该热点后的IP) 去ping连接到该热点的设备的IP地址 
710a7ce71fSopenharmony_ci    
720a7ce71fSopenharmony_ci    printf("stop AP ...\r\n");
730a7ce71fSopenharmony_ci    //关闭热点
740a7ce71fSopenharmony_ci    StopHotspot();
750a7ce71fSopenharmony_ci    printf("stop AP ...\r\n");
760a7ce71fSopenharmony_ci}
770a7ce71fSopenharmony_ci
780a7ce71fSopenharmony_ci```
790a7ce71fSopenharmony_ci
800a7ce71fSopenharmony_ci
810a7ce71fSopenharmony_ci
820a7ce71fSopenharmony_ci## 三、如何编译
830a7ce71fSopenharmony_ci
840a7ce71fSopenharmony_ci1. 将此目录下的 `wifi_hotspot_demo.c` 和 `BUILD.gn` 复制到openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下,
850a7ce71fSopenharmony_ci2. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
860a7ce71fSopenharmony_ci
870a7ce71fSopenharmony_ci```
880a7ce71fSopenharmony_ci    features = [
890a7ce71fSopenharmony_ci        "iothardware:wifi_demo",
900a7ce71fSopenharmony_ci    ]
910a7ce71fSopenharmony_ci```
920a7ce71fSopenharmony_ci
930a7ce71fSopenharmony_ci3. 在openharmony源码顶层目录执行:`python build.py wifiiot`
940a7ce71fSopenharmony_ci
950a7ce71fSopenharmony_ci## 四、运行结果
960a7ce71fSopenharmony_ci
970a7ce71fSopenharmony_ci```
980a7ce71fSopenharmony_ciready to OS start
990a7ce71fSopenharmony_cisdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
1000a7ce71fSopenharmony_ciFileSystem mount ok.
1010a7ce71fSopenharmony_ciwifi init success!
1020a7ce71fSopenharmony_ci
1030a7ce71fSopenharmony_ci00 00:00:00 0 132 D 0/HIVIEW: hilog init success.
1040a7ce71fSopenharmony_ci00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
1050a7ce71fSopenharmony_ci00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
1060a7ce71fSopenharmony_ci00 00:00:00 0 132 I 1/SAMGR: Init service:0x4af278 TaskPool:0xe4b38
1070a7ce71fSopenharmony_ci00 00:00:00 0 132 I 1/SAMGR: Init service:0x4af284 TaskPool:0xe4b58
1080a7ce71fSopenharmony_ci00 00:00:00 0 132 I 1/SAMGR: Init service:0x4af3ac TaskPool:0xe4b78
1090a7ce71fSopenharmony_ci00 00:00:00 0 164 I 1/SAMGR: Init service 0x4af284 <time: 0ms> success!
1100a7ce71fSopenharmony_ci00 00:00:00 0 64 I 1/SAMGR: Init service 0x4af278 <time: 0ms> success!
1110a7ce71fSopenharmony_ci00 00:00:00 0 8 D 0/HIVIEW: hiview init success.
1120a7ce71fSopenharmony_ci00 00:00:00 0 8 I 1/SAMGR: Init service 0x4af3ac <time: 0ms> success!
1130a7ce71fSopenharmony_ci00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
1140a7ce71fSopenharmony_ci00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
1150a7ce71fSopenharmony_ci00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
1160a7ce71fSopenharmony_ci00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
1170a7ce71fSopenharmony_cistarting AP ...
1180a7ce71fSopenharmony_ciRegisterWifiEvent: 0
1190a7ce71fSopenharmony_ciSetHotspotConfig: 0
1200a7ce71fSopenharmony_ciOnHotspotStateChanged: 1.
1210a7ce71fSopenharmony_ciEnableHotspot: 0
1220a7ce71fSopenharmony_cig_hotspotStarted = 1.
1230a7ce71fSopenharmony_cinetifapi_netif_set_addr: 0
1240a7ce71fSopenharmony_cinetifapi_dhcp_start: 0
1250a7ce71fSopenharmony_ciStartHotspot: 0
1260a7ce71fSopenharmony_ciAfter 59 seconds Ap will turn off!
1270a7ce71fSopenharmony_ciAfter 58 seconds Ap will turn off!
1280a7ce71fSopenharmony_ciAfter 57 seconds Ap will turn off!
1290a7ce71fSopenharmony_ci+NOTICE:STA CONNECTED
1300a7ce71fSopenharmony_ci PrintStationInfo: mac=54:19:C8:D9:A7:35, reason=0.
1310a7ce71fSopenharmony_ci+OnHotspotStaJoin: active stations = 1.
1320a7ce71fSopenharmony_ciAfter 56 seconds Ap will turn off!
1330a7ce71fSopenharmony_ciAfter 55 seconds Ap will turn off!
1340a7ce71fSopenharmony_ci
1350a7ce71fSopenharmony_ciAT+PING=192.168.1.2
1360a7ce71fSopenharmony_ci
1370a7ce71fSopenharmony_ci+PING:
1380a7ce71fSopenharmony_ci[0]Reply from 192.168.1.2:time=90ms TTL=64
1390a7ce71fSopenharmony_ciAfter 49 seconds Ap will turn off!
1400a7ce71fSopenharmony_ci[1]Reply from 192.168.1.2:time=25ms TTL=64
1410a7ce71fSopenharmony_ciAfter 48 seconds Ap will turn off!
1420a7ce71fSopenharmony_ci[2]Reply from 192.168.1.2:time=28ms TTL=64
1430a7ce71fSopenharmony_ciAfter 47 seconds Ap will turn off!
1440a7ce71fSopenharmony_ci[3]Reply from 192.168.1.2:time=4ms TTL=64
1450a7ce71fSopenharmony_ci4 packets transmitted, 4 received, 0 loss, rtt min/avg/max = 4/36/90 ms
1460a7ce71fSopenharmony_ci
1470a7ce71fSopenharmony_ciOK
1480a7ce71fSopenharmony_ci...
1490a7ce71fSopenharmony_ci...
1500a7ce71fSopenharmony_ci...
1510a7ce71fSopenharmony_ciAfter 3 seconds Ap will turn off!
1520a7ce71fSopenharmony_ciAfter 2 seconds Ap will turn off!
1530a7ce71fSopenharmony_ciAfter 1 seconds Ap will turn off!
1540a7ce71fSopenharmony_ciAfter 0 seconds Ap will turn off!
1550a7ce71fSopenharmony_cistop AP ...
1560a7ce71fSopenharmony_cinetifapi_dhcps_stop: 0
1570a7ce71fSopenharmony_ciUnRegisterWifiEvent: 0
1580a7ce71fSopenharmony_ci+NOTICE:STA DISCONNECTED
1590a7ce71fSopenharmony_ciEnableHotspot: 0
1600a7ce71fSopenharmony_cistop AP ...
1610a7ce71fSopenharmony_ci
1620a7ce71fSopenharmony_ci
1630a7ce71fSopenharmony_ci```
1640a7ce71fSopenharmony_ci
1650a7ce71fSopenharmony_ci
1660a7ce71fSopenharmony_ci
1670a7ce71fSopenharmony_ci
1680a7ce71fSopenharmony_ci
1690a7ce71fSopenharmony_ci### 【套件支持】
1700a7ce71fSopenharmony_ci
1710a7ce71fSopenharmony_ci##### 1. 套件介绍  http://www.hihope.org/pro/pro1.aspx?mtt=8
1720a7ce71fSopenharmony_ci
1730a7ce71fSopenharmony_ci##### 2. 套件购买  https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop
1740a7ce71fSopenharmony_ci
1750a7ce71fSopenharmony_ci##### 3. 技术资料
1760a7ce71fSopenharmony_ci
1770a7ce71fSopenharmony_ci- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg**
1780a7ce71fSopenharmony_ci
1790a7ce71fSopenharmony_ci- HiHope官网-资源中心(SDK包、技术文档下载)**http://www.hihope.org/**
1800a7ce71fSopenharmony_ci
1810a7ce71fSopenharmony_ci##### 4. 互动交流
1820a7ce71fSopenharmony_ci
1830a7ce71fSopenharmony_ci- 润和HiHope技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方群#,予以邀请入群)
1840a7ce71fSopenharmony_ci- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429**
1850a7ce71fSopenharmony_ci- 润和HiHope售后服务群(QQ:980599547)
1860a7ce71fSopenharmony_ci- 售后服务电话(025-52668590)
187