Dialogflow 使用 Firebase 连接到 MQTT 代理?
Posted
技术标签:
【中文标题】Dialogflow 使用 Firebase 连接到 MQTT 代理?【英文标题】:Dialogflow connect to MQTT broker using Firebase? 【发布时间】:2020-06-04 06:42:46 【问题描述】:我正在尝试使用 Cloud Functions for Firebase 使 Dialogflow 意图导致发布 MQTT 消息。我已经设法获得了我需要的数据,但我仍然无法完成的是:
-
建立与 MQTT 代理的连接;
向上述经纪人发布。
因为第二个需要前者,所以我不关心还。
我的看法是:要么我的代码错误,要么 MQTT 包含在 Firebase 所谓的“外部网络”中。我来这里主要是为了确保我不会白白升级到付费计划。
index.js
:
'use strict';
const functions = require('firebase-functions');
const WebhookClient, Card, Suggestion = require('dialogflow-fulfillment');
var mqtt = require('mqtt');
process.env.DEBUG = 'dialogflow:debug';
const HOST = 'broker.mqttdashboard.com';
const PORT = 1883;
const TOPIC = 'topic/voice_recog';
exports.dialogflowFirebaseFulfillment = functions.region('europe-west1').https.onRequest((request, response) =>
const agent = new WebhookClient( request, response );
try
function publishParameter(agent)
let message = agent.parameters.param_test;
return publishToMqtt(message).then((output) =>
agent.add(output);
).catch(error =>
agent.add('error from publishToMqtt');
);
let intentMap = new Map();
intentMap.set('Repeat parameter', publishParameter);
agent.handleRequest(intentMap);
catch (err)
console.error(err);
agent.add(err.message);
agent.send_();
);
function publishToMqtt(message)
console.log("------------------------------");
console.log("Topic: \""+TOPIC+"\"");
console.log("Message: \""+message+"\"");
console.log("------------------------------");
return new Promise((resolve, reject) =>
var options =
port: PORT,
host: HOST,
clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
keepalive: 60,
reconnectPeriod: 1000,
protocolId: 'MQIsdp',
protocolVersion: 3,
clean: true,
encoding: 'utf8'
;
var client = mqtt.connect("mqtt://"+HOST, port: PORT);
// is not executed
client.on('connect', function ()
console.log('client connected');
);
// is not executed
client.publish(TOPIC, message, , function (err)
console.log("Tried publishing \""+message+"\" to \""+TOPIC+"\".");
if (err)
console.log("But the future refused to change:" + err);
reject();
else
resolve(message);
client.end();
clearTimeout(noResp);
);
let noResp = setTimeout(() =>
console.log("No connection"); // always gives this result
reject();
client.end();
, 5000);
);
package.json
:
"name": "assistant-to-mqtt",
"description": "publishes intent parameter to MQTT broker",
"engines":
"node": "8"
,
"version": "0.0.1",
"private": true,
"scripts":
"start": "firebase serve --only functions:publish_mqtt",
"deploy": "firebase deploy --only functions:publish_mqtt"
,
"dependencies":
"actions-on-google": "^2.1.3",
"dialogflow-fulfillment": "^0.4.1",
"firebase-admin": "^5.12.1",
"firebase-functions": "^2.2.1",
"mqtt": "^2.13.0"
感谢所有帮助。谢谢。
编辑:日志。
【问题讨论】:
你能张贴日志的截图吗? 当然。就是这样。 【参考方案1】:您实际上使用的是 MQTT 代理,Firebase 将其视为外部访问 URL。尝试按照here 所述启用计费。
【讨论】:
那是我的恐惧。回家后我会试一试并在这里报告。谢谢。以上是关于Dialogflow 使用 Firebase 连接到 MQTT 代理?的主要内容,如果未能解决你的问题,请参考以下文章
html 用于连接DialogFlow和Firebase实时数据库的客户端示例