HiHope开发者社区
直播中

HiHope

4年用户 809经验值
擅长:嵌入式技术
私信 关注
[经验]

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

本帖最后由 l_xy 于 2020-11-19 16:24 编辑

主要是修改和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
  2. #include
  3. #include
  4. #include

  5. pthread_t g_wpaThread;

  6. char* g_wpaArg[20] = {0};
  7. int g_wpaArgc = 0;

  8. static void* ThreadMain()
  9. {
  10.     printf("[WpaSample]init wpa_supplicant.n");

  11.     void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL);
  12.     if (handleLibWpa == NULL) {
  13.         printf("[WpaSample]dlopen libwpa failed.n");
  14.         return NULL;
  15.     }
  16.     int (*func)(int, char **) = NULL;
  17.     func =  dlsym(handleLibWpa, "wpa_main");
  18.     if (func == NULL) {
  19.         dlclose(handleLibWpa);
  20.         printf("[WpaSample]dlsym wpa_main failed.n");
  21.         return NULL;
  22.     }
  23.     int ret = func(g_wpaArgc, g_wpaArg);

  24.     printf("[WpaSample]run wpa_main failed, ret:%d.n", ret);
  25.     for (int i = 0; i < g_wpaArgc; i++) {
  26.         printf("[WpaSample]arg %d:%s.n", i, g_wpaArg);
  27.     }

  28.     if (dlclose(handleLibWpa) != 0) {
  29.         printf("[WpaSample]dlclose libwpa failed.n");
  30.         return NULL;
  31.     }
  32.     return NULL;
  33. }

  34. int main(int argc, char *argv[])
  35. {
  36.     g_wpaArgc = argc;
  37.     for (int i = 0; i < g_wpaArgc; i++) {
  38.         g_wpaArg = argv;
  39.     }

  40.     int ret = pthread_create(&g_wpaThread, NULL, ThreadMain, NULL);
  41.     if (ret != 0) {
  42.         printf("[WpaSample]create thread failed error:%s.n", strerror(ret));
  43.         return 1;
  44.     }
  45.     pthread_join(g_wpaThread, NULL);
  46.     return 0;
  47. }


路径  ./applications/sample/camera/communication目录下面的BUILD.gn文件
  1. import("//build/lite/config/component/lite_component.gni")

  2. lite_component("sample") {
  3.     features = [
  4.        "wpa_supplicant:wpa_sample",
  5.     ]
  6. }

路径  ./applications/sample/camera/communication/wpa_supplicant目录下面的BUILD.gn文件
  1. import("//build/lite/config/component/lite_component.gni")

  2. sample_sources = [
  3.     "src/wpa_sample.c",
  4. ]

  5. config_file = [
  6.     "config/wpa_supplicant.conf"
  7. ]

  8. executable("wpa_sample_exe") {
  9.     output_name = "wpa_supplicant"
  10.     sources = sample_sources
  11. }

  12. group("wpa_sample") {
  13.     deps = [
  14.         ":wpa_sample_exe",
  15.     ]
  16. }

  17. copy("config") {
  18.     sources = config_file
  19.     outputs = [
  20.        "$root_out_dir/etc/wpa_supplicant.conf"
  21.     ]
  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"
  10. }


【修改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拷贝到单板中
  • 启动sta的命令:
    1. ./sdcard/wpa_supplicant -i wlan0 -c /sdcard/wpa_supplicant.conf

  • 启动ap的命令:
    1. ./sdcard/hostapd -i wlan0 /sdcard/hostapd.conf

【WIFI设备检测】进入OHOS界面后,输入
  1. 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

回帖(1)

asllan

2021-5-18 13:52:07
本帖最后由 asllan 于 2021-5-18 19:25 编辑

请问如何查看是否连上WiFi
举报

更多回帖

发帖
×
20
完善资料,
赚取积分