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: https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en
localStorage.getItem("session"). This will return something like this:
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:
In this case,
02f2d830-454c-11e7-b5e5-0383e230cbfais my device ID, and
82c007a0-4ad2-11e7-98bd-9ffdab1de3b9is 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
Authorizationwith a value of
Bearer <access_token>(the access token from step 2)
- A second key named
Content-Typewith a value of
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:
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: https://pastebin.com/9Q9Uty4A 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.
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:
using my example device above, this looks like:
This will return a larger JSON block that includes all widget data for the specified date range!