Last active
November 12, 2020 17:08
-
-
Save dirkvm/5c3b3cb9f2d55033181156ee36a76811 to your computer and use it in GitHub Desktop.
websocket based streaming for Tesla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require('util'); | |
const WebSocket = require('ws'); | |
const vehicle_id = ''; // as returned by the /vehicles api call | |
const auth_token = ''; // tokens as used in the REST API | |
const stream_columns = [ | |
'speed', | |
'odometer', | |
'soc', | |
'elevation', | |
'est_heading', | |
'est_lat', | |
'est_lng', | |
'power', | |
'shift_state', | |
'range', | |
'est_range', | |
// 'heading', | |
]; | |
let ws = new WebSocket('wss://streaming.vn.teslamotors.com/streaming/', { | |
followRedirects: true, | |
}); | |
const msg = { | |
msg_type: 'data:subscribe_oauth', | |
token: auth_token, | |
value: stream_columns.join(','), | |
tag: vehicle_id.toString(), | |
}; | |
console.log(msg); | |
ws.on('open', () => { | |
ws.send(JSON.stringify(msg)); | |
}); | |
ws.on('close', (code, reason) => { | |
util.log('websocket closed, code=' + code + ', reason=' + reason); | |
}); | |
ws.on('error', (err) => { | |
util.log('websocket error: ' + err); | |
}); | |
ws.on('message', (data) => { | |
const msg = JSON.parse(data); | |
util.log(msg); | |
}); |
New authentication method after Tesla disabled 'Basic Auth'.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 31:
vehicles_id
should bevehicle_id