600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 树莓派 Raspberry Pi 3B+ 无线路由器 WEB 服务器(Nginx PHP Sqlite3) UART 串口数据采集

树莓派 Raspberry Pi 3B+ 无线路由器 WEB 服务器(Nginx PHP Sqlite3) UART 串口数据采集

时间:2021-03-18 12:40:36

相关推荐

树莓派 Raspberry Pi 3B+ 无线路由器  WEB 服务器(Nginx PHP Sqlite3) UART 串口数据采集

目标: 测试四核 A53树莓派 Raspberry Pi 3B+ 无线路由器, 轻量级WEB 服务器(Nginx,PHP,Sqlite3),UART 串口数据采集性能

初始配置,部分选项需要sudo reboot 重启后生效

开启 ssh,serial功能,设置时区, 编码等

$ sudo raspi-config

树莓派3加入了蓝牙功能,把/dev/ttyAMA0串口分配给蓝牙了。之前使用J8.8, J8.10这个串口是对应/dev/ttyAMA0的,但是现在重新分配给/dev/ttyS0了;

启用Serial 串口, ttyS0:

$sudovi/boot/config.txt#内容enable_uart=1

更换为国内软件源,以加快软件更新和安装速度:

$sudo vi /etc/apt/sources.list#添加内容deb /raspbian/raspbian/ stretch main contrib non-free rpi

系统更新及软件升级

$ sudo apt-get update$ sudo apt-get upgrade$ sudo apt-get dist-upgrade$ sudo rpi-update

安装输入法

$sudo apt install fcitx

更改时区为中国上海时区

$ date // 检查当前时区$ dpkg-reconfigure tzdata // 选择 Asia & Shanghai

无线路由器软件安装

(hostapd: 软件能使无线网卡工作在软AP(Access Point)模式,即无线路由器,dnsmasq 软件提供DHCP和DNS服务, pppoeconf 网络拔号软件)

$ sudo apt-get install dnsmasq hostapd pppoeconf

raspbian 系统中默认使用 dhcpd 来配置网络,因 wlan0 工作在AP模式,所以需要设为静态IP地址,故在配置文件 /etc/dhcpcd.conf中禁用wlan0.

$sudo vi /etc/dhcpcd.conf#内容#interface eth0#fallback static_eth0denyinterfaces wlan0

在 /etc/network/interfaces 设置网卡的静态IP地址

$sudo vi /etc/network/interfacessource-directory /etc/network/interfaces.dauto loiface lo inet loopbackauto eth0iface eth0 inet staticaddress 192.168.0.250netmask 255.255.255.0gateway 192.168.0.1allow-hotplug wlan0iface wlan0 inet staticaddress 192.168.10.1netmask 255.255.255.0

hostapd 配置文件

$sudo vi /etc/hostapd/hostapd.conf#内容# This is the name of the WiFi interface we configured aboveinterface=wlan0# Use the nl80211 driver with the brcmfmac driverdriver=nl80211# This is the name of the networkssid=RPI_3B_Plus_Router# Use the 2.4GHz bandhw_mode=g# Use channel 6channel=6# Enable 802.11nieee80211n=1# Enable WMMwmm_enabled=1# Enable 40MHz channels with 20ns guard intervalht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]# Accept all MAC addressesmacaddr_acl=0# Use WPA authenticationauth_algs=1# Require clients to know the network nameignore_broadcast_ssid=0# Use WPA2wpa=2# Use a pre-shared keywpa_key_mgmt=WPA-PSK# The network passphrasewpa_passphrase=raspberry# Use AES, instead of TKIPrsn_pairwise=CCMP

hostapd 配置文件启动设置

$sudo vim /etc/default/hostapd// 找到“#DAEMON_CONF=”,去掉#注释,补全为DAEMON_CONF="/etc/hostapd/hostapd.conf"

完成配置后,检查是否配置成功

$sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

如果最后两行出现了如下,则为正常

wlan0: interface state UNINITIALIZED->ENABLE

wlan0: AP-ENABLED

重启hostapd

$sudo service hostapd restart

如启动不成功的处理办法

$sudo systemctl disable dhcpcd$sudo systemctl enable networking$sudo reboot$sudo hostapd -B /etc/hostapd/hostapd.conf #开机后再次启动hostapd服务, 把此命令写入 rc.local 开机时自动启动

$sudo vim /etc/rc.local#新增内容$sudo hostapd -B /etc/hostapd/hostapd.conf #开机时自动启动 hostapd 服务exit 0

dnsmasq 配置

$sudo vim /etc/dnsmasq.conf#内容interface=wlan0 # Use interface wlan0listen-address=192.168.10.1 # Explicitly specify the address to listen onbind-interfaces # Bind to the interface to make sure we aren't sending things elsewhereserver=8.8.8.8 # Forward DNS requests to Google DNSdomain-needed # Don't forward short namesbogus-priv # Never forward addresses in the non-routed address spaces.dhcp-range=192.168.10.100,192.168.10.200,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time

重启 dnsmasq 服务

$sudo service dnsmasq restart

开启Linux内核的IP转发以及使用iptables做NAT表,让无线网卡的数据通过有线网卡转发出去

开启Linux内核的IP转发功能

$sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

开启树莓派有线网卡和无线网卡的转发功能 (手动命令行开启,如不保存,重新开机则会失效)

$sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE$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

保存当前的防火墙策略到配置文件中

$sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

修改系统启动脚本,添加启动任务

$sudo vim /etc/rc.local#新增内容sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"iptables-restore < /etc/iptables.ipv4.natexit 0

dnsmasq经常会出现问题,原因是wlan0还没热启动完成,而dnsmasq先启动了,所以出现了启动失败的现象。

这里的解决方案就是采用延时启动

在 /etc/rc.local 文件中,exit 0 之前加入以下命令

$sudo bash /etc/dnsmasq_delayinit.sh

建立文件 dnsmasq_delayinit.sh

$sudo vim /etc/dnsmasq_delayinit.sh#内容#!/bin/shsleep 10sudo service dnsmasq restart

把 dnsmasq_delayinit.sh 设置成可执行

$sudo chmod +x /etc/dnsmasq_delayinit.sh

查看服务

$service hostapd status$service dnsmasq status

待继续测试和完善....

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。