【鸿蒙HarmonyOS HiSpark IPC DIY Camera】hi3518-wifi的配置与使用

系统 OpenHarmony Spark
主要是修改和3518相关的wpa_supplicant.conf hostapd.conf 两个文件,对wifi相关的ssid、passwd等进行配置

[[353328]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

主 要是修改和3518相关的wpa_supplicant.conf hostapd.conf 两个文件,对wifi相关的ssid、passwd等进行配置

【编译生成wifi可执行文件】 请参考以下示例代码编译可执行程序wpa_supplicant:(SDK里面有这些代码,不做修改)

路径 ./applications/sample/camera/communication/wpa_supplicant/src/wpa_sample.c

  1. #include <dlfcn.h> 
  2. #include <pthread.h> 
  3. #include <stdio.h> 
  4. #include <string.h> 
  5. ​ 
  6. pthread_t g_wpaThread; 
  7. ​ 
  8. char* g_wpaArg[20] = {0}; 
  9. int g_wpaArgc = 0; 
  10. ​ 
  11. static void* ThreadMain() 
  12.     printf("[WpaSample]init wpa_supplicant.\n"); 
  13. ​ 
  14.     void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL); 
  15.     if (handleLibWpa == NULL) { 
  16.         printf("[WpaSample]dlopen libwpa failed.\n"); 
  17.         return NULL
  18.     } 
  19.     int (*func)(intchar **) = NULL
  20.     func =  dlsym(handleLibWpa, "wpa_main"); 
  21.     if (func == NULL) { 
  22.         dlclose(handleLibWpa); 
  23.         printf("[WpaSample]dlsym wpa_main failed.\n"); 
  24.         return NULL
  25.     } 
  26.     int ret = func(g_wpaArgc, g_wpaArg); 
  27.      
  28.     printf("[WpaSample]run wpa_main failed, ret:%d.\n", ret); 
  29.     for (int i = 0; i < g_wpaArgc; i++) { 
  30.         printf("[WpaSample]arg %d:%s.\n", i, g_wpaArg[i]); 
  31.     } 
  32.      
  33.     if (dlclose(handleLibWpa) != 0) { 
  34.         printf("[WpaSample]dlclose libwpa failed.\n"); 
  35.         return NULL
  36.     } 
  37.     return NULL
  38. ​ 
  39. ​ 
  40. int main(int argc, char *argv[]) 
  41.     g_wpaArgc = argc; 
  42.     for (int i = 0; i < g_wpaArgc; i++) { 
  43.         g_wpaArg[i] = argv[i]; 
  44.     } 
  45. ​ 
  46.     int ret = pthread_create(&g_wpaThread, NULL, ThreadMain, NULL); 
  47.     if (ret != 0) { 
  48.         printf("[WpaSample]create thread failed error:%s.\n", strerror(ret)); 
  49.         return 1; 
  50.     } 
  51.     pthread_join(g_wpaThread, NULL); 
  52.     return 0; 
  53. ​ 

 路径 ./applications/sample/camera/communication目录下面的BUILD.gn文件

  1. import("//build/lite/config/component/lite_component.gni"
  2.  
  3. lite_component("sample") { 
  4.     features = [ 
  5.        "wpa_supplicant:wpa_sample"
  6.     ] 

 路径 ./applications/sample/camera/communication/wpa_supplicant目录下面的BUILD.gn文件 

  1. import("//build/lite/config/component/lite_component.gni"
  2.  
  3. sample_sources = [ 
  4.     "src/wpa_sample.c"
  5.  
  6. config_file = [ 
  7.     "config/wpa_supplicant.conf" 
  8.  
  9. executable("wpa_sample_exe") { 
  10.     output_name = "wpa_supplicant" 
  11.     sources = sample_sources 
  12.  
  13. group("wpa_sample") { 
  14.     deps = [ 
  15.         ":wpa_sample_exe"
  16.     ] 
  17.  
  18. copy("config") { 
  19.     sources = config_file 
  20.     outputs = [ 
  21.        "$root_out_dir/etc/wpa_supplicant.conf" 
  22.     ] 

 路径 ./build/lite/product/ipcamera_hi3518ev300.json  

 【修改station模式的配置】 将以下配置写到配置文件wpa_supplicant.conf

  1. country=GB 
  2. ctrl_interface=udp 
  3. network={ 
  4.     #要连接的SSID 
  5.     ssid="example" 
  6.     #如果不需要加密就写key_mgmt=NONE 
  7.     #key_mgmt=NONE 
  8.     #如果需要加密就写这行密码 
  9.     psk="12345678" 

 【修改ap模式的配置】 将以下配置写到配置文件hostapd.conf 

  1. interface=wlan0 
  2. driver=hdf wifi 
  3. ctrl_interface=udp 
  4. ssid=testap 
  5. hw_mode=g 
  6. channel=1 
  7. ignore_broadcast_ssid=0 
  8. #下面是wpa2-psk类型加密的配置 
  9. #如果不需要加密可以删除 
  10. auth_algs=1 
  11. wpa=2 
  12. wpa_passphrase=12345678 
  13. rsn_pairwise=CCMP 

 【使用方法】

将wpa_supplicant、wpa_supplicant.conf、hostapd.conf拷贝到单板中(sdcard目录)

启动sta的命令:

  1. ./sdcard/wpa_supplicant -i wlan0 -c /sdcard/wpa_supplicant.conf 

启动ap的命令:

  1. ./sdcard/hostapd -i wlan0 /sdcard/hostapd.conf 

【WIFI设备检测】 进入OHOS界面后,输入ifconfig,检查wlan0网口是否加载正常,加载成功标识如下:

OHOS # ifconfig

wlan0 ip:0.0.0.0 netmask:0.0.0.0 gateway:0.0.0.0

HWaddr 0a:11:31:aa:7e:1a MTU:1500 Stop Link UP

lo ip:127.0.0.1 netmask:255.0.0.0 gateway:127.0.0.1

ip6: ::1/64

HWaddr 00 MTU:16436 Running Link UP

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-01-26 14:27:21

鸿蒙HarmonyOS应用

2021-03-16 09:49:16

鸿蒙HarmonyOS应用

2021-02-02 15:52:17

鸿蒙HarmonyOS应用开发

2021-01-13 14:00:24

鸿蒙hi3518开发

2021-07-05 14:29:28

鸿蒙HarmonyOS应用

2020-11-03 11:39:22

wifi小车

2021-02-06 13:28:21

鸿蒙HarmonyOS应用开发

2020-10-30 09:41:44

鸿蒙Hi3861WiFi小车

2020-10-16 09:50:37

Hi3861WiFi热点

2021-03-29 15:36:46

鸿蒙HarmonyOS应用

2021-04-09 09:45:21

鸿蒙HarmonyOS应用

2020-10-12 11:33:00

鸿蒙

2022-05-16 11:17:01

应用开发JSJAVA

2021-07-12 09:50:39

鸿蒙HarmonyOS应用

2020-12-15 11:57:49

Hi3861 HarmonyOS开发板

2021-06-25 15:04:31

鸿蒙HarmonyOS应用

2020-11-10 10:00:10

HarmonyOS

2020-10-23 08:31:15

Nodejs-Ipc设计实现

2020-11-06 10:15:16

HiBurn

2020-11-02 11:56:57

鸿蒙 WiFi操作
点赞
收藏

51CTO技术栈公众号