The Eclipse Kura communicates with a MQTT broker using any one of the following two mechanisms:
1 --> Using CloudClient
2 --> Using DataService
I'm guessing the CloudClient you are using might have some restrictions imposed on the topic namespace segregation due to the hierarchy of the communication structure between the devices and the gateways.
If you'd rather have your own namespace segregation, I'd advice using the DataService to directly define your own MQTT namespace for both publishing and receiving MQTT messages.
You'll need to use the OSGi framework and direct it to inject the instance into your component class like in this following sample code:
public class MyComponent {
private DataService m_dataService;
public void setDataService(DataService dataService) {
m_dataService = dataService;
}
public void unsetDataService(DataService dataService) {
m_dataService = null;
}
// activate() deactivate() and all required methods
public void publish() {
String topic = "your/topic";
String payload = "Hello!";
int qos = 0;
boolean retain = false;
try {
m_dataService.publish(topic, payload.getBytes(), qos, retain, 2);
s_logger.info("Publish ok");
} catch (Exception e) {
s_logger.error("Error while publishing", e);
}
}
}
Now, inside your component OSGI-INF/mycomponent.xml, you'll need to set the methods that you want the OSGi to call for injecting the DataService. You can do that by adding the following:
<reference name="DataService"
interface="org.eclipse.kura.data.DataService"
bind="setDataService"
unbind="unsetDataService"
cardinality="1..1"
policy="static" />
After you've done the above, you should be able to pass your topic to the method DataService.publish(...). And, make sure to convert Payloads to byte[] arrays.