Sentry Saver
Sentry Saver is a gadget that manages the video clips saved in a USB drive by a Tesla. You can build one yourself by following the DIY instructions below.
If you have a Tesla, then skip this paragraph. Tesla vehicles have cameras that record video of the surroundings for later viewing. The front, back, left & right views can be saved to a USB drive. There are three types of recordings: Recent driving clips, Sentry events, and Saved events. Recent driving clips are useful if there was an incident. Sentry clips can help identify a potential miscreant by recording near-by activity while the car is parked. The recordings are stored as one minute clips. Though this capability is cool, there are several problems in using it effectively, like:
1. Sentry Mode tends to generate a lot of false alarms. It can trigger when someone innocently walks past the car. This generates a lot of video clips. If there is activity on any one camera, the other three views are also saved, even though they may show nothing useful.
2. Reviewing video is quite tedious. Since most of the clips are false alerts, watching the videos consumes a lot of time, even when playback is sped up and you are watching all 4 views simultaneously.
3. The video files are large and can easily fill up the USB drive. Recording may stop when the drive is full. The typical user would plug in a USB device and go for weeks without reviewing the old footage. This could lead to an unfortunate situation where an interesting event does not get recorded because the drive is full.
4. Lastly, while it is not really difficult, setting up the simple USB drive for both music and video requires a bit of effort and is not easy for some people.
The goal is to build a "smart" USB drive. The drive would appear to be a traditional USB stick to the Tesla, but it would actually be a computer that can:
1. Track the space available and delete old clips as needed, even if they have not been reviewed. This ensures that there will always be space for new videos. Clips will be deleted by age and also by the type (Recent, Sentry and Saved).
2. The device will have a built in WiFi capability, and can upload clips to a larger server when in its WiFi range. Typically, when the car returns home in the evening, it would automatically connect to a server with larger storage capacity and move the clips there. Clips that have been saved will be deleted from the USB space. Though clips can be viewed on the Tesla console screen, a PC based app would be much more capable and flexible, perhaps using SentryKeeper.
3. The device could constantly scan through clips on the USB drive and remove frames and clips where there is no interesting motion. This will help save space, but more importantly, it will save the user a lot of time while reviewing clips. This is the main goal. The processing may continue while the car is parked if power is available.
There are even more features, like:
4. The device appears as two USB drives, one to store video clips and the other to store music.
5. The device has SMB drivers that will allow the user to conveniently manage the files (both video and music) over WiFi, without needing to unplug the USB device. The web server will be reachable via the WiFi link when the car is parked nearby. The device can also be reached via WiFi using ssh.
A prototype has been built using a Raspberry Pi Zero 2 W and the schematic is shown below. It does not involve any hardware mods. The BOM cost for a 128GB device comes to about $50. It is powered by the Tesla USB connector. Tesla's power is available for about 10 mins after it locks itself. However, the power can be turned back on any time for another 10 mins by sending it a wake up API call. The "home" computer can have terra bytes of storage to hold interesting saved clips and clips can be more conveniently reviewed from here using the SentryKeeper viewer.
· Sentry Keeper, a nice way to view videos: https://www.allprosoftware.com/sentrykeeper/
· Other instructions for Pi based devices:
o https://magpi.raspberrypi.org/articles/pi-zero-w-smart-usb-flash-drive
o https://desertbot.io/blog/headless-pi-zero-w-wifi-setup-windows
o https://github.com/marcone/teslausb (This reference describes a very sophisticated approach, but there is not much explanation)
Item & URL |
Approx cost |
$16 |
|
$20 |
|
USB A male to Micro male (Tesla to Pi) |
$5 |
$7 |
|
SD card adapter (for setup only) |
|
Mini HDMI to HDMI (optional, for setup only) |
|
1. Make a SD card:
· Insert the SD card into your PC, you may need a Memory Card Adapter. I suggest 128GB.
· Verify that there is only one partition on the SD card. Samsung SD cards may come with 2 partitions.
o On a PC, run the "Disk Management" app. (On WIn 10, search for "Create and Format Disk Partitions")
o The SD card will show up in the list of volumes.
o If you see more than 1 partition, delete them all and recreate one
· Verify that the new partition is exFAT or FAT32
2. Install Raspbian Lite OS on the SD card:
· There are several ways to do this, but the simplest may be to install the "Raspberry Pi Installer" on your PC.
o Choose OS -> Raspberry Pi OS (other) -> Raspberry Pi OS Lite
§ This avoids the GUI packages and supports only a shell interface
o Choose Storage -> Select your SD card
o Click on Write. Takes about 10 mins, depending on the speed of your SD card
· Alternatively, download zip file: https://www.raspberrypi.org/downloads/raspbian/ Pick the LITE version
o Use Balena Etcher to flash it.
o OR unzip and extract img file & use Win32 Disk Imager (Run as Admin) to image SD card (15 mins)
3. Enable SSH on Pi
· After installing the Pi OS, pull out the SD card from the PC and push it back in. This will allow you to view the data on the SD card.
· Use the Windows File Explorer to browse the root dir of SD card.
o create a non-zero length file called 'ssh'
o There should be no extension for the file.
4. Enable WiFi
· Create file called 'wpa_supplicant.conf' in root dir of SD card with the following contents.
· Note: after the Pi is set up, this file is copied to /etc/wpa_supplicant/wpa_supplicant.conf
· Contents:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="yourWiFiName"
psk="yourWiFiPassword"
}
5. Power up the Pi
· Eject the SD safely from your PC
· Install SD in Raspberry Pi Zero W.
· Power up the Pi.
o Use the Pi's USB port (not the socket marked PWR that is closer to the edge). Connect it to a PC's USB port. This provides power to the Pi and also connects the USB data path.
o It may take a few mins to boot (first time)
o If you had also plugged in a HDMI connector to a monitor, you would see a whole bunch of messages
o The Pi will connect to the WiFi network specified in step 4
6. Find its IP address on the WiFi LAN
· If you had plugged in a monitor using the HDMI connector, you would see a message like:
"My IP address is 192.168.x.x"
The IP address would be DHCP assigned by your WiFi router.
· Alternatively, install and use a Ping scanner on your PC or mobile phone to find the address. There are several free tools available, like: Advanced IP Scanner. It should display the hostname as 'raspberrypi'.
· Alternatively, log in to your WiFi router and look for a new device on the LAN. It should display the hostname as 'raspberrypi'.
7. Install a remote terminal app, like PuTTY.
· On a Windows PC, you could download and install PuTTY (or alternative like Bitvise)
o On an Android, try JuiceSSH
· Alternatively, plug in a monitor and keyboard into the Pi
o The monitor requires a Mini HDMI connector, which may be hard to find
o The keyboard requires a micro USB connector.
o No mouse is required for a headless Pi
8. Connect to the IP address using PuTTY
· Login using the default credentials: User: pi, Password: raspberry
· For me, the connection between the PC and Pi breaks frequently, perhaps because of ARP timeouts. A simple fix is to open a Command shell on the PC and ping the Pi continuously.
C:\$ ping -t 192.168.x.x
9. Do some basic housekeeping on the Pi using the cmd shell:
pi@raspberrypi:~ $ sudo raspi-config
· Change the password: System Options -> password or
pi@raspberrypi:~ $ passwd pi
· Set the time zone: Localization Options -> time zone, or:
pi@raspberrypi:~ $ timedatectl
pi@raspberrypi:~ $ timedatectl list-timezones | grep America # pick one
pi@raspberrypi:~ $ sudo timedatectl set-timezone America/New_York
10. Get OS updates over WiFi
pi@raspberrypi:~ $ sudo su
pi@raspberrypi:~ # apt-get update -y # update package lists
pi@raspberrypi:~ # apt-get upgrade -y # upgrades all installed software packages, takes a while
pi@raspberrypi:~ # apt-get clean # removes all packages from the package cache.
pi@raspberrypi:~ # apt-get autoremove -y # removes unused dependencies
11. Enable USB driver
· edit and append following line to /boot/config.txt
dtoverlay=dwc2
· append following line to /etc/modules:
dwc2
12. Create a large container file called /usbdata.
· First see how much space is available
pi@raspberrypi:~ # df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 122823908 1291596 116510908 2% /
· Use up as much space as possible. 116510908 / (1024 *1024) = ~111 GB. We will use 110 GB, leaving a bit for log files.
pi@raspberrypi:~ # truncate -s 110G /usbdata
pi@raspberrypi:~ # chmod a+rw /usbdata
13. Create 2 partitions interactively.
pi@raspberrypi:~ # sfdisk /usbdata
Welcome to sfdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Checking that no-one is using this disk right now ... OK
Disk /usbdata: 110 GiB, 118111600640 bytes, 230686720 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
sfdisk is going to create a new 'dos' disk label.
Use 'label: <name>' before you define a first partition
to override the default.
Type 'help' to get more information.
>>> ,115343360 # use half the 'sectors' for the first partition, or whatever
Created a new DOS disklabel with disk identifier 0xb7811ab4.
Created a new partition 1 of type 'Linux' and of size 55 GiB.
/usbdata1 : 2048 115345407 (55G) Linux
/usbdata2: ; # type in ; to use remaining 'sectors' for second partition
Created a new partition 2 of type 'Linux' and of size 55 GiB.
/usbdata2 : 115345408 230686719 (55G) Linux
/usbdata3: quit
New situation:
Disklabel type: dos
Disk identifier: 0xb7811ab4
Device Boot Start End Sectors Size Id Type
/usbdata1 2048 115345407 115343360 55G 83 Linux
/usbdata2 115345408 230686719 115341312 55G 83 Linux
Do you want to write this to disk? [Y]es/[N]o: Y
The partition table has been altered.
Syncing disks.
# set up a loop device that allows a file to look like a file system
pi@raspberrypi:~ # losetup -v -f /usbdata # connect first unused loop device to file
pi@raspberrypi:~ # losetup -a # check status of devices
/dev/loop0: [45826]:5703 (/usbdata)
pi@raspberrypi:~ # partx -v --add /dev/loop0 # add detected partitions to /dev/loop*
partition: none, disk: /dev/loop0, lower: 0, upper: 0
/dev/loop0: partition table type 'dos' detected
range recount: max partno=2, lower=0, upper=0
/dev/loop0: partition #1 added
/dev/loop0: partition #2 added
pi@raspberrypi:~ # partx --show /dev/loop0 # check things
NR START END SECTORS SIZE NAME UUID
1 2048 264191 262144 128M b153ba27-01
2 264192 1048575 784384 383M b153ba27-02
pi@raspberrypi:~ # blkid /dev/loop* # display file systems in partition
/dev/loop0: PTUUID="bddc4159" PTTYPE="dos"
/dev/loop0p1: PARTUUID="bddc4159-01"
/dev/loop0p2: PARTUUID="bddc4159-02"
14. Format the 2 partitions
· Use a PC to format them or use the following Linux commands
TESLACAM needs to be FAT32, MUSIC
can be FAT32 or exFAT
pi@raspberrypi:~ # mkdosfs -F 32 -I /dev/loop0p1 -n TESLACAM
pi@raspberrypi:~ # mkdosfs -F 32 -I /dev/loop0p2 -n MUSIC
15. Mount the partitions
pi@raspberrypi:~ # mkdir /mnt/teslaCam /mnt/music
pi@raspberrypi:~ # chmod a+rwx /mnt/ teslaCam /mnt/music
pi@raspberrypi:~ # mount /dev/loop0p1 /mnt/teslaCam -o users,umask=000
pi@raspberrypi:~ # mount /dev/loop0p2 /mnt/music -o users,umask=000
# test it, and create required subdirectories
pi@raspberrypi:~ # mkdir /mnt/teslaCam/TeslaCam
pi@raspberrypi:~ # mkdir /mnt/teslaCam/TeslaCam/RecentClips /mnt/teslaCam/TeslaCam/SavedClips /mnt/teslaCam/TeslaCam/SentryClips
pi@raspberrypi:~ # mkdir /mnt/music/Music
16. Make the partitions available on the USB port.
pi@raspberrypi:~ # modprobe g_mass_storage file=/usbdata stall=0 ro=0 removable=1 idVendor=0x0781 idProduct=0x5572 bcdDevice=0x0126 iManufacturer="SanDisk" iProduct="Cruzer Switch"
· If the Pi was plugged into a PC, then the two partitions should appear and contain the expected subfolders.
17. Set up automatic mounting of partitions during reboots. Download the shell scripts by clicking on the file names below.
· Create a file /home/pi/addmod.sh and add the following as contents:
#!/bin/bash
# Used to initially load the driver
# ALSO used to make changes the USB storage made by the Pi visible to the host on the other side of the USB cable
# remove driver. Doesn't hurt if driver has not yet been installed
modprobe -r g_mass_storage
# install USB driver. This causes the host to recognize the USB partitions
modprobe g_mass_storage file=/usbdata stall=0 ro=0 removable=1 idVendor=0x0781 idProduct=0x5572 bcdDevice=0x0126 iManufacturer="SanDisk" iProduct="Cruzer Switch"
echo USB drivers added
· Create a file /home/pi/dosfsck.sh and add the following as contents.
If a partition gets corrupted, then this script will try to repair and remount it.
It runs in the background and can take a while, depending on the contents of the drive.
#!/bin/bash
# check and fix the USB file systems. This can take up to an hour to run in the background
# force Tesla/host to remount USB if the drive was repaired
# -a automatically repair the filesystem
# -l list path names
# -t test for bad clusters
# -v verbose mode
# -w write changes to disk immediately
(echo 'Starting:' `date` ----; dosfsck -w -l -a -v -t /dev/loop0p1; if [ $? != 0 ]; then /home/pi/addmod.sh; fi; echo 'Ending:' `date`) >> /home/pi/dosfsck1.log 2>&1 &
(echo 'Starting:' `date` ----; dosfsck -w -l -a -v -t /dev/loop0p2; if [ $? != 0 ]; then /home/pi/addmod.sh; fi; echo 'Ending:' `date`) >> /home/pi/dosfsck2.log 2>&1 &
· Create a file /home/pi/remount.sh and add the following as contents:
#!/bin/bash
# remount.sh: allows Pi to see changes made to the USB storage via the USB
if [ -d /mnt/teslaCam/TeslaCam ]; then
printf "Unmounting /mnt/teslaCam\n"
umount /mnt/teslaCam
fi
if [ -d /mnt/music/Music ]; then
printf "Unmounting /mnt/music\n"
umount /mnt/music
fi
mount /dev/loop0p1 /mnt/teslaCam -o users,rw
mount /dev/loop0p2 /mnt/music -o users,rw
· Create a file /home/pi/startup.sh and add the following as contents:
#!/bin/bash
echo 'Starting:' `date`
losetup -v -f /usbdata # connect first unused loop device to file
partx -v --add /dev/loop0 # add detected partitions to /dev/loop*
/home/pi/remount.sh # mount USB partitions
/home/pi/addmod.sh # install USB driver
/home/pi/dosfsck.sh # start a file system check in the background
· Set hostname and run the file on Pi startup by inserting the following lines to /etc/rc.local before 'exit 0'
hostname TeslaDashPi # use your favorite name
/home/pi/startup.sh &
18. Setup crontab
Sync every 5 secs to flush out writes in case Tesla turns the power off without flushing
pi@raspberrypi:~ $ sudo crontab -e
add:
*/5 * * * * sync
19. Notes
· If the host (PC or Tesla) makes a change on the USB drive, like adding a new file, the Pi will not see the changes unless the Pi unmounts & remounts the drive.
/home/pi/remount.sh
· If the Pi makes a change, like adding or removing a file, or changing a file's contents, the host will not see the change unless
1) host (PC or Tesla) dismounts and re-mounts the drive
Or
2) The Pi forces the host to remount using:
/home/pi/addmod.sh
· Optionally, restart the Pi using: "sudo shutdown now" or "sudo restart"
The two partitions should show up on the PC 30-60 secs after the Pi is restarted
20. Setup Samba on Pi
· Install Samba
pi@raspberrypi:~ # apt-get install samba samba-common-bin
· Configure it. Append the following text to /etc/samba/smb.conf
pi@raspberrypi:~ # vi /etc/samba/smb.conf
[Music]
path = /mnt/music/Music/
writeable=Yes
create mask=0777
directory mask=0777
public=yes
[TeslaCam]
path = /mnt/teslaCam/TeslaCam/
writeable=Yes
create mask=0777
directory mask=0777
public=yes
· Set a SMB username and password
pi@raspberrypi:~ # smbpasswd -a pi
New SMB password:
Retype new SMB password:
Added user pi.
· Restart SMB server
pi@raspberrypi:~ # systemctl restart smbd
· Verify
pi@raspberrypi:~ # smbclient -L 192.168.x.x -U pi
Enter WORKGROUP\pi's password: <password>
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
Music Disk
TeslaCam Disk
IPC$ IPC IPC Service (Samba 4.13.13-Debian)
pi Disk Home Directories
21. Add music from a PC
· Mount SMB from a PC
o Windows File Explorer: Tools -> Map Network Drive
o Select any available drive on the PC
NOTE: this networked share drive is different from the USB drives we used earlier
o Folder: \\Pi_IP_address\Music
o Check: "Connect using different credentials"
o Finish
§ User name: pi
§ Password: whatever was entered in step 21
§ Uncheck: "Reconnect at logon"
§ Check: "Remember my credentials"
§ OK
o The selected drive should be available
· Copy music as desired. You can create sub folders
· Repeat mount for TeslaCam, if you wish. This allows for direct viewing of video clips
o Folder: \\Pi_IP_address\TeslaCam
· Disconnect the SMB drives from the PC to flush out any cached data
· Done. Shut down the Pi using: "sudo shutdown now"
22. Plug the Pi into the Tesla
· Use the USB micro to USB A connector
· Plug into the USB socket on the Pi, not the socket marked Power.
· Verify that the Tesla is awake. After about a minute, you should see the Camera icon on the Tesla dash. Different in v11
· Select USB on the Tesla music console. You should see the music files.
· The network shared drives should show up on your PC
· Keep the Tesla awake by doing things to it via your mobile app.
o Like charge it, turn on sentry mode, honk, climate on/off, ...
o There are also ways to keep it awake using the Tesla API
23. Tell the host (Tesla) to look at any new music files, if you add them
· Restart the Pi, or type in
@raspberrypi:~ # /home/pi/addmod.sh
24. Video curator (More TBD)
All in fun...
-- tess@pejaver.com