Using mqtt from ESP to Openhab2

I have ventured into the realm of mqtt with a fair bit of reading up my sleeve. I wish to use mqtt to post DS18B20 temperatures from an ESP12E to OH2 on Raspberry Pi 2.
I loaded up ESPEasy on the ESP and used this to install mqtt on the Pi:
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
to get the right version of mosquitto thanks to this post:
http://www.esp8266.nu/forum/viewtopic.php?t=111

and as a result I see this on the ESP serial monitor:
DS : Temperature: 20.19 (28-6-a3-69-2-0-0-9d)
WD : Uptime 82 ConnectFailures 0 FreeMem 26584
WD : Uptime 82 ConnectFailures 0 FreeMem 26584
DS : Temperature: 20.12 (28-6-a3-69-2-0-0-9d)
WD : Uptime 83 ConnectFailures 0 FreeMem 26584

so looking nice so far.

In OH2 I have this in my Items file:
Number DS18B20ESP “Temperature ESP2 [%.2f °C]” (HouseTemps) {mqtt="<[localbrokerRB:openHAB2:state:default]"}

and get this in the OH2 log:

2016-06-13 12:08:21.104 [INFO ] [penhab.io.transport.mqtt.MqttService] - Stopping broker connection 'localbrokerrb’
2016-06-13 12:09:20.546 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2016-06-13 12:09:20.548 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'localbrokerrb’
2016-06-13 12:09:22.689 [DEBUG] [org.openhab.binding.mqtt ] - BundleEvent STARTING - org.openhab.binding.mqtt
2016-06-13 12:09:22.697 [DEBUG] [.binding.mqtt.internal.MqttActivator] - MQTT binding has been started.
2016-06-13 12:09:22.702 [DEBUG] [org.openhab.binding.mqtt ] - BundleEvent STARTED - org.openhab.binding.mqtt
2016-06-13 12:09:22.755 [DEBUG] [org.openhab.binding.mqtt ] - ServiceEvent REGISTERED - {org.openhab.model.item.binding.BindingConfigReader, org.openhab.binding.mqtt.MqttBindingProvider}={component.name=org.openhab.binding.mqtt.genericbindingprovider, component.id=187, service.id=324, service.bundleid=204, service.scope=bundle} - org.openhab.binding.mqtt
2016-06-13 12:09:22.761 [DEBUG] [org.openhab.binding.mqtt ] - ServiceEvent REGISTERED - {org.osgi.service.event.EventHandler}={event.topics=openhab/, component.name=org.openhab.binding.mqtt.MqttItemBinding, component.id=186, service.id=326, service.bundleid=204, service.scope=bundle} - org.openhab.binding.mqtt
2016-06-13 12:09:22.789 [DEBUG] [org.openhab.binding.mqtt ] - ServiceEvent REGISTERED - {org.osgi.service.event.EventHandler, org.osgi.service.cm.ManagedService}={event.topics=openhab/
, service.pid=org.openhab.mqtt-eventbus, component.name=org.openhab.binding.mqtt.eventbus, component.id=188, service.id=327, service.bundleid=204, service.scope=bundle} - org.openhab.binding.mqtt
2016-06-13 12:09:22.813 [DEBUG] [ng.mqtt.internal.MqttEventBusBinding] - MQTT: Activating event bus binding.
2016-06-13 12:09:22.832 [DEBUG] [ng.mqtt.internal.MqttEventBusBinding] - Initializing MQTT Event Bus Binding
2016-06-13 12:09:22.837 [DEBUG] [ng.mqtt.internal.MqttEventBusBinding] - MQTT Event Bus Binding initialization completed.
2016-06-13 12:09:22.863 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:broker
2016-06-13 12:09:22.864 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:topic
2016-06-13 12:09:22.866 [WARN ] [mqtt.internal.MqttPersistenceService] - mqtt-persistence:message
2016-06-13 12:09:30.873 [DEBUG] [binding.mqtt.internal.MqttItemConfig] - Loaded MQTT config for item ‘DS18B20ESP’ : 1 subscribers, 0 publishers

I have tried replacing default with + and also with *. But still no temperature data displayed.

In testing I have tried this from the command line
mosquitto_sub -h http://192.168.1.112 -t localbrokerRB:openHAB2/DeviceESP12E/Temperature:*
Unable to connect (Lookup error.).
So I obviously have something NQR. Anyone have any thoughts on this?

The -h parameter to mosquitto_sub is just a host (192.168.1.112) not a URL. The -t is a topic, something like openHAB2/DeviceESP12E/Temperature.

