[SOLVED] Looking for help with MQTT and ESP8266 communication

That’s always a good idea.

Still learning myself.:smiley: I found that helping others also helps me expand my knowledge.

Please do! Sharing what you build or issues encountered during the process, is a great way to give back to the community.


Well, I think I’m making progress. I set up two buttons in the sitemap to simulate the door switches for our dining room. After getting some naming conventions corrected, I started using some of the code from the MK site to set up the home.items file as a reference. I have the OH site on my phone and when I touch the buttons I can see the changes in the log file. I think something isn’t set right because I’m seeing

2018-10-02 13:43:11.694 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP POST request at 'items/DR_CPDoor' with an invalid status value 'OFF'.

2018-10-02 13:43:14.172 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP POST request at 'items/DR_CPDoor' with an invalid status value 'ON'.

2018-10-02 13:43:16.593 [INFO ] [rest.core.internal.item.ItemResource] - Received HTTP POST request at 'items/DR_PDoor' for the unknown item 'DR_PDoor'.

2018-10-02 13:43:18.491 [INFO ] [rest.core.internal.item.ItemResource] - Received HTTP POST request at 'items/DR_PDoor' for the unknown item 'DR_PDoor'.

2018-10-02 13:43:19.058 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP POST request at 'items/DR_CPDoor' with an invalid status value 'OFF'.

in the log files. I’m sure it has something to do with the mqtt message being sent. I think it has to do with a difference between a switch and a contact. OPEN/CLOSED instead of ON/OFF.
So it looks like the mqtt client is working, now I just have to get the right information being sent.
I don’t know if I should see the same changes on my computer screen when I make changes on my phone.

Below is what I have in the sitemap and items files. Any quick pointers as to what I’m doing wrong?

From the home.items file

Contact DR_CPDoor "Carport Door" <door> { mqtt="<[broker:home/Dining Room Doors/Carport:state:CLOSE]" }

From the home.sitemap file

sitemap home label="Our Home Control Center"
        Frame label="Date"
                Text item=Date

        Frame label="Dining Room Doors"

                Switch item=DR_CPDoor label="Carport"
                Switch item=DR_PDoor label="Patio"


What happens when you change the CLOSED to default?

You may want to start with checking that mqtt is communicating. From the terminal type the following:

$mosquttio_sub -h your_IP_here -p 1883 -u your_user_name -P your_password -t home/DiningRoomDoors/Carport

or for your topic use the # like this
-t home/#

This will spit out everything, but you’ll confirm mqtt is working.
Use Ctrl c to stop the msg from coming in.

I’m not 100% sure but I don’t think you should have spaces in the mqtt topic between Dining and Room and Doors.

OK … I changed Contact to Switch in the items file and now I get this

2018-10-02 14:36:30.270 [vent.ItemStateChangedEvent] - DR_CPDoor changed from OFF to ON

2018-10-02 14:36:30.691 [ome.event.ItemCommandEvent] - Item 'DR_CPDoor' received command OFF

2018-10-02 14:36:30.792 [vent.ItemStateChangedEvent] - DR_CPDoor changed from ON to OFF

2018-10-02 14:36:31.546 [ome.event.ItemCommandEvent] - Item 'DR_CPDoor' received command ON

2018-10-02 14:36:31.635 [vent.ItemStateChangedEvent] - DR_CPDoor changed from OFF to ON

So now it looks more like an ON/OFF switch instead of a door OPEN/CLOSE event.

This is what I got when I typed in your line

Error: Invalid subscription topic 'home/#DiningRoomDoors/Carport', are all '+' and '#' wildcards correct?

and if I leave the # out it just runs with nothing happening until I hit ctrl+c

It’s has to be the way I’m sending the mqtt data. I thought I read some where about the using a switch for OPEN/CLOSED instead of ON/OFF.

That indicates there’s nothing to subscribe to. Check the topic name in the script.

To use the wildcard place it on the end after the last/
so in your case home/# would be the topic. If still nothing, then its a config issue with either the sketch or mqtt,cfg

Here’s a pic. with the password and topic not included. The first run was the temp/humid reading, topic used> Esp/Ds/#. The second using the wild card and topic was> Esp/#

If I wanted temp only then the topic would be> Esp/Ds/Temp no wild card used b/c this is the full topic for temperature.


1 Like

UGH !! Nothing with either command with or without a the default password.
I tried using mqttfx to see if I could get any combination of commands to work nothing there either.

I’m ready to toss this whole thing in the trash …

@johntech lets try one more thing.
Open up two terminal windows in ssh, were gonna publish from one terminal and subscribe from the other. This will verify OH side.

Use this to Publish
$mosquitto_pub -h your IP -p 1883 -u your user -P password -t test/topic -m ‘test message’

In the other terminal to subscribe
$mosquitto_sub -h your ip -p 1883 -u your user -P password -t test/topic

EDIT Addon: I did a quick mqtt password change so I could show an example pic.

Top terminal is subscribing and the bottom is publishing. These are both terminal windows from my OH. I know it may seem weird to publish and subscribe on the same machine but it’s the mosquitto broker not the machine your pub and sub to.

I should have spaced the two out a bit but in the pic its two terminals, just one placed on top of the other.

1 Like

OK !!! I had to change the single ’ to a double " in the sending line

So it looks like the mqtt is working . I just need to get the correct format to send and receive to OH

