树莓派 4b 配置 USB 网络连接

树莓派 4b 配置 USB 网络连接

  • 树莓派镜像: 2022-09-22-raspios-bullseye-arm64.img

  • 电脑: windows 10

1 打开树莓派的 USB 网络

  • /boot/config.txt 最后一行添加 dtoverlay=dwc2

例如:

# ....

[all]
dtoverlay=dwc2
  • /boot/cmdline.txtrootwait 后添加 modules-load=dwc2,g_ether

例如:

console=serial0,115200 console=tty1 root=PARTUUID=3d7323dc-02 rootfstype=ext4 fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet splash plymouth.ignore-serial-consoles
  • 重启树莓派
sudo reboot

2 在 windows 上安装 Linux USB Ethernet/RNDIS Gadget 驱动

  • 将树莓派充电的 type-c 口,使用手机数据线连接到电脑上。打开设备管理器,此时 windows 系统将设备识别成 COM 口。
    在这里插入图片描述

2.1 下载驱动

驱动下载链接:

RPI Driver OTG

百度网盘(提取码: e69j)

如果没有安装 VS 或者不用自己编译驱动,建议直接跳到 2.3 小节。

2.2 自己编译驱动(需要安装VS)

  • 编写 linux.inf 文件,内容为
; Based on template INF file found at
;    <https://msdn.microsoft.com/en-us/library/ff570620.aspx>
; which was:
;    Copyright (c) Microsoft Corporation
; and released under the MLPL as found at:
;    <http://msdn.microsoft.com/en-us/cc300389.aspx#MLPL>.
; For use only on Windows operating systems.

[Version]
Signature           = "$Windows NT$"
Class               = Net
ClassGUID           = {4d36e972-e325-11ce-bfc1-08002be10318}
Provider            = %Linux%
DriverVer           = 10/10/2020,6.1.6000.16384
CatalogFile			= linux.cat

[Manufacturer]
%Linux%             = LinuxDevices,NTx86,NTamd64,NTia64

; Decoration for x86 architecture
[LinuxDevices.NTx86]
%LinuxDevice%       = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00

; Decoration for x64 architecture
[LinuxDevices.NTamd64]
%LinuxDevice%       = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00

; Decoration for ia64 architecture
[LinuxDevices.NTia64]
%LinuxDevice%       = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00

;@@@ This is the common setting for setup
[ControlFlags]
ExcludeFromSelect=*

; DDInstall section
; References the in-build Netrndis.inf
[RNDIS.NT.5.1]
Characteristics     = 0x84   ; NCF_PHYSICAL + NCF_HAS_UI
BusType             = 15
; NEVER REMOVE THE FOLLOWING REFERENCE FOR NETRNDIS.INF
include             = netrndis.inf
needs               = Usb_Rndis.ndi
AddReg              = Rndis_AddReg_Vista

; DDInstal.Services section
[RNDIS.NT.5.1.Services]
include             = netrndis.inf
needs               = Usb_Rndis.ndi.Services

; Optional registry settings. You can modify as needed.
[RNDIS_AddReg_Vista]
HKR, NDI\params\VistaProperty, ParamDesc,  0, %Vista_Property%
HKR, NDI\params\VistaProperty, type,       0, "edit"
HKR, NDI\params\VistaProperty, LimitText,  0, "12"
HKR, NDI\params\VistaProperty, UpperCase,  0, "1"
HKR, NDI\params\VistaProperty, default,    0, " "
HKR, NDI\params\VistaProperty, optional,   0, "1"

; No sys copyfiles - the sys files are already in-build
; (part of the operating system).
; We do not support XP SP1-, 2003 SP1-, ME, 9x.

[Strings]
Linux                 = "Linux Developer Community"
LinuxDevice           = "Linux USB Ethernet/RNDIS Gadget"
Vista_Property        = "Optional Vista Property"
  • 编写 DriverHelper.ps1 文件,内容为
# script parameters
param(
    [ValidateSet("Sign", "UntrustCertificates")]
    [string] $Mode = "Sign",

    [string] $InfFile,
    [string] $CommonName = "linux.local",
    [switch] $Force
)

$ErrorActionPreference = "Stop"

# check if this script is run as administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
	Write-Error "You are not an Administrator. This script needs to be run by an Adminsitrator!"
}

# check if InfFile exists
if(-NOT (Test-Path -Path $InfFile))
{
    Write-Error "$InfFile not found!"
}

# utility functions
function Step
{
    param([string] $Desc, [System.Management.Automation.ScriptBlock] $Code)

    Write-Output "[*] $Desc";
    Invoke-Command -ScriptBlock $Code
}

