"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Saturday, 3 March 2018

Sharing files on the Raspberry Pi Zero W


I eventually managed to find some time to continue working to my Pi-zero camera project. During initial tests I took advantage of Pi-Zero wireless interface and used “sftp" command in order to transfer files between Raspberry and desktop computer. A more suitable way of transferring files using USB, like real digital cameras, would be of course advisable.

Raspberry Pi Zero as mass storage

The Pi Zero USB port is directly connected to processor, unlike others Raspberrys using a on-board USB Hub. This, cheaper, solution means the Pi Zero can be configured to act as USB host, like a computer, or as a “USB gadget” like all cell-phones and digital cameras do. In order to activate such “USB gadget” mode some specific “dcw2” kernel modules must be enabled, in my specfic case the needed module is called “g_mass_storage”. I followed this simple guide from a Github user.
First I enabled DCW2 USB driver
echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txtecho "dwc2" | sudo tee -a /etc/modules

then I created a virtual disk image to be mounted as mass storage and formatted if as a FAT32 disk.
sudo dd if=/dev/zero of=/usb-drive.img bs=1M count=1000sudo mkdosfs -F 32 /usb-drive.img
Once the Raspberry PI is conncted to a computer the mass storage can be enabledl ike this
sudo modprobe g_mass_storage file=/usb-drive.img stall=0 removable=1
and disabled with following modprobe command
sudo modprobe -r g_mass_storage
the same virtual disk can be availabe to Raspberri PI by mounting it
sudo mkdir /media/usb-drivesudo mount -o loop,rw /usb-drive.img /media/usb-drive/
of course the same virtual disk cannot be both available as mass storage drive and mounted on Raspberry at the same time, since it could lead to disk corruption. The camera software will have so to manage the switch between two modes.


Automatically mount a removable USB disk


As alternative to using Raspberry Pi Zero as mass storage device a USB disk, or a SD card, could be used to store produced media. Advantage of such solution is the increased available disk space available by swapping the removable drives. On the other side this solution needs some adapter like a USB-OTG cable or a Pi Zero specific USB Hub or card reader.

The “pmount” command is the key to automatically mount USB drives once are connected, I configured it by following these instructions from another Github user.
I so installed pmount
sudo apt-get install pmount
then I configured the udev rule file
sudo vi /etc/udec/rules.d/usbstick.rules
with following content
ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"
and the systemd service file
vi /lib/systemd/system/usbstick-handler@.service
like this
[Unit] 
Description=Mount USB sticks 
BindsTo=dev-%i.deviceAfter=dev-%i.device 
 
[Service] 
Type=oneshot 
RemainAfterExit=yes 
ExecStart=/usr/bin/pmount --umask 000 --noatime -r --sync %I usbstick 
ExecStop=/usr/bin/pumount /dev/%I


No comments :

Post a Comment