[SOLVED] MQTT not connecting to nodemcu

If mosquitto is running and you have not set a password on it you need to check taht it is set-up to allow anonymous connections
Google mosquitto allow anonymous

Your computer and the nodeMCU are on the same lan 192.168.43.x

Can can also check to mosquitto logs to see why it refuses the connection
Google mosquitto logs

Thank you @vzorglub
I secured mqtt with a password and it connected now.

@vzorglub can you help me configuring the mqtt on openhab for the same code.

Install the mqtt binding in OpenHAB
in your openHAB server, in your conf/services folder you should have a file called mqtt.cfg

The config for it is described in:

https://docs.openhab.org/addons/bindings/mqtt1/readme.html#transport-configuration
Your will need to choose a broker name, choose mosquitto
so you configuration will be:

mosquitto.url=tpc://192.168.43.102:1883
mosquitto.clientId=openhab
mosquitto.user= (YOUR MOSQUITTO USERNAME)
mosquitto.pwd= (YOUR MOSQUITTO PASSWORD)

Save
Restart openHAB

OH should now be connected as a client to your broker

Your can now bind items to mqtt topics by following:

https://docs.openhab.org/addons/bindings/mqtt1/readme.html#item-configuration-for-inbound-messages

and

https://docs.openhab.org/addons/bindings/mqtt1/readme.html#item-configuration-for-outbound-messages

EVERYTHING you need to know in in the docs. If it doesn’t work read the docs again, and again, and again


Good luck

Thank you @vzorglub

Did you get it working?

No I tried configuring the mqtt.cfg files as you said but it still not responding.

Can you show me your mqtt.cfg file, please?
Did you install the mqtt binding?

Here is the mqtt.cfg

#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
broker.url=tcp://192.168.43.102:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
#broker.clientId=

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
broker.user=ajay

# Optional. Password to authenticate with the broker.
broker.pwd=******

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
#broker.qos=0

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#<broker>.retain=<retain>

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
#<broker>.async=<async>

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>

Items file

Switch Light "Light" {mqtt=">[broker:light:command:ON:1],>[broker:light:command:OFF:0]"}

sitemap

sitemap default label="Demo"
{
	Switch item=Light label="Light"
}

I changed topic in my nodemcu code to “light”

Switch Light "Light" { mqtt=">[broker:light:command:ON:1],>broker:light:command:OFF:0]" }

Remember when testing if the broker was running, you start an mqtt client on the command line: do this one now:

mosquitto_sub -t "light" -u "ajay" -P "password"

Then action the switch for the light in Openhab
Do you see the messages coming in?

No!

There is an error in your binding
Didn’t see it at first, sorry

Switch Light "Light" { mqtt=">[broker:light:command:ON:1],>[broker:light:command:OFF:0]" }

Your forgot a [

No, I actually placed it in my original binding

Switch Light "Light" {mqtt=">[broker:light:command:ON:1],>[broker:light:command:OFF:0]"}

Can you show me the openhab.log when openhab starts, please?
And also when you edit something in your item file and save, thanks

I followed this path /var/log/openhab2 as suggested in the openhab docs https://docs.openhab.org/installation/linux.html#file-locations

But found no log files there

How did you install OH?

using repository as per this
https://docs.openhab.org/installation/linux.html#installation

In your console:

cd /var/log/openhab2
ls -l