I also tried it without the username and password - still worked.

Good deal, here is another quick pic to show from a separate machine.

Then you have not set up a username and password in the mqtt.cfg

Do you see how the topic must match? Try changing it by one letter on either terminal. Change the topic to something like> my/very/own/topic
Give yourself a username and password in the mqtt.cfg (see post #7 as reference) and try it.
Now you see a bit about how it works?

1 Like

I am taking notes on all that you are showing me!

I have not setup any passwords and left everything as default until I get this thing working.

Ok now, I think this is the part where I have to figure out how to format the mqtt command to make this all work. I have downloaded the tasmota file but haven’t loaded up mt wemos yet.

So I’m thinking that the code in the home.items file needs to be changed.

 { mqtt="<[broker:home/Dining Room Doors/Carport:state:default]" }

So can I assume that “broker:” is used in place of the IP address? No UN or PW needed for now. That just leaves the message??

Where? Broker is tied to the IP and your user name…:open_mouth: see post number 7

Yes, there should not be any spaces. For now don’t worry about the items file. When you get the wemos set up and publishing messages, check using mosquitto_sub like above, then we’ll take care of the item.

This is good.:grinning:

This is not as good.:roll_eyes:

1 Like

I greatly appreciate all the help you are giving me. I think I will sit down tonight and start a step by set of instructions on a basic setup of the RPi and all the things you are showing me for troubleshooting !!

I will get tasmots setup and loaded into the wemos and follow your suggestions. If you don’t mind, I will get back to you tomorrow with the results.

Again, Thank you very much!!


1 Like

You get tasmota loaded, then tomorrow morning, we’ll have you up and running before you finish your first cup of coffee.:wink:

BTW I watch the sun rise every morning so I’m up in less than 11 hrs from now.:face_with_raised_eyebrow:

Have a good evening.

Well, I have been pounding on this mqtt code from tasmota for a couple hours only to find invalid library files! I got the latest rev right from the Github site. Followed the instructions and no go!

Invalid library found in C:\Users\Owner\Documents\Arduino\libraries\esp-epaper-29-ws-20171230-gemu-1.0: C:\Users\Owner\Documents\Arduino\libraries\esp-epaper-29-ws-20171230-gemu-1.0
Invalid library found in C:\Users\Owner\Documents\Arduino\libraries\esp-epaper-29-ws-20171230-gemu-1.0: C:\Users\Owner\Documents\Arduino\libraries\esp-epaper-29-ws-20171230-gemu-1.0

So I finally found a library called PubSubClient. Real simple code and easy to read and edit as needed.
So I loaded it up and changed the internal connections, UN, PW, and my mqtt host IP. Compiled and it connected right away. I pulled up MQTTfx and it is sending/receiving data about once every couple seconds!!

So I may go back and delete all the files I installed and try again to get tasmota to work later. I have a doctors appointment on Wednesday at 10:30 so I don’t know when I’ll get that done. For now, here is the code for the PubSubClient, mqtt_esp8266 sketch that I have up and running.

Here is the link to the library: https://github.com/knolleary/pubsubclient

 Basic ESP8266 MQTT example

 This sketch demonstrates the capabilities of the pubsub library in combination
 with the ESP8266 board/library.

 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.

 To install the ESP8266 board, (using Arduino 1.6.4+):
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  - Select your ESP8266 in "Tools -> Board"


#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "........";
const char* password = "........";
const char* mqtt_server = "broker.mqtt-dashboard.com";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {


  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i = 0; i < length; i++) {

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  client.setServer(mqtt_server, 1883);

void loop() {

  if (!client.connected()) {

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    snprintf (msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    client.publish("outTopic", msg);

I haven’t used Arduino IDE since moving to PlatformIO and that was a while back. You might want to check some YouTube videos if your having an issue.

If you want to try PlatformIO then down load Atom, its a free text editor. Here is a guide to install on Windows https://www.youtube.com/watch?v=w3HNFXMTSsA

After Atom’s install go up to post 19 and watch the Video guide using PlatformIO to flash tasmota to sonoff. This will show you how to download tasmota and setup Atom to use PlatformIO. The part using the serial adapter doesn’t apply to you.

After Atom and PlatformIo is setup read this https://github.com/arendst/Sonoff-Tasmota/wiki/Wemos%20D1%20Mini
You will need to copy the lines listed into the platformio.ini file as per instructed. Continue reading and following the guide for selecting the wemos.

Now you should be able to flash your wemos with tasmota.

Ok I’ll give that a try. In the mean time I was playing around with that sketch above and got it to send the text ON and OFF or what ever text I put in, to mqttfy screen using my IP address. I have a Huzza board and loaded the same sketch and changing the text being sent and it worked too. I was fiddling around with the site and item files plus reading on the OH site about how to setup the mqtt lines. I thought I had it but still no go.
It still may be how I have those two files coded but then again I didn’t see any thing change in the log file.

If your setting up an item and the message is a text you may need to use transformation for OH to display it. More on that later.
You should be able to open a terminal in OH and use mosquitto_sub -h etc… like yesterday and see the message. Until that happens there is no need to worry with an items file.

I tried d/led the file from his site and unzipped it. Lots more errors displayed about too long of file names etc.

Any way , back to the mosquitto_sub command line as above, when I enter that in I am getting ON OFF displaying on the screen scrolling. That is the data I’m sending.