我在我的raspberry pi上运行一个Node.js mqtt订阅服务器,通过我的lambda aws代码监听已发布的事件。发布事件是通过Alexa Skill事件触发的。
实施例
" Alexa,告诉电视转到第10频道" - 然后我的Lambda代码将此意图识别为ChannelNumberIntent意图,然后将带有频道号的正确消息发布到我的Raspi上的mqtt服务器。然后Raspi执行更改通道逻辑。
我已经使用alexa-app-server在本地运行这个并且它完全正常。 Alexa的响应工作,mqtt服务器收到正确的数字/消息包。
当我在测试选项卡上使用开发人员技能门户中的Alexa技能进行测试时,会出现问题。它运行正常,从收到的lambda说消息返回响应,但mqtt用户从未收到通道数字/消息包?我相信在困扰我的lambda代码几个小时之后的某个地方搞砸了。这是我的代码;
LAMBDA AWS CODE
'use strict';
module.change_code = 1;
var Alexa = require('alexa-app');
var skill = new Alexa.app('tvRemote');
var awsIot = require('aws-iot-device-sdk');
var deviceName = "tvRemote";
var _ = require('lodash');
var path = require('path');
var host = "randomletters.iot.us-east-1.amazonaws.com";
//App id is the skill being used.
var app_id = "amzn1.ask.skill.more random letters";
var thingShadows = awsIot.thingShadow({
keyPath: path.join(__dirname, '/Raspi.private.key'),
certPath: path.join(__dirname, '/Raspi.cert.pem'),
caPath: path.join(__dirname, '/root-CA.crt'),
clientId: deviceName,
region: "us-east-1",
});
var reprompt = 'I didn\'t hear a channel, tell me a channel number or name to change to that channel';
skill.launch(function (request, response) {
var prompt = 'To change channel, tell me a channel number.';
response.say(prompt).reprompt(reprompt).shouldEndSession(true);
});
skill.intent('ChannelNumberIntent', {
'slots': {
'CHANNELNUMBER': 'CHANNELID'
},
'utterances': ['{|Change|put} {|the|on} {|channel} {|to} {-|CHANNELNUMBER}']
},
function (request, response) {
var channelNumber = request.slot('CHANNELNUMBER');
if (_.isEmpty(channelNumber)) {
var prompt = 'I didn\'t hear a channel code. Tell me a channel code.';
response.say(prompt).shouldEndSession(true);
return true;
} else {
thingShadows.publish('lambda/channelNumber', channelNumber, function () {
var prompt1 = 'Okay.';
response.say(prompt1).shouldEndSession(false);
return true;
});
return true;
}
}
);
module.exports = skill;
MQTT服务器
'use strict'
var mqtt = require('mqtt')
var fs = require('fs')
var path = require('path')
var KEY = fs.readFileSync(path.join(__dirname, '/Raspi.private.key'))
var CERT = fs.readFileSync(path.join(__dirname, '/Raspi.cert.pem'))
var TRUSTED_CA_LIST = fs.readFileSync(path.join(__dirname, '/root-CA.crt'))
var PORT = Corretportnumber
var HOST = 'RASPIHOSTWORDSASDFSDF.iot.us-east-1.amazonaws.com'
var options = {
port: PORT,
host: HOST,
key: KEY,
cert: CERT,
rejectUnauthorized: true,
// The CA list will be used to determine if server is authorized
ca: TRUSTED_CA_LIST
}
var client = mqtt.connect('mqtts://' + HOST, options)
client.subscribe('lambda/channelNumber')
client.on('connect', function () {
console.log('Connected')
})
client.on('message', function (topic, message) {
if (topic === 'lambda/channelNumber') {
// logic.changeChannelCode(message);
console.log(message.toString());
}
})
显然隐藏了主机和端口号