milesburton / teslacam

Experiment to use the Tesla (S/3/X) V9 Dashcam with the Raspberry Pi Zero W for automatic backup
Apache License 2.0
86 stars 19 forks source link
tesla tesla-cam teslamotors

Tesla Cam - An experimental application to repair, store and upload Tesla dash cam footage

Current capabilities

Overview

As of late 2018 Tesla released V9 which among a number of improvements included dash cam functionality. This works by placing a suitably sized USB drive in one of the available USB ports at the front of the vehicle (Model S).

One drawback of this system, not uncommon in dash cams, there's no easy way to push this video to the 'cloud' - nor any capability to view in near real-time. This project aims to make this possible.

Using a couple of tricks I've learned through tinkering with various single board computers, it is possible emulate a USB drive on the fly. In essence we are going emulate a USB drive, and periodically store the data on the SDHC. Once we have the video we can do what ever we'd like - maybe live stream, upload to your favourite cloud provider or simply backup the files when you return home.

Hardware Requirements

  1. 2017 (AP 2.5) or beyond Tesla
  2. Raspberry Pi Zero W (only this model is supported)
  3. A wireless access point within reasonable distance of the Pi (mobile phone, home router etc)
  4. A sufficiently large SDHC card with the fastest write speeds you can find, at least 16Gig, ideally the largest you can buy.
  5. High quality short USB A to USB Micro cable - Anker is quite decent
  6. Optional, a case to house the Raspberry Pi - anything with ventilation would be fine

Software Requirements

  1. 2018-11-13-raspbian-stretch-lite or later
  2. Etcher to write the disk image to the SDHC card (dd, win32diskimager etc etc will also work)
  3. Docker
  4. OTG Mode enabled in the boot configuration

Instructions

On your desktop computer

  1. Download and burn the latest "lite" Raspbian to a suitable SDHC card using Etcher (or equivalent)
  2. Modify the /boot partition to enable USB OTG.
    • Add dtoverlay=dwc2 as a new line to the bottom of config.txt
    • Enable g_mass_storage and dw2 by adding modules-load=dwc2,g_mass_storage right after rootwait in cmdline.txt
  3. Add your WIFI configuration details (consider adding several, including a portable hotspot such as your phone)
  4. Enable ssh by adding an empty file called ssh on the /boot partition

On your TeslaCam Pi (via SSH)

  1. Plug the Pi Zero W into the Tesla media USB ports (the front ports). Make sure you use the data port on the Pi, google if you are unsure.

  2. Connect to the Pi

    $ ssh pi@raspberrypi.local
  3. Execute the get-teslacam script, this should install everything you need to get up and running.

    $ GET_TESLACAM=`mktemp` \
    curl -fsSL https://git.io/JeWlq -o ${GET_TESLACAM} && \ 
    sh ${GET_TESLACAM} && \
    rm ${GET_TESLACAM}
  4. Once the automatic configuration completes the car should detect the Pi as a USB drive.

Optionally install extra services

Rsync

  1. Generate a ssh key for the rsync service

    $ docker run \
    --rm \
    -v teslacam_rsync_ssh:/root/.ssh \
    --entrypoint "ssh-keygen" \
    teslacam/dashcam-rsync-upload \
    -f /root/.ssh/id_rsa -q -N ""
  2. Run rsync upload service initial setup to copy making sure to update user@server to where you want to upload your key

    
    $ docker run \
    --rm \
    -it \
    -v teslacam_rsync_ssh:/root/.ssh \
    --entrypoint "ssh-copy-id" \
    teslacam/dashcam-rsync-upload \
    user@server
  3. Run the following command after you update the RSYNC_TARGET

    $ docker run \
    --restart=always \
    -d \
    -v teslacam_rsync_ssh:/root/.ssh \
    -v ${HOME}/teslacam/video:/video \
    -e "RSYNC_TARGET=user@server:~/TeslaCam" \
    --name rsync-upload \
    teslacam/dashcam-rsync-upload

Dropbox Upload

  1. Obtain a dropbox token for your account
  2. Configure the uploader container by running
    $ docker run \
    --rm \
    -it \
    -v dropbox_uploader_config:/config \
    --entrypoint=./dropbox_uploader.sh \
    teslacam/dropbox-uploader \
    -f /config/dropbox_uploader.conf
  3. Run the container and set it to restart always
    docker run \
    --restart=always \
    -v dropbox_uploader_config:/config \
    --name dropbox-uploader \
    teslacam/dropbox-uploader

Research & notes

Approach

Primarily there is a trade-off between lost video vs accessibility (our ability to do something useful with the captured footage). To download the Tesla Dash cam video we need to temporarily stop the recording, as Dash cam records in 1 minute increments we are likely to lose at least this much video - possibly more, possibly less depending on timing.

The second concern is we have no signal for when the car will be powered down - ie, you've parked up for the day - the longer we allow the car to record, the higher the possibility that video will be "trapped" in the vehicle till you next power up.

Finally to enable capabilities such as near-real-time monitoring or streaming that video must be transferred to the Pi as quickly as possible. The longer the car records, the longer it takes to transfer - and so on.

To mitigate the issue we need to pick a comfortable number of minutes, say between 10-30 minutes. To add to the fun, we must minimise the duration the car is not recording - to this end we need to switch out our emulated USB drives as quickly as possible which can be done by using two (or more) images swapped over whilst the video files are transferred across.

With all this in mind, logically speaking the following steps need to be followed

TODO

Referrals, and coffee