您现在的位置是:首页 >学无止境 >配置MQTT网站首页学无止境
配置MQTT
简介配置MQTT
MQTT
快速上手
# 安装Broker服务
apt install mosquitto mosquitto-clients
# 启动服务
mosquitto
# (tty1)阻塞订阅一个主题
mosquitto_sub -t "test/topic" -v
# (tty2)向指定主题发布一个事件
mosquitto_pub -t "test/topic" -m "hello world"
源码安装mosquitto
编译前环境准备。
apt install cmake gcc g++ ninja-build
- 下载 cJSON
- 下载 libwebsockets
- 下载 mosquitto
TARGET_PREFIX="${HOME}/applications/mosquitto"
mkdir -p $TARGET_PREFIX
# cJSON
cmake -G Ninja
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=$TARGET_PREFIX
-B build_cache -S .
cmake --build build_cache/
cmake --install build_cache/
# libwebsockets
cmake -G Ninja
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=$TARGET_PREFIX
-B build_cache -S .
cmake --build build_cache/
cmake --install build_cache/
# mosquitto
cmake -G Ninja
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=$TARGET_PREFIX
-DCMAKE_PREFIX_PATH=$TARGET_PREFIX
-DDOCUMENTATION=OFF
-DWITH_WEBSOCKETS=ON
-B build_cache -S .
cmake --build build_cache/
cmake --install build_cache/
配置Broker
mosquitto.conf
allow_anonymous true
listener 1883
protocol mqtt
listener 1884
protocol websockets
# 启动服务
./mosquitto -c ../etc/mosquitto/mosquitto.conf
SDK调用接口
Python
下载 paho-mqtt 或从软件源安装:
apt install python3-paho-mqtt
demo-mqtt.py
#!/bin/python3
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test/topic")
client.publish("test/topic", "Hello Python")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)
client.loop_forever()
NodeJs/WebJs
下载 MQTT.js 在 Web 中使用,或使用npm安装模块:
npm install mqtt
demo-mqtt.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>MQTT</title>
</head>
<body>
<script src="https://unpkg.com/mqtt@4.3.7/dist/mqtt.min.js"></script>
<script>
console.log(mqtt);
const client = mqtt.connect("ws://127.0.0.1:1884");
client.on("connect", function () {
client.subscribe("test/topic", function (err) {
client.publish("test/topic", "Hello WebJs");
});
});
client.on("message", function (topic, message) {
// message is Buffer
console.log(message.toString());
// client.end();
});
</script>
</body>
</html>
Java
// ...
C
使用 Eclipse Paho C 或 Eclipse Paho Embedded C 进行连接。
编译Eclipse Paho C
cmake -G Ninja
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=$HOME/3rdparty/paho.mqtt.c
-B build_cache -S .
cmake --build build_cache/
cmake --install build_cache/
使用Eclipse Paho C
demo-mqtt.c
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "MQTTClient.h"
#define ADDRESS "tcp://127.0.0.1:1883"
#define CLIENTID "emqx_test"
#define TOPIC "test/topic"
#define PAYLOAD (char*)"Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
// MQTT 连接参数
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
printf("Failed to connect, return code %d
", rc);
exit(-1);
}
// 发布消息
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for up to %d seconds for publication of %s
"
"on topic %s for client with ClientID: %s
",
(int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered
", token);
// 断开连接
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。