Please note that this post only applies to MQTT (‘Bring Your Own Thing’) and LoRa connected devices. For non-MQTT Raspberry Pi and Arduino devices, you want this sister post.
I’ve found that the “Download Chart Data” button doesn’t always satisfy everyone’s needs to pull data which has been logged to Cayenne out of the system. The purpose of this post is to show you how you can extract the raw sensor data for LoRa and MQTT (‘Bring Your Own Thing’) devices from our system.
The process is a bit complex, but should allow you to work around any bugs or limitations in our data download feature until such time as we improve it. Once you’ve done it a few times it becomes fairly straightforward.
Extracting historical data for a single widget
-
Install the Postman Chrome App: Postman - Chrome Web Store
-
You’ll need to get something we call an ‘access token’. To get this, log into the Cayenne web dashboard on your account in Chrome, and open the console with View > Developer > JavaScript Console. In the console, paste:
localStorage.getItem("session")
. This will return something like this:{“id":“728005”,“email”:"testtest@test.com”,“first_name”:“Test”,“last_name”:“Test”,“access_token”:“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InJvYm55MjAxN0B5b3BtYWlsLmNvbSIsImZpcnN0X25hbWUiOiJSb2IiLCJsYXN0X25hbWUiOiJUZXN0Iiwic2NvcGUiOlsic2VsZiJdLCJ1c2VyX2lkIjoiNzI4MDA1IiwiaWF0IjoxNTAzMDg3NDE5LCJleHAiOjE1MDQzODM0MTksImF1ZCI6Im15ZGV2aWNlcy5jYXllbm5lLnByb2QiLCJpc3MiOiJteWRldmljZXMuYXNnYXJkLnByb2QiLCJqdGkiOiJmZjI5OThjNS0yNGU4LTRiYWMtODIwZC0zODAyNzgzMjBjODkifQ.h_KQmJR8wcCghHMqgcvcYFgfv8PqmWUJ1VbaYlR9fXA”,“refresh_token”:“cf265d2aeee235a93b876d3773fce106baa89fc8”,“client_id”:“0d31d860-3591-11e7-85a7-c34806d4e04d”,“client_secret”:“b1ba96b44f102eedfd47aafdf92474bd2782b563”}
The access token is the part that I’ve bolded, and will be different for your account.
-
Next, you’ll need two more items, a ‘device id’ which identifies the parent device on your Cayenne account, and a ‘sensor id’ that identifies the widget you want to pull historical data for. You can get these both at once. Open the Settings dialog for the widget whose data you’re pulling on the web dashboard, and make note of what’s in your browser address bar. Here is an example from a Temperature Widget on a LoRa device on my account:
https://cayenne.mydevices.com/cayenne/dashboard/lora/02f2d830-454c-11e7-b5e5-0383e230cbfa/settings/82c007a0-4ad2-11e7-98bd-9ffdab1de3b9
In this case,
02f2d830-454c-11e7-b5e5-0383e230cbfa
is my device ID, and82c007a0-4ad2-11e7-98bd-9ffdab1de3b9
is my sensor ID. -
Next you need to get the UNIX timestamp in milliseconds for the start and end times of the date range you want to pull. This website is pretty useful for that.
I’m writing this at Tue, 29 Aug 2017 15:10:55 MDT (GMT -6), so I’ll make that my end time. Using the site, it converts to the timestamp 1504041055976. Let’s say I want to pull the last two days of sensor data, so using Sun, 27 Aug 2017 15:10:55 MDT as my start time, that converts to 1503868255000.
-
Next, launch Postman that you downloaded in step #1. Go first to the ‘Headers’ tab. You’ll need to add two keys here:
- One key named
Authorization
with a value ofBearer <access_token>
(the access token from step 2) - A second key named
Content-Type
with a value ofapplication/json
-
In the large “GET” field, you need a URL of this format:
https://platform.mydevices.com/v1.1/telemetry/YOURDEVICEID/sensors/YOURSENSORID/summaries?type=custom&startDate=STARTDATE&endDate=ENDDATE
. So using all of my examples in this post, my GET URL looks like:https://platform.mydevices.com/v1.1/telemetry/02f2d830-454c-11e7-b5e5-0383e230cbfa/sensors/82c007a0-4ad2-11e7-98bd-9ffdab1de3b9/summaries?type=custom&startDate=1503868255000&endDate=1504041055976
-
The moment of truth: press the big blue ‘Send’ button in Postman. If everything was set correctly, you’ll be rewarded with a JSON readout of each of the data points you requested. To show the general format, mine looks like this: [ { "v": 35, "ts": "2017-08-29T21:10:00.000Z", " - Pastebin.com if you’re curious about the weather outside our office
That’s it! Let me know if you get stuck following that or have any questions. It’s finicky but once you get the hang of it, just rinse and repeat for each sensor.
-or-
Extracting historical data for all widgets on a MQTT/LoRa device
To do this, follow steps 1 through 5 above. You can skip gathering the Sensor ID in step 3 since we’re going to pull all sensors in this example.
For the GET field in Postman, you’ll want to build a URL like:
https://platform.mydevices.com/v1.1/telemetry/YOURDEVICEID/summaries?type=custom&startDate=YOURSTARTDATE&endDate=YOURENDDATE
using my example device above, this looks like:
https://platform.mydevices.com/v1.1/telemetry/02f2d830-454c-11e7-b5e5-0383e230cbfa/summaries?type=custom&startDate=1503868255000&endDate=1504041055976
This will return a larger JSON block that includes all widget data for the specified date range!