MQTT implementation using Arduino and Mosquitto - mqtt

I downloaded the Mosquitto broker and trying to implement MQTT protocol using Arduino UNO + Ethernet shield as client and Mosquitto on a desktop. As soon as I run the .exe file of the broker named "mosquitto", a Command line prompt pops up, and stays just like that with no lines! Does this happen as such or should it be displaying some message?
Also, the pub and sub files of the Mosquitto (.exe) when run, open a command propmpt and immediately close! They don't stay stagnant at all! I am using Arduino UNO with Ethernet shield to publish data but then the broker is what creates the above mentioned problem.

The mosquitto broker and the mosquitto_pub & mosquitto_sub commands are not GUI applications.
The mosquitto broker command on windows when run by double clicking on it will just open a terminal and wait for connections, this is working properly.
The mosquitto_pub and mosquitto_sub commands need to be run from a command prompt, not by just clicking on them. This is because they require command line arguments in order to do anything useful.
Basically to publish something with mosquitto_pub you need to supply a message to send
mosquitto_pub -t some/topic -m "message payload"
To subsribe you need to supply the topic to listen on:
mosquitto_sub -v -t some/topic
Please read the following answer for more details: How to test the `Mosquitto` server?

Related

can't connect to mosquitto broker from a different machine on my local network [duplicate]

I have a virtual machine that is supposed to be the host, which can receive and send data. The first picture is the error that I'm getting on my main machine (from which I'm trying to send data from). The second picture is the mosquitto log on my virtual machine. Also I'm using the default config, which as far as I know can't cause these problems, at least from what I have seen from other examples. I have very little understanding on how all of this works, so any help is appreciated.
What I have tried on the host machine:
Disabling Windows defender
Adding firewall rules for "mosquitto.exe"
Installing mosquitto on a linux machine
Starting with the release of Mosquitto version 2.0.0 (you are running v2.0.2) the default config will only bind to localhost as a move to a more secure default posture.
If you want to be able to access the broker from other machines you will need to explicitly edit the config files to either add a new listener that binds to the external IP address (or 0.0.0.0) or add a bind entry for the default listener.
By default it will also only allow anonymous connections (without username/password) from localhost, to allow anonymous from remote add:
allow_anonymous true
More details can be found in the 2.0 release notes here
You have to run with
mosquitto -c mosquitto.conf
mosquitto.conf, which exists in the folder same with execution file exists (C:\Program Files\mosquitto etc.), have to include following line.
listener 1883 ip_address_of_the_machine(192.168.1.1 etc.)
By default, the Mosquitto broker will only accept connections from clients on the local machine (the server hosting the broker).
Therefore, a custom configuration needs to be used with your instance of Mosquitto in order to accept connections from remote clients.
On your Windows machine, run a text editor as administrator and paste the following text:
listener 1883
allow_anonymous true
This creates a listener on port 1883 and allows anonymous connections. By default the number of connections is infinite. Save the file to "C:\Program Files\Mosquitto" using a file name with the ".conf" extension such as "your_conf_file.conf".
Open a terminal window and navigate to the mosquitto directory. Run the following command:
mosquitto -v -c your_conf_file.conf
where
-c : specify the broker config file.
-v : verbose mode - enable all logging types. This overrides
any logging options given in the config file.
I found I had to add, not only bind_address ip_address but also had to set allow_anonymous true before devices could connect successfully to MQTT. Of course I understand that a better option would be to set user and password on each device. But that's a next step after everything actually works in the minimum configuration.
For those who use mosquitto with homebrew on Mac.
Adding these two lines to /opt/homebrew/Cellar/mosquitto/2.0.15/etc/mosquitto/mosquitto.conf fixed my issue.
allow_anonymous true
listener 1883
you can run it with the included 'no-auth' config file like so:
mosquitto -c /mosquitto-no-auth.conf
I had the same problem while running it inside docker container (generated with docker-compose).
In docker-compose.yml file this is done with:
command: mosquitto -c /mosquitto-no-auth.conf

I get "unintentionally" mqtt topics with old values. how can I delete topics

My mqtt broker runs very well and reliably on a Raspberry 3B;
However, I get "unintentionally" mqtt topics with old values. (probably connecting corpses)
1: How can I reset the broker or delete (not unscribe) individual topics
2: How can I determine the client where or why the values come from?
I'm using python 3.7, it would be great to get a hint for python,
I've already tried this without success:
First we have to stop the mosquitto broker.
sudo systemctl stop mosquitto.service
or
sudo service mosquitto stop
Delete the mosquitto.db containing all the stored message data in the persistence. By default, located in /var/lib/mosquitto/mosquitto.db
sudo rm /var/lib/mosquitto/mosquitto.db
Restart the mosquitto service
sudo systemctl start mosquitto.service
or
sudo service mosquitto start

