AWS Lambda that handles click events from Amazon's AWS IoT Button, a customizable version of their dash button, to send commands to a Tesla vehicle. Watch a demo of the charging port and trunk being opened from a single click on my Instagram or YouTube.
- AWS IoT Button
- AWS Account
- Tesla Account
- Tesla Vehicle ID
- Set up your IoT Button.
- Clone or download repo.
- Copy
env.yml.template
toenv.yml
- Replace
YOUR_DSN_HERE
value with your IoT Button DSN. - Provide
USERNAME
,PASSWORD
, andVEHICLE_ID
(theid
orid_s
value from vehicles endpoint reponse) values. - Configure the click commands as you see fit.
- Install Node
- Install Serverless Framework,
npm i -g serverless
- Deploy Lambda,
sls deploy -r aws-region-code
- Go to IoT Button app and change to Lambda.
- Click button and show off to friends.
Get an access token by making a request to the token endpoint. Copy and paste the command below and replace YOUR_TESLA_LOGIN_EMAIL
and YOUR_TESLA_LOGIN_PASSWORD
with your values before running the command.
curl -X POST \
https://owner-api.teslamotors.com/oauth/token \
-d 'grant_type=password' \
-d 'client_id=81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384' \
-d 'client_secret=c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3' \
-d 'email=YOUR_TESLA_LOGIN_EMAIL&password=YOUR_TESLA_LOGIN_PASSWORD'
A successful response looks something like this.
{
"access_token":"8unc4ofnumb3r5a17dl3tt3r5ih0p3urn0tr36d1ngth15b1337w00tth33nd",
"token_type":"bearer",
"expires_in":3888000,
"refresh_token":"8jdf9j3hd82kd02o5djs92dq8id03odkw2kdk034odkwf23ekri0356ks24tk",
"created_at":1535859787
}
Use the value of the access_token
key to make a request for all vehicles in your account. Copy and paste the command below and replace with your access token. I've used the access token from the example response above so you can see what it should look like below.
curl -X GET \
https://owner-api.teslamotors.com/api/1/vehicles \
-H 'Authorization: Bearer 8unc4ofnumb3r5a17dl3tt3r5ih0p3urn0tr36d1ngth15b1337w00tth33nd'
The response will have a key called response
whose value is an array of vehicles.
{
"response": [
{
"id": 70987654321234567,
"vehicle_id": 1430999717,
"vin": "5YJ3E1A2B3C4D5E6F",
"display_name": "null",
"option_codes": "AD15,MDL3,PBSB,RENA,BT37,ID3W,RF3G,S3PB,DRLH,DV2W,W39B,APF0,COUS,BC3B,CH07,PC30,FC3P,FG31,GLFR,HL31,HM31,IL31,LTPB,MR31,FM3B,RS3H,SA3P,STCP,SC04,SU3C,T3CA,TW00,TM00,UT3P,WR00,AU3P,APH3,AF00,ZCST,MI00,CDM0",
"color": null,
"tokens": [
"5282a1b2c3d45e6f",
"7f09a1b2c3d45e6f"
],
"state": "online",
"in_service": false,
"id_s": "70987654321234567",
"calendar_enabled": true,
"backseat_token": null,
"backseat_token_updated_at": null
}
],
"count": 1
}
You will need the value from the id
or id_s
field to set the VEHICLE_ID
in the env.yml configuration file. Hint: To tell which vehicle is which, look at the option_codes
and look for MDL3
, MDLS
, or MDLX
for the Model 3, Model S, and Model X, respectively.
I took the approach of less is more:
- Single Source File
- editable directly in AWS console.
- No Dependencies
- secure by nature of smaller attack surface area.
- faster deployment and runtime execution.
- Vehicle ID Required
- saves the hassle of having to fetch it at runtime.