当前位置:网站首页>Lesson 2 WiFi experiment of hi3861 AP mode
Lesson 2 WiFi experiment of hi3861 AP mode
2022-07-21 07:53:00 【Dandelion, let it be】
introduction
In this paper , Take you to write a program , test Hi3861 Of WiFi-AP Pattern , Get more familiar with relevant API Use .
Please follow this column first Lesson one In the fourth part, prepare the experimental environment .
One 、 Programming
First , open DevEco Device Tool
, In Hongmeng project hispark_pegasus_312
Folder applications/sample/wifi-iot/app/experiment
Create a new folder C01_wifi_ap
.
then , In the folder C01_wifi_ap
Create a new file :wifi_ap_test.c
. below , We write the program in this file .
1.1 The header file
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_hotspot.h"
#include "netifapi.h"
1.2 Macro definition
#define AP_SSID "Hi3861-AP" //Hi3861 WiFi The name of the hot spot
#define AP_PSK "12345678" //Hi3861 WiFi The password of the hotspot
1.3 Global variables
//WiFi Event callback function structure
WifiEvent g_wifiEventHandler = {0};
// Network interface structure pointer
static struct netif *g_lwip_netif = NULL;
//Hi3861 Of WiFi-AP The state of the pattern , 1: open , 0: close
static volatile int g_hotspotStarted = 0;
// Access Hi3861 Of WiFi Number of hot spots
static volatile int g_joinedStations = 0;
1.4 function
1.4.1 Subfunctions
The function prototype | Function description |
---|---|
int WiFiEventHandlerRegister(void) | Yes WiFi Event callback function to register . |
void PrintStationInfo(StationInfo* info) | Print access / Leave WiFi Hot WiFi Device information . |
int ChangeHotspotIpAddr(void) | change Hi3861-WiFi-AP Address information for . |
int StartHotspot(const HotspotConfig* config) | Turn on Hi3861 Of WiFi hotspot . |
int StopHotspot(void) | close Hi3861 Of WiFi hotspot . |
These sub functions are mainly introduced by calling API To achieve , They are in the task function or WiFi Event is called in the callback function .
1、 function :PrintStationInfo
// Print the information of the site
static void PrintStationInfo(StationInfo* info)
{
static char macAddress[32] = {0};
unsigned char* mac = info->macAddress;
if (!info) return;
snprintf(macAddress, sizeof(macAddress),
"%02X:%02X:%02X:%02X:%02X:%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("PrintStationInfo: mac=%s, reason=%d \r\n", macAddress, info->disconnectedReason);
}
Function is called in this function snprintf
, The function is : According to section 3 Format specified by parameters , Put the last 6 Parameters (MAC Address of the 6 Bytes ) Become a string , And assign this character to the first parameter .
2、 function :ChangeHotspotIpAddr
// change Hi3861-WiFi-AP Address information for
static int ChangeHotspotIpAddr(void)
{
int errCode = 0;
ip4_addr_t ipaddr; //IP Address
ip4_addr_t gateway; // gateway
ip4_addr_t netmask; // Subnet mask
g_lwip_netif = netifapi_netif_find("ap0");
if(g_lwip_netif)
{
// stop it WiFi Hot DHCP The server
printf("\r\n");
printf("Stopping DHCP server ...... \r\n");
errCode = netifapi_dhcps_stop(g_lwip_netif);
if(errCode != ERR_OK)
{
printf("Error!!! errCode = %d \r\n", errCode);
return -1;
}
printf("Succeed! \r\n");
// Set up WiFi Address information of hotspot
printf("Changing IP address ...... \r\n");
IP4_ADDR(&ipaddr, 192, 168, 3, 1);
IP4_ADDR(&gateway, 192, 168, 3, 1);
IP4_ADDR(&netmask, 255, 255, 255, 0);
errCode = netifapi_netif_set_addr(g_lwip_netif, &ipaddr, &netmask, &gateway);
if(errCode != ERR_OK)
{
printf("Error!!! errCode = %d \r\n", errCode);
return -1;
}
printf("Succeed! IP: 192.168.3.1 \r\n");
// start-up WiFi Hot DHCP The server
printf("Starting DHCP server ...... \r\n");
errCode = netifapi_dhcps_start(g_lwip_netif, 0, 0);
if(errCode != ERR_OK)
{
printf("Error!!! errCode = %d \r\n", errCode);
return -1;
}
printf("Succeed! \r\n");
}
else
{
printf("Error!!! g_lwip_netif is NULL!!! \r\n");
return -1;
}
return 0;
}
Function IP4_ADDR
Is in header file ip4_addr.h
A macro with parameters defined in , Its effect is : Is to divide 4 The number of addresses is synthesized into one stored in the big end mode 32bit Unsigned integer .
1.4.2 WiFi Event callback function
stay AP In mode , There are three kinds of WiFi event . therefore , We need to write three callback functions accordingly , For these three WiFi Deal with the incident .
The function prototype | Function description |
---|---|
void OnHotspotStateChangedHandler(int state) | Callback function ,WiFi event :Hi3861-AP The state of the mode changes ( open / close ) |
void OnHotspotStaJoinHandler(StationInfo* info) | Callback function ,WiFi event : There is site access Hi3861 Of WiFi hotspot |
void OnHotspotStaLeaveHandler(StationInfo* info) | Callback function ,WiFi event : There are sites leaving Hi3861 Of WiFi hotspot |
1.4.3 Task function and task entry function
The task function can be understood as the main function in this program ; Task entry function is to create task function as a task , And make it ready , Accept the scheduling of the operating system .
The function prototype | Function description |
---|---|
void WifiHotspotTaskFun(void) | Task function |
void WifiHotspotTaskEntry(void) | Task entry function |
The basic process of task function is shown in the following figure :
step Step3-3 It's not necessary , If not changed WiFi Address information for ,WiFi Default for hotspots IP The address is :192.168.5.1.
The code of the task function is as follows :
// Task function
static void WifiHotspotTaskFun(void)
{
int errCode = 0;
int timeout;
HotspotConfig config = {0}; //WiFi Configuration parameter structure of hotspot
//Step1: Get ready WiFi Configuration parameters of hotspots
strcpy(config.ssid, AP_SSID); //Hi3861 WiFi The name of the hot spot
strcpy(config.preSharedKey, AP_PSK); //Hi3861 WiFi The password of the hotspot
config.securityType = WIFI_SEC_TYPE_PSK; //Hi3861 WiFi Encryption type of hotspot
config.band = HOTSPOT_BAND_TYPE_2G; //Hi3861 WiFi Hot band
config.channelNum = 7; //Hi3861 WiFi Hot channel
while(1)
{
//Step2: register WiFi Callback function for event
printf("\r\n");
WiFiEventHandlerRegister();
//Step3: Turn on Hi3861 Of WiFi hotspot
printf("\r\n");
printf("starting AP ...\r\n");
StartHotspot(&config);
//Step4: Time delay
printf("\r\n");
printf("Please connect this hotspot with your mobile phone ...\r\n");
printf("\r\n");
for(timeout=120; timeout>0; timeout--)
{
osDelay(100);
if(timeout == 10)
{
printf("\r\n");
printf("Hotspot will be closed after %d s ...... \r\n", timeout);
}
}
//Step5: close WiFi hotspot
StopHotspot();
//Step6: Cancellation WiFi Callback function for event
printf("\r\n");
printf("UnRegister WifiEvent ...... \r\n");
errCode = UnRegisterWifiEvent(&g_wifiEventHandler);
if(errCode != WIFI_SUCCESS)
{
printf("Error!!! errCode = %d \r\n", errCode);
}
printf("Succeed! \r\n");
//Step7: Time delay
for(timeout=30; timeout>0; timeout--)
{
osDelay(100);
if(timeout < 10)
{
printf("Hotspot will be opened after %d s ...... \r\n", timeout);
}
}
}
}
Two 、 To write / Modify the compilation script
1、 In the folder C01_wifi_ap
Create a new file :BUILD.gn
. The contents of this compilation script are as follows :
static_library("C01_wifi_ap") {
sources = [
"wifi_ap_test.c",
]
include_dirs = [
"//utils/native/lite/include",
"//device/hisilicon/hispark_pegasus/hi3861_adapter/kal/cmsis",
"//foundation/communication/wifi_lite/interfaces/wifiservice",
"//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/lwip_sack/include/lwip",
]
}
2、 Modify folder applications/sample/wifi-iot/app
The files under the BUILD.gn
:
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"startup",
"experiment/C01_wifi_ap",
]
}
3、 ... and 、 compile 、 burning
A little . Reference resources :《 Hongmeng equipment development Hello World》 Third 、 Four parts .
Four 、 test
1、 After burning , Reference resources 《 Hongmeng equipment development Hello World》 The fifth part , Start the test program . The test program will print out some information in the terminal window according to the process of task function , As shown in the figure below :
2、 When you see the last prompt in the above figure , You can use the mobile phone WiFi You can see it in the settings Hi3861 Of WiFi hotspot . Connect this with your mobile phone WiFi hotspot , The terminal output is shown in the figure below :
mac
It's a cell phone MAC Address ,active stations
Is the current access WiFi Number of hot devices .
When the phone starts from Hi3861 Of WiFi When the hotspot is disconnected , The terminal output is shown in the figure below :
3、 When it is about to close WiFi Hot spot , The terminal outputs the first prompt message shown in the figure below , And then in 10s After closing DHCP service 、 close AP Pattern 、 Cancellation WiFi Event callback function .
边栏推荐
- Audio and video learning (VIII) -- RTP protocol
- R language uses the mean function to calculate the relative frequency of the specified variables in the sample (observation) data: calculate the proportion of the value of the specified data column in
- 第2讲 Hi3861的WiFi实验-API-3
- VIM encrypted file "recommended collection"
- OCR/STR生僻字数据训练 | PaddleOCR的垂类Fine-tune(2)
- Seata 多语言体系建设
- R语言Kolmogorov-Smirnov检验比较两个样本是否来自同一个分布:ks.test函数执行Kolmogorov-Smirnov检验比较两个样本是否来自同一个分布
- 多级缓存解决方案
- Hcip notes day 10
- grpc设置连接存活时间及服务端健康检查
猜你喜欢
开源demo| ARCall 小程序开源示例发布
VPS、独服和云服务器哪种最适合您的业务?
Redis学习笔记(2)——B站动力节点
How to improve the clarity of LED display?
精品方案|海泰云密码应用服务解决方案 打造安全合规的云上应用
血压血氧健康监测,一个手环就搞定,dido Y1S体验
Redis learning notes (1) - power node of station B
pytorch常见问题整理与解决方法
2022 | Sample Efficiency Matters: A Benchmark for Practical Molecular Optimization
Multi level cache solution
随机推荐
【每日一题】1260. 二维网格迁移
Hcip notes day 9
【Redis】常见知识点总结
Surface strongest editor sublime | regular expression skills
【C语言进阶】自定义类型——结构体的位段&&枚举&&联合体
面试真题2
The drawing principle and application of Bao Jiao Bao Hui Bezier curve
Question d'entrevue 2
Hcip notes day 10
[golang] go parse XMIND
Dall-E2和Imgen有什么区别?Reddit热帖盘点效果差异
OCR/STR生僻字数据训练 | PaddleOCR的Fine-tune常见问题汇总(3)
【Golang】go 解析xmind
R language ggplot2 visualization: the label data of visual faceted scatter diagram is word cloud, and geom using ggrep package_ text_ The repl function avoids overlapping word cloud labels
sql 命令insert into 的作业没办法通过savepoint启动么?或是我集群关闭重启的时
第一章 创建 REST 服务简介
The KAP function of the epidisplay package of R language calculates the proportion of the calculation consistency of the paired contingency table and the value of kappa statistics (total consistency,
Unity learning notes: code record of launching a small ball to collide with an object
ovirt:api接口+keystone接口+neutron接口示例
Audio and video learning (VIII) -- RTP protocol