TTN Forum: proxy TTN uplink
@avbentem
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
decodeAndForward
auth
to use OAuth tokens and API keys in code via theauths object
params
Target URL

The target URL for the proxied payload. For testing, https://requestbin.com and https://beeceptor.com are great resources.

 
string ·params.targetUrl
Optional
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
}
67
const axios = require('axios');
const { headers, body } = event;
const { authorization, targetUrl } = params;

if (authorization && authorization !== headers.authorization) {
  await $respond({
    immediate: true,
    status: 401,
    body: 'Missing or incorrect Authorization header in request'
  });
  $end('Missing or incorrect Authorization header in request');
}

console.log('Got body', body);

// See https://www.thethingsnetwork.org/docs/applications/http/
// When using a Decoder in the application's Payload Formats in
// TTN Console, then body.payload_fields will be available too.
const bytes = Buffer.from(body.payload_raw, 'base64');

// Assuming a Kickstarter The Things Node, with the default sketch;
// see https://www.thethingsnetwork.org/docs/devices/node/
const events = {
  1: 'setup',
  2: 'interval',
  3: 'motion',
  4: 'button'
};

const decoded = {};
decoded.event = events[body.port] || 'unknown';
decoded.battery = (bytes[0] << 8 | bytes[1]) / 1000;
decoded.light = bytes[2] << 8 | bytes[3];
// Sign-extend to 32 bits to support negative values, by shifting
// 24 bits to the left (16 too far), followed by a sign-propagating
// right shift of 16 bits, to effectively shift 8 bits:
decoded.temperature = (bytes[4] << 24 >> 16 | bytes[5]) / 100;
decoded.comment = 'Decoded and enhanced in pipedream workflow';

const options = {
  url: targetUrl,
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  data: decoded
};

console.log('Will POST', options);

// Rather than using axios, one could also use $send.http (which would
// send fully asynchronously, after the workflow runs, and would not
// count against workflow quota)
const response = await axios(options);

const result = `Proxied to ${options.url}, ${response.status} ${response.statusText}`;
console.log(result);

// The HTTP Integration doesn't really care about the response
await $respond({
  immediate: true,
  status: 202,
  body: result
});