-2

I am trying to get data into DynamoDB from an IOT SENSOR connected to AWS IOT CORE sending data via MQTT. I want all values in seperate columns so I use a Lambda function for this. Here it goes:

On topic EXAMPLE/shadow/update I receive following as a UTF8 STRING from the sensor:

{"SystemID":34,"SensorID":96,"ValueMax":87,"ValueMin":78}

I then have a ACT/RULE which sorts by: SELECT * FROM 'EXAMPLE/shadow/update' and trigger a LAMBDA function with following code:

console.log('Loading function');
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB.DocumentClient();
var table = "TEST_TABLE";

exports.handler = function(event, context) {
    console.log('Received event:', JSON.stringify(event, null, 2));
   var params = {
    TableName:table,
    Item:{
        "SystemID": event.SystemID,
        "SensorID": event.SensorID,
        "ValueMax": event.ValueMax,
        "ValueMin": event.ValueMin,
        "Timestamp": String(Date.now())
        }
    };

    console.log("Adding...");
    dynamo.put(params, function(err, data) {
        if (err) {
            console.error("Error JSON:", JSON.stringify(err, null, 2));
            context.fail();
        } else {
            console.log("Added:", JSON.stringify(data, null, 2));
            context.succeed();
        }
    });
}

Issue: It doesnt work! But if I copy the payload from the sensor and send to the MQTT via AWS test console it gets posted as a JSON and then the Lambda function works and puts it properly into the DynamoDB.

What am I doing wrong?

aurelius
  • 448
  • 7
  • 23

1 Answers1

0

Ok I found the solution. I narrowed the SELECT down a bit from earlier "*" and now it works!

SELECT SystemID,SensorID,ValueMin,ValueMax FROM 'EXAMPLE/shadow/update'
aurelius
  • 448
  • 7
  • 23