--- page-title: "树莓派 OpenThread 边界路由器配置 - YP.Lam" url: https://yplam.com/IOT/openthread/raspberry-pi-openthread/ date: "2024-12-13 11:14:15" --- ## 树莓派 OpenThread 边界路由器配置[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#openthread "Permanent link") OpenThread 边界路由器的作用是作为 Thread 网络与其他基于IP的网络(如WIFI、以太网)的桥梁。有了边界路由器的存在才让 Thread 网络中的设备成了跟手机、电脑等对等的一员(是的,基于IP就是那么自信)。 OpenThread 官方提供 Docker、 BeagleBone Black、Raspberry Pi 3B 的支持(代码中 OpenWRT 也是有支持的,只是可能还未正式稳定)。本文将尽量详细地记录 Raspberry Pi 3B 配置成边界路由器的过程。 ## 树莓派基础配置[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#_1 "Permanent link") 第一步当然是下载镜像,并且写入到SD卡。官网地址:[https://www.raspberrypi.org/downloads/raspberry-pi-os/](https://www.raspberrypi.org/downloads/raspberry-pi-os/) 。Linux系统下可以直接用以下命令写入镜像: ``` # 注意:请根据真实情况选择磁盘,不然可能会导致你的数据丢失 sudo dd bs=4M if=2020-08-20-raspios-buster-armhf-lite.img of=/dev/mmcblk0 conv=fsync ``` ### 启用wifi与ssh[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#wifissh "Permanent link") 使用headless的配置方式,不需要显示器与外接键盘。在SD卡的 boot 分区中创建一个空的 ssh 文件: ``` cd /run/media/yplam/boot touch ssh ``` 在刚刚那个 boot 分区新建一个 wpa\_supplicant.conf 文件,输入 WIFI 网络信息: ``` country=CN ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="NETWORK-NAME" psk="NETWORK-PASSWORD" } ``` 插入SD卡上电启动,如果有mdns的话直接 : ``` ping raspberrypi.local ``` 获取IP,如果没有则登录路由器把它找出来,然后ssh登录,用户名pi,密码 raspberry 。 ### 基础软件安装[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#_2 "Permanent link") ``` sudo apt install git ``` ## OTBR编译安装[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#otbr "Permanent link") ``` git clone https://github.com/openthread/ot-br-posix cd ot-br-posix ./script/bootstrap ./script/setup ``` 注意,可能需要改改 script/\_dns64 中关于 dns 服务器的配置(国内无法访问) ## RCP 配置[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#rcp "Permanent link") 编译 RCP 固件,如 NRF52840 可以使用以下编译选项: ``` cd /src/openthread/ ./bootstrap make -f examples/Makefile-nrf52840 BORDER_AGENT=1 BORDER_ROUTER=1 COMMISSIONER=1 UDP_FORWARD=1 USB=1 LINK_RAW=1 BOOTLOADER=USB cd /src/openthread/output/nrf52840/bin arm-none-eabi-objcopy -O ihex ot-rcp ot-rcp.hex ``` 插入 RCP 到树莓派 USB 口,查看: ``` ls /dev/tty* ``` 名称为 /dev/ttyACM\* 的设备即为 RCP;修改配置文件 /etc/default/otbr-agent ``` OTBR_AGENT_OPTS="-I wpan0 spinel+hdlc+uart:///dev/ttyACM0" ``` 修改配置后重启系统,运行以下命令: ``` sudo systemctl status ``` 如果安装正常,则可以看到相关服务正常运行: ``` avahi-daemon.service otbr-agent.service otbr-web.service ``` 而运行下面命令可以看到OpenThread网络为 disabled 状态 ``` sudo ot-ctl state ``` ## AP模式配置[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#ap "Permanent link") 树莓派可以配置运行在 AP 模式,其他设备可以接入树莓派提供的网络来管理 OpenThread 网络。 安装以下软件: ``` sudo apt-get install hostapd dnsmasq tayga ``` - hostapd — 允许使用树莓派的WIFI允许在AP模式 - dnsmasq — 提供 DHCP 与 DNS 服务 - tayga — NAT64服务,提供外网 IPV4 地址到 IPV6 地址的转换 修改文件 /etc/dhcpcd.conf,末尾增加一行 ``` denyinterfaces wlan0 ``` 新建文件 /etc/network/interfaces.d/wlan0 ``` allow-hotplug wlan0 iface wlan0 inet static address 192.168.1.2 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 ``` 配置 /etc/hostapd/hostapd.conf,因为使用的是 PI3B+,支持5G网络,配置有所不同 ``` # The Wi-Fi interface configured for static IPv4 addresses interface=wlan0 # Use the 802.11 Netlink interface driver driver=nl80211 # The user-defined name of the network ssid=BorderRouter-AP # Use the 5GHz band hw_mode=a # Use channel 6 channel=40 # Enable 802.11n ieee80211n=1 # Enable WMM wmm_enabled=1 require_ht=1 ht_capab=[HT40-][DSSS_CCK-40] # Accept all MAC addresses macaddr_acl=0 # Use WPA authentication auth_algs=1 # Require clients to know the network name ignore_broadcast_ssid=0 # Use WPA2 wpa=2 # Use a pre-shared key wpa_key_mgmt=WPA-PSK # The network passphrase wpa_passphrase=12345678 # Use AES, instead of TKIP rsn_pairwise=CCMP ``` 修改 /etc/default/hostapd ``` DAEMON_CONF="/etc/hostapd/hostapd.conf" ``` ``` sudo systemctl unmask hostapd sudo systemctl start hostapd ``` 修改 /etc/systemd/system/hostapd.service ``` [Unit] Description=Hostapd IEEE 802.11 Access Point After=sys-subsystem-net-devices-wlan0.device BindsTo=sys-subsystem-net-devices-wlan0.device [Service] Type=forking PIDFile=/var/run/hostapd.pid ExecStart=/usr/sbin/hostapd -B /etc/hostapd/hostapd.conf -P /var/run/hostapd.pid [Install] WantedBy=multi-user.target ``` 在 /etc/rc.local 的 exit 0 之前添加 ``` sudo service hostapd start ``` 重启树莓派,可以看到多了一个名叫 BorderRouter-AP 的 wifi 网络可以加入。 ## 配置 dnsmasq[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#dnsmasq "Permanent link") 修改 /etc/dnsmasq.conf ``` # The Wi-Fi interface configured for static IPv4 addresses interface=wlan0 # Explicitly specify the address to listen on listen-address=192.168.1.2 # Bind to the interface to make sure we aren't sending things elsewhere bind-interfaces # Forward DNS requests to the Google DNS server=119.29.29.29 # Don't forward short names domain-needed # Never forward addresses in non-routed address spaces bogus-priv # Assign IP addresses between 192.168.1.50 and 192.168.1.150 with a 12 hour lease time dhcp-range=192.168.1.50,192.168.1.150,12h ``` 修正 /lib/systemd/system/bind9.service 与 dnsmasq 的冲突 ``` After=network.target dnsmasq.service ``` ## 配置 tayga[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#tayga "Permanent link") 修改 /etc/tayga.conf 关于以下配置项 ``` prefix 64:ff9b::/96 dynamic-pool 192.168.255.0/24 ipv6-addr 2001:db8:1::1 ipv4-addr 192.168.255.1 ``` 启用 tayga ``` sudo systemctl enable tayga ``` 使能网络转发 ``` sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" sudo sh -c "echo 1 > /proc/sys/net/ipv6/conf/all/forwarding" ``` 为了重启后能生效,修改配置文件 /etc/sysctl.conf,更改对应项配置 使能 NAT44 ``` sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE ``` 使能 wlan0 与 eth0 之间的转发 ``` sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT ``` 保存iptables配置 ``` sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" ``` 在 /etc/rc.local 的 exit 前增加 ``` iptables-restore < /etc/iptables.ipv4.nat ``` 重启系统,查看各服务是否正常启动,譬如 ping -6 一个外网 IP ``` ping -6 64:ff9b::*** ``` ## 创建 OpenThread 网络[¶](https://yplam.com/IOT/openthread/raspberry-pi-openthread/#openthread_1 "Permanent link") ``` sudo ot-ctl panid 0xc80b sudo ot-ctl extpanid f37fcf5bc5cbe195 sudo ot-ctl masterkey e35efdce91b5d2ad6ee96350c31e56d3 sudo ot-ctl pskc 7beafb2ea25f3f8ce244b1e92a79a623 sudo ot-ctl networkname MyOT sudo ot-ctl channel 11 sudo ot-ctl ifconfig up sudo ot-ctl thread start sudo ot-ctl state sudo ot-ctl prefix add fd11:22::/64 pasor sudo ot-ctl netdata register ``` 需要注意的是后面两行如果不运行,OpenThread网络中的设备将无法访问外网。 至此,OpenThread网络配置完成,可以通过 ping 外网 ip 进行测试(通过nat64)。