ThingsBoard: Latest Telemetry From The API

To use ThingsBoard data in other applications, you can pull the latest telemetry from the API.

Details for the API and background about obtaining the JWT token can be found at ThingsBoard Rest API. The JWT token is an authentication token to secure data. You can obtain this from the command line using curl:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"username":"<TB User>", "password":"------"}' 'http://<SERVER>:<PORT>/api/auth/login'

A JSON object will be returned (much longer than that shown below):

{"token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","refreshToken":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

The first part, just the token is the section you need.

Details of obtaining telemetry can be found at Working With Telemetry. The key we need to access is:

/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries

This can be pulled using curl. The header defines JSON as the expected type and also use the JWT token as the X-Authorization. Note, you must prefix the JWT token with the word “Bearer”.

curl -v -X GET http://<SERVER>:<PORT>/api/plugins/telemetry/DEVICE/<DEVICE ID>/values/timeseries --header "Accept:application/json" --header "X-Authorization: Bearer xxxxxxx JWT Token xxxxxx"

---- output cut -----
{"temperature":[{"ts":1604646983559,"value":"2.67"}],
"humidity":[{"ts":1604646983559,"value":"100.0"}],
"battery":[{"ts":1604646983559,"value":"3.31"}],
"busVoltage":[{"ts":1604646983559,"value":"4.08"}]}

From Python

import requests 

      URL = 'http://'+tbServer+'/api/plugins/telemetry/DEVICE/'+devId+'/values/timeseries'
        APIheader = {
            "Accept": "application/json",
            "X-Authorization": "Bearer "+tbJWTtoken
        }
        try:
            r = requests.get(URL, headers=APIheader)
            #print "Response = "+str(r.status_code)
            if(r.status_code==200):
                js=r.json()
                print json.dumps(js, indent=2)
 
            else:
                print "ERROR - "+str(r.status_code)+": "+r.content
        except:
            print "Error contacting ThingsBoard server"

Ensure you set the variables tbServer, devId and tbJWTtoken appropriately.

JWT Tokens Expiring

By default, the token will expire after 2.5 hours. This will get annoying if you embed the token in your applications. While you could make this auto-refresh by automating the above method, it requires hard coding your password in your code, which is more of a security risk than sticking with the same token. You can use the refresh token, but if you want to use something like a Particle Webhook to pull telemetry, that is not going to work. The refresh token also breaks down if the device/system is offline for a while and misses the refresh Window.

You can extend the token expiration time by editing /etc/thingsboard/conf/thingsboard.yml:

    #tokenExpirationTime: "${JWT_TOKEN_EXPIRATION_TIME:9000}" # Number of seconds (2.5 hours)
    tokenExpirationTime: "${JWT_TOKEN_EXPIRATION_TIME:15768000}" # Number of seconds (6 months)

Note, this is not considered good security practice. Only do this if you are not dealing with personal data or you do not care if your telemetry can be leaked into the wild.

1 thought on “ThingsBoard: Latest Telemetry From The API

Leave a Reply

Your email address will not be published. Required fields are marked *