MAKE A PI ZERO W SMART USB FLASH DRIVE/把树莓派零做成智能U盘

Built-in wireless on the Pi Zero W opens up a huge number of possibilities for the various USB gadget modes. The Pi Zero W can be configured to spoof different USB device types, such as a keyboard, a webcam, or a smart USB flash drive.

Pi Zero W内置无线功能为各种USB小工具模式提供了大量可能性。Pi Zero W可配置为欺骗不同的USB设备类型,例如键盘,网络摄像头或智能USB闪存驱动器。

At home, many people use a USB flash drive to transfer files to a TV, but it takes time to move the drive to and from the source computer. How about a remotely accessible USB flash drive, permanently located in the TV, where you could transfer files using the wireless connection? Drag and drop, job done!

在家里,很多人使用USB闪存驱动器将文件传输到电视,但是将驱动器移入和移出源计算机需要时间。远程可访问的USB闪存驱动器如何永久位于电视中,您可以使用无线连接传输文件?拖放,完成工作!

STEP-01: The Pi Zero W USB flash drive

STEP-01:Pi Zero W USB闪存盘

On Raspbian Jessie, wireless connections can be made via the networking icon on the right-hand end of the desktop menu bar. Left-clicking the icon will bring up a list of available networks. If you see the scanning message, wait for a moment and it should find your network.

在Raspbian Jessie上,可以通过桌面菜单栏右侧的网络图标进行无线连接。左键单击该图标将显示可用网络列表。如果您看到扫描消息,请稍等片刻它应该找到您的网络。

Click the network that you want to join. If the network is secured, you will see a dialogue box. Enter the password, click OK, and wait for a few seconds. Finally, write down the IP address the Pi has acquired. Hover the mouse over the signal strength icon and the IP address will appear in a tooltip. If you prefer, you can complete this process on another Pi, and move the SD card over to the Pi Zero W when you reach Step 03.

单击要加入的网络。如果网络安全,您将看到一个对话框。输入密码,单击“确定”,然后等待几秒钟。最后,记下Pi获取的IP地址。将鼠标悬停在信号强度图标上,IP地址将显示在工具提示中。如果您愿意,可以在另一个Pi上完成此过程,并在到达步骤03时将SD卡移动到Pi Zero W.

STEP-02: Disable desktop and enable SSH

步骤02:禁用桌面并启用SSH

From this point we don’t need the desktop, because the Pi Zero is going to have one job: to be a file server for the TV. To save CPU cycles, we should disable the automatic boot to desktop. Go to Menu (Raspberry icon at the top-left corner of the screen) Preferences Raspberry Pi Configuration.

从这一点来说,我们不需要桌面,因为Pi Zero将有一个工作:成为电视的文件服务器。为了节省CPU周期,我们应该禁用自动启动到桌面。转到菜单(屏幕左上角的覆盆子图标)首选项Raspberry Pi配置。

On the System tab, find the Boot option. Select ‘To CLI’ (command line interface), and disable the ‘Login as user pi’ checkbox.
On the Interfaces tab, make sure that SSH is enabled. Click OK and choose Yes to reboot. After the reboot, you should see a text login prompt with a flashing cursor.

在“系统”选项卡上,找到“引导”选项。选择“To CLI”(命令行界面),然后禁用“以用户pi身份登录”复选框。
在“接口”选项卡上,确保已启用SSH。单击确定,然后选择是以重新启动。重新启动后,您应该看到带有闪烁光标的文本登录提示。

STEP-03: Switch to remote access

步骤03:切换到远程访问

We recommend using SSH (Secure Shell), because we’ll be doing everything using the command line. SSH allows us to see the Raspberry Pi command prompt in a window on another PC, Mac, smartphone, or tablet.

我们建议使用SSH(Secure Shell),因为我们将使用命令行执行所有操作。SSH允许我们在另一台PC,Mac,智能手机或平板电脑的窗口中查看Raspberry Pi命令提示符。

Go to magpi.cc/2sLqBmM and follow the instructions for the platform you’re using. You’ll need the IP address you wrote down earlier, and the login. The default login for Raspbian is pi, with the password raspberry.

转到magpi.cc/2sLqBmM并按照您正在使用的平台的说明进行操作。您需要先前写下的IP地址和登录信息。Raspbian的默认登录名是pi,密码为raspberry。

When the SSH link is working, you can disconnect any keyboard and screen.

当SSH链接正常工作时,您可以断开任何键盘和屏幕。

STEP-04: Free up some disk space

步骤04:释放一些磁盘空间

When you’ve logged in over SSH, you can free up disk space by removing some programs that we’re not going to need for this project.
The command df -h shows your disk space usage. Look at the Avail column for /dev/root to see how much free space you have. We can claw back about 1GB if we remove LibreOffice and Wolfram.