How do I specify a host for mqtt nodes in node-red?

I am keen on knowing how can I specify a host in node-red mqtt in node, let's say that my command line would be like this
mosquitto_sub -h <Region>.thethings.network -t '+/devices/+/events/activations' -u '<AppID>' -P '<AppKey>' -v
How can I set the host part -h <Region>.thethings.network in the mosquitto node that node-red provides?
So far I have using this nomemclature <AppID>/devices/+/up as topic, but now I don't know where to add the host part.
Thanks in advance,
Kind regards!
The MQTT in and out nodes share a set of config nodes that contain the information about connecting to the broker.
If you click on the pencil icon at the end of the Server property in the editor
This will open a new config section that you can enter all the broker details.
Because these are shared you only need to add the details once, then you can just select them from the Server drop down list in the original node config.

Broadcasting UDP from within a Docker container

I've just started experimenting with Docker and have hit an issue I haven't been able to resolve (please note, I'm using boot2docker).
My container has a simple service that listens on 8080/tcp and 5000/udp.
docker run -d -p 0.0.0.0:5000:5000/udp -p 0.0.0.0::8080 test/service
From my macos terminal, I can telnet to 192.168.59.103:8080 and issue simple commands, so TCP is working fine.
Next, I try UDP by issuing the following:
echo "HELLO" | socat - UDP-DATAGRAM:192.168.59.103:5000,broadcast
via Wireshark, I can see the datagram making it to the service and the service tries to echo it back, but receives an ICMP response stating the port is invalid.
So it seems I'm pretty close to having a working test case, but I'm just not sure what I need to configure to allow the broadcast back to the mac terminal that initiated the call.
Any advice would be appreciated.

How to test the `Mosquitto` server?

I am new to Mosquitto and MQTT, I downloaded the Mosquitto server library but I do not know how to test it.
Is there any way to test the Mosquitto server?
In separate terminal windows do the following:
Start the broker:
mosquitto
Start the command line subscriber:
mosquitto_sub -v -t 'test/topic'
Publish test message with the command line publisher:
mosquitto_pub -t 'test/topic' -m 'helloWorld'
As well as seeing both the subscriber and publisher connection messages in the broker terminal the following should be printed in the subscriber terminal:
test/topic helloWorld
EDIT:
It is worth pointing out that from v2.0.0 of Mosquitto it will only be listening for connections on the loopback interface by default. If you want to access the broker from machines other than the one it is installed on you will need to edit the config file (and pass it to the broker with the -c option e.g. mosquitto -c /path/to/mosquitto.conf) to enable listening on other interfaces. Details can be found in the v2.0.0 release notes here
Start the Mosquitto Broker
Open the terminal and type
mosquitto_sub -h 127.0.0.1 -t topic
Open another terminal and type
mosquitto_pub -h 127.0.0.1 -t topic -m "Hello"
Now you can switch to the previous terminal and there you can able to see the "Hello" Message.One terminal acts as publisher and another one subscriber.
To test and see if you can access your MQTT server from outside world (outside of your VM or local machine), you can install one of the MQTT publishing and monitoring tools such as MQTT-Spy on your outside-world machine and then subscribe for '#" (meaning all the topics).
You can follow this by the method #hardillb mentioned in his answer above and test back and forth such as this:
On the machine with Mosquitto Server running,
On the outside-word machine with mqtt-spy running,
I have mainly mentioned mqtt-spy since it's multi-platform and easy to use. You can go with any other tool really.
And also to my knowledge to run the mosquitto_sub and mosquitto_pub you need to have mosquitto-clients installed on your Linux machine running the test (in my case Ubuntu) which can be done easily by,
sudo apt-get install mosquitto-clients
If you are using Windows, open up a command prompt and type 'netstat -an'.
If your server is running, you should be able to see the port 1883.
If you cannot go to Task Manager > Services and start/restart the Mosquitto server from there. If you cannot find it here too, your installation of Mosquitto has not been successful.
A more detailed tutorial for setting up Mosquitto with Windows / is linked here.
The OP has not defined the scope of testing, however, simple (gross) 'smoke testing' an install should be performed before any time is invested with functionality testing.
How to test if application is installed ('smoke-test')
Log into the mosquitto server's command line and type:
mosquitto
If mosquitto is installed the machine will return:
mosquitto version 1.4.8 (build date Wed, date of installation) starting
Using default config.
Opening ipv4 listen socket on port 1883
If you wish to have an GUI based broker testing without installing any tool you can use Hive Mqtt web socket for testing your Mosquitto server
just visit http://www.hivemq.com/demos/websocket-client/ and enter server connection details.
If you got connected means your server is configured properly.
You can also test publish and subscribe of messages using this mqtt web socket

Resources