安装

  1. 下载 Raspberry Pi Imager
  2. 烧录镜像
    1. 对于 Pi Zero W,目前烧录成功的镜像是 bullseye 32 bits:2024-03-12-raspios-bullseye-armhf-lite.img.xz,下载地址: Raspberry Pi - Operating system images
  3. 烧录时可以选择配置用户名密码,新的版本已经不再使用默认的 pi/raspberry 作为默认账密
  4. 烧录时可以配置 WiFi,iPhone 热点可用;配置勾选允许 SSH 登录

USB 连接 SSH

  1. 在文件 config.txt 中最后加上一行 dtoverlay=dwc2,表示开启 DesignWare USB 2.0 driver
  2. 在文件 cmdline.txt, 中的 rootwait 后面加上 modules-load=dwc2,g_ether,进入 USB Ethernet adapter 模式

常见问题

  1. 树莓派无法启动:检测 USB 线的问题,可能换成较粗较短的线后启动成功
  2. 配置了 USB 连接 SSH 后仍然无法连接
    1. USB 线只支持供电不支持传输数据,换条 USB 线后解决
    2. 是否连接在树莓派的 data Micro USB 口上
  3. WiFi 无法连接
    1. iPhone 热点在开启一段时间后可能不再发送广播帧,需要手段开关 iPhone 热点后,然后在树莓派上 sudo wpa_cli -i wlan0 reconfigure

WiFi

扫描 WiFi

sudo apt update
sudo apt install wireless-tools

sudo iwlist wlan0 scan

连接 WiFi

  1. /etc/wpa_supplicant/wpa_supplicant.conf 中加上
network={
    ssid="your_ssid"
    psk="your_password"
}
  1. 重启无线服务
sudo wpa_cli -i wlan0 reconfigure
  1. 检查是否连接成功
ifconfig wlan0
  1. 如有必要,可以手动更新 IP 租约
sudo dhclient -r wlan0
sudo dhclient wlan0

接口图

raspberrypi_3b_plus.jpg

BCM模式:按照下图示中有底色的GPIO编号

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

BOARD模式:按照下图示中内侧的物理针脚编号

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

raspberrypi_pinout.jpg

raspberrypi_zero_pinout.jpg

wpa_supplicant

linux 中的一个守护进程,它在无线网络连接中充当 WPA/WPA2/WPA3 协议的客户端,负责与无线路由器进行安全认证和密钥协商。

Usage

# scan
sudo wpa_cli scan -i wlan0
sudo wpa_cli scan_results -i wlan0

# 查看已配置网络
sudo wpa_cli list_networks -i wlan0

# 禁用网络
sudo wpa_cli disable_network -i wlan0

# 启用网络
sudo wpa_cli enable_network -i wlan0

# 重新连接
sudo wpa_cli reconnect -i wlan0

# 查看状态
sudo wpa_cli status -i wlan0

外设

微雪

ReSpeaker

Install

git clone https://github.com/HinTak/seeed-voicecard.git
cd seeed-voicecard
sudo ./install.sh
sudo reboot now

LED 指示灯

  1. 安装 spidev: pip install spidev

  2. Github - mic_hat 中的 interfaces/apa102.py 安装到 python 库中

# for user pi
cp apa102.py /home/pi/.local/lib/python3.9/site-packages/

# for user root
cp apa102.py /usr/local/lib/python3.9/dist-packages/
  1. 可以在 python 中使用驱动,代码可以参考 Github - mic_hat 中的 interfaces/pixels.py
from apa102 import APA102

driver = APA102(num_led=3)
driver.set_pixel(led_num=0, red=255, green=0, blue=0, bright_percent=100)
driver.set_pixel(1, 0, 255, 0, bright_percent=10)
driver.set_pixel(2, 0, 0, 255, bright_percent=1)
driver.show()

按钮

驱动安装同上,代码参考 interfaces/button.py

import RPi.GPIO as GPIO
BUTTON = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON, GPIO.IN)

state = GPIO.input(BUTTON)

或者使用 gpiozero

from gpiozero import LED, Button

button = Button(17)
def button_pressed():
    print("button pressed")

def button_released():
    print("button released")

def button_held():
    print("button held")

button.when_pressed = button_pressed
button.when_released = button_released
button.when_held = button_held

while True:
    time.sleep(0.1) 

record & playback

get device index: recording_examples/get_device_index.py do record: recording_examples/record.py playback: recording_examples/play.py

Problem:

  1. PyAudio Input Overflowed -9981 - No solution working

wake word

Audio

ALSA

Raspbian comes pre-installed with ALSA (Advanced Linux Sound Architecture), which is the low-level software that makes sound work

PulseAudio

PulseAudio is a software layer that sits on top of ALSA to provide more features, including streaming capabilities

Install

sudo apt-get install pulseaudio pulseaudio-module-bluetooth

Usage

# start
pulseaudio --start

# stop
pulseaudio --kill

Bluetooth

bluetoothctl

# within bluetoothctl
power on
agent on
scan on

pair XX:XX:XX:XX:XX:XX
trust XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX

quit

Players

mpg321

Install

sudo apt install mpg321

Usage

mpg321 kiseki.mp3

omxplayer

Raspberry Pi command line OMX player

Note: omxplayer has been deprecated since 2020; resources are directed at improving [vlc](Note: omxplayer has been deprecated since 2020; resources are directed at improving vlc)

Install

Raspberry bullseye (Debian 11) 安装 omxplayer

  1. 修改/boot/config.txt 文件,注释 dtoverlay=vc4-kms-v3d 这一行

  2. 下载 omxplayer 所需要的依赖库

sudo apt --fix-broken install

git clone https://github.com/hitesh83/pwomxplayer-support.git
cd pwomxplayer-support && sudo chmod +x install.sh && sudo ./install.sh
  1. 下载 omxplayer 的安装包:
wget http://archive.raspberrypi.org/debian/pool/main/o/omxplayer/omxplayer_20190723+gitf543a0d-1+bullseye_armhf.deb
sudo dpkg --install omxplayer_20190723+gitf543a0d-1+bullseye_armhf.deb

Python SDK Usage

from omxplayer.player import OMXPlayer

player = OMXPlayer(file_name)
time.sleep(duration)
player.quit()