Python

How to connect a Python Script to Datacake

HTTP Request

Example Script

import requests

# The Webhook URL for your API Product on Datacake
url = "https://api.datacake.co/integrations/api/85dd4a6f-f587-4e5d-a522-c871caaba059/"

# Serial for device identification on Datacake
serial = "pythondevice001"

# Some random sensor data (replace with real data)
temperature = 16.04
humidity = 53.23
pressure = 1015
light = 734
power_status = True
lat = 51.4356
lon = 6.953

# Create a dictionary containing payload for webhook call
payload = {
    "s": serial,
    "t": temperature,
    "h": humidity,
    "p": pressure,
    "l": light,
    "ps": power_status,
    "loc": "(" + str(lat) + "," + str(lon) + ")"
}

# Do a post request on the webhook including payload data
r = requests.post(url, json=payload)

Decoder on Datacake

Below you will find the corresponding HTTP Payload decoder for the above Python Script that perform as HTTP Post Request.

function Decoder(request) {

    // Parse JSON string in request body to real json
    var payload = JSON.parse(request.body);

    // Extract serial number in payload for device routing
    var serial = payload.s;
    
    // Return an array of dictionaries to Datacake
    // Each dictionary contains device serial, database identifier and value
    return [
        {
            device: serial, // Serial number of device
            field: "TEMPERATURE", // Identifier of database field
            value: payload.t // Actual data value for field
        },
        {
            device: serial,
            field: "HUMIDITY",
            value: payload.h
        },
        {
            device: serial,
            field: "PRESSURE",
            value: payload.p
        },
        {
            device: serial,
            field: "LIGHT",
            value: payload.l
        },
        {
            device: serial,
            field: "POWER_STATUS",
            value: payload.ps
        },
        {
            device: serial,
            field: "LOCATION",
            value: payload.loc
        },
    ];
}

Routing via Serial Number

Python

# Serial for device identification on Datacake
serial = "pythondevice001"

Datacake Decoder

// Extract serial number in payload for device routing
var serial = payload.s; // "pythondevice001"

// Return an array of dictionaries to Datacake
// Each dictionary contains device serial, database identifier and value
return [
    {
        device: serial, // Serial number of device "pythondevice001"
        field: ...
        value: ...
    },
    ...
]

Last updated