硬件准备

  • (1) 树莓派3 or 4
  • (2) 128G的SDCARD
  • (3) 16G的SDCARD
  • (4) SD卡读卡器

软件准备

格式化系统SD卡 16G

  • 安装 SD Card Formatter 5.0.1 Setup.exe
  • 插入读卡器,然后打开 “SD Card Formatter” 应用软件,选择对应的SD盘符,选择格式化即可。

烧写系统

  • win32diskimager 打开系统镜像烧写即可

插上网线上电

  • nmap 扫描工具扫描树莓派地址
  • 或者通过路由器查看树莓派地址
  • 举例:(B8-27-EB-ED-78-65 192.168.0.113)
  • 然后通过ssh登录树莓派(默认用户名/密码为pi/raspberry)

由于默认镜像是关闭SSH,打开方法

在烧录后的SD卡根目录下建一个名字为ssh的空文件(没有扩展名)或者文件夹就可以了

修改密码

sudo raspi-config 第一项改密码 第二项设置hostname 比如 pinas 第三项设置无线,或者用后面的方法添加无线 第四项设置时区为上海

更新为国外镜像源

sudo sed -i 's#://raspbian.raspberrypi.org#s://mirrors.ustc.edu.cn/raspbian#g' /etc/apt/sources.list 
sudo sed -i 's#://archive.raspberrypi.org/debian#s://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian#g' /etc/apt/sources.list.d/raspi.list

更新镜像源(https://www.jianshu.com/p/67b9e6ebf8a0)

更新系统

sudo apt-get update
sudo apt-get upgrade
sudo apt install -y vim curl wget git build-essential

其他常用工具vim git等,可以自行选择安装

系统版本

板子的版本(Pi 3B+)

pi@pinas:~ $ gpio readall
 +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+

系统版本

pi@pinas:~ $ cat /etc/rpi-issue
Raspberry Pi reference 2019-09-26
Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, 80d486687ea77d31fc3fc13cf3a2f8b464e129be, stage5

镜像版本

pi@pinas:~ $ vcgencmd version
Sep 24 2019 17:37:19 
Copyright (c) 2012 Broadcom
version 6820edeee4ef3891b95fc01cf02a7abd7ca52f17 (clean) (release) (start)

Linux版本

pi@pinas:~ $ cat /proc/version
Linux version 4.19.75-v7+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1270 SMP Tue Sep 24 18:45:11 BST 2019

树莓派支持 exFAT格式

使用exFAT格式,用于NAS盘

sudo apt-get install exfat-fuse

自动挂载128G外置SD卡,通过USB口挂载上来

fidisk -l
sudo vim /etc/rc.local
增加下面一行
sudo mount -o nonempty /dev/sda1 /home/pi/data

参考 https://elinux.org/RPi_Adding_USB_Drives

设置无线网络

(1) 设置无线 wlan0(编辑 /etc/network/interfaces,不需要了,只需要第二步即可)

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual
#iface eth0 inet static
#address 192.168.20.230
#netmask 255.255.255.0
#gateway 192.168.20.1 

allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

(2) 设置多无线网络密码(编辑wpa_supplicant.conf):

country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1


network={
ssid="111111"
psk="11111111"
priority=4
}


network={
ssid="222222"
psk="2222222222"
priority=4
}

参考:http://www.52pi.net/archives/58

supervisoid 安装和配置

sudo apt-get install supervisor 自动设置为开机启动,无需其他手动配置

vim /etc/supervisor/supervisord.conf

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)
username=xxx
password=xxx 


