简体   繁体   English

AWS Lambda 无法正确发布到 IoT 主题

[英]AWS Lambda cannot publish to IoT topic properly

I have written Lambda function using JavaScript, which responses to my voice and turns on the LED on my Raspberry.我使用 JavaScript 编写了 Lambda 函数,它会响应我的声音并打开我的 Raspberry 上的 LED。 But I have a problem with publishing its state to my thing topic.但是我在将其状态发布到我的事物主题时遇到了问题。 While Alexa responses correct ("Turning on" if Im asking to turn it on and "Turning off" if asking to off), my topic doesn't always get the state changes.虽然 Alexa 响应正确(如果我要求将其打开,则为“打开”,如果要求关闭,则为“关闭”),但我的主题并不总是会发生状态变化。 Some times it gets data and sometime it doesn't and after few more invocations it gets data in bulk, and I cant even get the logic of creating a sequence of data in that bulk.有时它会获取数据,有时它不会,在多次调用之后,它会批量获取数据,我什至无法获得在该批量中创建数据序列的逻辑。

var AWS = require('aws-sdk');

var config = {};
config.IOT_BROKER_ENDPOINT      = "xxxxxx.iot.us-east-1.amazonaws.com";
config.IOT_BROKER_REGION        = "us-east-1";
config.IOT_THING_NAME           = "raspberry";

var iotData = new AWS.IotData({endpoint: config.IOT_BROKER_ENDPOINT});
var topic = 'LED';

exports.handler = function (event, context) {
...
...
function updatePowerState (intent, session, callback) {
    var speechOutput = '';
    var newValue = '';
    var repromptText = '';
    const cardTitle = 'Power';
    var sessionAttributes = {};
    const shouldEndSession = true;
    var value = intent.slots.PowerState.value;
    if(value == 'on' || value == 'off') {
        newValue = value.toUpperCase();
        speechOutput = 'Turning your lamp ' + value;
        updateShadow(newValue);
    } else {
        speechOutput = 'I didnt understand you. Please, repeat your request.';
    }
    callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));    
}

function updateShadow(newValue) {
        let payload = {
        state: {
            desired: {
                power_state: newValue
            }
        }
    };

    var JSON_payload = JSON.stringify(payload);

    var updates = {
        topic: topic,
        payload: JSON_payload,
        qos: 0
    };

    iotData.publish(updates, (err, data) => {
  if(err) {
      console.log(err);
  }
  else {
      console.log('Success!');
  }
});
}

Do you have any ideas about its causes?你对它的原因有什么想法吗? Thank you!谢谢!

Async methods like iotData.publish cause problems into AWS Lambda, because you request a execution and the lambda function ends soon without waiting for the response and processing the request. 诸如iotData.publish之类的异步方法会导致AWS Lambda出现问题,因为您请求执行,并且lambda函数很快结束,而无需等待响应和处理请求。

Another problem could be your permissions. 另一个问题可能是您的权限。

var AWS = require('aws-sdk');
var iotdata = new AWS.IotData({endpoint: 'iotCoreEndpoint.iot.us-east-1.amazonaws.com'});
    
exports.handler = async (event) => {
    var params = {
        topic: 'topic/topicName',
        payload: JSON.stringify(event.body),
        qos: 0
    };
    
    await iotdata.publish(params).promise()
};

Just make sure to add the required permissions or you can attach the following policy to your lambda role: AWSIoTWirelessFullPublishAccess只需确保添加所需的权限,或者您可以将以下策略附加到您的 lambda 角色:AWSIoTWirelessFullPublishAccess

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM