You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++应用通过Mosquitto向Cumulocity发送测量数据失败排查求助

问题:C++ Mosquitto客户端向Cumulocity IoT发布测量数据无响应

环境与背景

  • 运行环境:VMware Player虚拟机内的QNX OS,使用Momentics IDE开发C++应用
  • 依赖:基于Mosquitto MQTT库对接Cumulocity IoT平台
  • 现状:C#预构建应用mqttMultimeter可正常创建设备并上传数据;自行编写的C++客户端能完成设备注册,但发布温度测量数据时平台无记录,程序编译无报错

设备注册成功的代码

#include <iostream>
#include <mosquittopp.h>

int main() {

    int mid_val {1221}; // message id

    const char* client_id {"MOSQ_CLIENT_QNX"};
    const char* username { "[tenantID]/[username]"};
    const char* password {"[password"};
    const char* host {"[domain].cumulocity.com"};
    const int port {1883};

    mosqpp::lib_init();

    mosqpp::mosquittopp msq_client {client_id, true};

    int auth_status = msq_client.username_pw_set(username, password);

    if(auth_status == MOSQ_ERR_SUCCESS) {
        std::cout << "User authentication success\n";

        int connect_status = msq_client.connect(host, port, 60);
        if(connect_status == MOSQ_ERR_SUCCESS){
            std::cout << "Connection success\n";

            int* mid {&mid_val};
            const char* topic {"s/us"};
            const int payloadlen {8};
            const char* payload {"100,New_device,Temperature_type "};

            int pub_status = msq_client.publish(mid, topic, payloadlen, payload);
            if(pub_status == MOSQ_ERR_SUCCESS){
                std::cout << "Message published to channel " << topic << " with message: " << payload << std::endl;
            }
            else
                std::cout << "Error publishing message to channel " << topic << std::endl;
        }
    }
    mosqpp::lib_cleanup();

    return 0;
}

发布测量数据无响应的代码

#include <iostream>
#include <mosquittopp.h>

int main() {

    int mid_val {1234}; // message id

    const char* client_id {"MOSQ_CLIENT_QNX"};
    const char* username { "[tenantID]/[username]"};
    const char* password {"[password"};
    const char* host {"[domain].cumulocity.com"};
    const int port {1883};

    mosqpp::lib_init();

    mosqpp::mosquittopp msq_client {client_id, true};

    int auth_status = msq_client.username_pw_set(username, password);

    if(auth_status == MOSQ_ERR_SUCCESS) {
        std::cout << "User authentication success\n";

        int connect_status = msq_client.connect(host, port, 60);
        if(connect_status == MOSQ_ERR_SUCCESS){
            std::cout << "Connection success\n";

            int* mid {&mid_val};
            const char* topic {"s/us"};
            const int payloadlen {8};
            const char* payload {"211,43"};

            int pub_status = msq_client.publish(mid, topic, payloadlen, payload);
            if(pub_status == MOSQ_ERR_SUCCESS){
                std::cout << "Message published to channel " << topic << " with message: " << payload << std::endl;
            }
            else
                std::cout << "Error publishing message to channel " << topic << std::endl;
        }
    }
    mosqpp::lib_cleanup();

    return 0;
}

排查方向与解决方法

1. 修复Payload长度参数错误

发布测量数据的代码中,payloadlen手动设置为8,但实际payload"211,43"的字节数为6(不含NULL终止符),手动计算长度错误会导致平台接收的消息不完整。建议改用strlen(payload)自动获取长度:

const int payloadlen = strlen(payload);

2. 添加MQTT消息循环处理

Mosquitto客户端需要执行消息循环才能完成网络通信、消息确认等操作。当前代码调用publish后直接退出,可能消息尚未发送到代理就被终止。需在publish后添加循环逻辑:

// 同步循环:运行一次,等待100ms确保消息发送完成
msq_client.loop(100, 1);

或使用异步循环:

#include <chrono>
#include <thread>

// ...
msq_client.loop_start();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
msq_client.loop_stop();

3. 验证设备关联与模板格式

  • 确认两次请求使用的client_id完全一致,Cumulocity通过client_id绑定设备,若不一致会导致测量数据无法关联到已注册设备
  • 检查SmartREST模板格式:211模板要求格式为211,<温度值>,若平台期望浮点数格式,可尝试将payload改为"211,43.0"

4. 开启Mosquitto调试日志

开启调试日志可查看详细通信过程,排查协议错误或发送失败问题:

msq_client.set_logger_level(MOSQ_LOG_DEBUG);

或通过环境变量MOSQUITTO_DEBUG=1运行程序,查看输出的调试信息。

5. 修正密码字符串语法错误

两段代码中password字符串均为"[password",缺少闭合双引号,需确认实际运行时的密码字符串格式正确,避免因认证问题导致消息被平台拒绝。

内容的提问来源于stack exchange,提问作者liberlux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 13:30:21