【问题标题】:Why does my Arduino fail to connect to my Mosca Broker?为什么我的 Arduino 无法连接到我的 Mosca Broker?
【发布时间】:2020-10-05 05:05:28
【问题描述】:

我正在学习 MQTT,并将开源 mosca 代理部署到一个天蓝色的 Web 应用程序。问题是代理在 2 台之间工作,但是当我尝试将我的 arduino nano 连接到代理时,它总是失败。这是我的服务器代码:

//Mosca
const mosca = require('mosca')
const settings = {
   http: {
    // port for websockets, MQTT is running in default port 1883
    port: 8000,
    bundle: true,
    static: './public'
  }
}

// start mosca
const moscaServer = new mosca.Server(settings)
moscaServer.on('ready', setup)

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running in port 1883!')
  console.log('Using port 8000 for MQTT over Web-Sockets!')
}

// fired when a client is connected
moscaServer.on('clientConnected', function(client) {
  console.log('client connected', client.id)
})

// fired when a message is received
moscaServer.on('published', function(packet, client) {
  //if (packet.topic == '/example') {
    console.log(packet.payload.toString('utf-8'))
  //}
})

// fired when a client subscribes to a topic
moscaServer.on('subscribed', function(topic, client) {
  console.log('subscribed : ', topic)
})

// fired when a client unsubscribes to a topic
moscaServer.on('unsubscribed', function(topic, client) {
  console.log('unsubscribed : ', topic)
})

// fired when a client is disconnecting
moscaServer.on('clientDisconnecting', function(client) {
  console.log('clientDisconnecting : ', client.id)
})

// fired when a client is disconnected
moscaServer.on('clientDisconnected', function(client) {
  console.log('clientDisconnected : ', client.id)
})

【问题讨论】:

  • 鉴于您说这可以在 PC 上运行,为什么您认为 mosca 代码有问题,而不是 Arduino 中运行的代码?
  • 是的,hardilib 说的。哪个代码有错误?发布该代码!而且,告诉我们“总是失败”的真正含义是什么?什么错误?有错误代码吗?怎么会失败?!?

标签: arduino mqtt azure-web-app-service broker mosca


【解决方案1】:

从 Azure 应用服务 WebApp 的角度来看 - 默认情况下,应用服务假定您的自定义容器正在侦听端口 80。如果您的容器侦听不同的端口,请在您的应用服务应用中设置 WEBSITES_PORT 应用设置。

可以通过 Internet 访问 WebApp 是通过已经公开的 HTTP (80) 和 HTTPS (443) TCP 端口。

应用服务目前只允许您的容器为 HTTP 请求公开一个端口。 我不太确定 - MQTT 代理。另外,您尝试连接的具体方式。

来自应用服务 - Docker Compose 选项 - 请检查 doc,支持和不支持的配置。 • 只能打开一个容器以供取用 • 只能访问端口 80 和 8080(暴露端口)

请参考this document了解更多详情。

注意:Linux 应用程序支持 Web Sockets。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多