Skip to main content

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

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


Back to the top