作者
QQ群:852283276
微信:arm80x86
微信公众号:青儿创客基地
B站:主页 https://space.bilibili.com/208826118
参考
官方文档
官方Github
Github raspberrypi/documentation
树莓派(Raspberry pi) 使用Pi Imager安装烧录操作系统
树莓派配置wifi的几种方法.md
通过SD卡为树莓派配置WIFI
树莓派 Raspberry Pi 更换国内源
极客DIY,树莓派远程开发51单片机~
树莓派仪表盘 Pi Dashboard v1.1 更新
树莓派搭建openbmc
树莓派的监控系统 RPi-Monitor
树莓派中搭建ELK监控平台(可打造物联网监控平台、魔镜系统)
树莓派防火墙配置
树莓派-时区设置
树莓派自动校准时间
官方文档既可以在官网查看,也可以在Github上看,我觉得Github上看更方便。
硬件
参考raspberry-pi-zero-2-w-reduced-schematics.pdf
,40PIN排针信号定义,包括SPI和Uart接口,3V3在板子上的焊盘是正方形的,
树莓派zero-2-w尺寸参考文档raspberry-pi-zero-2-w-mechanical-drawing.pdf
,
安装
- 下载操作系统,官网提供三种类型,其中lite最小,不带桌面,下载速度也快,full的下载速度很慢,很容易下载失败。
- 烧录固件,下载Raspberry Pi Imager。
SSH
由于使用的是Lite系统,没有桌面,只能通过SSH,在SD的boot分区新建一个文件名为ssh
的文件,用网线把树莓派连接到路由器LAN口,扫描IP,默认的用户名是pi,默认的密码是raspberry,
WIFI
官方配置网络的文档
树莓派Pi Zero W无线WIFI终端配置
电脑连接树莓派Zero W
在树莓派3B上,安装Lite系统,选用SD连接最方便,在boot分区添加文件wpa_supplicant.conf
,
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={ssid="Redmi K30 Pro"psk="zhuce1994"
}
如果设备支持mDNS,用下面命令查看树莓派IP,或者扫描即可,
$ ping raspberrypi.local
防火墙
$ sudo apt install ufw
$ sudo ufw enable
$ sudo ufw disable
$ sudo ufw status
# 使能SSH
$ sudo ufw allow 22/tcp
$ sudo ufw delete allow 22/tcp
$ sudo ufw allow 10000:11000/tcp
$ sudo ufw delete allow 10000:11000/tcp
$ sudo ufw allow from 192.168.2.127
$ sudo ufw delete allow from 192.168.2.127
# NTP
$ sudo ufw allow from any to any port 123 proto udp
日期与时区
树莓派日期正确,但是时间差8个小时,
sudo raspi-config
# Localisation Options > Timezone > Asia > Shanghai
设置时间,
$ sudo date --s="2022-05-13 10:30:00"
NTP
$ sudo apt install ntp
$ sntp --version
sntp 4.2.8p15@1.3728-o Wed Sep 23 11:46:38 UTC 2020 (1)
$ sudo systemctl start ntp
$ sudo systemctl enable ntp# /etc/ntp.conf
server ntp.fudan.edu.cn iburst perfer
server time.asia.apple.com iburst
server asia.pool.ntp.org iburst
server ntp.nict.jp iburst
server time.nist.gov iburst# 报这个错好像不影响使用 kernel reports TIME_ERROR: 0x41: Clock Unsynchronized
$ sudo systemctl status ntp
● ntp.service - Network Time ServiceLoaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)Active: active (running) since Fri 2022-05-13 10:22:24 CST; 4min 26s agoDocs: man:ntpd(8)Process: 17250 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)Main PID: 17256 (ntpd)Tasks: 2 (limit: 409)CPU: 196msCGroup: /system.slice/ntp.service└─17256 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 115:124May 13 10:22:24 raspberrypi ntpd[17256]: Listen and drop on 0 v6wildcard [::]:123
May 13 10:22:24 raspberrypi ntpd[17256]: Listen and drop on 1 v4wildcard 0.0.0.0:123
May 13 10:22:24 raspberrypi ntpd[17256]: Listen normally on 2 lo 127.0.0.1:123
May 13 10:22:24 raspberrypi ntpd[17256]: Listen normally on 3 wlan0 192.168.2.121:123
May 13 10:22:24 raspberrypi ntpd[17256]: Listen normally on 4 lo [::1]:123
May 13 10:22:24 raspberrypi ntpd[17256]: Listen normally on 5 wlan0 [fe80::27ff:f899:cd22:c651%2]:123
May 13 10:22:24 raspberrypi systemd[1]: Started Network Time Service.
May 13 10:22:24 raspberrypi ntpd[17256]: Listening on routing socket on fd #22 for interface updates
May 13 10:22:24 raspberrypi ntpd[17256]: kernel reports TIME_ERROR: 0x41: Clock Unsynchronized
May 13 10:22:24 raspberrypi ntpd[17256]: kernel reports TIME_ERROR: 0x41: Clock Unsynchronized
# 配置防火墙端口
$ sudo ufw allow 123/udp
Rules updated
Rules updated (v6)
# 使用NTP更新时间
$ sudo ntpd -s -d
# System clock synchronized: yes
$ timedatectlLocal time: Fri 2022-05-13 10:29:38 CSTUniversal time: Fri 2022-05-13 02:29:38 UTCRTC time: n/aTime zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yesNTP service: n/aRTC in local TZ: no
或,
sudo apt-get install ntpdate
sudo dpkg-reconfigure tzdata
sudo ntpdate cn.pool.ntp.org
换源
阿里源,注意你的操作系统版本,实测截至20210104,阿里源不能用,换清华的就可以了。
$ sudo nano /etc/apt/sources.list
deb http://mirrors.aliyun.com/raspbian/raspbian/ bullseye main non-free contrib
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ bullseye main non-free contribdeb http://mirrors.aliyun.com/raspbian/raspbian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ buster main non-free contribdeb http://mirrors.aliyun.com/raspbian/raspbian/ wheezy main non-free contrib
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ wheezy main non-free contrib
# 清华源
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib
$ sudo apt update
内核
树莓派内核都在官方Github上,raspberrypi/linux。
Pi Zero 2 W
Raspberry Pi Zero 2 W硬件资料
串口配置
学习笔记-Raspberry Pi Zero W-4:串口(UART)的配置和使用
编辑sudo nano /boot/config.txt
,
enable_uart=1
编辑sudo nano /boot/cmdline.txt
,添加后成砖头了,
earlycon=uart8250,mmio32,0x20215040
默认的用户名是pi
,默认的密码是raspberry
,开始配置sudo raspi-config
,
System Options > Wireless LAN
手动输入wifi名和密码Interface Options
使能SSH/SPI/I2CLocalisation Options > WLAN Country
选CN
重启后,ifconfig查看无线网卡IP,随后可以用SSH登录,后面就不需要串口了,
pi@raspberrypi:~$ ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Local Loopback)RX packets 22 bytes 2368 (2.3 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 22 bytes 2368 (2.3 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.2.152 netmask 255.255.255.0 broadcast 192.168.2.255inet6 fe80::2c7:ddb0:caef:9942 prefixlen 64 scopeid 0x20<link>ether e4:5f:01:4a:01:b7 txqueuelen 1000 (Ethernet)RX packets 11 bytes 1586 (1.5 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 47 bytes 6819 (6.6 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
关闭串口终端,用作通信,
- Start raspi-config: sudo raspi-config.
- Select option 3 - Interface Options.
- Select option P6 - Serial Port.
- At the prompt Would you like a login shell to be accessible over serial? answer ‘No’
- At the prompt Would you like the serial port hardware to be enabled? answer ‘Yes’
- Exit raspi-config and reboot the Pi for changes to take effect.