Can’t connect to MQTT broker

Hello,

I'm trying to connect my openwrt router with Home Assistant via this topic:

So I create an account in Home Assistant for OpenWRT to login.

Installed the following:

opkg list-installed | grep collect

collectd - 5.12.0-49
collectd-mod-conntrack - 5.12.0-49
collectd-mod-cpu - 5.12.0-49
collectd-mod-exec - 5.12.0-49
collectd-mod-interface - 5.12.0-49
collectd-mod-iwinfo - 5.12.0-49
collectd-mod-load - 5.12.0-49
collectd-mod-memory - 5.12.0-49
collectd-mod-mqtt - 5.12.0-49
collectd-mod-network - 5.12.0-49
collectd-mod-rrdtool - 5.12.0-49
collectd-mod-snmp - 5.12.0-49
collectd-mod-thermal - 5.12.0-49
collectd-mod-uptime - 5.12.0-49

opkg list-installed | grep mosquitto

libmosquitto-nossl - 2.0.18-2
mosquitto-client-nossl - 2.0.18-2

I've created the config in /etc/collectd.conf,

LoadPlugin mqtt
<Plugin "mqtt">
  <Publish "OpenWRT">
    Host "192.168.1.202"
    Port "1883"
    User "mqtt"
    Password "1234"
    ClientId "OpenWRT"
    Prefix "collectd"
    Retain true
  </Publish>
</Plugin>

My collect.conf:

BaseDir “/etc/collectd/”
Include “/etc/collectd/conf.d/*.conf”
PIDFile “/var/run/collectd.pid”
PluginDir “/usr/lib/collectd”
TypesDB “/usr/share/collectd/types.db”
Interval 30
ReadThreads 2

Mqtt explorer shows me nothing...

Buuuut, now comes the problem:
Somehow they can't connect with each other, pw wrong:

logread -f | grep collectd shows:

connected to broker "192.168.1.202:1883"
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: mosquitto_loop failed: The connection was refused.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: publish failed: Connection pending.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: successfully reconnected to broker "192.168.1.202:1883"
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: mosquitto_loop failed: The connection was refused.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: publish failed: Connection pending.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: successfully reconnected to broker "192.168.1.202:1883"
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: mosquitto_loop failed: The connection was refused.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: publish failed: Connection pending.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: successfully reconnected to broker "192.168.1.202:1883"
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: mosquitto_loop failed: The connection was refused.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: publish failed: Connection pending.
Wed Jun 26 22:00:33 2024 daemon.err collectd[4806]: mqtt plugin: successfully reconnected to broker "192.168.1.202:1883"

Mqtt in home assistant gives me this as error:

2024-06-26 22:25:01: New connection from 192.168.1.1:60656 on port 1883.
error: received null username or password for unpwd check
2024-06-26 22:25:01: Client OpenWRT disconnected, not authorised.
2024-06-26 22:25:01: New connection from 192.168.1.1:60672 on port 1883.
error: received null username or password for unpwd check
2024-06-26 22:25:01: Client OpenWRT disconnected, not authorised.
2024-06-26 22:25:20: New connection from 172.30.32.2:60140 on port 1883.
2024-06-26 22:25:20: Client <unknown> closed its connection.
2024-06-26 22:25:31: New connection from 192.168.1.1:49038 on port 1883.
error: received null username or password for unpwd check
2024-06-26 22:25:31: Client OpenWRT disconnected, not authorised.

I even made changes to the firewall:

firewall.@rule[9]=rule
firewall.@rule[9].name='Allow-MQTT'
firewall.@rule[9].src='lan'
firewall.@rule[9].dest_ip='192.168.1.202'
firewall.@rule[9].dest_port='1883'
firewall.@rule[9].proto='tcp'
firewall.@rule[9].target='ACCEPT'

Can anyone help me?

The guys from the Home Assistant forum send me here.

You do not need to add a rule for LAN connections, those are enabled by default.

Anyway, the server is complaining about "received null username or password", but both username and password seem to be configured... How is the MQTT server configured?

@eduperez

Mosquitto broker

Optionen

1 logins:

2 username: mqtt

3 password: 1234

4 require_certificate: false

5 certfile: fullchain.pem

6 keyfile: privkey.pem

7 customize:

8 active: false

9 folder: mosquitto

10 anonymous: true

11 require_certificate: false

So, I wanted to try to connect to another mqtt broker, set up und on my phone, so I've changed the IP to connect to my phone.

I've changed the IP in

/etc/collectd/mqtt.conf & /etc/collectd.conf

But "logread -f | grep collectd" still says he tries to connect to the previous IP, from my home assistant??

I have no clue I have to change somewhere else the IP address?
Maybe in another setting I didn't set up username and password?

The config file for collectd is generated by the /etc/init.d/luci_statistics file, so you have to restart the luci_statistics service for the changes to take effect.

Is this correct?

root@OpenWrt:~# /etc/init.d/luci_statistics restart
root@OpenWrt:~# /etc/init.d/collectd restart
root@OpenWrt:~# logread -f | grep collectd
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: mosquitto_publish failed: Broken pipe
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: publish failed: Connection pending.
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: successfully reconnected to broker "192.168.1.202:1883"
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: mosquitto_loop failed: The connection was refused.
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: publish failed: Connection pending.
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: successfully reconnected to broker "192.168.1.202:1883"
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: mosquitto_loop failed: The connection was refused.
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: publish failed: Connection pending.
Tue Jul  2 07:14:42 2024 daemon.err collectd[10322]: mqtt plugin: successfully reconnected to broker "192.168.1.202:1883"

But I've changed IP to connect to .130

root@OpenWrt:~# vi /etc/collectd/mqtt.conf

LoadPlugin mqtt
<Plugin "mqtt">
  <Publish "OpenWRT">
    Host "192.168.1.130"
    Port "1883"
    User "mqtt"
    Password "1234"
    ClientId "OpenWRT"
    Prefix "collectd"
    Retain true
  </Publish>
</Plugin>
~

If you use "luci_statistics", the configuration resides at "/etc/config/luci_statistics", and the scripts create (temporary) config files when starting.

@eduperez I've added:

config statistics 'collectd_mqtt'
    option enable '1'
    option Host '192.168.1.202'
    option Port '1883'
    option User 'mqtt'
    option Password '1234'
    option ClientId 'OpenWRT'
    option Prefix 'collectd'
    option Retain 'true'

To vi /etc/config/luci_statistics, but nothing changed in home assistant.

Now, see the contents of "/var/collectd.conf"; that is the file created by the init script, and it should reflect your changes.