Please help a MQTT beginner ;)

827 views
Skip to first unread message

burner

unread,
Jul 3, 2014, 3:34:35 PM7/3/14
to mq...@googlegroups.com
Hey there,
I'm completely new to MQTT but it helps me very much with my aim, while programming an Android app.

I have a MQTT-Broker running on a Raspberry Pi.

My aim is that the broker (and of course the code behind the broker) should know, when my Android device is connected to the (wifi-)network.

I've already an existing/ functioning app with an MQTT client running. The broker pushes sensor data to the app.
Now I additionally want to implement that the app sends MQTT messages to the broker. These messages than get stored and I can see when the app was connected and when it was not.

When the app starts, there's also a Mqtt-service which gets started.
This service starts in its onStartCommand() method a method which lets the app connect to the broker, sets a 'last will' and then lets the app/mqttclient publish a message. All this stuff already works ;)

Now if the android device loses wifi connection the 'last will' is published, which is exactly what I want because then my broker/ other code on the raspberry Pi knows, that the Device is gone.

BUT:
If the device reconnects to the wifi, my wish is that the Mqttservice running on the Android device reconnects to the server and again sends its message, showing 'hey i'm there again'.
At the moment this doesn't happen.
If the wifi connection is lost and than reconnects after some time to connection to the broker could not be reestablished :(

I hope I've made my problem clear enough, please ask if something is not clear enough!
(I'm using Eclipse and the 'org.eclipse.paho.client.mqttv3' package)

Thanks for any hints or help in advance!!!

Dave Locke

unread,
Jul 6, 2014, 7:27:10 AM7/6/14
to mq...@googlegroups.com
Hi ,
the eclipse paho mailing list is also a good place for this type of question

When the Paho MQTT Java client library is used an application can register interest in knowing when the connection breaks. Hence if you mqttservice registers interest in bening told when the connection is broken it can then put in place logic to reconnect. One approach to do this is to use Android APIs to determine when the network is available again and once available to then re-establish the MQTT connection.   Not sure if you are aware but there is a basic MQTT Android service available here http://www.eclipse.org/paho/clients/android/   As it stands it does not implement the reconnection logic.  I have pasted below details from the package.html.  If implement this then maybe you can contribute it to the Paho project.


<p><strong>Note regarding network connectivity</strong></p>
<p>
The service does not attempt to track network state and automatically reconnect to MQTT servers as connectivity is lost and regained
While this is clearly possible, determining appropriate behaviour presents certain challenges. <br>
Any maintainer adding support for such functionality should look to providing a <i>BroadcastReceiver</i> for
<i>ConnectivityManager.CONNECTIVITY_ACTION</i> probably as part of the MqttService object.<br>
Changes in network connectivity would then need to be notified to the relevant MqttConnection objects.
</p>



All the best
Dave Locke

Senior Inventor
Product Management and Ecosystem for IoT and M2M

lo...@uk.ibm.com
Dave Locke/UK/IBM@ibmgb
37246165 (int)       +44 1962816165 (ext)
37274133 (mobex) +44 7764132584 (ext)

Twitter: @DaveJLocke
My Cattail: Share files in IBM and save your in-box

--
To learn more about MQTT please visit
http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
mqtt+uns...@googlegroups.com.
To post to this group, send email to
mq...@googlegroups.com.
Visit this group at
http://groups.google.com/group/mqtt.
For more options, visit
https://groups.google.com/d/optout.

Unless stated otherwise above:
IBM United Kingdom Limited - Registered in England and Wales with number 741598.
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU

burner

unread,
Jul 6, 2014, 5:19:27 PM7/6/14
to mq...@googlegroups.com
Hi Dace,

thats exactly the way I implemented it just yesterday ;)

If registered a Broadcast receiver to get informed when the wifi connection status changes.
If wifi becomes available (.isconnected()==true) I now start the .connect() method again.

Nevertheless, thank you for your help!

BroadcastReceiver wifiReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (wifi.isConnected()) {......

To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+unsubscribe@googlegroups.com.


To post to this group, send email to

Alexis LE GOFF

unread,
Nov 30, 2015, 12:47:47 PM11/30/15
to MQTT, zufa...@gmail.com
Hey,
thanks for this info. What about if the wifi comes back but without any Internet access during first minutes. Then the client will try to reconnect but it will never work.
How can we handle this not-so-rare case?

Reply all
Reply to author
Forward
0 new messages