|
|
Re: MQTT Subscribe not working using DataService [message #1754973 is a reply to message #1737404] |
Fri, 24 February 2017 18:26 |
Talha Ghaffar Messages: 6 Registered: February 2017 |
Junior Member |
|
|
Hi Dave,
I am having a similar problem.
I can send data to the broker (my desktop) but cannot publish the data from the desktop to my DataService in Kura (Pi). I am using mosquitto_pub to publish the data to the DataService and subscribe to the topic in OnConnectionEstablished().
Here's how I try to publish to DataService.
mosquitto_pub -h <Pi's IP> -t "mqtt-test" -m "Message"
I don't see any logs from onConnectionEstablished on /var/log/kura.log which is confusing since the data service has established a connection with the broker (that's why publish and onMessagePublished work).
Can you tell me where I'm going wrong with this? Am I not subscribing correctly or not publishing to Kura correctly?
Also, when in the life cycle of this bundle does onConnectionEstablished actually gets called?
Here's the relevant code:
public void setDataService(DataService dataService) {
_dataService = dataService;
}
public void unsetDataService(DataService dataService) {
_dataService = null;
}
protected void activate(ComponentContext componentContext,
Map<String, Object> properties) {
_properties = properties;
s_logger.info("Activating Sensehat Publisher... Done.");
if (this._dataService != null ) {
this._dataService.addDataServiceListener(this);
s_logger.info("SenseHatExamplePublisher: Added data service listener\n");
}else {
s_logger.info("SenseHatExamplePublisher: Data service listener is null\n");
}
}
public void onConnectionEstablished() {
try {
String prefix = (String) _properties
.get(PUBLISH_TOPICPREFIX_PROP_NAME);
/* Subscribe to the topic */
s_logger.info("SenseHatExamplePublisher: Subscribing to topic : {}\n" + prefix);
_dataService.subscribe(prefix + "#", 0);
} catch (...) {
...
}
}
@Override
public void onMessageArrived(String topic, byte[] payload, int qos,
boolean retained) {
String prefix = (String) _properties.get(PUBLISH_TOPICPREFIX_PROP_NAME);
/* Do something depending on topic here */
s_logger.info("SenseHatExamplePublisher: Message arrived on topic {} with qos = {}\n", topic, qos);
s_logger.info("SenseHatExamplePublisher: Payload: {}\n", new String(payload));
}
@Override
public void onMessagePublished(int messageId, String topic) {
// TODO Auto-generated method stub
s_logger.info("SenseHatExamplePublisher: Executing onMessagePublished");
s_logger.info("SenseHatExamplePublisher: Message published on topic {}\n", topic);
}
Also attached the source file for the class here.
Thank you,
Talha.
|
|
|
|
|
Re: MQTT Subscribe not working using DataService [message #1754987 is a reply to message #1754978] |
Sat, 25 February 2017 00:56 |
Talha Ghaffar Messages: 6 Registered: February 2017 |
Junior Member |
|
|
Hi Dave,
I looked at the log and it seemed that onConnectionEstablished got called when DataServiceImpl tried to reconnect to the broker earlier:
2017-02-22 18:03:02,594 [DataServiceImpl:ReconnectTask] INFO o.e.k.r.s.e.p.SenseHatExamplePublisher - Executing : onConnectionEstablished ...
So, now I tried to reconnect the dataservice again (from Web UI) with correct topic and now onMessageArrived works for the messages I send from the dataservice (since I've subscribed to the same topic I'm sending to.) [Attached updated log!]
Question: Does it mean that users always have to reconnect the DataService when they deploy a bundle that implements DataServiceListener?
But still, I cannot send messages from the broker using
mosquitto_pub -h 128.173.239.231 -t "mqtt-test/abc" -m "Message"
which suggests I'm not sending the messages correctly.
Question 2: Can you suggest what I might be doing wrong here?
Question 3: Also, if sending messages from the broker to a DataService works (as demonstrated by Greenhouse demo), we can also send data from a Kura gateway to another Kura gateway (by specifying the second one as the broker in MqttDataTransport). Correct?
I appreciate any help you can provide here!
Thanks,
Talha.
-
Attachment: kura.log.1
(Size: 5.82MB, Downloaded 68 times)
[Updated on: Mon, 27 February 2017 17:19] Report message to a moderator
|
|
|
Re: MQTT Subscribe not working using DataService [message #1755653 is a reply to message #1754987] |
Mon, 06 March 2017 15:38 |
David Woodard Messages: 420 Registered: July 2014 |
Senior Member |
|
|
Hello,
Question 1: No, you should not have to reconnect the DataService. The trackers should automatically add/remove listeners as they are added/removed from the framework.
Question 2: It is hard to diagnose messages coming from the mosquitto_pub client as I don't know when they were sent in relation to activity in the log. For example, I saw a few periods of time where Kura was not connected to your broker. I also saw this:
2017-02-22 18:03:02,595 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.d.t.m.MqttDataTransport - Subscribing to topic: mqtt-test# with QoS: 0
Note the "mqtt-test#", which is incorrect. It appeared you corrected this, but I am not sure if that was before or after sending messages from the Mosquitto client. You can try connecting to the broker with the mosquitto_sub client, just to make sure the messages are getting through.
Question 3: I am not sure I understand this question. Kura is only an MQTT client. If you want to send messages to a Kura gateway, you would have to publish to a broker, then have your gateway subscribe to the proper topic. For example, gateway A could publish to mqtt-test/gatewayB/hello, while gateway B subscribed to mqtt-test/gatewayB/#.
Thanks,
--Dave
|
|
|
Powered by
FUDForum. Page generated in 0.02139 seconds