Skip to content

Instantly share code, notes, and snippets.

@pettazz
Last active July 29, 2018 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pettazz/f83efb4f06e6852faa5a1dfef616f166 to your computer and use it in GitHub Desktop.
Save pettazz/f83efb4f06e6852faa5a1dfef616f166 to your computer and use it in GitHub Desktop.
AWS Lambda function to be triggered by an IoT button click, sends a message to SQS consumed by a homebridge server
/**
* This is a sample Lambda function that sends a message to an SQS queue
* when an IoT button is pressed. The message format is defined by the
* homebridge-sqs plugin: https://www.npmjs.com/package/homebridge-sqs
*
* The following JSON template shows what is sent as the payload:
{
"serialNumber": "GXXXXXXXXXXXXXXXXX",
"batteryVoltage": "xxmV",
"clickType": "SINGLE" | "DOUBLE" | "LONG"
}
*
* A "LONG" clickType is sent if the first press lasts longer than 1.5 seconds.
* "SINGLE" and "DOUBLE" clickType payloads are sent for short clicks.
*
* Partly stolen from:
* http://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html
*/
'use strict';
const AWS = require('aws-sdk');
const SQS = new AWS.SQS();
const QUEUE_URL = 'YOUR_QUEEUEUE_URL_HERE';
const SOURCE_NAME = "buttonNameSomething";
exports.handler = (event, context, callback) => {
console.log('Received event:', event);
var message = {
"source": SOURCE_NAME,
"message": event.clickType
};
var params = {
MessageBody: JSON.stringify(message),
QueueUrl: QUEUE_URL,
DelaySeconds: 0
};
SQS.sendMessage(params, function(err, data){
if(err){
console.log(err, err.stack);
callback(err);
return;
}else{
console.log(data); //we did it im proud of us
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment