Ingesting JSON Data into Datacake API Devices
Discover How to Generate JSON Data for Ingestion into Datacake API Devices Using HTTP Calls, Node-RED or Other Methods, and Find the Right Payload Decoder.
Introduction
Sensor readings are a crucial part of many IoT applications, providing valuable data on a wide range of environmental and operational parameters. The Datacake API makes it easy to collect and manage sensor readings from multiple devices, allowing you to integrate them with other applications and services.
To send sensor readings to the Datacake API, you can use a variety of protocols and formats, including JSON. In this example, we demonstrate how to generate sensor readings as a dictionary with a unique serial_number
field, and how to format these readings into an array of objects that can be sent to the Datacake API.
Additionally, the Datacake API allows for timestamps to be recorded for each reading, which enables you to back-record multiple measurements of the same field in time. In this example, we include timestamps for each reading object in the array, and demonstrate how to modify the decoder function to handle these timestamps.
Overall, the Datacake API provides a flexible and powerful platform for collecting and managing sensor readings from a wide range of devices and applications.
Video
Single Device Data
Let's assume you send the following JSON to a Datacake API device.
In this example, the serial_number
field is a string that uniquely identifies the sensor that produced the readings. The other fields represent different measurements that the sensor might be capable of taking, such as temperature, humidity, pressure, acceleration, magnetic field, gyroscope, UV index, and ambient light. The values of these fields are floating-point numbers that represent the actual readings taken by the sensor.
Payload Decoder
Here's an example of a decoder function that parses the sensor readings from the payload of a Datacake API device request and returns them as an array of objects with the required format:
Simply paste the above decoder into the decoder section of your API device on Datacake.
Multiple Devices in Single Payload
In case you want to embed the data of multiple devices into a single payload, you simply have to adapt the payload decoder.
n this example, the Decoder
function receives a request
object that contains an array of JSON objects, each representing the sensor readings from a different device. The payload
variable is assigned the parsed JSON array from the request body, and the readings
array is initialized.
The function then iterates over the devices in the payload using a for
loop, and for each device, it extracts the serial_number
and assigns it to the device
variable. It then iterates over the fields in the device's payload using another for
loop, skipping the serial_number
field. For each field, it creates an object with the required field
, value
, and device
properties, and adds it to the readings
array.
Finally, the function returns the readings
array, which contains an object for each sensor reading with the required format of {field: field, value: value, device:device}
.
And here's an example of a fake data array containing readings from two devices:
Providing Timestamps
Datacake API devices allow you to backrecord historical data from both single and multiple devices (yes that is super awesome!). See the following example.
Demo Data
Decoder
Use the following decoder to decode the above data. Simply paste into Datacake API device decoder section.
In this example, the readings
array contains objects for each device with each field containing an array of readings that includes both a value
and a timestamp
. The Decoder
function now loops over each field in the payload, extracts the values
array, and loops over each reading in the array. For each reading, it creates an object with the required field
, value
, device
, and timestamp
properties, and adds it to the readings
array.
Note that the timestamp
property is now included in each reading object, and represents the Unix timestamp in seconds when the reading was taken.
Last updated