网站首页 > 开源技术 正文
大家好,我是杂烩君。
前言
本篇博文我们来分享嵌入式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 name
interface=wlan0
# SSID to be used in IEEE 802.11 management frames
ssid=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=1
hw_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 SSID
ignore_broadcast_ssid=0
# WPA/IEEE 802.11i configuration
wpa=2
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
rsn_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 block
start 192.168.3.2
end 192.168.3.254
# The interface that udhcpd will use
interface wlan0
opt dns 114.114.114.114
option subnet 255.255.255.0
opt router 192.168.3.1
option domain local
option 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
# 启动DHCP
udhcpd /etc/udhcpd.conf
# 启动热点
hostapd /etc/hostapd.conf -B
开启热点:
连接测试:
可见,手机分配到的IP为192.168.3.2,属于192.168.3.2~192.168.3.254的范围,我们的设备热点开启成功!我们的PC可以连接这个热点对设备进行调试。
以上就是本次的分享,如果觉得文章有帮助,麻烦帮忙转发,谢谢!
如果文章对你有帮助,麻烦帮忙点赞、收藏、转发,谢谢!
猜你喜欢:
私信回复【嵌入式书籍】,可获取博主精心整理的嵌入式电子书一份。
- 上一篇: 第五章 网络配置(续六)
- 下一篇: 安卓系统手机文件夹及其文件详细解析
猜你喜欢
- 2024-11-19 树莓派4B系统烧录及Docker安装步骤
- 2024-11-19 Android四月更新:Google修复40处安全漏洞
- 2024-11-19 安卓系统手机文件夹及其文件详细解析
- 2024-11-19 第五章 网络配置(续六)
- 2024-11-19 第五章 网络配置(续三)
- 2024-11-19 「上海晶珩EDATEC」「工业树莓派」dhcpcd 使用介绍
- 2024-07-26 Archlinux安装(archLinux安装kde)
- 2024-07-26 纯手工打造树莓派3双无线网卡应用无线热点
- 2024-07-26 archlinux 安装教程(archlinux安装教程holo)
- 2024-07-26 树莓派4B新手入门3-基础设置(树莓派4b说明书)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)