Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » How to publish only one message using a Java MQTT client using Eclipse Paho(MqttAndroidClient paho.mqtt.java.pahoandroidsample)
icon8.gif  How to publish only one message using a Java MQTT client using Eclipse Paho [message #1746826] Sat, 05 November 2016 08:58 Go to next message
Javier Sanchez is currently offline Javier SanchezFriend
Messages: 3
Registered: November 2016
Junior Member
I'm doing a project for my final University work. I want do a android app that comunicates with my raspberry pi using MQTT protocol. To do it I'm using a mosquitto broker, but when I publish message in a broker using the Java MQTT client using Eclipse Paho, my app publish the same message 5 times, and I don't know if the fault is of my class that publish message, or of my class on_create() of android.

My code is the following:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText distanceCondition;
EditText timeCondition;
EditText countCondition;

String distance;
String time;
String count;
String topic;

int notificationID = 1;
int functionality;
int ok = 1;

public void mqttconnect(final String topic, final String message){

final MqttAndroidClient mqttAndroidClient = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.1.36", "androidSampleClient");
mqttAndroidClient.setCallback(new MqttCallback() { //iot.eclipse.org:1883
@Override
public void connectionLost(Throwable cause) {
System.out.println("Connection was lost!");

}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("Message Arrived!: " + topic + ": " + new String(message.getPayload()));
//displayNotification("DETECT MOVEMENT! visit LCESS app now.");
JSONObject inf = new JSONObject(Arrays.toString(message.getPayload()));
try {
if (inf.optString("Functionality").equals(6)) {
if (inf.optString("ok").equals(1)) {
displayNotification("DETECT MOVEMENT! visit LCESS app now.");
}
}
if (inf.optString("Functionality").equals(7)) {
if (inf.optString("ok").equals(1)) {
displayNotification("DETECT MOVEMENT IN" + inf.optString("Distance") + " CM! visit LCESS app now.");
}
}
if (inf.optString("Functionality").equals(Cool) {
if (inf.optString("ok").equals(1)) {
displayNotification("DETECT MOVEMENT IN" + inf.optString("Time") + " HOURS! visit LCESS app now.");
}
}
if (inf.optString("Functionality").equals(9)) {
if (inf.optString("ok").equals(1)) {
displayNotification("DETECT MOVEMENT IN" + inf.optString("Distance") + " CM and in " + inf.optString("Time") + " HOURS! visit LCESS app now.");
}
}
if (inf.optString("Functionality").equals(10)) {
if (inf.optString("ok").equals(1)) {
displayNotification("DETECT " + inf.optString("Count") + " OBJECTS/PERSONS! visit LCESS app now.");
}
}
}
catch (Exception functionality){

}

}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("Delivery Complete!");
}
});

try {
mqttAndroidClient.connect(null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
System.out.println("Connection Success!");
try {
System.out.println("Subscribing to: "+ topic);
mqttAndroidClient.subscribe(topic, 0);
System.out.println("Subscribed to: "+ topic);
System.out.println("Publishing message..");
mqttAndroidClient.publish(topic, new MqttMessage(message.getBytes()));
} catch (MqttException ex) {
}
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
System.out.println("Connection Failure!");
}
});
} catch (MqttException ex) {

}


}

public JSONObject sendJSONmessage() {
topic = "Mesurement";
distance = distanceCondition.getText().toString();
time = timeCondition.getText().toString();
count = countCondition.getText().toString();

JSONObject post_dict = new JSONObject();
try {
post_dict.put("Topic", topic);
post_dict.put("ok" ,ok);
post_dict.put("Functionality", functionality);
post_dict.put("Distance", distance);
post_dict.put("Time", time);
post_dict.put("Count", count);

} catch (JSONException e) {
e.printStackTrace();
}
if (post_dict.length() > 0) {
System.out.println(post_dict);
}
return post_dict;
}