function Get-CertificateThumbprint
{
    param([string] $FilePath)

    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
    $cert.Import($(Resolve-Path $FilePath))

    return $cert.Thumbprint
}


# Get Kits root
try {
    $InstalledRoots = Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots"
    $WinKits = $InstalledRoots.KitsRoot10
    $ToolsPrefix = "$($WinKits)\bin\10.0.18362.0\x64"
} catch {
    Write-Error "Could not locate the Windows Driver Kit. Head to https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk and download the WDK"
}

function RunKit
{
    param([Parameter(Mandatory=$true)][string] $ToolName, [string[]] $Arguments)

    Write-Output "Running $($ToolsPrefix)\$($ToolName)"
    $Proc = Start-Process -FilePath "$($ToolsPrefix)\$($ToolName)" -ArgumentList $Arguments -NoNewWindow -PassThru -Wait

    if ($Proc.ExitCode -NE 0)
    {
        Write-Error "Process failed with exit code: $($Proc.ExitCode)"
    }
}

# Script vars
$CatFile = [io.path]::GetFileNameWithoutExtension($InfFile) + ".cat"
$CertFile = [io.path]::GetFileNameWithoutExtension($InfFile) + ".cer"

# Scripts
function DoSign
{
    # verify that the Inf file contains a "Catalog" entry
    if (-NOT (Select-String -Path $InfFile -Pattern "CatalogFile" -CaseSensitive -SimpleMatch -Quiet))
    {
        Write-Error "Could not find the 'CatalogFile' entry inside the INF file! Make sure it points to a certificate with the same Name."
    }

    Step -Desc "Create Catalog file" -Code {
        New-FileCatalog -Path $InfFile -CatalogFilePath $CatFile -CatalogVersion 2.0
    }

    if (-NOT (Test-Path -Path $CertFile) -OR $Force)
    {
        Step -Desc "Create a Test Certificate" -Code {
            # see https://docs.microsoft.com/en-us/windows-hardware/drivers/install/creating-test-certificates
            # This will create a self-signed certificate and store it in the Personal certificate stora on the local machine.
            RunKit -ToolName "makecert.exe" -Arguments "-r -sr LocalMachine -ss My -pe -n CN=$($CommonName) -eku 1.3.6.1.5.5.7.3.3 $CertFile"
        }

        Step -Desc "Import the Test Certificate into the Local Computer Trusted Root CAs and Trusted Publishers stores" -Code {
            Import-Certificate -FilePath $CertFile -CertStoreLocation Cert:\LocalMachine\Root
            Import-Certificate -FilePath $CertFile -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
        }
    }
    else
    {
        Write-Output "Certificate already exists. Using $CertFile. If you still want to create a new one, use the -Force option"
    }

    Step -Desc "Sign the Catalog with the Test Certificate" -Code {
        # see https://docs.microsoft.com/en-us/windows-hardware/drivers/install/test-signing-a-catalog-file
        RunKit -ToolName "signtool.exe" -Arguments "sign /v /sm /s My /n $($CommonName) /t http://timestamp.digicert.com $($CatFile)"
    }

    Write-Output "Successfully singed the driver and added the certificate to the trusted store."
}

function DoUntrustCertificates
{
    Step -Desc "Remove the Test Certificate from the Local Computer Trusted Root CAs and Trusted Publishers stores" -Code {
        $thumbprint = Get-CertificateThumbprint -FilePath $CertFile

        Remove-Item -Path ("Cert:\LocalMachine\My\$($thumbprint)")
        Remove-Item -Path ("Cert:\LocalMachine\Root\$($thumbprint)")
        Remove-Item -Path ("Cert:\LocalMachine\TrustedPublisher\$($thumbprint)")
    }

    Write-Output "Sucessfully removed all certificates from the trusted certificate stores."
}

# entry
Write-Output "Selected Mode: $Mode"

Switch($Mode)
{
    "Sign" { DoSign }
    "UntrustCertificates" { DoUntrustCertificates }
}
  • 以管理员的身份打开 PowerShell,切换到当前目录,运行 .\DriverHelper.ps1 -Mode Sign -InfFile .\linux.inf 命令,得到 linux.catlinux.cer 文件。
    在这里插入图片描述

2.3 安装驱动

  • 打开设备管理器,选择中树莓派的 COM 口,选择更新驱动程序。

在这里插入图片描述

  • 选择 游览我的电脑以查找驱动程序

在这里插入图片描述

  • 目录调整到创建前面文件所在目录,点击下一页

在这里插入图片描述

  • 安装完成

