0

I am currently developing a C++ application with the Momentics IDE that is running on a QNX OS host on a VMware Player virtual machine. The application uses the MQTT protocol provided by Mosquitto to publish messages to Cumulocity IoT’s Device Management portal.

I had previously managed to establish a connection with Cumulocity and publish messages using a pre-built application in C# called the mqttMultimeter. The application would automatically create a new device once I published my message to the s/us topic, after which I had no trouble posting device measurements to the cloud.

Now, I am trying to replicate the same success with my own C++ application. I have been following the official documentation to create a new mosquitto client, authenticate the client vis-a-vis Cumulocity, and publish messages to the MQTT broker. The corresponding methods are:

user_pw_set()
connect()
publish()

Both publish() and user_pw_set() return integers, represented by a set of enum values, to indicate whether their respective method call went through or not.

Consequently, I wrote a couple of if blocks to verify whether the client was passing through each of the phases successfully to communicate with the MQTT broker. If the broker returns the enum MOSQ_ERR_SUCESS, then the method call was successful.

The full source code for my first message registering a new device is as follows:

#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;
}

For the first run-through, I am able to register a device to the cloud. However, any subsequent attempts to send new measurements for the device via the temperature measurement template {211, Temperature}, does not register any new inputs to my device.

That being, said the program compiles without any errors, so I am at a loss as to what the problem is.

Source code for publishing messages:

#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;
}
liberlux
  • 85
  • 8
  • 1
    HI @liberlux - I don't see any code in your question that tries to publish measurements. When you're asking a question about code that's not working it's much more helpful to share the code that's not working than the code that is working. Also, what do your Mosquitto logs say? Set the log level in mosquitto.conf to 'information', run the program that's not working; what shows up in the logs? – romkey May 10 '23 at 16:55
  • Hi @romkey, whenever I want to publish measurements, I rerun the program but set the `payload` to `{211,42}` to represent a temperature measurement. The code publishes data (supposedly) with the `publish()` method that takes the payload as one of its parameters. – liberlux May 11 '23 at 13:37
  • Please post the code that's not working, don't describe it in the comments. Describing code is a waste of time; if it worked the way you expected it to then you wouldn't be asking questions. – romkey May 11 '23 at 17:13
  • Just looking at it for a minute, payloadlen in your first code snippet seems wrong. Probably affects the rest of the test. – Gambit Support Jul 07 '23 at 10:35

0 Answers0