In this article, I am going to explain how to call functions on the Particle Console using AWS Lambda.
This use case was initially developed for a switchCase around Particle Tracker SOM Evaluation Board in order to allow different personas based on goods being tracked; for this example I chose "cold chain" and "chemical transport" as my useCases.The main reasons for using a switchCase include improving clarity, by reducing otherwise repetitive coding, and also offering the potential for faster execution based on multiple use cases.
A Bird's-Eye ViewIt all starts when calling a Lambda function. This triggers Particle's API callFunction to call a function on Particle Cloud. From there, the device use case will change based on persona that is being tracked.
Let's try to illustrate that with a picture:
Here is a list of what needs to be done in order to achieve our goal:
- Flash switchCase code onto your device.
- Create an AWS Lambda function
- Edit AWS Lambda function based on Particle API JS callFunction
- Configure test event
- Test event
For this project I am using a Tracker SOM, however you are able to do this with any Particle device.
Assuming you have on-boarded your Particle device, go ahead and flash switchCase FW onto your device (check git below).
This will allow your Tracker SOM to create a function called "useCase", when this function is called the persona of the device changes. For this example I have used the following personas:
- "cold chain" : Push location with Temperature and Humidity tracking connected through J10 using BME 280 sensor
- "chemical" : Push location with Temperature/Humidity and Air Quality tracking connected through J11 using Air Quality v1.2 sensor
After onboarding your Particle device and flashing the FW, log into your AWS console and do the following steps;
- Create a new function in Lambda Services.
- Use a blueprint and search for Hello World Node JS
- Under role; Create a new role from AWS policy templates
- For policy template search "Simple Microservice Permissions"
Should look something like this:
Now go to your terminal and create the following:
- Create a directory on your desktop named particlelambda
- Create a file called trackersomAWS.js
- Create an npm program and enter till the end.
- After that go ahead an install particle-api-js
Should look something like this
cd Desktop && mkdir particlelambda
cd particle lambda
touch trackersomAWS.js
npm init
npm install particle-api-js
Now go ahead and open trackersomAWS.js file that you just created in workbench and paste this Lambda function code to this:
var Particle = require('particle-api-js');
var particle = new Particle();
console.log('Loading function');
exports.handler = async (event, context, callback) => {
var fnPr = particle.callFunction({ deviceId: event.deviceId, name: 'useCase', argument: event.argument, auth: event.auth });
fnPr.then(
function(data) {
callback(null, data);
}, function(err) {
callback(err, null);
});
};
This snippet is tailored using Particle SDK and the original function code seen above.
Now go ahead to the file on your desktop name particlelambda and compress the file you created along with node modules. Should look something like this:
After compressing, go ahead into your lambda function that we just created and upload zip file under Actions.
Finally in your lambda function, go ahead and create a new test function (cmd + J in mac)
Add the following script:
{
"argument": "chemical",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
deviceId: can be found on your Particle device page under deviceId
auth: follow particle create token on CLI (make sure to run never expires)
Go ahead and test the Lambda function and your device should switchCase between "cold chain" and "chemical" based on your argument.
This will trigger the callFunction on the Particle console and change data based on useCase (check screenshots above of data published on Particle Console)
Comments
Please log in or sign up to comment.