protected void displayNotification(CharSequence contentText){
Intent i = new Intent(this, NotificationActivity.class);
i.putExtra("notificationID", notificationID);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
CharSequence ticker ="Notification in LCESS";
CharSequence contentTitle = "LCESS";

Notification noti = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setTicker(ticker)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(R.drawable.lcess)
.addAction(R.drawable.lcess, ticker, pendingIntent)
.setVibrate(new long[] {100, 250, 100, 500})
.build();
nm.notify(notificationID, noti);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

distanceCondition = (EditText) findViewById(R.id.edit_detectDistanceCondition);
timeCondition = (EditText) findViewById(R.id.edit_detectTimeCondition);
countCondition = (EditText) findViewById(R.id.edit_countDetects);

Button button_detectDistance = (Button) findViewById(R.id.button_detectDistance);
Button button_detectDistanceCondition = (Button) findViewById(R.id.button_detectDistanceCondition);
Button button_detectTimeCondition = (Button) findViewById(R.id.button_detectTimeCondition);
Button button_detectIfAllCondition = (Button) findViewById(R.id.button_detectIfAllCondition);
Button button_count = (Button) findViewById(R.id.button_count);
assert button_detectDistance != null;
/*button_detectDistance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
functionality = 1;
mqttconnect("Device1/Detect", sendJSONmessage().toString());
}
});*/
button_detectDistance.setOnClickListener(this);
assert button_detectDistanceCondition != null;
button_detectDistanceCondition.setOnClickListener(this);
assert button_detectTimeCondition != null;
button_detectTimeCondition.setOnClickListener(this);
assert button_detectIfAllCondition != null;
button_detectIfAllCondition.setOnClickListener(this);
assert button_count != null;
button_count.setOnClickListener(this);
}

public void onClick(View v){
if (R.id.button_detectDistance==v.getId()){
functionality = 1;
mqttconnect("Mesurement", sendJSONmessage().toString());
}
else if (R.id.button_detectDistanceCondition==v.getId()){
functionality = 2;
mqttconnect(sendJSONmessage().optString(topic), sendJSONmessage().toString());
}
else if (R.id.button_detectTimeCondition==v.getId()){
functionality = 3;
mqttconnect(sendJSONmessage().optString(topic), sendJSONmessage().toString());
}
else if (R.id.button_detectIfAllCondition==v.getId()){
functionality = 4;
mqttconnect("Device1/Detect1", sendJSONmessage().toString());
}
else if (R.id.button_count==v.getId()){
functionality = 5;
mqttconnect("Device1/Detect1", sendJSONmessage().toString());
}
}

First, I created the Mqtt class to publish and subscribe to the topic, After, the class to publish the message that I need in JSON format.

The next class I use it to display a notification. And finally, onCreate() and onClick classes where I declared my buttons, edittext... And where I put the conditions to publish a message. More concretly, when push a button, I publish a message in a topic with JSON format.

The App run well. But the question is: How I can publish ONLY one message in a broker?

In the attached picture you can see how publish 5 message when I press the button.
In red you can see the message published 5 or 6 times
Re: How to publish only one message using a Java MQTT client using Eclipse Paho [message #1747029 is a reply to message #1746826] Tue, 08 November 2016 15:57 Go to previous messageGo to next message
James Sutton is currently offline James SuttonFriend
Messages: 69
Registered: July 2015
Member
Looks like you're creating a brand new MqttAndroidClient every time you press a button in your app. This is definitely an inefficient design and will leave a lot of active subscriptions running and many threads spawned.

Take a look at the example application here: https://github.com/eclipse/paho.mqtt.android/tree/develop/paho.mqtt.android.example

You likely want to create and connect the MqttAndroidClient when your activity is created, then when the buttons are pressed, simply publish using the same instance of the client.
Re: How to publish only one message using a Java MQTT client using Eclipse Paho [message #1747051 is a reply to message #1747029] Tue, 08 November 2016 21:55 Go to previous messageGo to next message
Javier Sanchez is currently offline Javier SanchezFriend
Messages: 3
Registered: November 2016
Junior Member
Okay, as you said, I generate a subscribe always that I press a button. Is true.
I dind't think in this.
Thanks!!!!
I try to change and do best my code, and if is resolt, I wiil let know you.
Re: How to publish only one message using a Java MQTT client using Eclipse Paho [message #1747626 is a reply to message #1747029] Wed, 16 November 2016 15:35 Go to previous message
Javier Sanchez is currently offline Javier SanchezFriend
Messages: 3
Registered: November 2016
Junior Member
Thanks! how you said, this was the wrong and seeing the example that you mentioned I resolved Smile

Previous Topic:Callback on start of message received
Next Topic:mqtt architechture
Goto Forum:
  


Current Time: Sun Sep 23 09:56:30 GMT 2018

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

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

Back to the top