Datacake Docs
  • Welcome
  • Get Started
  • Changelog
  • Best practices
    • Best practices: Getting started
    • Best practices: Resolution (Timeframe)
    • Best practices: Dashboards
    • Best practices: Reports
    • Best practices: Grouping Devices
  • Datacake LNS
    • Getting Started
    • Gateways
      • Milesight LoRaWAN Gateway - UG6x
      • RAKwireless WisGate Edge Light 2
    • Devices
    • Add Devices without Gateway
  • Device
    • Product
    • Configuration
    • Claiming
    • Historical Data
    • Database
      • Configuration Fields
      • Fields
        • Manual input
        • Field Roles
        • Formulas
        • Mapping Fields
      • Data retention & Datapoints
      • Examples
        • Mapping 4-20mA Signals
        • Converting Units
  • Dashboards
    • Global Dashboards
      • Setting Homepage
    • Device Dashboards
    • Multi-Device Dashboards
    • Widgets
      • Value Widget
      • Chart Widget
      • Image Map
      • Map Widget
      • Text Widget
      • SOS Widget
      • Boolean Widget
      • iFrame Widget
      • Downlink Widget
      • Set Value Widget
      • Measurement List Widget
      • Heatmap Widget
      • Table Widget
      • Image Widget
  • Portal
    • Multi-Tenancy (Workspaces)
    • Reports
      • Energy Report
    • Administrators
    • Members
      • API Users
    • Security & Privacy
    • Billing
      • Support Packages
      • VAT Exemption / Tax ID
      • SMS Credits
      • Access Invoices
      • Unused time and Remaining time in Invoices (Prorations)
      • Codes
    • White Label
    • Rules
      • Legacy Rule Engine
        • Sending notifications via Telegram
      • New Rule Engine
        • Rule Engine Table Overview
        • Copy/Paste and Template Functionality
        • Advanced Rule Engine E-Mail and Webhook Templates
        • Time Restrictions
    • Zones
  • Cake Red
    • Get Started
    • Overview
  • LoRaWAN
    • Get Started
      • CSV Import
      • Custom LoRaWAN Device
    • Configuring LNS
      • The Things Stack (TTN / TTI) Automated Setup
      • The Things Stack (TTN / TTI) Manual Setup
      • Loriot
      • Kerlink Wanesy
      • Helium
      • ChirpStack
      • Tektelic
      • Actility ThingPark
      • Senet
      • Milesight Gateway
      • KPN
    • Downlinks (LoRaWAN)
      • Set Time Frame
    • Securing Webhooks
    • Payload Decoders
      • Location and GPS
      • Access Gateway Metadata
      • Access Measurements
      • Global Variables
    • Using Cayenne LPP
    • Converting Payload
  • Generic API Devices
    • HTTP Downlinks
  • Integrations
    • MQTT
    • Particle
      • Get Started
      • Adding Integrations
      • Decoding Payloads
      • Calling Functions
      • Templates
        • Particle Tracker One
    • Incoming Webhooks
    • Outgoing Webhooks
      • Securing Outgoing Webhooks in Datacake
    • Golioth
    • Blues Wireless Notecard
    • Sigfox
    • Swarm Asset Tracker
    • Grandcentrix Modbus Cloud Connect
    • YuDash LYNX IoT Gateway
    • Dragino NB-IoT
      • Changing NB-IoT Bands
    • Hardwario CHESTER
    • 1NCE OS
  • API
    • Exporting Data
    • Record Measurements via API
    • Node RED to Datacake
    • Generate Access Token
    • Internal MQTT
      • Get Started
      • MQTT Documentation
      • MQTT over WebSocket
      • Example Code
    • GraphQL API
      • Using GraphQL
      • Device Creation
      • Tutorials
        • Read Group via Tags
  • Guides
    • Python
    • Send Slack Notifications
    • Forward Data to Cake Red
    • Multiple Data Feeds
    • Automated Dynamic Downlinks
    • Ingesting JSON Data into Datacake API Devices
    • Working with Default HTTP Decoder on API Devices and Code Examples
    • Accessing Measurements in Decoders
    • Connecting Datacake to Notion Using Zapier
    • How to set up and use location on non-GPS devices
    • How to integrate with AWS IoT Core
    • How to Schedule Mass-Downlinks (Multicast) using Datacake Rule Engine
    • How to Map Sensor Values to Ranges or Booleans, Strings using Mapping Fields
    • Understanding Internal vs. External MQTT Brokers on Datacake
    • Sending UDP Messages between 1NCE OS and Datacake
    • Concepts of LoRaWAN Payload Decoders on Datacake
    • How to check if a value stays above or below limits for given time
