homepage
  • GitHub
  • Community
  • Network
  • Introduction
  • Device

Triggering IFTTT Services with Device Data

This guide will show you how to route your device data from Console to a serverless function service that will allow you to decode the base64 encoding that the packet payload is encoded with and then trigger a simple service on IFTTT with the payload.

IMPORTANT:

This guide is intended for the longfi-arduino ButtonTransmit example sketch.

If you haven’t completed the Console and device quickstart instructions, please perform those here before continuing with this guide.

This guide will show you how to perform the following:

  • Create and configure a PubNub Function to decode a base64 payload from your device.
  • Add the PubNub Function endpoint as an HTTP channel in Console.
  • Create and configure an IFTTT Applet to send an email with the payload.
  • Update the PubNub Function with the IFTTT Webhook endpoint address.

Creating PubNub Function

First you will need to register for a free PubNub account. No credit card required!

Next login to the PubNub dashboard.

Choose Other Messaging Use Cases and click START USING PUBNUB.

Navigate to the Apps page on the left menu.

Click CREATE NEW APP, name it DecodeAndForward, and finally select Other Messaging Use Cases.

Your app should appear:

Next on the left-hand menu click Functions, and make sure the DecodeAndForward app is selected.

Click CREATE NEW MODULE to create a new module and enter the following name decode_and_forward.

Next create a function by clicking the decode_and_forward module.

Click CREATE FUNCTION and fill in the fields with the following:

  • Function name: decode64_and_forward
  • Event type: On Request
  • URI path: decode

Click CREATE.

You will be dropped into the Function editor with a default Javascript request function:

Replace the entire function with the provided code below:

export default (request, response) => {
const pubnub = require('pubnub');
const xhr = require('xhr');
const base64Codec = require('codec/base64');
let headersObject = request.headers;
let paramsObject = request.params;
let methodString = request.method;
let bodyString = request.body;
// ref: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
var b64ToUint6 = function(nChr) {
return nChr > 64 && nChr < 91 ?
nChr - 65
: nChr > 96 && nChr < 123 ?
nChr - 71
: nChr > 47 && nChr < 58 ?
nChr + 4
: nChr === 43 ?
62
: nChr === 47 ?
63
:
0;
}
// ref: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
var base64DecToArr = function(sBase64, nBlockSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
nOutLen = nBlockSize ? Math.ceil((nInLen * 3 + 1 >>> 2) / nBlockSize) * nBlockSize : nInLen * 3 + 1 >>> 2, aBytes = new Uint8Array(nOutLen);
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
aBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;
}
}
return aBytes;
}
// ref: https://stackoverflow.com/a/34310051
var byteArrayToHexString = function(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
// ref: https://stackoverflow.com/a/14090278
var parseFloatHex = function(str) {
var float = 0, sign, order, mantiss,exp,
int = 0, multi = 1;
var i;
int = parseInt(str,16);
sign = (int>>>31)?-1:1;
exp = (int >>> 23 & 0xff) - 127;
mantiss = ((int & 0x7fffff) + 0x800000).toString(2);
for (i=0; i<mantiss.length; i+=1){
float += parseInt(mantiss[i])? Math.pow(2,exp):0;
exp--;
}
return float*sign;
}
// parse request body json
let bodyJson = JSON.parse(request.body);
// decode payload into byte array
var DecodedByteArray = base64DecToArr(bodyJson.payload, 2);
//console.log("DecodedByteArray: " + DecodedByteArray);
// convert byte array to hex string
var hexString = byteArrayToHexString(DecodedByteArray);
//console.log("hexString: " + hexString);
// parse bytes 0-3 and reverse order for value1
var value1HexString = byteArrayToHexString(DecodedByteArray.slice(0,4).reverse());
var value1 = parseInt(value1HexString, 16);
console.log("value1(uint32_t): " + value1);
// parse bytes 4-7 and reverse order for value2
var value2HexString = byteArrayToHexString(DecodedByteArray.slice(4,8).reverse());
var value2 = parseInt(value2HexString, 16);
// check sign, ref: https://stackoverflow.com/a/13468626
if ((value2 & 0x8000) > 0) {
value2 = value2 - 0x10000;
}
console.log("value2(int16_t): " + value2);
// parse bytes 8-11 and reverse order for value3
var value3HexString = byteArrayToHexString(DecodedByteArray.slice(8,12).reverse());
var value3 = parseFloatHex(value3HexString).toFixed(5);
console.log("value3(float): " + value3);
// create new JSON payload with values
var iftttJson = {
value1: value1,
value2: value2,
value3: value3
}
//POST parsed JSON payload to IFTTT webhook service
xhr.fetch('PLACEHOLDER', {
'method': 'POST',
'body' : iftttJson
}).then(() => {
console.log("Packet sent with payload: " + JSON.stringify(iftttJson));
}).catch((err) => {
console.log(err);
});
// Set the status code - by default it would return 200
response.status = 200;
// Set the headers the way you like
response.headers['X-Custom-Header'] = 'CustomHeaderValue';
return request.json().then((body) => {
return response.send("OK");
}).catch((err) => {
console.log(err)
return response.send("Malformed JSN body.");
});
};

