Mqtt show data in Openhab

Hello everyone,

i want to get data from mqtt but always value dosent appear.

the default.items file contains :

String text “MQTT [%s]” { mqtt="<[msg:mtt/rx/5/values:state:default]" }

the sitemap file contains :

sitemap default label=“Welcome to First Page of SmA” {
Frame label=“MQTT” {

Text item=text [label="text value : "]

}
}

the config file contains :

mqtt:msg=tcp://localhost:1883
mqtt:msg.clientId=mtt

Finally, i create my mqtt client with java program to send date beacause i dont have sonsor yet.

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class main {

public static void main(String[] args) {

    String topic        = "MQTT Examples";
    String content      = "Message from MqttPublishSample";
    int qos             = 1;
    String broker       = "tcp://localhost:1883";
    String clientId     = "mtt";
    MemoryPersistence persistence = new MemoryPersistence();

    try {
        MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        System.out.println("Connecting to broker: "+broker);
        sampleClient.connect(connOpts);
        System.out.println("Connected");
        System.out.println("Publishing message: "+content);
        MqttMessage message = new MqttMessage(content.getBytes());
        message.setQos(qos);
        sampleClient.publish(topic, message);
        System.out.println("Message published");
        sampleClient.disconnect();
        System.out.println("Disconnected");
        System.exit(0);
    } catch(MqttException me) {
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
       // System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
       // me.printStackTrace();
    }
}

}

there is my client console :

Connecting to broker: tcp://localhost:1883
Connected
Publishing message: Message from MqttPublishSample
Message published
Disconnected

I dont know why my openhab log dosent show me any data the .app too.

Please can someone help me ?

thank you

Your Item looks correct assuming your publisher is sending the message to exactly mtt/rx/5/values. I notice in your Java code you are publishing to “MQTT Examples”, not mtt/rx/5/values.

Your sitemap looks to have incorrect syntax. It should be:

Text item=text

If you want to override the label as defined on the Item it should be:

Text item=text "Text value: [%s]"

Assuming you are running all of this on Linux, there is a mosquitto_pub command you can use to send MQTT messages.

There is also an mosquitto_sub command you can use to see if the messages are actually being published to the broker.

Thank you for your response.

You are right, i have fix my sitemap file.

I my Java code im publishing “MQTT Examples” and i found in some blogs that the message should be mtt/rx/5/values. Please did you know what changment that i should do it in my java code ? im newest and dont know what imput that i should do.

im using windows 7, so dose exist an alternative to use mosquitto_sub ? I have not find a favorable way in internet.

Thank you

Replace “MQTT Examples” in your code where you assign the topic variable with “mtt/rx/5/values”

it dosent work, i think that data sent in my client should have a specific format. Please check this link : MQTT - show data

Download one of these tools so you can see whether you areeven publishing anything at all to the topic.

This really isn’t an openHAB issue, its an MQTT issue.

There is no special format required.