Powered by GitBook
On this page
  • Introduction
  • Video
  • Single Device Data
  • Payload Decoder
  • Multiple Devices in Single Payload
  • Providing Timestamps
  • Demo Data
  • Decoder

Was this helpful?

  1. Guides

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.

Last updated 11 months ago

Was this helpful?

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.

{
    "serial_number": "ABC123",
    "temperature": 24.5,
    "humidity": 53.2,
    "pressure": 1013.25,
    "acceleration_x": 0.12,
    "acceleration_y": -0.23,
    "acceleration_z": 1.01,
    "magnetic_field_x": 0.85,
    "magnetic_field_y": -0.92,
    "magnetic_field_z": 0.12,
    "gyroscope_x": -0.02,
    "gyroscope_y": 0.15,
    "gyroscope_z": -0.08,
    "uv_index": 5.2,
    "ambient_light": 132.5
}

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:

function Decoder(request) {
    var payload = JSON.parse(request.body);
    var device = payload.serial_number;
    var readings = [];
    
    for (var field in payload) {
        if (field !== "serial_number") {
            readings.push({
                field: field,
                value: payload[field],
                device: device
            });
        }
    }
    
    return readings;
}

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.

function Decoder(request) {
    var payload = JSON.parse(request.body);
    var readings = [];
    
    for (var i = 0; i < payload.length; i++) {
        var device = payload[i].serial_number;
        for (var field in payload[i]) {
            if (field !== "serial_number") {
                readings.push({
                    field: field,
                    value: payload[i][field],
                    device: device
                });
            }
        }
    }
    
    return readings;
}

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:

[
    {
        "serial_number": "ABC123",
        "temperature": 24.5,
        "humidity": 53.2,
        "pressure": 1013.25
    },
    {
        "serial_number": "DEF456",
        "temperature": 22.1,
        "humidity": 60.7,
        "pressure": 1012.85
    }
]

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

// Example fake data array containing readings from two devices with timestamps
var readings = [
    {
        "serial_number": "ABC123",
        "temperature": [
            { value: 24.5, timestamp: 1648909263 },
            { value: 23.8, timestamp: 1648909273 },
            { value: 22.9, timestamp: 1648909283 }
        ],
        "humidity": [
            { value: 53.2, timestamp: 1648909263 },
            { value: 52.5, timestamp: 1648909273 },
            { value: 51.8, timestamp: 1648909283 }
        ],
        "pressure": [
            { value: 1013.25, timestamp: 1648909263 },
            { value: 1013.12, timestamp: 1648909273 },
            { value: 1012.98, timestamp: 1648909283 }
        ]
    },
    {
        "serial_number": "DEF456",
        "temperature": [
            { value: 22.1, timestamp: 1648909263 },
            { value: 22.9, timestamp: 1648909273 },
            { value: 23.5, timestamp: 1648909283 }
        ],
        "humidity": [
            { value: 60.7, timestamp: 1648909263 },
            { value: 59.8, timestamp: 1648909273 },
            { value: 58.9, timestamp: 1648909283 }
        ],
        "pressure": [
            { value: 1012.85, timestamp: 1648909263 },
            { value: 1013.01, timestamp: 1648909273 },
            { value: 1013.18, timestamp: 1648909283 }
        ]
    }
];

Decoder

Use the following decoder to decode the above data. Simply paste into Datacake API device decoder section.

// Example decoder function that includes timestamps for each reading
function Decoder(request) {
    var payload = JSON.parse(request.body);
    var readings = [];
    
    for (var i = 0; i < payload.length; i++) {
        var device = payload[i].serial_number;
        for (var field in payload[i]) {
            if (field !== "serial_number") {
                var values = payload[i][field];
                for (var j = 0; j < values.length; j++) {
                    var reading = {
                        field: field,
                        value: values[j].value,
                        device: device,
                        timestamp: values[j].timestamp
                    };
                    readings.push(reading);
                }
            }
        }
    }
    
    return readings;
}

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.