当您通过SSH登录时,可以通过删除一些我们不需要此项目所需的程序来释放磁盘空间。
命令df -h显示磁盘空间使用情况。查看/ dev / root的Avail列以查看您有多少可用空间。如果我们删除LibreOffice和Wolfram,我们可以回收大约1GB。

Enter the commands below into your SSH client:

在SSH客户端中输入以下命令:

sudo apt-get remove --purge libreoffice* -y

You can safely ignore any errors you see here. They happen because the command will try to remove some parts of LibreOffice that aren’t actually installed.

您可以安全地忽略您在此处看到的任何错误。它们发生是因为该命令将尝试删除实际未安装的LibreOffice的某些部分。

sudo apt-get purge wolfram-engine -y
sudo apt-get clean
sudo apt-get autoremove -y

Now run df -h again and check the Avail column.

现在再次运行df -h并检查Avail列。

STEP-05: Enable the USB driver

步骤05:启用USB驱动程序

Next, we need to enable the USB driver which provides the gadget modes, by editing two configuration files.

接下来,我们需要通过编辑两个配置文件来启用提供小工具模式的USB驱动程序。

sudo nano /boot/config.txt

Scroll to the bottom and append the line below:

滚动到底部并附加以下行:

dtoverlay=dwc2

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。

sudo nano /etc/modules

Append the line below, just after the i2c-dev line:

在i2c-dev行之后添加以下行:

dwc2

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
Now shut down the Pi Zero W with the command:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。
现在使用以下命令关闭Pi Zero W:

sudo halt

STEP-06: Switch to TV power

步骤06:切换到电视电源

On the Pi Zero W, you’ll see two micro USB ports. One is marked ‘USB’ and the other ‘PWR IN’. You can supply power through either port, but the USB port is for data as well. There are two options.

在Pi Zero W上,您将看到两个微型USB端口。一个标记为“USB”,另一个标记为“PWR IN”。您可以通过任一端口供电,但USB端口也可用于数据。有两种选择。

You could plug the TV into the Pi Zero W USB port, not the PWR IN port, using a standard micro USB cable. The cable will both supply power from the TV and make the USB data connection. The disadvantage is that the TV must be switched on to supply power to the Pi. When someone turns the TV off with the remote, the Pi will also lose power, which can corrupt your SD card.

您可以使用标准的micro USB线将电视插入Pi Zero W USB端口,而不是PWR IN端口。电缆既可以从电视供电,也可以进行USB数据连接。缺点是必须接通电视以向Pi供电。当有人用遥控器关闭电视时,Pi也会断电,这可能会损坏您的SD卡。

Alternatively, you can connect a separate, always-on power supply to the PWR IN port, and use a slightly modified micro USB cable to connect the TV to the USB port. The modification is to cut the red wire inside the micro USB cable. This protects the Pi from damage that could be caused by drawing power from two different power sources. The advantage of this method is that the Pi is powered independently from the TV. It will be available on the network even if the TV is off, and there is a reduced risk of sudden power loss and SD card corruption.

或者,您可以将单独的常开电源连接到PWR IN端口,并使用略微修改的micro USB线缆将电视连接到USB端口。修改是切断micro USB线内的红线。这可以保护Pi免受可能由两个不同电源汲取电力造成的损坏。这种方法的优点是Pi独立于电视供电。即使电视关闭,它也可以在网络上使用,并且可以降低突然断电和SD卡损坏的风险。

You might want to test the system with the first option, and then move onto the second when you want a more permanent setup. Don’t forget to cut the red wire if you use the second option.

您可能希望使用第一个选项测试系统,然后在需要更永久的设置时移动到第二个选项。如果使用第二个选项,请不要忘记切断红线。

Connect the Pi Zero W USB port to the TV using your chosen method, power everything up, and log back in over SSH.

使用您选择的方法将Pi Zero W USB端口连接到电视,为所有设备供电,然后通过SSH重新登录。

STEP-07: Create a container file

步骤07:创建容器文件

To enable mass storage device mode, we need to create a large file to act as the storage medium on the SD card. This file will emulate the USB flash drive that the TV sees.

要启用大容量存储设备模式,我们需要创建一个大文件作为SD卡上的存储介质。此文件将模拟电视看到的USB闪存驱动器。

The command below will create an empty 2GB binary file (change the count=2048 parameter if you want a different size). Please note that this will be limited by the available free space on your SD card (check the Avail column in df -h), and it may take a few minutes to complete the setup:

下面的命令将创建一个空的2GB二进制文件(如果您想要不同的大小,请更改count = 2048参数)。请注意,这将受到SD卡上可用空间的限制(请参阅df -h中的Avail列),完成设置可能需要几分钟时间:

