TelsaPi is a security alarm project that integrates an outdoor motion sensor with the headlights and horn of a Tesla car. It works with all models. The project uses a Raspberry Pi, an outdoor PIR motion detector, and the Tesla API. The code is written in the Python programming language. TeslaPi
Written by Mike Haldas
- TeslaPi.py - this is the main project file that runs all of the alarm code.
- vars.py - I wanted to keep the Tesla API login and password in a separate file so I would not accidentally upload updates to TelsaPi.py with my login credentials in it.
- gps.py - simple script that logs into the Tesla API and gets the current GPS longitute and lattitute location of your car. I will use this in the future when I implement GPS location checking into the TeslaPi project.
Integration with the Telsa API is done using the TeslaJSON API wrapper written by Greg Glockner. (https://github.com/gglockner/teslajson).
Greg's library is a pleasure to work with and made it very easy to develop TeslaPi quickly.
Open the terminal program on your Raspbery Pi run the following commands. Please note that before you run the program, you should also complete the wiring setup in the next section.
- wget https://github.com/gglockner/teslajson/archive/master.zip
- unzip master.zip
- cd teslajson-master
- sudo python setup.py install
- cd ..
- rm master.zip
- wget https://github.com/mikehaldas/TeslaPi/archive/master.zip
- unzip master.zip
- cd TeslaPi-master
- Edit the vars.py file. You should enter your user ID and password that you use on the teslamotors.com website for the USERID and PASS variables, then save the file and exit.
- To start the program, python TeslaPi.py
If you want to test the API communication with your Tesla before you wire the motion detector to your Raspberry Pi, you can run the following command: python gps.py
This will connect to your car and print the GPS longitute and lattitute. If the program gives you an error the first time you run it, try to run it again. If you car is sleeping the first time it is run and takes a while to wake up, there may be an error. This is a bug that needs to be fixed. Try to run it again.
The gps.py script will be used in the future to setup GPS location verification of your Tesla to determine if it should be used in the alarm. For example, you may not want to flash your lights and sound your horn unless the car is parked in the lcoation to be effective for the alarm.
Raspberry Pi / Motion Sensor Wiringhttps://www.cctvcamerapros.com/PIR-Outdoor-Motion-Sensor-p/takex-ms-100e.htm
This is how it is wired.
- The outputs of the motion sensor are wired to GPIO 16 pin and a ground pin on the Raspberry Pi.
- A 12V DC power supply is used to power the motion detector.
- Although not pictured, the Raspberry Pi obviously also needs to be connected to a power source.
- The Raspberry Pi must be connected to an Internet connection, so that it can communicate with the Tesla API.
There are a lot of comments in the TeslaPi.py program that explain the logic of the application. The program basically works as follows. TelsaPi Alarm System Logic
- After you run TeslaPi.py, a connection is initiated to your Tesla.
- The program waits for the motion detector to sense motion.
- When motion is detected, an alarm cycle begins.
- An alarm cycle consists of a duration and 5 escalation levels.
- Each escalation level has it's own set of alarm actions that can be easily modified. For example: level 1 - flash headlight 3x, level 2, flash headlights 3x and beep horn 1x, level 3 - flash lights 5x and beep horn 2x, etc.
- If motion is continuous or occurs again within the alarm cycle threshold, the escalation level is incremented.
- The escalation level resets after level 5 or if the alarm cycle threshold time has passed.
Disclosure and Use
Please note that I would not consider this a production ready security application by any means. I created this project for fun shortly after I took delivery of my Model 3 Tesla. Please use it with that in mind and at your own risk.
This software is provided as-is. This software is not supported by or endorsed by Tesla Inc. Tesla Inc. does not publicly support the underlying JSON API, so this software may stop working at any time. The author makes no guarantee to release an updated version to fix any incompatibilities.