Why
So my car records the last 60 minutes from multiple cameras to a USB stick if you plug one in. I want to do something more long term like take a snapshot every 5 mins when driving. Sowhen COVID eventually goes away I can get a time lapse for each road trip I do. I’ll probably have it generate automatically, but needing to have a sanity you may publish check from me.
Hardware
I was doing some reading and I came across the TeslaUSB project on reading about this I discovered that the Pi Zero and Pi 4 can be used as USB gadgets. (It’s all to do with how the USB is set up on the boards that only these options will work). So I got a Raspberry Pi 4 and m.2 SSD drive and an enclosure for it.
Software
First I put Raspberry Pi OS Lite onto my Pi my sdcard then added the ssh flag file so I could workout it’s IP and connect to it over a wired network connection, from that connection I set the Pi up for the home wifi and then gave that a static IP address in the router’s interface.
Next I added my m.2 USB enclosure to the Pi and got that mounted as /data then added to fstab as I always want my data drive mounted.
Loop devices
Damn I learnt a lot about how not to do it here. To create a file system in a file is really easy, but to get it so both Windows and Linux accept the same file system is where I really struggled.
sudo fallocate -l 100GB /data/piusb.bin
sudo mkdosfs /data/piusb.bin -F 32 -I
From fstab I have this mounted readonly so I can get my data off to process on a read write file system without confusing the car as it should have sole access to a USB stick that is plugged into it.
/data/piusb.bin /mnt/usb_share vfat auto,user,ro,umask=000,offset=1048576 0 0
We present the same file to the USB gadget in rc.local (lazy start up script)
/bin/sleep 5
/sbin/modprobe g_mass_storage file=/data/piusb.bin stall=0
As the car is expecting a device with partition table and partition we don’t need to worry about the offset for that, but without working out what the offset should be I couldn’t see the data on the read only filesystem so couldn’t copy it off.
pi@raspberrypi:~ $ sudo fdisk -lu /data/piusb.bin
Disk /data/piusb.bin: 32 GiB, 34359738368 bytes, 67108864 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xfe02295c
Device Boot Start End Sectors Size Id Type
/data/piusb.bin1 2048 67108863 67106816 32G c W95 FAT32 (LBA)
And 2048 x 512 = 1,048,576 which is the number you might recognise from the fstab line offset.
I did consider pulling the video off to process on a device not in the car, but I’m not going to add a access point just for the car and transfers happen at about 300 KB/s so I think it makes more sense to make my time lapse images in the car then upload the images for rejoining to a video outside the car.
What else could I do with this
Some equipment writes it’s log or image files to USB. By using the same approach (probably with a better wifi connection that in a glovebox in a metal cage of a car). It would be possible to get data off this kind of device and onto machines able to process it faster.