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

WiFi RPi - AutoSync Music Folder - 99% Automated

This site may earn commission on affiliate links.
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.
1630709219945.png
1630711244187.png
1630711341456.png


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.
1630709058899.png




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
Code:
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
Code:
This is explained elsewhere...
- Create a folder on RPi to 'mount' USB drive
Code:
sudo mkdir -p -m 0777 /mnt/usb
- Create a folder on RPi to 'mount' NAS shared folder
Code:
sudo mkdir -p -m 0777 /mnt/music
- Modify fstab according to your USB and NAS folder names
Code:
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
Code:
nano rsync-2d.sh

#!/bin/bash
### 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
drive1='/mnt/music/'
drive2='/mnt/usb/'
touch $drive1 2>/dev/null
touch $drive2 2>/dev/null

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

if [ $retval -eq 0 ]
        then
        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

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

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

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




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.

Code:
### 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

exit


### 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.
 
Thanks to Preventing duplicate cron job executions - Benjamin Cane
Updated the auto-syncing script (rsync-2d.sh) so multiple instances don't run every minute if several files need to be copied that may take longer than one minute. The script still fires every minute but if it finds this trace file with a PID of the running instance, the new instance will quickly exit and not add to the sync queue.

Code:
#!/bin/bash
### 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

################################################
### Prevent Duplicate Instances from running ###
################################################
PIDFILE=/home/pi/rsync.pid
if [ -f $PIDFILE ]
then
  PID=$(cat $PIDFILE)
  ps -p $PID > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "Process already running"
    exit 1
  else
    ## Process not found assume not running
    echo $$ > $PIDFILE
    if [ $? -ne 0 ]
    then
      echo "Could not create PID file"
      exit 1
    fi
  fi
else
  echo $$ > $PIDFILE
  if [ $? -ne 0 ]
  then
    echo "Could not create PID file"
    exit 1
  fi
fi
################################################
### Prevent Duplicate Instances from running ###
################################################


### Drive Folder Paths
drive1='/mnt/music/'
drive2='/mnt/usb/'
touch $drive1 2>/dev/null
touch $drive2 2>/dev/null

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

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

    else
    echo "Did not find both drives to sync... exiting..."
fi


################################################
### Prevent Duplicate Instances from running ###
### Remove PID Trace File to clean up        ###
################################################
rm $PIDFILE
################################################

Besides that, no other updates. This system is working well enough for my needs. I did get a shorter cable to clean up the wiring.
RPi-Music.png
 
How often do you update your music library? Seems like a hassle to replace a simple USB stick with music on it. I might update it with new stuff once a month which is still easier than this convoluted way. Cool concept though.