TMC is an independent, primarily volunteer organization that relies on ad revenue to cover its operating costs. Please consider whitelisting TMC on your ad blocker and becoming a Supporting Member. For more info: Support TMC

Model S REST API

Discussion in 'Model S: User Interface' started by timdorr, Feb 2, 2013.

  1. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
  2. AnOutsider

    AnOutsider S532 # XS27

    Joined:
    Apr 3, 2009
    Messages:
    11,945
    I was thinking the same. Though, it probably does keep track of total charges somewhere.
     
  3. Banahogg

    Banahogg Member

    Joined:
    Jan 22, 2013
    Messages:
    293
    Location:
    San Jose, CA
    That seems to be the case. I did a full standard charge last night and it reset to 0 at the point the charge completed. That still seems to indicate that the did range charges before I picked it up.
     
  4. Robert.Boston

    Robert.Boston Model S VIN P01536

    Joined:
    Oct 7, 2011
    Messages:
    7,843
    Location:
    Portland, Maine, USA
    Somewhere in Tesla's HQ, someone has now won the over/under on how long it would take the Model S owners to crack the API. :)
     
  5. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    #105 hans, Feb 14, 2013
    Last edited: Feb 14, 2013
    The only way I can get a token is to first login using my Android phone and then call the API from my app. I wonder if there is a step I am missing to trigger to creation of the token?

    I also get an HTTP timeout if I don't drive the car for a few minutes. I tried setting --keepalive-time on CURL but my stupid windows OS doesn't support it. Will try again with Linux or MacOS.

    8474759116_9687ccd307.jpg

    Values shown are for streaming API query for speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state

    First field is clearly #milliseconds since the Unix epoch. I did check the timezone to see if it's local or in UTC and it is in Coordinated Universal Time.
    Valid shift states are D(rive), R(everse), P(ark)
    When you go in reverse the speed is still positive but the shift state is "R"

    Speed and Odometer appear to be in miles but I didn't test with the car set for metric to see if it changes.
    est_heading is in degrees (compass direction) so their must be a magnetometer in the car somewhere. Perhaps the module is installed backwards or something because I appear to get the direction of the ass end of the car (rather than the frunk)

    No idea what "power" is or whether it goes to 11 ;-)

    My goal is to finish a node.js app that republishes the live stream of telemetry via PubNub or a websocket server of my own.
     
  6. Beaker

    Beaker Sig #997

    Joined:
    Apr 9, 2012
    Messages:
    98
    Location:
    San Jose, CA
    I'm sending in the token, but it's failing with an "HTTP/1.1 401 Unauthorized" :(
     
  7. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    Are you sending in the first (of two) tokens that you get as a response from portal.vn.teslamotors.com/vehicles (i.e. not the tokens set in the cookies)

    ... "tokens":["abc123xyz456","098qwe765rty"],"state":"online"}
     
  8. PureAmps

    PureAmps Model S P85 (#2817)

    Joined:
    Oct 22, 2012
    Messages:
    359
    Location:
    SF Bay Area
    Try invoking GET on /vehicles/{id}/command/wake_up via the REST api, seems to fix the missing tokens issue.

    The streaming connection will eventually timeout or be closed by their servers, and you will have to re-establish. This happens even with the mobile app, when I watch the traffic via the proxy. You will notice a little pause in the live updating of the vehicle on the location map when this happens.

    The "power" setting seems to correspond to the power being used or generated by the vehicle and is equivalent to the right-part of the dial on the car's speedometer. I think the intent was for the mobile app's speedometer to mirror the car's because it has the same colors in the outer circle, but it is just not drawing anything yet.
     
  9. Beaker

    Beaker Sig #997

    Joined:
    Apr 9, 2012
    Messages:
    98
    Location:
    San Jose, CA
    Yep, I've tired both in desperation. I've observed that the second token is the previous token, so they roll to the right.
     
  10. PureAmps

    PureAmps Model S P85 (#2817)

    Joined:
    Oct 22, 2012
    Messages:
    359
    Location:
    SF Bay Area
    Are you using the correct vehicle id? For the streaming API it is the "vehicle_id" field in the vehicle's JSON description not the "id" field. The ids are not the same for the REST and streaming APIs. For the streaming API mine is a 10-digit number, compared to a 3-digit number ror the rest API.
     
  11. Beaker

    Beaker Sig #997

    Joined:
    Apr 9, 2012
    Messages:
    98
    Location:
    San Jose, CA
    :redface: just figured that out - and it works !! yea!.

    I thought it was the "." in my email address.


    Another interesting side note the JSON "id" field changes if you login with your username instead of your email address. (Both seem to log you in though)

    Thanks for your help.
     
  12. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    Yup. Calling wake_up on the REST API gets me new tokens to use on the streaming API. Thanks a bunch PureAmps! Couldn't do it without your help.
     
  13. Osiris

    Osiris Member

    Joined:
    Feb 14, 2013
    Messages:
    15
    Location:
    Germany, Munich
    Greets from Germany

    I have finalized my Sig. P today and I'm glad you guys have already to began hacking the car for the german tuners. :) Now I have an addidtion motivation to update my programming skillz. Thx dudes
     
  14. JoshG

    JoshG Member

    Joined:
    Jun 23, 2011
    Messages:
    422
    Forgive me if wrong terminology, or if a dumb question, but is there API access to the media player in the car? can it switch inputs, set channels or sources, or select songs to play? if so, there could be some cool things done where the car starts my favorite podcasts or streams at certain times of the day?
     
  15. aviators99

    aviators99 Model S - R140

    Joined:
    Jan 1, 2010
    Messages:
    1,458
    Location:
    Weston, Florida, United States
    Not that's been discovered...yet.
     
  16. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    #116 hans, Feb 15, 2013
    Last edited: Feb 15, 2013
    I guess the downside of living close to sea level is that it easy to get elevation and compass bearing measurements mixed up ;-)
    Sorry for the false alarm but the est_heading values are indeed correct and indicate the direction the front of the car is facing.

    Power is clearly in kilowatts and a negative number indicates regen (also in kilowatts)

    The lat/long values are extremely accurate and could pinpoint the part of my driveway where the car was located. That NYT journalist better quit making up stories about what he did/didn't do as it's clear to me that Tesla can tell exactly how many times he circled the parking lot and in which parking spot he eventually parked.

    The streaming interface is very basic. It's a HTTP long polling interface so all you need to do is read the body of the HTTP response and then poll again to get more data.

    I put the output into a file and read it into Excel as a CSV file for more advanced graphing and analytics. I was hoping to get more accurate 0-60 mph data but since the samples are 250 ms apart I can only get a 1/4 of a second resolution. I was looking for 1/10 second or better.
     
  17. aviators99

    aviators99 Model S - R140

    Joined:
    Jan 1, 2010
    Messages:
    1,458
    Location:
    Weston, Florida, United States
    A negative number also can indicate charging.
     
  18. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    #118 hans, Feb 15, 2013
    Last edited: Feb 15, 2013
    So it is! You can tell the difference between regen and charging because when the car is charging, the speed is "null" (not 0).
    Usually, but not always, the shift_state is also "null" but I have seen a shift_state of "P" for a few samples while the charge_state was being switched on and off using the REST API.
     
  19. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    When the car is fully charged in standard range mode, has anyone figured out how to get it to start charging again in max range mode?

    I can set the range to max_range successfully but the car rejects any attempt to start charging again with a result code of "charge complete".
     
  20. astrotoy

    astrotoy Supporting Member

    Joined:
    Jan 24, 2013
    Messages:
    271
    Location:
    SF Bay Area
    Could someone please give a simply layperson's explanation of this thread? I last programmed a computer using Fortran 4 and punch cards about 40 years ago. Have you folks invented a new app that non programmers can use or do you have to be able to write code to do these things? Not even sure what these things are. I looked up several of the terms, like REST API, in wikipedia, but I can't make heads or tails of what they are.

    Thanks from a senior citizen Model S owner.
     

Share This Page

  • About Us

    Formed in 2006, Tesla Motors Club (TMC) was the first independent online Tesla community. Today it remains the largest and most dynamic community of Tesla enthusiasts. Learn more.
  • Do you value your experience at TMC? Consider becoming a Supporting Member of Tesla Motors Club. As a thank you for your contribution, you'll get nearly no ads in the Community and Groups sections. Additional perks are available depending on the level of contribution. Please visit the Account Upgrades page for more details.


    SUPPORT TMC