Using mqtt from ESP to Openhab2

@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.

The winning line in the Items file is:
Number DS18B20ESP “Temperature ESP2 [%.2f °C]” (HouseTemps) {mqtt="<[localbrokerrb:/DeviceESP12E/Room1/Temperature:state:default]"}

Hope this thread helps some others with DS18B20 sensors using mqtt on ESP8266 to Openhab2.

Hi Rob,

this are good news that your MQTT is working now.
Because of you I gave ESPEasy a try on my project with ESP8266 and 11 DS18B20 and one DHT11.
The setup of the devices is easy going, but my ESP8266 won’t connect to my home wifi :wink:

So now I am on my way to get this (normally easy) part done…

Andreas

Hi Andreas
Can you ping your ESP device?

Slightly OT, but another (very good) ESP framework for MQTT IMO is Homie. YMMV and I am sure ESPEasy is great. Just thought I would add it here for those interested.

Myself and @jpmens wrote a python based OTA update server which allows you to monitor and update your Homie devices from the comfort of your armchair!

Hi Rob,

I have some ESP-12E modules soldered to one of these white breakout boards.
With one of these boards I tried to use ESPEasy:

  • all 11 DS18B20 are detected and the right temperature is displayed
  • the DHT11 is working as well, temperature and humidity are in the expected range
  • the AP-mode is working, I can connect to ESP_0 and get the config pages
  • giving the right credentials for WifiSSID and Wifikey, the ESP cannot connect to my home wlan

This weekend I tried with an NodeMCU v1.0 compatible board and had the same sketch compiled and loaded. And here I have no problem connecting to my home wlan!!!
The way was the same:

  • loading sketch to ESP
  • restart ESP with power cycle
  • use serial console
  • set WifiSSID, Wifikey and do “save” to EEPROM
  • reset device
  • first attempt to connect to WLAN was sucessful!!!

So I have to find the difference of both designs to check, if the problem is hardware related or if I bought scrap.

Andreas

PS: first I was looking for Homie. But for me it is not obvious at first sight how to configure my 12 sensors within Homie. Within ESPEasy you just go step by step through the webbased configuration pages…
Maybe someone can give me an easy example, how to configure Homie for my Project (please in a new thread, I don’t want to hi-jack this thread)

Thanks for the post on Homie. I have not checked it out but will do so. Maybe it is a good option but certainly EasyESP is well developed for a range of devices.
I notice reference to Wemos in your device table. Would that be a Wemos D1 by any chance? I think this is a pretty smart ESP8266 device for being pin compatible with arduino boards so the use of existing shields can be extended. makes the 3V3 and 5V issue resolve easily also.

Have you tried setting the device (Unit Nr.) to a non-zero value so you have e.g. ESP_2 ? I think you just need to then connect wirelessly once to that AP and confirm your local IP address and then you can connect with your browser.
Maybe your [quote=“imhofa, post:32, topic:11549”]
NodeMCU v1.0 compatible board
[/quote]
had a device Number 1 so it went thru smoothly?

Wemos D1 Mini. Great little device, very cheap, and there is a range of
shields from Wemos as well.

Yes mine is D1 (retired) now replaced by:
http://www.wemos.cc/Products/d1_r2.html
which is still pin compatible with arduino - this was my interest. I particularly wanted the Arduino FET shield for driving an LED strip (about 60W).
The D1 Mini is indeed a great little device with its range of shields and you can do so much in a compact package.

I set the unit nr to a different value than 0 (first ESP to 1 and NodeMCU compatible I set to 2) within the first configuration. After this setting and a reboot I was able to connect to ESP_1 or ESP_2 but the ESP8266 was not connecting to my home WLAN…

Because the NodeMCU was working like a charme without any poblems, I think the different PCBs will be the answer. At the moment I added a R-C-timedelay at RESET to get a defined reset at power-up (and also have a stable voltage at RESET through the pull-up) and put a 4u7 capacitor direct on the ESP8266-PCB between 3V3 and GND.

This evening I will try if this is the solution for the probelm… Otherwise I have to look for other differences within the schematics.

Andreas

So you could connect to the wireless AP ESP_1 and after confirming you could not see a wireless IP address at your router? And you could for the NodeMCU? If so did you select the correct board in the Arduino IDE for the ESP8266? What is your ESP8266 device?

Hello Rob,

last night I just plugged in my ESP-12E with the additional electronic parts (Reset with pull-up R=10k and capacitor 220nF to GND; capacitor 4u7 between VCC and GND) and it connected to my home WLAN without anny further Settings (I compiled the source code with the credentials of my WLAN as Default values…)

So everything works fine now and I can see may temperature and humidity values within my MQTT Broker.

Now I just have to install the DS18B20 eithin my heating system and everything will work like a charme…

Thank you for your help,
Andreas

Glad you got it working.
Do you think it was all to do with the resistors and caps?