[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisor/conf.d/*.conf

frp反向穿透远程访问

(1) 树莓派板子supervisoid运行frpc的frp客户端

frpc.ini 配置如下:

[common]
server_addr = x.x.x.x
server_port = 7000

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6023

[minio]
type=http
local_ip = 127.0.0.1
local_port = 9000
custom_domains = minio.xxxxxx.com

[syncthing]
type=http
local_ip = 127.0.0.1
local_port = 8384
custom_domains = syncthing.xxxxxx.com

/etc/supervisor/conf.d/frpc.conf 配置如下:

[program:frpc]
command=/home/pi/frp/frpc -c /home/pi/frp/frpc.ini
stderr_logfile=/var/log/supervisor/frpc.log
stdout_logfile=/var/log/supervisor/frpc.log
#directory=/home/pi/frp
autostart=true  
user=root
autorestart=true
startsecs=20
(2) frp Server端只要云frps服务器端

nohup /home/pi/frp/frpc -c /home/pi/frp/frpc.ini > /home/pi/frp/frp.log 2>&1 &

frps.ini 配置如下:

[common]
log_file = ./frps.log
dashboard_user = xxxxx
dashboard_pwd = xxxx

dashboard_addr = 0.0.0.0
dashboard_port = 7500

vhost_http_port = 9090
vhost_https_port = 9443

bind_addr = 0.0.0.0
bind_port = 7000
bind_udp_port = 7000

[camera]
type=http
custom_domains = camera.xxx.com

[minio]
type=http
custom_domains = minio.xxx.com

[syncthing]
type=http
custom_domains = syncthing.xxx.com

远程通过 ssh -oPort=6022 pi@x.x.x.x 访问树莓派 更加详细的配置见文档 https://github.com/fatedier/frp/blob/master/README_zh.md

树莓派板子supervisoid运行 minio

其类似S3对象存储,非常好用。浏览器和命令行都可以

wget https://dl.min.io/server/minio/release/linux-arm64/minio
chmod +x minio

测试一下
export MINIO_ACCESS_KEY=admin
export MINIO_SECRET_KEY=password
nohup /home/pi/minio/minio server /home/pi/data > /home/pi/minio/minio.log 2>&1 &

后台运行 /etc/supervisor/conf.d/minio.conf 配置如下:

[program:minio]
command= /home/pi/minio/minio server /home/pi/data
stderr_logfile=/var/log/supervisor/minio.log
stdout_logfile=/var/log/supervisor/minio.log
directory=/home/pi/minio
autostart=true
autorestart=true
user=root
startsecs=15
environment=MINIO_ACCESS_KEY="xxx",MINIO_SECRET_KEY="xxx"

synctings

其类似P2P的文件传输,需要安装一个命令行或者客户端才能同步,自动同步 可以将minio 和 synthings一起使用

测试一下

killall -9  syncthing
nohup ./syncthing -home=./config & 
ps -aux | grep syncthing

另外修改config/config.xml中的address 

<address>0.0.0.0:8384</address>
为 0.0.0.0:8384

浏览器访问 ip:8384即可访问,使用比较简单,设置文件夹和登录用户名密码即可

后台运行

/etc/supervisor/conf.d/syncthing.conf 配置如下:

[program:syncthing]
command=sudo /home/pi/syncthing/syncthing -home=/home/pi/syncthing/config
stderr_logfile=/var/log/supervisor/syncthing.log
stdout_logfile=/var/log/supervisor/syncthing.log
directory=/home/pi/syncthing
autostart=true
autorestart=true
user=root
startsecs=16

caddy负载均衡配置

minio.michaelapp.com {
   proxy / minio.xxx.com:9090
   tls xxx@xxx.com
}

syncthing.michaelapp.com {
   proxy / syncthing.xxx.com:9090
   tls xxx@xxx.com
}

QA:遇到的问题

(1) frp会导致服务 [E] [control.go:236] authorization timeout

原因时间区不对:
pi@pinas:~/frp $ date
Thu Apr 16 21:09:50 CST 2020  //CST表示时区对了

切换时区

sudo dpkg-reconfigure tzdata  

选择亚洲上海,即可

(2) syncthing 错误 “scan: readdirent: input/output error”

磁盘出现坏道,使用下面的命令进行修复。

sudo fsck -a /dev/sda1 
sudo reboot

Camera截图和录制视频

从摄像头抓取一个图片
raspistill -t 2000 -o image.png 

录制一段H264视频
raspivid -t 50000 -o video2.h264

参考:https://www.raspberrypi.org/documentation/raspbian/applications/camera.md

视频播放

通过HDMI0播放视频(Pi4B),HDMI0为USB电源口边上的那个HDMI口
sudo apt install omxplayer 
omxplayer --display 0 f35.mp4

参考:https://www.raspberrypi.org/documentation/raspbian/applications/omxplayer.md

查看CPU温度等

vcgencmd measure_temp

参考:https://www.raspberrypi.org/documentation/raspbian/applications/vcgencmd.md