Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] issue in publish a message in MQTTT

Hi Peiyuan

The MqttMessage object has a constructor which takes the payload - specified as a byte array

MqttMessage(byte[] payload).

If your content is a String, you could code something like MqttMessage message = new MqttMessage(content.getBytes());

Alternatively you could use setPayload(byte[] payload)

There's an example at the bottom of this page...

https://www.eclipse.org/paho/clients/java/
 
Regards

Peter Niblett
IBM Senior Technical Staff Member
Member of the IBM Academy of Technology
+44 1962 815055
+44 7825 657662 (mobile)




From:        fang peiyuan <fang_peiyuan@xxxxxxxxx>
To:        "paho-dev@xxxxxxxxxxx" <paho-dev@xxxxxxxxxxx>
Date:        09/23/2015 04:04 PM
Subject:        [paho-dev] issue in publish a message in MQTTT
Sent by:        paho-dev-bounces@xxxxxxxxxxx





hi  sir / mdm

my name is peiyuan, my project is to extract data from online and publish it to MQTT server

now i am facing an issue how do you publish the extract data?


 example: hightemp =34;
System.out.println("high temperature is" + hightemp);

// i need to publish high temperature is 34 to my client using mqtt

currently i am using org.eclipse.paho.client.mqttv3 and tutorial from IBM this is my code

MQTTPUBLISH

package redbook.weather;

import java.io.IOException;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class MQTTTPublish {
   
   public static byte[] payload;
   public String title;

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       try
       {
           //a. create an instance of MQTT client
           MqttClient client = new MqttClient(publisher.TCPADDRESS,publisher.CLIENTID);
           
           //Connect to server with connection options
           client.connect();
           
           //d.Publish message to topics
           MqttTopic topic =client.getTopic(publisher.Topic);
           
           MqttMessage message =new MqttMessage();
           
           message.setPayload(payload); // where i need to insert the message            
           message.setQos(publisher.QoS);
           
           
           System.out.println("waiting for up to" + publisher.SLEEPTIMEOUT/1000 + "seconds for publication of \"" + message.toString()+"\" with Qos =" +
           message.getQos());
           
           System.out.println("On topic \"" +topic.getName() + "\" for client instance: \"" +
           client.getClientId()+"\" onn address "+client.getServerURI()+"\"");
           
           MqttDeliveryToken token = topic.publish(message);
           
           token.waitForCompletion(publisher.SLEEPTIMEOUT);
           
           System.out.println ("Delievery token \"" + token.hashCode() +"\" has been received:"
                   + token.isComplete());
           
           //e.Diconnect to server
           client.disconnect();
           
           
           
       }
       
       catch (Exception e)
       {
           e.printStackTrace();
       }

   }

}


thank you, Could you kindly tell me how to insert the in to the payload thank you
from  
peiyuan
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev

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


Back to the top