sudo dd bs=1M if=/dev/zero of=/piusb.bin count=2048

We now need to format the file as a FAT32 file system so that the TV can understand it. Enter the command below:

我们现在需要将文件格式化为FAT32文件系统,以便电视可以理解它。输入以下命令:

sudo mkdosfs /piusb.bin -F 32 -I

STEP-08: Mount the container file

步骤08:安装容器文件

Now let’s mount the container file locally so we can download some test files. First, create a folder on which we can mount the file system:

现在让我们在本地挂载容器文件,这样我们就可以下载一些测试文件了。首先,创建一个可以挂载文件系统的文件夹:

sudo mkdir /mnt/usb_share

Now let’s add this to fstab, the configuration file that records our available disk partitions:

现在让我们将其添加到fstab,即记录可用磁盘分区的配置文件:

sudo nano /etc/fstab

Append the line below to the end of the file:

将下面的行附加到文件末尾:

/piusb.bin /mnt/usb_share vfat users,umask=000 0 2

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
The line we added to fstab allows the USB file system to be error-checked and mounted automatically at boot time. Instead of rebooting, we can manually reload fstab with the command below:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。
我们添加到fstab的行允许对USB文件系统进行错误检查,并在引导时自动挂载。我们可以使用以下命令手动重新加载fstab,而不是重新启动:

sudo mount -a

STEP-09: Download a test file

步骤09:下载测试文件

Now let’s download a 300MB test file to view on the TV. Big Buck Bunny is a short open-source film, made by the Blender Foundation (www.blender.org), and released under the Creative Commons Attribution License 3.0:

现在让我们下载一个300MB的测试文件,以便在电视上观看。Big Buck Bunny是一部由Blender Foundation(www.blender.org)制作的短片开源电影,并根据知识共享署名许可3.0发布:

cd /mnt/usb_share
wget http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_720p_surround.avi

You’ll see a progress bar move from left to right. When the download is complete, run a command to flush any cached data to the disk:

您会看到进度条从左向右移动。下载完成后,运行命令将任何缓存的数据刷新到磁盘:

sync

STEP-10: Test mass storage device mode

步骤-10:测试大容量存储设备模式

Now comes the moment of truth. Let’s see whether the TV is going to be friends with the Pi Zero W. The command below will enable USB mass storage device mode, and the TV should pop up a dialogue box. If it doesn’t, you may need to use the Input or Source button on the TV remote to select the USB device.

现在是关键时刻。让我们来看看电视是否会成为Pi Zero W的朋友。下面的命令将启用USB大容量存储设备模式,电视应弹出一个对话框。如果没有,您可能需要使用电视遥控器上的输入或源按钮来选择USB设备。

sudo modprobe g_mass_storage file=/piusb.bin stall=0 ro=1

The TV should provide a file browsing interface. Locate the Big Buck Bunny file and hit Play.

电视应提供文件浏览界面。找到Big Buck Bunny文件并点击Play。

Once you’re satisfied that all is well, try a dismount:

一旦你对一切都很好感到满意,试试下马:

sudo modprobe -r g_mass_storage

The correct behaviour here is for the film or browsing interface to disappear from the screen. You may see a message saying that the USB device was disconnected.

这里的正确行为是电影或浏览界面从屏幕上消失。您可能会看到一条消息,指出USB设备已断开连接。

STEP-11: Install and configure Samba

步骤11:安装并配置Samba

The next step is to provide network access to the /mnt/usb_share folder that we created earlier.

下一步是提供对我们之前创建的/ mnt / usb_share文件夹的网络访问。

sudo apt-get update
sudo apt-get install samba winbind -y

When the installation is complete, we need to configure a Samba network share. For simplicity, this will not require a user name or password, as it is already protected by your wireless network security. If you want more security, see wiki.samba.org.

安装完成后,我们需要配置Samba网络共享。为简单起见,这不需要用户名或密码,因为它已受到无线网络安全保护。如果您想要更高的安全性,请参阅wiki.samba.org。

sudo nano /etc/samba/smb.conf

Scroll down to the end of the file and append the lines below:

向下滚动到文件末尾并附加以下行:

[usb]
browseable = yes
path = /mnt/usb_share
guest ok = yes
read only = no
create mask = 777

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
Now restart the Samba service for the changes to take effect:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。
现在重新启动Samba服务以使更改生效:

sudo systemctl restart smbd.service

STEP-12: Access the share from another computer

步骤12:从另一台计算机访问共享

Now we can try to access the share from a Windows PC or a Mac. You’ll need the host name the Raspberry Pi is using. To check this, enter the command below:

现在我们可以尝试从Windows PC或Mac访问共享。您将需要Raspberry Pi正在使用的主机名。要检查此项,请输入以下命令:

