前言
本篇博文我们来分享嵌入式linux设备开启无线ap/无线接入点(wireless access point)的方法。
什么情况下会用到无线ap?
我最近的工作中有如下两种情况需要用到:
(1)ap配网。设备热点配网,智能硬件处于ap模式,手机作为sta连接到处于ap模式的智能硬件后组成局域网。此时,手机就可以通过局域网把设备即将连接的路由的ssid和pwd信息至智能硬件,智能硬件接收后,连接路由器,完成配网。
(2)把废旧不用的板子作为开启无线ap组建各设备的局域网通信。对于移动机器人的开发来说,设备实际工作过程中,无线调试无疑是最方便的。
因为设备一直处于运动状态,如果接着有线,电脑需要跟着设备跑,很不方便。因为我们调试时,对路由器的需求比较大,而路由器比较有限,所以我把废旧不用的板子配成了无线ap模式。
经过实测,相同距离,旧板子局域网通信速度略低于我们路由器,但不影响我们作为调试时使用。
嵌入式linux设备,要开启无线接入点需要准备如下四个文件:
hostapd:一个用户态用于ap和认证服务器的守护进程。
hostapd.conf:hostapd配置文件,包含无线ap的名称、密码等信息。
udhcpd:dhcp拨号的服务器端。
udhcpd.conf:udhcpd配置文件,配置网关地址及ip地址的范围。
其中,hostapd、udhcpd工具busybox中包含有。当然,也可以自己下载源码进行编译,方法可参照我们往期的博文:
远程登陆开发板:rtl8723驱动移植+wpa_supplicant移植+ssh移植,编译方法都是大同小异的。
注意区分:udhcpc、udhcpd工具。
udhcpc是dhcp拨号的客户端。设备作为sta时,用于自动获取ip。
udhcpd是dhcp拨号的服务器端。设备作为ap时,用于自动分配ip。
其中,我们的往期博文如何实现程序开机自启动?中有用到udhcpc,本博文中我们用的是udhcpd。
下面我们来看hostapd及udhcpd的配置文件如何配置:
hostapd配置文件
hostapd的配置文件可参考hostapd源码下的hostapd.conf:
里面的内容很多,实际中我们可能用不到那么多配置,我们可以删减、修改,只保留我们所需的配置。
我们删减修改之后得到:
左右滑动查看全部代码>>>
# ap netdevice nameinterface=wlan0# ssid to be used in ieee 802.11 management framesssid=linuxzn_ap# driver interface type (hostap/wired/none/nl80211/bsd);# default: hostap). nl80211 is used with all linux mac80211 drivers.# use driver=none if building hostapd as a standalone radius server that does# not control any wireless/wired driver.driver=nl80211# interface for separate control program.# /var/run/hostapd is the recommended directory for sockets and by default,# hostapd_cli will use it when trying to connect with hostapd.ctrl_interface=/var/run/hostapd# channel number (ieee 802.11)channel=5# ieee80211n: whether ieee 802.11n (ht) is enabled# 0 = disabled (default)# 1 = enabled# note: you will also need to enable wmm for full ht functionality.# note: hw_mode=g (2.4 ghz) and hw_mode=a (5 ghz) is used to specify the band.ieee80211n=1hw_mode=g# send empty ssid in beacons and ignore probe request frames that do not# specify full ssid, i.e., require stations to know ssid.# default: disabled (0)# 1 = send empty (length=0) ssid in beacon and ignore probe request for# broadcast ssid# 2 = clear ssid (ascii 0), but keep the original length (this may be required# with some clients that do not support empty ssid) and ignore probe# requests for broadcast ssidignore_broadcast_ssid=0# wpa/ieee 802.11i configurationwpa=2wpa_passphrase=12345678wpa_key_mgmt=wpa-pskrsn_pairwise=ccmp
该文件主要配置了:
所用网卡:wlan0
ap名称:linuxzn_ap
ap密码:12345678
加密:wpa2
频段:2.4ghz
我们把hostapd.conf配置文件我们放到板子上的/etc目录下备用:
udhcpd配置文件
udhcpd的配置文件可参考udhcpd源码下的udhcpd.conf:
同样的,我们只保留如下内容:
左右滑动查看全部代码>>>
# the start and end of the ip lease blockstart 192.168.3.2end 192.168.3.254# the interface that udhcpd will useinterface wlan0opt dns 114.114.114.114option subnet 255.255.255.0opt router 192.168.3.1option domain localoption lease 864000 # 10 days of seconds
该文件主要配置了:
所能分配的ip地址的范围为:192.168.3.2~192.168.3.254
网卡接口:wlan0
网关地址:192.168.3.1
我们把udhcpd.conf配置文件放到板子上的/etc目录下备用:
开启热点
有了以上工具及相关配置文件之后,还需要进行一些操作,才可以开启我们的热点,我们把这些操作写成脚本:
start_ap.sh:
左右滑动查看全部代码>>>
#!/bin/bash# 杀掉网卡操作相关的进程killall wpa_supplicant udhcpc dhcpcd dnsmasq udhcpd hostapd > /dev/null 2>&1# 禁用网卡ifconfig wlan0 down# 启用网卡ifconfig wlan0 up# 给无线网卡设置ip地址(网关地址)ifconfig wlan0 192.168.3.1# 启动dhcpudhcpd /etc/udhcpd.conf # 启动热点hostapd /etc/hostapd.conf -b
开启热点:
连接测试:
可见,手机分配到的ip为192.168.3.2,属于192.168.3.2~192.168.3.254的范围,我们的设备热点开启成功!我们的pc可以连接这个热点对设备进行调试。
华为5G的发展并没有受到美国政府打压的影响
MOM压力传感器的改进设计方案解析
农药残留检测仪让农产品食用安全更有保障
什么是Verilog HDL?
利用泰克示波器进行电动后视镜转向电流测试
嵌入式Linux设备开启无线AP/无线接入点的方法
中国移动与华为联合完成了基于SA架构的端到端切片验证
中央处理器的频率为什么那么高?
微雪电子测距传感器VL53L0X测距模块简介
“人本照明”,能帮助我们做多大程度的改善呢?
便携式现场动平衡仪
小米MIX3与一加6T在拍照方面到底谁更强
永太联手佳陞,拟合资设立平板显示、半导体材料公司
射频 雷达 毫米波 微波天线pcb板 (需要用到的RO4000系列)
高品价比的教学神器-HUAWEI MateBook E Go
欧盟新一代高效太阳能电池技术获突破
一名注册消防工程师的备考经历
中国锂电池行业发展因素分析
联发科将于1月20日发布天玑系列新产品
提高智能电表精度新方法,内置高耐度nvSRAM