Thanks for yr reply Watou.
I realised I should have been addressing localhost not the IP of the ESP12E so the more appropriate command is :
mosquitto_sub -d -t localbrokerRB:openHAB2/temperature:state:default
but all i get is:
Client mosqsub/32564-robsberry sending CONNECT
Client mosqsub/32564-robsberry received CONNACK
Client mosqsub/32564-robsberry sending SUBSCRIBE (Mid: 1, Topic: localbrokerRB/openHAB2/temperature:state:default, QoS: 0)
Client mosqsub/32564-robsberry received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/32564-robsberry sending PINGREQ
Client mosqsub/32564-robsberry received PINGRESP
Client mosqsub/32564-robsberry sending PINGREQ
Client mosqsub/32564-robsberry received PINGRESP

I have tried with temperature:status and with temperature:value with similar results.

I’m pretty sure all the names are case sensitive, so in your item try localbrokerrb instead of localbrokerRB

Thanks for the support.
Used all lowercase in Items line and also tried the same at the cmd line with the same result. Does Subscribed (mid: 1): 0 suggest that the topic is being returned as empty? I figure that I must have a basic config setting wrong but have not been able to trace it down.

I have almost the same setup, so lets’s compare our settings:

On the config page of the espeasy interface:
MAIN CONFIG:
Name: esp8266ledbutton
You MUST use a “unit no” (it doesn’t matter which one)
Locate controller: use ip adress

On the devices page of the espeasy interface:
Task settings:
Name: alarm
idx/var: you MUST use a number, doesn’t matter which one
Optional settings: Value name 1: switch

Now it comes to the topic to subscribe:
Switch Alarm_Switch_WLAN_IN {mqtt="<[mosquitto:/esp8266ledbutton/alarm/switch:command:ON:1.00],<[mosquitto:/esp8266ledbutton/alarm/switch:command:OFF:0.00]"}

So the topic is: Name: esp8266ledbutton + Name: alarm + Value name 1: switch

Now try to adapt that from my switch to your number item.

Thanks for coming in on this sihui. This is helpful.
My Main config name is DeviceESP12E
Unit number is 2
I have used : Locate controller: use ip adress and the IP address is that of the Pi which is running the mqtt broker.
On Task settings Name is Room1
and Value name is Temperature.
So I should therefore have “<[localbrokerRB:/DeviceESP12E/Room1/Temperature:default]” where localbrokerRB is the broker name in mqtt.cfg - rather different to what I had come up with.
See screenshot of Task summary also.


But I am getting “Binding configuration of type ‘mqtt’ of item ‘DS18B20ESP’ could not be parsed correctly.”
using Item line : Number DS18B20ESP “Temperature ESP2 [%.2f °C]” (HouseTemps) {mqtt="<[localbrokerRB:/DeviceESP12E/Room1/Temperature/#]"}
I will play around with the last few chars after Temperature and see what I get.

Ok so using
Number DS18B20ESP “Temperature ESP2 [%.2f °C]” (HouseTemps) {mqtt="<[localbrokerRB:/DeviceESP12E/Room1/Temperature:state:default]"}
I get 20:52:07.000 [DEBUG] [binding.mqtt.internal.MqttItemConfig] - Loaded MQTT config for item ‘DS18B20ESP’ : 1 subscribers, 0 publishers
in the log.

Don’t forget the lower case: localbrokerrb

Ok using lowercase I get more lines:
20:57:30.812 [DEBUG] [.internal.MqttGenericBindingProvider] - Removing message consumers for item DS18B20ESP
20:57:30.812 [DEBUG] [.internal.MqttGenericBindingProvider] - Removing message publishers for item DS18B20ESP
20:57:30.827 [DEBUG] [binding.mqtt.internal.MqttItemConfig] - Loaded MQTT config for item ‘DS18B20ESP’ : 1 subscribers, 0 publishers
20:57:30.905 [DEBUG] [.internal.MqttGenericBindingProvider] - Removing message consumers for item DS18B20ESP
20:57:30.906 [DEBUG] [.internal.MqttGenericBindingProvider] - Removing message publishers for item DS18B20ESP
20:57:30.922 [DEBUG] [binding.mqtt.internal.MqttItemConfig] - Loaded MQTT config for item ‘DS18B20ESP’ : 1 subscribers, 0 publishers

Plugging this in at the cmdline:
mosquitto_sub -d -t localbrokerrb:DeviceESP12E/Room1/temperature:state:default
still gives just - sending PINGREQ and received PINGRESP

