Sentry Saver

Sentry Saver is a gadget that manages the video clips saved in a USB drive by a Tesla.  While there are excellent alternatives, you can build this yourself following the DIY instructions below.  Also see, Tesla Charge Minder, a cool tool being developed to monitor your car's charge state.

Tesla vehicles have cameras that constantly record video.  There are four cameras, each recording  front, back, left & right views.  Besides recording while the car is being driven, the cameras can be configured to record while the car is parked.  The latter is called Sentry Mode.  Recordings while driving are useful if there was an accident, while videos while parked are useful as an alarm system and can help identify a potential miscreant.  The recordings are stored as one minute clips on a USB drive.  There are three types of recordings: Recent driving clips, Sentry events, and Saved events.  Though this capability is cool, there are several problems in using it effectively.

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.  Each set of clips covers 10 minutes of activity.

2.     The video files are large and can easily fill up the USB drive.  Recording will stop when the drive is full.  The typical user would plug in the drive and go for weeks without reviewing the recordings and cleaning out old footage.  This could lead to an unfortunate situation where something interesting happens, but it was not recorded because the drive was full.

3.     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.  A major effort will be made to automatically skip false alerts.

4.     Lastly, while it is not really difficult, setting up the USB drive is not easy for some people.

Objective

The solution involves building a "smart" USB drive.  The drive would appear to be a traditional USB stick to the Tesla, but it would do a number of things:

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 now be viewed on the Tesla console screen, a PC based app would be much more capable and flexible, perhaps using SentryKeeper.

3.     The device will constantly scan through clips on the USB drive.  It will 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 value proposition.  The processing will continue while the car is parked.

4.     If the device is paired with a mobile phone using Bluetooth, then it can send clips of Sentry events immediately to the owner.  However, this requires a mobile phone to be left in the car. 

There are even more features, like:

5.     The device can act as two USB drives, one to store clips and the other to store music. 

6.     The device has a "web server" that will allow the user to conveniently manage the music set from the convenience of their home.  The web server will be reachable via the WiFi link when the car is parked near by.

7.     The Model Y does not have HomeLink and cannot automatically open and close the garage door.  It is currently a $400 option.  This device can also use its WiFi to determine proximity to a garage and control the garage door, lighting and other functions.

A prototype has been built and the schematic is shown below.  The BOM cost for a 64GB device comes to about $50.  It can be powered by the Tesla USB connector, but a power pack allows it to run even when the car is turned off.  The "home" computer shown can have terra bytes of storage to hold interesting saved clips and clips can be more conveniently reviewed from here.

 

References:

·        USB Formatting: https://cammem.com/tesla/preparing-your-drive/

·        Sentry Keeper: https://www.allprosoftware.com/sentrykeeper/

·        Similar instructions:

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

 

Bill of Material

Item & URL

Approx cost

Raspberry Pi Zero W  (with WiFi & Bluetooth)

$11

Micro SD card (32GB or more)

$20

Power pack, 5000 mAh

$11

USB cords, USB A male to Micro male

$5

12v Power plug, outputs 5v, on USB micro

$5

Case, but real hackers don't use a case

$7

 

Instructions ( to be cleaned up soon; ran out of time....)

1.     Verify that there is only 1 partition on the SD card.  Samsung SD cards may come with 2.

·        On a PC, run the "Disk Management" app.  (On WIn 10, search for "Partition")

·        Insert SD card, you may need a Memory Card Reader.

·        The SD card will show up in the app. 

·        If you see more than 1 partition, delete them all and recreate one

·        Verify that the new partition is exFAT or FAT32

 

2.     Install a remote terminal app, like  PuTTY.

·        For a Windows PC, download and install PuTTY

·        On an Android, try JuiceSSH

 

3.     Install Raspbian Lite on SD card:

·        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)

 

4.     Enable SSH on Pi

·        Use the Windows File Explorer to create a non-zero length file called 'ssh' in root dir of SSD. 

o   No extension for the file. 

 

5.     Enable WiFi

·        Create file called 'wpa_supplicant.conf' in root dir of SSD.  Contents:

country=US

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

 

network={

  ssid="yourWiFiName"

  psk="yourWiFiPassword"

}

 

 
 

 

 

 

 

 

 

 


6.     Fire up the Pi

·        Eject the SD safely from your PC

·        Install SSD in Raspberry Pi Zero W. 

·        Power it up.  Step back and take cover.

o   It may take a few mins to boot (first time)

·        Meanwhile, find its IP address by checking the WiFi router

o   <<<Describe how>>>

·        Connect using PuTTY

o   Default creds: User: pi, Password: raspberry

NOTES:

·        Do not to turn off the Pi by removing power, it WILL eventually damage the file system.

