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
  • Wireless IoT Hub
    • Overview
    • Datasheet
    • Getting Started
  • 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
        • Actions
          • Set Value
    • Zones
  • Cake Red
    • Get Started
    • Overview
  • LoRaWAN
    • Get Started
      • CSV Import
      • Custom LoRaWAN Device
        • How to create a device without a product template
    • 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
  • Concept
  • Raw Metadata
  • Example Usage
  • Create corresponding fields in Database
  • Extract Location
  • Example

Was this helpful?

  1. LoRaWAN
  2. Payload Decoders

Access Gateway Metadata

Concept

You can access the global variable normalizedPayload which contains the following metadata:

{
    "deveui": string, // The device's DevEUI
    "port": number, // The message's port
    "counter": number,
    "frequency": number, // GHz
    "data_rate": string, // Data rate according to https://blog.dbrgn.ch/2017/6/23/lorawan-data-rates/
    "coding_rate": string,
    "gateways": [
        "id": string,
        "datetime": date,
        "channel": number,
        "rssi": number,
        "snr": number
    ]
}

This data is extracted from the LoRaWAN Network Server as being forwarded to Datacake over the Webhook. This information is normalized, meaning no matter whether you use TTN, TTI, Loriot, ChirpStack, or Wanesy, the information is always equally available and always formatted in the same way.

Raw Metadata

If you rather want to access the raw payload received from the LNS, you can do so via the global rawPayload variable.

Please note that when you access the rawPayload, the information is not normalized and the content of rawPayload looks different between different LoRaWAN Network Server!

Example Usage

In the following snippet, you find an example of how to read the Metadata from your Payload and how to bring that into your Datacake Payload Decoder section.

function Decoder(payload, port) {
    
    // Output normalized Payload
    console.log(JSON.stringify(normalizedPayload,0,4));
    
    // Output raw payload coming from webhook of your LNS
    console.log(JSON.stringify(rawPayload,0,4));    
    
    // Extract RSSI and Data-Rate from normalized Payload:
    // As being "normalized" this works between all LNS:
    // Including safety check so that these values really exist
    var LORA_RSSI = (!!normalizedPayload.gateways && !!normalizedPayload.gateways[0] && normalizedPayload.gateways[0].rssi) || 0;
    var LORA_SNR = (!!normalizedPayload.gateways && !!normalizedPayload.gateways[0] && normalizedPayload.gateways[0].snr) || 0;
    var LORA_DATARATE = normalizedPayload.data_rate;    
    
    // extract timestamp from gateway
    var ts = rawPayload.recvTime; // Kerlink Wanesy
    
    // use normalized payload - NOTE: must have at least one Gateway!
    var ts = normalizedPayload.gateways[0]["datetime"];
    
    // Build up an array that can be used to forward it onto Datacake:
    var decoded = [
        {
            field: "LORA_RSSI",
            value: LORA_RSSI,
            timestamp: ts            
        },
        {
            field: "LORA_DATARATE",
            value: LORA_DATARATE,
            timestamp: ts
        }
    ];
    
    return decoded;
}

Alternate Version

function Decoder(bytes, port) {
    
    var decoded = {};
    
    decoded.battery = ((bytes[0] << 8 | bytes[1]) & 0x3FFF) / 1000;
    decoded.distance = bytes[2] << 8 | bytes[3];
    decoded.temperature = (bytes[4] << 8 | bytes[5]) / 10.0;
    
    // Extract Gateway Information
    
    try {
        
        decoded.LORA_RSSI = (!!normalizedPayload.gateways && !!normalizedPayload.gateways[0] && normalizedPayload.gateways[0].rssi) || 0;
        decoded.LORA_SNR = (!!normalizedPayload.gateways && !!normalizedPayload.gateways[0] && normalizedPayload.gateways[0].snr) || 0;
        decoded.LORA_DATARATE = normalizedPayload.data_rate;   
        
    } catch (e) {
        
        console.log(JSON.stringify(e));
    }
    
    return decoded;

}

Create corresponding fields in Database

In order to store those Metadata-Fields, you can create new Fields on your Device and have historical Data for each field that you forward and store. Here is how this looks for the above example:

Extract Location

Some LNS (depending also on settings) embed gateway information in uplinks and send it with the webhook forward to Datacake.

You can use the rawPayload variable to access it and forward it onto a location field on your device.

Example

The Things Network / The Things Stack

Because we are using the rawPayload Object, the following example only works with The Things Stack LNS, aka The Things Network

function Decoder(bytes, port) {

    var datacakeFields = [];
    
    try {

        // Please note that there can be more than just one Gateway
        // in the webhook data. This examples always uses the first.

        var lat = rawPayload.uplink_message.rx_metadata[0].location.latitude
        var lon = rawPayload.uplink_message.rx_metadata[0].location.longitude

        // in order to be able to forward location data to Datacake
        // We need to build a tuple that contains latitude + longitude
        // surrounded by parenthesis

        var location = "(" + lat + "," + lon + ")";
    
        // now forward the data to datacake
        // please make sure to create a location field called GATEWAY_LOCATION
        
        datacakeFields.push({
            field: "GATEWAY_LOCATION",
            value: location
        });
     
    } catch (e) {
        console.log("Error fetching location. Probably gateway data missing.");
    }
    
    return datacakeFields;
}

Last updated 2 years ago

Was this helpful?