I am currently developing a C++ application with the Momentics IDE that is hosted on a QNX OS running on 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 the message to the s/us
topic.
Now, I am trying to replicate the same success with a C++ application using one of the example applications found in Mosquitto’s official Github repository under the folder examples > temperature conversion. The application contains a derived class, temperature_conversion.cpp
that inherits all of the methods of mosquittopp.cpp
.
I have been following the official documentation from Mosquitto to create a new mosquitto client, authenticate the client vis-a-vis Cumulocity, and publish messages to the MQTT broker. The corresponding methods are:
connect()
user_pw_set()
publish()
Both publish()
and user_pw_set()
return integers to indicate whether their respective method call worked or not. Consequently, I wrote a couple of if
blocks to verify whether the client is successfully connecting with the broker.
The program compiles without errors; however, when I go to the Device Management portal under Devices > All devices, there is no record of a new device being registered.
The full source code is posted below:
#include <iostream>
#include "temperature_conversion.h"
int main() {
// declare temp_conversion instance
class mqtt_tempconv *tempconv;
// declare constants for host username and password.
const char host_val[] {"[domain].cumulocity.com"};
const char username_val[] {"[username]"};
const char password_val[] {"[password]"};
// define constant for port number.
const int port {1883};
// assign constants to pointers
const char* host {host_val};
const char* username {username_val};
const char* password {password_val};
// declare and define variable arguments for publish()
int payloadlen {8};
int qos {1};
bool retain {true};
// declare variables pointer arguments
int mid_val {4};
int *mid {&mid_val};
char topic_val[] {"s/us"};
char *topic {topic_val};
char payload_val[] {"301, 208"};
char *payload {payload_val};
// Initialize mosquitto library
mosqpp::lib_init();
// Instantiate new tempconv (i.e.mosquittopp) object.
// NOTE: mqtt_tempconv automatically connects to MQTT broker
tempconv = new mqtt_tempconv("tempconv", host, port);
// tempconv->loop_forever();
// Set up username and password for authentication.
int auth_status;
auth_status = tempconv->username_pw_set(username, password);
if(auth_status == MOSQ_ERR_SUCCESS){
std::cout << "Authentication successful. Proceeding towards publishing message." << std::endl;
// Publish message to Cumulocity MQTT broker.
int publish_status;
publish_status = tempconv->publish(mid, topic, payloadlen, payload, qos, retain);
if(publish_status == MOSQ_ERR_SUCCESS){
std::cout << "Success! Message published to Cumulocity MQTT broker." << std::endl;
}
else {
std::cout << "Error: unable to publish message to Cumulocity MQTT broker.";
}
}
else {
std::cout << "Error: unable to connect to broker" << std::endl;
}
std::cout << "Disconnecting MQTT client..." << std::endl;
int disconnect_status;
disconnect_status = tempconv->disconnect();
if(disconnect_status == MOSQ_ERR_SUCCESS){
std::cout << "Success! Disconnected MQTT client." << std::endl;
}
// Clean up mosquitto library
mosqpp::lib_cleanup();
return 0;
}
At first, I thought that the application was successfully connecting to Cumulocity. However, if I comment out the loop_forever()
method, which establishes an infinite blocking loop, I get the following error message published repeatedly on my IDE’s console:
Connected with code 5.
Connected with code 5.
Connected with code 5.
Connected with code 5.
...
After doing quick a Google search, I discovered that code 5
corresponds to an error related to my certificate. I know from the mqttMultimeter application, that I needed to provide to provide a ClientID and that, furthermore, my username included my tenant ID. However, I do not see any methods in the Mosquitto documentation where I would insert this information.
Could the lack of those two steps be the reason why I am unable to connect with Cumulocity’s MQTT broker?
Note: I could not post any links to my post; apologies for the inconveniance.