MQTT assistance

Ok, I’ve setup my first MQTT sensor but I’m having a final difficulty that has me stumped as to where to look to fix it.

I have a NodeMCU development board with a DHT11 temp and humidity sensor attached. Sketch written and uploaded to the DB after a few hitches and now I can see the device in my wifi network so all good there.

I know MQTT is working as I can run the “Hello World” test without any issues either on my RPI which is running Home Assistant.

Built the sensors into HA and can see them on my UI but here’s where the hitch is, they are constantly showing as unknown and I cannot seem to get a reading for either temp or humidity from the sensor.

Should I be thinking it’s a faulty sensor or is there a way I can test that out to see if I’ve made a mistake somewhere else?

Please provide the relevant parts of your sensor configuration (configuration.yaml) and a dump of your MQTT traffic. You can use for example

mosquitto_sub -v -h localhost -t '#'

to capture all MQTT traffic. Does the topics match? Is the payload properly structured?

Ok, here is the config info:

  port: 1883
  client_id: home-assistant-1
  username: my username 
  password: my password

The sensors

sensor 8:
  - platform: mqtt
    name: 'Bathroom Temperature'
    state_topic: 'bathroom/sensor1'
    unit_of_measurement: '°C'
    value_template: '{{ value_json.temperature }}'
  - platform: mqtt
    name: 'Bathroom Humidity'
    state_topic: 'bathroom/sensor1'
    unit_of_measurement: '%'
    value_template: '{{ value_json.humidity }}' 

Now when I try to do the dump of MQTT data I’m getting the following.

Connection Refused: not authorised.

Perhaps this is where my issue is? How would I give it authorisation?

You can provide a username and password:

mosquitto_sub -v -h -u username -P password -t '#'

Ok, that appears to work better (sorry it’s been a long day) although I’m getting no response from the pi. The sketch was designed to wait 10 minutes between the sensor reporting in to conserve energy so one would assume I need to wait up to 10 minutes perhaps.

If you aren’t seeing any output in the mosquitto_sub window (when a message hits the broker, you’ll see the topic and payload of the mqtt message) even after whatever interval the NodeMCU should be reporting on, it means that the NodeMCU isn’t sending any messages. Debug your code first.

I should add it could be because the NodeMCU is not connecting to your broker, or your code not publishing a message every 10 mins like you think it should.

thanks, that’s what it looks like but I used the example code form the HA MQTT page and even chatted with the guy who wrote it. It all compiled and loaded correctly.

Guess I’ll have to go through the entire code to see what the issue is (not that I’m sure I’ll even know what to look for)


I’ve tried the following command as well

mosquitto_sub -d -u username -P password -t bathroom/sensor1

and got the following responses

Client mosqsub/11023-raspberry sending PINGREQ
Client mosqsub/11023-raspberry received PINGRESP

but that’s it so far

Ok, found a rather large mistake. My 3 pin DHT11 has data from pin 1 not the middle pin.

so now my result is as follows.

Client mosqsub/11615-raspberry sending CONNECT
Client mosqsub/11615-raspberry received CONNACK
Client mosqsub/11615-raspberry sending SUBSCRIBE (Mid: 1, Topic: bathroom/sensor1, QoS: 0)
Client mosqsub/11615-raspberry received SUBACK
Subscribed (mid: 1): 0

Much happier now that I discovered my stupid mistake

I don’t think I understand why your DHT wired correctly to the NodeMCU would have anything to do with the Raspberry Pi (which I assume is running HA) subscribing to the topic. They are independent things. Is it working now? That’s all that matters.

No it is still not working but I would think that the better response the Pi received was a good sign. Sorry, this is the first time I’ve tried working with MQTT so not very sure of myself.

Is the output I’ve received back on the pi ok? My concerns with not receiving the temp and humidity is because I think the sensor is not kaput after I hooked it up to the power source not the data source.

Your ESP8266 should publish messages. We haven’t received/seen any message yet. May be you should add some debug output (serial) to your sketch. So you can be sure what the MCU is doing. If you can see proper measurements you can try to publish the measurements via MQTT. If you post your sketch the support would be easier.

the sketch I have used is the one linked on the HA site.

The only changes are on line 72 where I’ve changed DHT22 with DHT11. Obviously all the wifi info and MQTT info has been amended accordingly.

Please attach the nodeMCU to an USB port and provide the serial output:

  1. Arduino IDE -> Tools -> Port -> select the serial device (ttyUSB0?)
  2. Arduino IDE -> Tools -> Serial monitor

Press the reset button and provide the output. :slight_smile:

should it look like this


It just keeps going on an on, is that right?

ok, loaded the sketch and tried your instructions and absolutely nothing from the serial monitor

lol. Set the baudrate to 115200.

Still nothing. Should I unhook the dht11 and all associated wiring or leave it all hooked up?

I just flashed your sketch without hooking up an DHT sensor. The serial output should looks like this:

1384, room 16 
tail 8
chksum ⸮⸮

INFO: WiFi connected
INFO: IP address:
INFO: Attempting MQTT connection...INFO: connected
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!