Python
How to connect a Python Script to Datacake

HTTP Request

Example Script

1
import requests
2
3
# The Webhook URL for your API Product on Datacake
4
url = "https://api.datacake.co/integrations/api/85dd4a6f-f587-4e5d-a522-c871caaba059/"
5
6
# Serial for device identification on Datacake
7
serial = "pythondevice001"
8
9
# Some random sensor data (replace with real data)
10
temperature = 16.04
11
humidity = 53.23
12
pressure = 1015
13
light = 734
14
power_status = True
15
lat = 51.4356
16
lon = 6.953
17
18
# Create a dictionary containing payload for webhook call
19
payload = {
20
"s": serial,
21
"t": temperature,
22
"h": humidity,
23
"p": pressure,
24
"l": light,
25
"ps": power_status,
26
"loc": "(" + str(lat) + "," + str(lon) + ")"
27
}
28
29
# Do a post request on the webhook including payload data
30
r = requests.post(url, json=payload)
Copied!

Decoder on Datacake

Below you will find the corresponding HTTP Payload decoder for the above Python Script that perform as HTTP Post Request.
1
function Decoder(request) {
2
3
// Parse JSON string in request body to real json
4
var payload = JSON.parse(request.body);
5
6
// Extract serial number in payload for device routing
7
var serial = payload.s;
8
9
// Return an array of dictionaries to Datacake
10
// Each dictionary contains device serial, database identifier and value
11
return [
12
{
13
device: serial, // Serial number of device
14
field: "TEMPERATURE", // Identifier of database field
15
value: payload.t // Actual data value for field
16
},
17
{
18
device: serial,
19
field: "HUMIDITY",
20
value: payload.h
21
},
22
{
23
device: serial,
24
field: "PRESSURE",
25
value: payload.p
26
},
27
{
28
device: serial,
29
field: "LIGHT",
30
value: payload.l
31
},
32
{
33
device: serial,
34
field: "POWER_STATUS",
35
value: payload.ps
36
},
37
{
38
device: serial,
39
field: "LOCATION",
40
value: payload.loc
41
},
42
];
43
}
Copied!

Routing via Serial Number

Python

1
# Serial for device identification on Datacake
2
serial = "pythondevice001"
Copied!

Datacake Decoder

1
// Extract serial number in payload for device routing
2
var serial = payload.s; // "pythondevice001"
3
4
// Return an array of dictionaries to Datacake
5
// Each dictionary contains device serial, database identifier and value
6
return [
7
{
8
device: serial, // Serial number of device "pythondevice001"
9
field: ...
10
value: ...
11
},
12
...
13
]
Copied!
Last modified 1mo ago