cat /etc/hostname

By default this will be raspberrypi.

默认情况下,这将是raspberrypi。

In Windows, you can bring up Explorer (Windows key + E) and type \\raspberrypi into the address bar at the top. The Run dialogue also works (Windows key + R).
On macOS, the Raspberry Pi will show up in the Finder sidebar. Alternatively, from the Finder menu, select Go Connect to server (Apple key + K) and type smb://raspberrypi as the server address.

在Windows中,您可以调出资源管理器(Windows键+ E)并在顶部的地址栏中键入\\ raspberrypi。运行对话框也有效(Windows键+ R)。
在macOS上,Raspberry Pi将显示在Finder侧栏中。或者,从Finder菜单中选择Go Connect to server(Apple key + K)并输入smb:// raspberrypi作为服务器地址。

Depending on your network settings, you may still see a login dialogue. Any user name, including a guest or anonymous login, will work. Once you’re in, you’ll see a share named usb. Open this and test that you have write access, either by creating a new folder or by copying over a file.

根据您的网络设置,您仍可能会看到登录对话框。任何用户名(包括来宾或匿名登录)都可以使用。一旦你进入,你会看到一个名为usb的股票。打开它并测试您是否具有写入权限,方法是创建新文件夹或复制文件。

You can enable mass storage device mode to check that the TV can see your changes, but don’t forget to run the sync command first.

您可以启用大容量存储设备模式来检查电视是否可以看到您的更改,但不要忘记先运行同步命令。

If you want to change the host name, edit the name in /etc/hostname. Make the same change in /etc/hosts, and finally use sudo reboot to apply the changes.

如果要更改主机名,请在/ etc / hostname中编辑名称。在/ etc / hosts中进行相同的更改,最后使用sudo reboot来应用更改。

STEP-13: Automate USB device reconnect

步骤13:自动重置USB设备

In order for the TV to detect any changes we’ve made over the network (for example, file or folder creations and deletions), it needs to be tricked into thinking that the USB device has been removed and reinserted.

为了让电视能够检测到我们通过网络所做的任何更改(例如,文件或文件夹创建和删除),需要考虑将USB设备移除并重新插入。

We can use a Python library called watchdog (magpi.cc/2sLL1fi), which is designed for monitoring file system events and responding to them. Install this with the command below:

我们可以使用名为watchdog(magpi.cc/2sLL1fi)的Python库,它用于监视文件系统事件并对其进行响应。使用以下命令安装:

在lite版的镜像里,默认没有安装 python3所以也没有pip3。在执行pip3前可能需要安装一下:sudo apt-get install python3-pip

sudo pip3 install watchdog

We then need some code to start a timer whenever something changes in the shared folder. The timer is reset to zero every time a new change occurs, and the USB reconnect is only triggered if we see 30 seconds of inactivity after a change. This avoids spamming the TV while we’re copying over multiple files.

然后,我们需要一些代码来在共享文件夹中发生更改时启动计时器。每次发生新的更改时,计时器都会重置为零,并且只有在更改后看到30秒不活动时才会触发USB重新连接。这可以避免在我们复制多个文件时发送垃圾邮件。

We’ve written a program to do this. To download it, type:

我们写了一个程序来做这件事。要下载它,请键入:

cd /usr/local/share
sudo wget http://rpf.io/usbzw -O usb_share.py
sudo chmod +x usb_share.py

STEP-14: Background service

步骤14:后台服务

We need to make this program into a background service, so that it starts automatically at boot time. We can do that by making it into a systemd unit. Enter the commands below:

我们需要将此程序转换为后台服务,以便它在引导时自动启动。我们可以通过将它变成一个系统单元来做到这一点。输入以下命令:

cd /etc/systemd/system
sudo nano usbshare.service

This will start a new blank file. In the new file, enter:

这将启动一个新的空白文件。在新文件中,输入:

[Unit]
Description=USB Share Watchdog

[Service]
Type=simple
ExecStart=/usr/local/share/usb_share.py
Restart=always

[Install]
WantedBy=multi-user.target

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
Now we need to register the service, enable it, and set it running:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。
现在我们需要注册服务,启用它并将其设置为运行:

sudo systemctl daemon-reload
sudo systemctl enable usbshare.service
sudo systemctl start usbshare.service

Whenever you copy files over to the network share, the USB device should automatically reconnect to the TV after 30 seconds of inactivity.

每当您将文件复制到网络共享时,USB设备应在30秒不活动后自动重新连接到电视。

关于xmsg

技术面前人人平等.同时技术也不分高低贵贱.正所谓学无大小,达者为尊.
此条目发表在RPi分类目录,贴了标签。将固定链接加入收藏夹。

发表评论