I see this in the mosquitto log after stopping and re-starting the service:
1465818800: New connection from 192.168.1.112 on port 1883.
1465818800: New client connected from 192.168.1.112 as ESPClient2 (c1, k15).
1465819567: New connection from 127.0.0.1 on port 1883.
1465819567: New client connected from 127.0.0.1 as mosqsub/7449-robsberryp (c1, k60).
1465819674: Socket error on client mosqsub/7449-robsberryp, disconnecting.
1465819779: Error in poll: Interrupted system call.
1465819779: mosquitto version 1.4.8 terminating

Then your initial setup of mosquitto seems to have a problem 
 sorry, but I don’t know the reason.
A while ago I’ve written a small tutorial to setup mosquitto and mqtt, maybe that is a little help in the right direction:

That’s not the correct syntax. The MQTT topic in the above is DeviceESP12E/Room1/temperature. The other colon-separated bits are openHAB MQTT item binding config strings, which mosquitto doesn’t know a thing about.

Be careful with slashes within Topic definitions:

DeviceESP12E/Room1/temperature

is a different Topic as

/DeviceESP12E/Room1/Temperature

(the leading slash)

Andreas

1 Like

Ok thanks for the support guys.

I have fixed up the commandline syntax - Thanks @Watou.

So I think the mosquitto terminating may have been due to me playing with the log filename to try and get a fresh log. However it is definitley still disconnecting with a socket error after connecting:
465891360: mosquitto version 1.4.8 (build date Tue, 17 May 2016 11:26:59 +0100) starting
1465891360: Config loaded from /etc/mosquitto/mosquitto.conf.
1465891360: Opening ipv4 listen socket on port 1883.
1465891360: Opening ipv6 listen socket on port 1883.
1465891360: Warning: Address family not supported by protocol
1465891373: New connection from 192.168.1.112 on port 1883.
1465891373: New client connected from 192.168.1.112 as ESPClient2 (c1, k15).
1465891385: New connection from 127.0.0.1 on port 1883.
1465891385: New client connected from 127.0.0.1 as mosqsub/1926-robsberryp (c1, k60).
1465891414: New connection from 127.0.0.1 on port 1883.
1465891414: New client connected from 127.0.0.1 as openHAB2 (c1, k60).
1465891458: Socket error on client mosqsub/1926-robsberryp, disconnecting.
1465891464: New connection from 127.0.0.1 on port 1883.
1465891464: New client connected from 127.0.0.1 as mosqsub/2155-robsberryp (c1, k60).
1465891617: Socket error on client mosqsub/2155-robsberryp, disconnecting.

So I will do some searching on that and see what I can discover. I will check out your tutorial thanks @sihui.
Interesting how it disconnects after Openhab client connects and again when I start a commandline connection.
Also have made sure I don’t have a leading slash.

I am starting to think the issue might be at the ESP12E end. Openhab is seeing nothing arrive, cat openhab.log | grep mqtt gives:
2016-06-14 19:58:31.061 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting connection helper to periodically try restore connection to broker 'localbrokerrb’
2016-06-14 19:58:41.062 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection ‘localbrokerrb’

and cat openhab.log | grep 18B20 gives:
2016-06-14 19:29:03.236 [DEBUG] [binding.mqtt.internal.MqttItemConfig] - Loaded MQTT config for item ‘DS18B20ESP’ : 1 subscribers, 0 publishers

and the only thing I see on the commandline is PINGREQ and PINGRESP.

On the serial monitor it shows this on a reboot:
WIFI : Connected!
INIT : I2C
MQTT : Connected to broker
Subscribed to: /DeviceESP12E/#
and then
DS : Temperature: 20.50 (28-6-a3-69-2-0-0-9d)
WD : Uptime 1 ConnectFailures 0 FreeMem 26888
WD : Uptime 2 ConnectFailures 0 FreeMem 26856
DS : Temperature: 20.50 (28-6-a3-69-2-0-0-9d)

no mqtt messages that mention sub or pub. can this be right?

seems your ESP12 subscribed to the topic (and there is the leading slash
).
Is there any Option to publish within ESPEasy? I don’t use ESPEasy


For testing I use an Android App called “MQTT Dashboard” with which you can subscribe to topics and publish them.

Andreas

So given the ESP log shows the sysname with a leading slash then I should be including a leading slash in my OH Item line?
I thought the ESPEasy is already doing the publishing. I have seen no instructions for its setup. I think the following in EasyESP sets it up.
#define DEFAULT_PROTOCOL 5 // Protocol used for controller communications
// 5 = OpenHAB MQTT

I will give the Android App a go.

According to http://www.esp8266.nu/index.php/ESPEasy#Tools_Advanced_page you can check within the “Tools - Advanced” to which topic you publish


But I don’t use ESPEasy by myself, so just another guess