Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Kura » MQTT Subscribe not working using DataService
MQTT Subscribe not working using DataService [message #1737267] Wed, 06 July 2016 18:02 Go to next message
Sneha Hegde is currently offline Sneha HegdeFriend
Messages: 1
Registered: July 2016
Junior Member
I followed greenhouse tutorial with which i am able to publish messages using MQTT to the broker. But subscribe is not working.

When I tried to subscribe from the console of my raspberry pi using mosquitt_sub I was able to receive messages.

Please find the code below , Please tell me if I am missing something.

public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained) {
// TODO Auto-generated method stub

s_logger.info("meaage arrived");

}



public void onConnectionEstablished() {
// TODO Auto-generated method stub


String prefix = "sensor" ;
s_logger.info("Connection established");

try {
_dataservice.subscribe(prefix , 0);

}

catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Re: MQTT Subscribe not working using DataService [message #1737404 is a reply to message #1737267] Thu, 07 July 2016 19:57 Go to previous messageGo to next message
David Woodard is currently offline David WoodardFriend
Messages: 420
Registered: July 2014
Senior Member
Hello,

I believe you are missing the "#". From the demo source code:
_dataService.subscribe(prefix + "#", 0);


If that doesn't work, please post exactly the topic you are publishing to.

Thanks,
--Dave
Re: MQTT Subscribe not working using DataService [message #1754973 is a reply to message #1737404] Fri, 24 February 2017 18:26 Go to previous messageGo to next message
Talha Ghaffar is currently offline Talha GhaffarFriend
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 #1754978 is a reply to message #1754973] Fri, 24 February 2017 19:49 Go to previous messageGo to next message
David Woodard is currently offline David WoodardFriend
Messages: 420
Registered: July 2014
Senior Member
Hello,

Could you please post your complete log (/var/log/kura.log)?

Thanks,
--Dave
Re: MQTT Subscribe not working using DataService [message #1754986 is a reply to message #1754978] Sat, 25 February 2017 00:56 Go to previous messageGo to next message
Talha Ghaffar is currently offline Talha GhaffarFriend
Messages: 6
Registered: February 2017
Junior Member
Hi Dave,

Please find the latest kura.log attached.

I noticed one instance of onConnectionEstablished while trying to post this message (with a wrong wild-card in the topic at that time), so I re-tried by restarting the bundles and then, re-installing the bundles but still have the same result.

Thanks for having a look into this !
Talha.
  • Attachment: kura.log
    (Size: 2.88MB, Downloaded 91 times)
Re: MQTT Subscribe not working using DataService [message #1754987 is a reply to message #1754978] Sat, 25 February 2017 00:56 Go to previous messageGo to next message
Talha Ghaffar is currently offline Talha GhaffarFriend
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 Go to previous message
David Woodard is currently offline David WoodardFriend
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
Previous Topic:Receiving sensor data in Eclipse via Kura (IoT)
Next Topic:ClockService very strange behaviour when network connection lost
Goto Forum:
  


Current Time: Thu Apr 18 11:42:43 GMT 2024

Powered by FUDForum. Page generated in 0.02139 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top