在这里插入图片描述

  • 此时任务管理器驱动显示正常

在这里插入图片描述

3 在树莓派上安装并打开 DHCP 服务器

  • /etc/modules 末尾添加 libcomposite

例如:

i2c-dev
libcomposite
  • /etc/dhcpcd.conf 末尾添加 denyinterfaces usb0

例如:

#....
# fallback to static profile on eth0
#interface eth0
#fallback static_eth0
denyinterfaces usb0
  • 安装 dnsmasq
sudo apt update
sudo apt install dnsmasq
  • 创建 /etc/dnsmasq.d/usb 文件,内容为
interface=usb0
dhcp-range=10.55.0.2,10.55.0.254,255.255.255.0,12h
dhcp-option=3
leasefile-ro

其中 dhcp-range 设置表示 DHCP 服务分配的 IP 地址范围从 10.55.0.2-10.55.0.254,子网掩码为 255.255.255.0,有效期为 12 小时。

  • 创建 /etc/network/interfaces.d/usb0 文件,内容为
auto usb0
allow-hotplug usb0
iface usb0 inet static
  address 10.55.0.1
  netmask 255.255.255.0

其中,address 表示 USB 接口的 IP 地址为 10.55.0.1,子网掩码为 255.255.255.0。

  • 创建 /root/usb.sh ,内容为
#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p pi4
cd pi4
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
echo 0xEF > bDeviceClass
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol
mkdir -p strings/0x409
echo "fedcba9876543211" > strings/0x409/serialnumber
echo "Ben Hardill" > strings/0x409/manufacturer
echo "PI4 USB Device" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
# Add functions here
# see gadget configurations below
# End functions
mkdir -p functions/ecm.usb0
HOST="00:dc:c8:f7:75:14" # "HostPC"
SELF="00:dd:dc:eb:6d:a1" # "BadUSB"
echo $HOST > functions/ecm.usb0/host_addr
echo $SELF > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1/
udevadm settle -t 5 || :
ls /sys/class/udc > UDC
ifup usb0
service dnsmasq restart
  • 给脚本添加可执行权限
sudo chmod +x /root/usb.sh
  • 添加脚本开机启动,在 /etc/rc.localexit 0 上一行添加 /root/usb.sh

例如

_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/root/usb.sh

exit 0
  • 重启树莓派,在树莓派上看到 USB 接口的 IP 地址。

在这里插入图片描述

  • 在 windows 电脑上查看到树莓派 type-c 接口分配的 IP 地址。

在这里插入图片描述

此时树莓派 IP 地址为 10.55.0.1,window 电脑的 IP 地址为 10.55.0.2。

4 测试连接

  • 用 windows 电脑 ping 树莓派。

在这里插入图片描述

  • 如果树莓派上打开了 ssh,则可以使用 ssh 连接。

在这里插入图片描述

  • 如果树莓派上打开了VNC,也可以用 VNC 连接

在这里插入图片描述

参考链接

Ethernet Gadget
Pi4 USB-C Gadget
Fix USB connection from Raspberry Pi on Windows

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在树莓派4B上驱动USB摄像头,你可以按照以下步骤进行操作: 1. 首先,使用命令`lsusb`查看已连接的USB设备,确保摄像头被正确识别。 2. 安装Motion软件,可以使用以下命令进行安装:`sudo apt-get install motion`。安装完成后,需要修改Motion的配置文件。 3. 修改Motion的配置文件motion.conf,可以使用以下命令进行编辑:`sudo nano /etc/motion/motion.conf`。在文件中找到并修改以下参数: - 修改分辨率:将capture_width和capture_height设置为所需的分辨率,例如640 x 480。 - 修改访问地址和端口号:将stream_localhost设置为off,将stream_port设置为8081。 - 可选:如果你遇到摄像头卡顿的问题,可以尝试调整framerate参数来提高帧率。 4. 保存并关闭文件,然后重新启动Motion服务:`sudo systemctl restart motion`。 5. 接下来,你可以通过访问`http://[树莓派地址]:8081`来查看实时监控视频。默认的用户名是admin,密码为空。 6. 如果你希望将摄像头接入到Home Assistant中,你需要修改Home Assistant的配置文件configuration.yaml,添加以下内容: ``` camera: - platform: generic name: 摄像头 still_image_url: http://{树莓派地址}:8081/picture/1/current/ username: admin framerate: 30 binary_sensor: - platform: ffmpeg_motion input: http://{树莓派地址}:8081/ name: 移动侦测 ``` 保存并关闭文件,然后重新启动Home Assistant。 这样,你就可以通过树莓派4B驱动USB摄像头并进行监控了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值