I am trying to build a WiFi USB Stick using a Raspberry Pi Zero W. I want to be able to plug in the RPI to a computer or whatever device able to output data to a USB port and then the specific mounted partition of the RPI will be recognized as a normal USB drive. Then, when a file is saved on this "special" USB drive the file gets automatically uploaded to a server.
I started following the tutorial to build the pi zero w into a smart usb flash drive (link) and then try to inverse the process where an external device saves some information onto the RPi.
Until here I managed but the problem is that the files I saved on the RPi don't appear in the RPi's filesystem unless I reboot the RPi. It seems to be coming from the IO cache however, the
sync function is not helping at all.
This is really problematic as I want to use something like watchdog that would trigger some actions once a new file is detected (basically, upload the new file and then delete it from the device).
In my mind, this is very close to what an Eye-Fi WiFi SD Card does but I am not sure anymore the Raspberry Pi Zero W has the hardware necessary to build such a thing.
Thanks a lot for your time,
Here are the steps I did in details to try to emulate a USB mass storage where I could then trigger specific actions when new files are save to it:
Setup the headless RPi by adding on the
bootmount an empty
sshfile and the needed information for the wifi connection in
Plug the RPi with a USB cable to a Windows computer (not the
PWR_INport on the RPi)
Connect to the RPi using ssh and updating the system:
sudo apt-get updateand
sudo apt-get upgrade.
Enable the USB driver: add
dtoverlay=dwc2to the bottom of
dwc2the bottom of
/etc/modules(In my case
i2c-devwas not in
/etc/modulesas mentioned by the instructions.
Reboot the RPi with
Create a container file with
sudo dd bs=1M if=/dev/zero of=/piusb.bin count=2048and
sudo mkdosfs /piusb.bin -F 32 -I
Mount the container file with
sudo mkdir /mnt/usb_share, adding
/piusb.bin /mnt/usb_share vfat users,umask=000 0 2at the end of
/etc/fstaband mounting it with
sudo mount -a.
Use the mass storage device mode:
sudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=y. (In comparison to the initial instructions I removed
removable=yto allow Windows to write on the USB)
At this point I could see the USB Mass Storage detected on Windows but I couldn't write to it (and Windows displays a warning that the USB Mass Storage may have a problem), then I use the Windows dialog to "Fix" the problem.
At this stage I can read and write on the emulated USB mass storage from my Windows machine. The problem is that when I do
ls -lha /mnt/usb_shareI don't see any modification that I made on the Windows computer. Even unmounting with
sudo modprobe -r g_mass_storageand then remounting it with
sudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=ydoesn't change it. The only way to see the changes in the folder from the terminal (The RPi side) is to reboot the RPi with
I also tried to do some
syncor to flush the cache and the swap using
sync; echo 3 > /proc/sys/vm/drop_cachesand
swapoff -a && swapon -abut it doesn't make a difference.
A quick fix I found is to unmount and remount the partition to force it to write it to the disk. Therefore, I do something like this:
sudo modprobe -r g_mass_storage; sudo umount /mnt/usb_share; sudo mount -a; sudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=y
However, I would definitely like something less extreme to "refresh" the filesystem periodically and then be able to monitor the changes on it. I am open to any suggestions 😛