Help out with mqtt binding - beginner

hi,

I have installed mqtt - broker on openhab2 (linux ubuntu)

Seems to work:

2018-02-28 20:55:47.587 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2018-02-28 20:55:47.593 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection ‘localbroker’

and i have an esp8266 with a dht22 attached to it.

The log of the esp says its connects to the mqtt broker
5498 : MQTT : Connected to broker

what i do not get is - how to get the info of the esp to display in my basicUI.

What do i put in the .items file

please help me out to understand

That is pretty simple:
Create an item that subscribes to the data that are sent to the MQTT Broker by your ESP.
In my case, I do have an ESP that is using the pubsubclient to publish the following data:

  if(MQTT_Client.connect("ESP32_GardenSensors", MQTT_User, MQTT_Pass))
  {
    MQTT_Client.publish(SensorRaisedBed.MQTT_BatteryTag, "-1");
    MQTT_Client.publish(SensorRaisedBed.MQTT_ValueTag, "-1");
  }

The value of SensorRaisedBed.MQTT_ValueTag is:

Home/Garden/RaisedBed/HumiditySensor/Value

And the MQTT_Client is an instance of the PubSubClient:

PubSubClient MQTT_Client(MQTT_Server, MQTT_Port, MQTT_callback, espClient);

And on the other hand, I do have the following item:

String MoistureRaisedBed			"Feuchtigkeit Hochbeet [%s]"	        {mqtt="<[mosquitto:Home/Garden/RaisedBed/HumiditySensor/Value:state:default"}

So once the ESP boots up and connects to the WiFi it sends “-1”.
This currently is for testing purposes since i am waiting for the rest of the hardware. But you should get the idea.

If Sascha’s reply did not help, check out my thread here as it gives you rules and items to send and receive MQTT messages. The first post in the thread is long but it should spark some ideas and you have the knowledge that they are working examples you can copy and adjust to what you need…

oke,
i installed MQTT.fx - here i have a reading on the esp through the broker on the same computer as openhab. So the MQTT broker works and the esp get’s its data to the broker

i added this to .item

Number		Temperature_dht     "Temperatuur DHT22 [%.1f °C]"						<temperature> {mqtt="<[localbroker:dht22_01/temp/Temperature:state:default]"}

and this to the sitemap:

Text item=Temperature_dht

but then the site map does not work any more.

The mistake is an the .item file put where ?

Can you show your sitemap file?

Update:
Map works, but i do not get a value

You said that you added your item to .item
The item definition looks right
What is the name of your items file
It must end with .items NOT .item

Regards

It’s items. i added the mqtt in an exesting items file.
i tried differrent versions as ik guessing the mistake is in the broker name:

|Number||Temperature_dht     "Temperatuur DHT22 [%.1f °C]"||||||<temperature> {mqtt="<[localbroker:dht22_01/temp/Temperature:state:default]"}|
 
|Number||Temperature_dht1     "Temperatuur DHT22_01 [%.1f °C]"||||||<temperature> {mqtt="<[localBroker:dht22_01/temp/Temperature:state:default]"}|
 
|Number||Temperature_dht2     "Temperatuur DHT22_02 [%.1f °C]"||||||<temperature> {mqtt="<[openHab:dht22_01/temp/Temperature:state:default]"}|

But no luck yet

What does your mqtt.cfg looks like?
Mine is:

mybroker.url=tcp://192.168.0.34:1883
mybroker.clientId=openhab
mybroker.user=XXXXXXX
mybroker.pwd=XXXXXX

If your broker is running in the same machine then you can use tcp://127.0.0.1:1883
the clientId doesn’t really matter as long as it is unique to the broker
I have set up mosquitto for username and password
If you haven’t you don’t need the last two lines and can be commented out with ‘#’

Regards

Then one of my items definition:

Switch	LivingRoom_WallSwitch	      "Wall Switch"	                 <switch2> (LivingRoom, Switches, MSwitches)     { mqtt="<[mybroker:House/LivingRoom/WallSwitch:state:default]" }

Hi,
I just re-read the topic and it looks like the openhab is connecting to mosquitto
Sorry.
your broker name is localbroker (no capitals) so your first item definition is correct

What topic does your ESP publishes the value under
The topic in openHAB must be exactly the same.

Regards

in mqtt.cfg i have this:

mqtt:localBroker.url=tcp://localhost:1883
mqtt:localBroker.clientId=openHab
mqtt:localBroker.qos=1

I installed MQTTfx and there this value produceses a temp

/dht22_01/temp/Temperature

So i was thinking that this

Number		Temperature_dht1     "Temperatuur DHT22_01 [%.1f °C]"                   <temperature> {mqtt="<[localBroker:dht22_01/temp/Temperature:state:default]"}

would produce a value in the site map but it does not

Change your mqtt.cfg as follow:

localBroker.url=tcp://127.0.0.1:1883
localBroker.clientId=openHab
#mqtt:localBroker.qos=1

Remove the “mqtt:” in front of each line (Not necessary in OH2)
You are on a local machine so the qos is really not needed

Regards

Victory - while banging my head against the wall

{mqtt="<[localBroker:/dht22_01/temp/Temperature:state:default]"}
{mqtt="<[localBroker:dht22_01/temp/Temperature:state:default]"}

Spot the diffenrence

Yes of course
Sorry I missed that!
As I said, the topics need to be exactly the same

Enjoy