Skip to content

Instantly share code, notes, and snippets.

@dirkvm
Last active November 12, 2020 17:08
Show Gist options
  • Save dirkvm/5c3b3cb9f2d55033181156ee36a76811 to your computer and use it in GitHub Desktop.
Save dirkvm/5c3b3cb9f2d55033181156ee36a76811 to your computer and use it in GitHub Desktop.
websocket based streaming for Tesla
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);
});
@apearson
Copy link

apearson commented Apr 1, 2019

Line 31: vehicles_id should be vehicle_id

@dirkvm
Copy link
Author

dirkvm commented Nov 12, 2020

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