·        Instead, do this: From PuTTY:

sudo halt

WAIT for the green LED to go OFF (stop blinking)  Green LED is SD card activity

·        To restart while running, do

sudo reboot

 

7.     Change passwd (passwd pi) or

            sudo raspi-config

   Expand fs to max.  It probably is already at max

            sudo raspi-config --expand-rootfs

 

8.     Get updates and make room for real data

sudo apt-get update -y

sudo apt-get upgrade -y # upgrades all installed packages, takes a while

sudo apt-get clean      # removes all packages from the package cache.

sudo apt-get autoremove -y   # removes unused dependencies

 

9.     Enable USB driver

·        append following line to /boot/config.txt

            dtoverlay=dwc2

·        append following line to /etc/modules:

            dwc2

10.            Create container file with 2 partitions.

First see how much space is available

     df /              

     Filesystem     1K-blocks    Used Available Use% Mounted on

     /dev/root      122823908 1291596 116510908   2% /

 

divide available by 1024 * 1024.

            # leave 1 gb

            sudo truncate -s 28G /usbdata

            sudo chmod a+rw /usbdata

            # partition the file

            pi@raspberrypi:~ $ sudo 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

 

            >>> ;                                                  # remaining 'sectors' for second partition

            >>> quit

            Write = Yes

 

            # loop device allows a file to look like a file system

            sudo losetup -v -f /usbdata                     # connect first unused loop device to file

            sudo losetup -a                                                       # check status of devices

            /dev/loop0: [45826]:5703 (/usbdata)

 

            # sudo 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

 

            # sudo partx --show /dev/loop0

            NR  START     END SECTORS SIZE NAME UUID

             1   2048  264191  262144 128M      b153ba27-01

             2 264192 1048575  784384 383M      b153ba27-02

 

            # sudo blkid /dev/loop*                           # display file systems in partition

            /dev/loop0: PTUUID="bddc4159" PTTYPE="dos"

            /dev/loop0p1: PARTUUID="bddc4159-01"

            /dev/loop0p2: PARTUUID="bddc4159-02"

 

10) Format the 2 partitions

            # Use a PC to format  or  use the following commands

            # TeslaCam needs to be FAT32, Music can be FAT32 or exFAT
            # Tesla will support exFAT soon.

            sudo mkdosfs -F 32 -I /dev/loop0p1 -n TESLACAM

            sudo mkdosfs -F 32 -I /dev/loop0p2 -n MUSIC

 

11) Mount the partitions

            sudo mkdir /mnt/usb1

            sudo mkdir /mnt/usb2

            sudo chmod a+rwx /mnt/usb1 /mnt/usb2

 

            sudo mount  /dev/loop0p1 /mnt/usb1 -o users,umask=000

            sudo mount /dev/loop0p2 /mnt/usb2 -o users,umask=000

 

            # test it

            mkdir /mnt/usb1/TeslaCam

            mkdir /mnt/usb2/Music

 

12) Make the partitions available on the USB port. 

sudo modprobe g_mass_storage file=/usbdata stall=0 ro=0 removable=1  idVendor=0x0781 idProduct=0x5572 bcdDevice=0x0126 iManufacturer="SanDisk" iProduct="Cruzer Switch"

Plug it into a PC to verify that the two partitions appear and contain the expected folders.

13) Automate mounting of partitions during next reboot

            sudo vi /etc/rc.local

            /home/pi/addmod &

 

#!/bin/bash

losetup -v -f /usbdata        # connect first unused loop device to file

partx -v --add /dev/loop0       # add detected partitions to /dev/loop*

mount /dev/loop0p1 /mnt/usb1 -o users

mount /dev/loop0p1 /mnt/usb2 -o users

modprobe g_mass_storage file=/usbdata stall=0 ro=0 removable=1  idVendor=0x0781 idProduct=0x5572 bcdDevice=0x0126 iManufacturer="SanDisk"  iProduct="Cruzer Switch" iSerialNumber="4C597515973308202393"

 

           

If the PC makes a change on the USB drive, like adding a new file, the Pi will not see the changes unless, Pi unmounts & remounts the drive.  The PC is not affected by the operation.  Pi does not see changes to a file's contects either.

            umount /mnt/usb_share

            mount /mnt/usb_share

 

If The Pi makes a change, like adding or removing a file, or changing a file's contents, the PC will not see the change unless

            1) PC dismounts and re-mounts the drive

Or

            2) The Pi does a modprobe -r and a modprobe add, which is basically forcing the PC to unmount and re-mount

                        sudo modprobe -r g_mass_storage

 

-  dpkg-reconfigure tzdata  change TZ from London to local

 

All in fun...

--  tess@pejaver.com