Developing local AWS Lambda 

We use Node.js to develop local AWS Lambda. Firstly, we write the Node.js program with the Raspberry Pi environment. Open the Raspberry Pi Terminal and create a folder called blinking. We will use the node-rpio library to access the Raspberry Pi GPIO.  This library can be found at https://github.com/jperkin/node-rpio.

  1. Navigate to the blinking folder from the Terminal. Then, we install the node-rpio library by typing the following command:
$ npm install rpio
  1. You also need to configure AWS Lambda to enable working with gpiomem. You can do that by typing the following command:
$ sudo cat >/etc/udev/rules.d/20-gpiomem.rules <<EOF
SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660"
EOF
  1. Now, create the index.js file to write our blinking program. You can write this complete program for the index.js file as follows:
const ggSdk = require('aws-greengrass-core-sdk');
var rpio = require('rpio');

const iotClient = new ggSdk.IotData();
const os = require('os');
const util = require('util');

function publishCallback(err, data) {
console.log(err);
console.log(data);
}

...

setTimeout(greengrassBlinkingdRun, 2000);

exports.handler = function handler(event, context) {
console.log(event);
console.log(context);
};
  1. Compress the index.js file included in the node_modules folder into a file called blinking.zip. Now you can upload the blinking.zip file into AWS Lambda Management Console.

Since this Lambda function accesses local resources, this Lambda cannot perform Lambda testing on AWS Lambda Management Console. You should only perform publishing directly.

Explanation: The following program will call the greengrassBlinkingRun() function once. Inside the function, it will open GPIO 18 (12 pin) and write the ;HIGH/LOW value into the GPIO. The program will also publish a message using the publish() function:

function greengrassBlinkingdRun() {
rpio.open(12, rpio.OUTPUT, rpio.LOW);
rpio.write(12, rpio.HIGH);
iotClient.publish(pubOpt, publishCallback);
rpio.sleep(1);

rpio.write(12, rpio.LOW);
state = rpio.LOW;
iotClient.publish(pubOpt, publishCallback);
rpio.sleep(1);
rpio.close(12);
}
setTimeout(greengrassBlinkingdRun, 2000);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.201.217