我正在尝试设置一个 mosquitto 客户端来收集和记录来自我的 iot 设备的 mqtt 消息,该设备不断发送它的 gps 数据。
我正在使用eclipse mosquitto 和 cpp 包装器来完成这项任务。
我确信我的设备将数据传递给代理,因为在服务器上的mosquitto_sub -t mqtt_test --cafile ca.crt --client.crt --key client.key -p 8883 -q1
命令返回与我发送的完全相同的数据。在设备上我得到这些日志:
...
[16] Client mosq/LDb;eF^;<P7fZd0d>f sending PUBLISH (d0, q1, r0, m5, 'mqtt_test', ... (175 bytes))
[16] Client mosq/LDb;eF^;<P7fZd0d>f received PUBACK (Mid: 5)
[16] Client mosq/LDb;eF^;<P7fZd0d>f sending PUBLISH (d0, q1, r0, m6, 'mqtt_test', ... (175 bytes))
[16] Client mosq/LDb;eF^;<P7fZd0d>f received PUBACK (Mid: 6)
...
这是我的服务器客户端代码
主.cpp:
#include "mqtt_client.h"
#include <string.h>
#define CLIENT_ID "Client_ID"
#define BROKER_ADDRESS "127.0.0.1"
#define MQTT_PORT 8883
int main(int argc, char *argv[]) {
std::cout << "gps logging app has started\n" << std::endl;
class mqtt_client *iot_client;
char client_id[] = CLIENT_ID;
char host[] = BROKER_ADDRESS;
int port = MQTT_PORT;
if (argc > 1) {
strcpy (host, argv[1]);
}
mosqpp::lib_init();
iot_client = new mqtt_client(client_id, host, port, true);
iot_client->loop_forever();
mosqpp::lib_cleanup();
return 0;
}
mqtt_client.cpp:
#include "mqtt_client.h"
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#define MQTT_TOPIC "mqtt_test"
mqtt_client::mqtt_client(const char *id, const char *host, int port, bool clean) : mosquittopp(id, clean) {
int ret;
ret = tls_set("../ssl/ca.crt", nullptr, "../ssl/client.crt", "../ssl/client.key", nullptr);
printf("tls_set ret %d %s\n", ret, strerror(ret));
tls_insecure_set(true);
ret = connect(host, port, DEFAULT_KEEP_ALIVE);
printf("connect ret %d %s\n", ret, strerror(ret));
ret = subscribe(nullptr, MQTT_TOPIC, 1);
printf("subscribe ret %d %s\n", ret, strerror(ret));
}
void mqtt_client::on_subscribe(int mid, int qos_count, const int *granted_qos) {
printf("on_subscribe mid %d qos_count %d\n", mid, qos_count);
}
void mqtt_client::on_message(const struct mosquitto_message *message) {
int payload_size = MAX_PAYLOAD + 1;
char buf[payload_size];
memset(buf, 0, payload_size * sizeof(char));
memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char));
std::cout << "message recieved:\n" << buf << std::endl;
}
void mqtt_client::on_log(int level, const char *str) {
printf("[%d] %s\n", level, str);
}
mqtt_client.h:
#ifndef MQTT_CLIENT_H
#define MQTT_CLIENT_H
#include <cpp/mosquittopp.h>
#include <thread>
const int MAX_PAYLOAD = 50;
const int DEFAULT_KEEP_ALIVE = 60;
class mqtt_client : public mosqpp::mosquittopp {
public:
mqtt_client (const char *id, const char *host, int port, bool clean);
~mqtt_client() override;
void on_message(const struct mosquitto_message *message) override;
void on_subscribe(int mid, int qos_count, const int *granted_qos) override;
void on_log(int level, const char *str) override;
};
#endif //MQTT_CLIENT_H
tls_set、connect 和 subscribe 返回 0。
现在奇怪的部分。每次我的物联网设备发布消息时,都会调用 on_subscribe 方法。这就是我的输出的样子:
gps logging app has started
tls_set ret 0 Success
connect ret 0 Success
subscribe ret 0 Success
on_subscribe mid 40401736 qos_count 40401736
on_subscribe mid 40426056 qos_count 40426056
on_subscribe mid 40401128 qos_count 40401128
on_subscribe mid 40429016 qos_count 40429016
...
在我看来,on_subscribe 应该在调用 subscribe 时在 mqtt_client 构造函数中调用,但是每次代理收到消息时都会这样做。而且似乎 on_log 和 on_message 方法都不起作用。
请帮我找出为什么这不起作用,或者至少指出寻找问题的正确方向。