Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.

WiFi RPi - AutoSync Music Folder - 99% Automated

Shake Shinde

Oct 5, 2019
I liked TeslaUSB using a Raspberry Pi (RPi) for how it works seamlessly fully automated, but I just couldn't wait for it to boot up every-time, every-drive to hear a song playing.

I decided to switch to a 'wireless sync with manual trigger' approach. This method retains the immediate auto-resume of the last song playing upon opening a door. For normal use, I have to do nothing since when the car wakes up, one of the ports (Position B) always gets selected first. I have that going into the Tesla's center console port so music always defaults to being channeled to the car (instead of the RPi).

If I feel like I haven't synced the car with my NAS music folder at home in a while, I'll toggle (to Position A) this 2-port USB switch while parked in my garage and either wait a couple of minutes or let the car charge after a drive overnight. The RPi then runs a script every minute to check if a USB drive is connected (toggled into Position A) and if so, copies files from a NAS shared folder.

A timestamp 'placeholder' file shows up when viewing on the Tesla screen that tells me the last date (YY/MM/DD) and time (HH/MM/SS) a sync occurred so I can know whether it actually worked. I've sat there toggling back and forth to see a new filename/timestamp show up and tested this pretty thoroughly. If you can get it to work once, it'll work every time.

Here are some instructions if anyone wants to try this approach. It's basically consists of the following:

- Have a pre-existing Samba SMB/CIFS shared folder with music on your network
This is easily done with another RPi connected to your wifi router but not explained here.
- Install RPi OS normally using RPi Imager with Ctrl + Shift + X settings to enable SSH and WiFi
This is explained elsewhere...
- Create a folder on RPi to 'mount' USB drive
sudo mkdir -p -m 0777 /mnt/usb
- Create a folder on RPi to 'mount' NAS shared folder
sudo mkdir -p -m 0777 /mnt/music
- Modify fstab according to your USB and NAS folder names
sudo nano /etc/fstab

### Define USB and NAS shared folder for auto-mounting ###
LABEL="MUSIC" /mnt/usb exfat,vfat rw,noatime,uid=pi,gid=pi,umask=000,x-systemd.automount,sync,x-systemd.device-timeout=1 0 0
//NAS/SHARED-FOLDER /mnt/music cifs nofail,_netdev,username=USERNAME,password=PASSWORD,uid=pi,gid=pi,x-systemd.automount,x-systemd.requires=network-online.target,x-systemd.device-timeout=4 0 0
- Copy/Paste onto RPi, a script that ensures USB and NAS are available, syncs them, and creates timestamp 'placeholder' file
- Before script can be executed, allow required permissions : chmod +x rsync-2d.sh
nano rsync-2d.sh

### Script : rsync-2d.sh
### #!/bin/bash    must be first line of script
### *.mp3 files must be in folders on USB drive
### chmod +x rsync-2d.sh    before executing script

### Drive Folder Paths
touch $drive1 2>/dev/null
touch $drive2 2>/dev/null

sudo mount -a; retval=$?        # Exit 0 if both drives mounted

if [ $retval -eq 0 ]
        rsync -auhP --delete $drive1 $drive2
        echo "Sync Completed : $(date)" >> /mnt/usb/rsync.txt
        rm -f /mnt/usb/*.mp3
        mv /mnt/usb/rsync.txt /mnt/usb/AutoSyncUSB--`/bin/date +\%y-\%m-\%d--\%H-\%M-\%S`.mp3

        echo "Did not find both drives to sync...exiting..."
- Create a cronjob to run this script every minute of every day as available
crontab -e

* * * * * /home/pi/rsync-2d.sh

Here's a photo of my setup with cables that will hopefully be shorter soon.

A hybrid Video/Music partitioned USB drive can be used with the method described below. For reference, I'm including my personal notes on how to create one but YMMV.

### Use windows disk partition command line utility ###
[Win+R] : diskpart            # launch diskpart as Admin

list disk

select disk ?                 # ? is target USB drive

list partition                # view current partitions

select partition 1            # select first partition
delete partition            # delete partition

convert mbr                    # set MBR instead of GPT

create partition primary size=230000    # approx. 230GB

create partition extended    # using rest of drive
create partition logical
assign letter=M                # assign drive letter to logical partition


### Use Windows Disk Management GUI to Quick Format as exFAT ###
{Right-Click} Start Menu > Disk Management
{Right-Click} on USB drive > Primary Partition > TeslaCam > Format
{Right-Click} on USB drive > Logical Partition > Music    > Format

Feel free to modify or package more neatly for others as needed. It's possible I've left out some 'common sense' steps that may not be so trivial for newcomers. I'll update this post with errors posted below as found, maybe.

Products we're discussing on TMC...

About Us

Formed in 2006, Tesla Motors Club (TMC) was the first independent online Tesla community. Today it remains the largest and most dynamic community of Tesla enthusiasts. Learn more.

Do you value your experience at TMC? Consider becoming a Supporting Member of Tesla Motors Club. As a thank you for your contribution, you'll get nearly no ads in the Community and Groups sections. Additional perks are available depending on the level of contribution. Please visit the Account Upgrades page for more details.