... like so

This simple function allows us to take the payload field of the packet coming from Console, perform a base64 decode operation, and then forward it to any desired HTTP endpoint.

After replacing the code, click SAVE, and then Click Start Module.

If you receive “ERROR: Missing required fields: code”, completely remove the code and re-paste it. Then click **SAVE**.

Before leaving this dashboard, copy the Function’s endpoint URL by clicking COPY URL on the left panel in the Path entry box.

Save that URL in a text editor, you'll need it later.

Creating Console HTTP Channel with PubNub Function Endpoint

Next in another tab open the Helium Console, and navigate to the Channels section.

Click the HTTP icon to create an HTTP channel. Then copy the PubNub Function endpoint URL into the Endpoint field, and select POST from the drop down.

Name your channel pubnub_decode_forward.

Make sure your device is configured to use this pubnub_decode_forward channel. If you need to change the Channel from a previous one, this process may take up to 5 minutes or longer.

Return to the previous PubNub Function editor window.

IMPORTANT:

If you haven’t plugged in your transmitting device from the device quickstart do so now.

You should see error messages in the PubNub console window located at the bottom of the editor view.

If you are not seeing anything, restart the Function by clicking Restart module at the top right of the window.

Let’s unplug the transmitting device to avoid exhausting the maximum transactions allowed with the free plan, although it's 1,000,000.

Creating IFTTT Applet to email packet payload

First create a free IFTTT account if you don’t already have one and sign in.

Next visit https://ifttt.com/create to create an applet.

Click the large plus button after If to select a triggering service. Search for Webhooks and select the service by clicking the box seen below.

Next select the only trigger action this service provides, seen below. Click the Receive a web request box.

Let’s name this forward_packet_email, and click Create trigger.

Great, you’ve added the service that will receive the payload packet from the PubNub Function, we’ll configure that soon, but first let's pick the action, or what happens when the trigger is activated.

At this point you could pick any one of thousands of actions like sending a tweet, text, or even turning on a smart light bulb, but for simplicity we will use the email service to send a simple email with the payload each time a packet is forwarded.

Click the + sign after Then, search for Email, and select Email.

Enter your email address and the pin sent to that address.

Select the single action offered from this service:

Next use the default field configuration and click Create action.

Finally click Finish.

Navigate to the Webhooks service page and click Documentation at the top right:

On this page enter the name chosen for the webhook event name, forward_packet_email. Go ahead and type that into the box with ‘{event}’.

Next copy the webhook endpoint URL at the bottom, this is what the PubNub Function will be making an HTTP Post request to.

Update PubNub Function POST URL

Ok, almost done! Return to the PubNub Function editor window.

At the line with xhr.fetch replace the PLACEHOLDER string with the IFTTT Webhook URL in the function.

Next click Save, and then the Restart module.

Congratulations! You've configured the entire pipeline, click CLEAR in the bottom console and then plug the device back in to begin transmitting again.

You should no longer see errors in the Console and should receive emails from IFTTT. Congrats!