I want to use MQTT protocol using mosquitto library.
First of all, I want to do some test installing mosquitto-clients
sudo apt-get install mosquitto-clients
This program provides two "method":
Following this instructions I'm trying to submit new topic:
mosquitto_sub -d -t newtopic/test
using default host/port [localhost/1883].
I obtain:
Error: Connection refused
Is too generic as error.. can anyone help me?
Could be is a firewall problem? In this case, how can I check if is this the problem?
I'm using linux ubuntu ( 3.8.0-42-generic #62~precise1-Ubuntu)
nb same behaviour writing custom program using libmosquitto.
Just edit Mosquitto configuration file ( /etc/mosquitto/conf.d/mosquitto.conf
) adding these lines...allow_anonymous true
listener 1883 0.0.0.0
... and restart Mosquitto (as service or not).$ sudo service mosquitto restart
or$ mosquitto --verbose --config-file /etc/mosquitto/conf.d/mosquitto.conf
As informed here, since v.1.7 allow_anonymous
defaulted to false.
It is also useful to check log messages ( /var/log/mosquitto/mosquitto.log
).
Finally, run Mosquitto subscriber/publisher using --host
(-h
) parameter and the host IP address (get if from ifconfig
or ip -color addr
command).
The default host:port combination for mosquitto_pub/sub is localhost:1883. If you do not have a broker running on your local computer then it will not be able to connect of course.
The solution is to either run the broker on your local computer, or to tell the utilities where to connect. For example:
mosquitto_sub -t newtopic/test -h test.mosquitto.org
None of the other answers worked for me. In my case, I had upgraded from mosquitto 1.X to mosquitto 2.0, which requires a new configuration to be added to your mosquitto.conf
:
listener 1883
For clients other than localhost to connect (ie, via Docker)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With