了解物联网的 MQTT 协议

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

我正在考虑如何通过互联网远程控制我的 气培 系统。控制系统的树莓派通过路由器连接到互联网。我可以通过 端口转发 之类的方式访问树莓派,但这很复杂。我的下一个选择可能是使用 websockets,但我觉得它对于在 pi 中运行的应用程序来说太过分了。

最近我从 dzone 收到了一张关于名为 mqtt 的协议的 推荐卡 。我以前不知道这个协议。所以我想用它做一些实验。我不会深入研究协议,dzone refcard 很好地解释了它。

简而言之,mqtt 由三部分组成。

  • 经纪人
  • 订户
  • 出版商

发布者向特定主题发布消息,订阅该主题的任何订阅者都会收到该消息。经纪人是中心枢纽。发布者和订阅者都连接到代理,它负责将消息传递给订阅该主题的所有订阅者。

经纪人

我们可以使用 rabitmq 或 node.js 的 mosca 插件或市场上可用的任何其他 mqtt 代理来实现我们自己的代理。为了试验它,我使用了 heroku 中的 cloudmqtt 插件。我使用 heroku 只是为了从一个中心位置管理每一件事。

开发环境

我创建了两组 node.js 应用程序,一组作为发布者在我的计算机中运行,另一组作为订阅者在我的 raspberrypi 中运行。两者没有直接联系;相反,他们连接到 cloudmqtt 代理。下面是一些测试代码。

出版商代码


 var mqtt = require('mqtt');
var client = mqtt.createclient('<<portnumber>>', 'm11.cloudmqtt.com', {
    username: '<<username>>',
    password: '<<password>>'
});

client.on('connect', function () { // when connected

// subscribe to a topic
client.subscribe('temperature_reading', function () {
    // when a message arrives, do something with it
    client.on('message', function (topic, message, packet) {
        console.log("received '" + message + "' on '" + topic + "'");
    });
});

// publish a message to a topic
client.publish('set_temperature', '24', function () {
    console.log("message is published");
  });

});

上面的代码充当发布者和订阅者。例如,上面的代码可以是在互联网上运行的一段代码,pi 可以定期发布温度读数并将其记录在中央数据库中。我们可以通过网络应用程序或任何我们需要的方式查看读数。如果需要,我们还可以通过向主题“set_temperature”发布消息来为所有连接的树莓派设置温度。

用户代码


 var mqtt = require('mqtt');
var client = mqtt.createclient('<<portnumber>>', 'm11.cloudmqtt.com', {
    username: '<<username>>',
    password: '<<password>>'
});

client.on('connect', function () { // when connected

// subscribe to a topic
client.subscribe('temperature_reading', function () {
    // when a message arrives, do something with it
    client.on('message', function (topic, message, packet) {
        console.log("received '" + message + "' on '" + topic + "'");
    });
});

// publish a message to a topic
client.publish('set_temperature', '24', function () {
    console.log("message is published");
  });

});


 var mqtt = require('mqtt');
var client = mqtt.createclient('<<portnumber>>', 'm11.cloudmqtt.com', {
    username: '<<username>>',
    password: '<<password>>'
});

client.on('connect', function () { // when connected

    // subscribe to a topic
    client.subscribe('temperature_reading', function () {
        // when a message arrives, do something with it
        client.on('message', function (topic, message, packet) {
            console.log("received '" + message + "' on '" + topic + "'");
        });
    });

    // publish a message to a topic
    client.publish('set_temperature', '24', function () {
        console.log("message is published");
      });
});

代码非常少,我们可以轻松地与所有连接的设备进行通信。在上面的场景中,客户端始终处于连接状态。如果您想结束连接,请调用“client.end()”。

后来我使用 mosca 实现了一个代理,并且在这两种情况下系统都运行良好。