liberlux
liberlux

Reputation: 109

Configuring C++ Application to publish messages to Cumulocity using Mosquitto

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

Upvotes: 0

Views: 161

Answers (0)

Related Questions