Using mqtt from ESP to Openhab2

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…

@andreas. Thanks for your interest and support. I have previously checked out “Tools - Advanced” It shows the template that is used after I have nominated the Openhab MQTT protocol. In my case I get as attached.


So that suggests a topic looking like /DeviceESP12E/Room1/Temperature
just as suggested by @sihui earlier.

This is the correspoding code in ESPEasy.ino on the ESP:
strcpy_P(Settings.MQTTpublish, PSTR("/%sysname%/%tskname%/%valname%"));

I am going to check out other sections of this code to see if I can send the contents of this to the serial monitor:
String pubname = Settings.MQTTpublish;
pubname.replace("%sysname%", Settings.Name);
pubname.replace("%tskname%", ExtraTaskSettings.TaskDeviceName);
pubname.replace("%id%", String(event->idx));

I am hoping Serial.println(pubname) inserted in the right place will show me what the code is sending to the broker.

Other than that I might have to try some other code on the ESP. ESPEasy just seemed to be a very concise, well-structured code to use and build on with other devices as things developed on my system.
Perhaps I should head over to the ESPEasy forum with this problem.

I think you make things too complicated (or I don’t understand where the problem is):

Using for example MQTT.fx or any other mqtt tool would show you instantly what topic you need to subscribe to and what data is sent to openhab (if everything is set up properly).

Just subscribe to topic # and you will see all data … that’s the way I found out how espeasy and openHAB work together …

1 Like

mosquitto_sub -d -t /DeviceESP12E/# -v
Client mosqsub/16860-robsberry sending CONNECT
Client mosqsub/16860-robsberry received CONNACK
Client mosqsub/16860-robsberry sending SUBSCRIBE (Mid: 1, Topic: /DeviceESP12E/#, QoS: 0)
Client mosqsub/16860-robsberry received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/16860-robsberry sending PINGREQ
Client mosqsub/16860-robsberry received PINGRESP

That’s all I get hence I reckon I am getting nothing from the ESP. I believe I should see all messages sent by DeviceESP12E. True?

Maybe … I never had to debug mqtt or mosquitto, it worked almost right out of the box.

I don’t know where all the parameters in your sub command stand for, but for comparison I did the same command to my system, this is the output:

pi@raspberrypi2 ~ $ mosquitto_sub -d -t /esp8266ledbutton/# -v
Client mosqsub/29565-raspberry sending CONNECT
Client mosqsub/29565-raspberry received CONNACK
Client mosqsub/29565-raspberry sending SUBSCRIBE (Mid: 1, Topic: /esp8266ledbutton/#, QoS: 0)
Client mosqsub/29565-raspberry received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/29565-raspberry received PUBLISH (d0, q0, r1, m0, '/esp8266ledbutton/alarm/switch', ... (1 bytes))
/esp8266ledbutton/alarm/switch 0
Client mosqsub/29565-raspberry sending PINGREQ
Client mosqsub/29565-raspberry received PINGRESP
Client mosqsub/29565-raspberry sending PINGREQ
Client mosqsub/29565-raspberry received PINGRESP

There is definitely a difference!

Then I did subscribe to the topic # which gives the following output:

pi@raspberrypi2 ~ $ mosquitto_sub -d -t /# -v
Client mosqsub/29677-raspberry sending CONNECT
Client mosqsub/29677-raspberry received CONNACK
Client mosqsub/29677-raspberry sending SUBSCRIBE (Mid: 1, Topic: /#, QoS: 0)
Client mosqsub/29677-raspberry received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/29677-raspberry received PUBLISH (d0, q0, r1, m0, '//ledbutton/alarm/switch', ... (1 bytes))
//ledbutton/alarm/switch 0
Client mosqsub/29677-raspberry received PUBLISH (d0, q0, r1, m0, '/ledbutton/alarm/switch', ... (1 bytes))
/ledbutton/alarm/switch 1
Client mosqsub/29677-raspberry received PUBLISH (d0, q0, r1, m0, '/esp8266ledbutton/alarm/switch', ... (1 bytes))
/esp8266ledbutton/alarm/switch 0
Client mosqsub/29677-raspberry sending PINGREQ
Client mosqsub/29677-raspberry received PINGRESP

Note that this shows you also the topic where espeasy publishes the output.

I would do the following to first test your mosquitto:

open two terminal windows, in the first one:
pi@raspberrypi2 ~ $ mosquitto_sub -h 127.0.0.1 -t myTopic/#

in the second one:
pi@raspberrypi2 ~ $ mosquitto_pub -h 127.0.0.1 -t myTopic/Temp -m "23.7"

Output in the first one should be:
pi@raspberrypi2 ~ $ mosquitto_sub -h 127.0.0.1 -t myTopic/#
23.7

Edit: or use your dedicated ip adress instead of 127.0.0.1 if you open the terminal windows from another computer system.

This is great - thanks.
I have never seen anything like:

hence my pursuit of an answer.
The two terminals test works perfectly as do similar tests I have done previously.
I think I will try re-loading the ESP software and see if anything changes.

Ok. Solved. It was my ESP setup. I decided I had to publish something else from the ESP12E so used a system info parameter in a new task to prove I could send a message of some sort. Had some success there. Went back and adjusted a couple of things with my temperature device and am now sending data which can be seen at the cmdline.
Not yet getting data into OH2 but with a bit of trial and error with the items line should be good to go.
Thanks for the help and patience guys.