For this demonstration of the MQTT protocol. 1
We need a broker and at least one publisher and one subscriber. For the broker, 2
there are many choices: 3
First, we could set up our own. 4
That's possible. 5
But I won't be the choice for this demonstration because we want to keep things very simple, 6
so we'll use a public broker. 7
A public broker is a broker available in an Internet to test the MQTT protocol. 8
They are maintained by companies or foundation and are sometimes open source. 9
The most famous is probably mosquitto, but there are also others like HiveMq that we might also use 10
later. 11
Right. 12
So for the broker, that will be very easy. 13
Now, we also need to set up an MQTT client for the subscriber and the publisher. 14
There are also many clients available. 15
Some clients can be directly controlled with a Command Line Interface in the terminal. 16
Another option is to set up an MQTT using node-red. 17
... 18
And finally, some other clients have a standalone user interface. 19
I don't really have a favorite choice among all these methods, so I will show you two possibilities. 20
The first one is using node-red, 21
because, we'll use Node-red anyway later in this course. 22
And the second one is the MQTT Explorer Client that we find very useful in some situation and often 23
a bit quicker to set up than node-red. 24
... 25
But whatever MQTT client we're using, the configuration is always the same. 26
We need to specify: - whichever we want to connect to, - the login and password for the connection, 27
- the topic we want to target: if we are a subscriber, the topic refers to the topic we want to read and 28
if we are a publisher, then it refers to the topic we want to write. 29
- And one last thing, only for the publisher. 30
It needs to provide a message to send to the topic. 31
So let's try that first with the node-red MQTT client. 32
Then after I will also make a demonstration with the MQTT Explorer client. 33
In both cases, we're going to use the public broker called Mosquitto. 34
Right. 35
Let's start with node-red. 36
I'm not going to explain how to install it. 37
We've done it before, 38
and I just remind you that you can find all the useful explanation at the beginning of this course in 39
the HOW TO section. 40
For this demonstration, I assume that you're already logged on the node-red user interface. 41
Great. 42
We need to find 2 nodes, one for the publisher and one for the subscriber. 43
We can look for the MQTT node in the filter field here. 44
"MQTT out" is our publisher and "MQTT in" is our subscriber. 45
So I place them in the center and I will just change their names because right now it's a little bit 46
confusing. 47
And we need to imagine here, that there is the Mosquitto broker in the middle. 48
That is just a comment. 49
The node 50
configuration is pretty easy. 51
As we said earlier, we need to configure the publisher and subscriber with the broker address, the 52
credential for the connection, and the topic. 53
Great. 54
Let's start with the publisher. In the server section, 55
I need to add a new MQTT broker. 56
I give it a name. 57
And I specify the address which is "test.mosquitto.org" on port 1883. 58
For this public broker there is no credentials, so we don't have to fill any of these fields. 59
So now the new mosquito broker is configured and we just have to specify the topic here. 60
I will use the topic LoRaWAN. 61
Okay. 62
This publisher is ready. 63
The only thing missing is to feed this node with the message we would like to send. 64
For that we take a new node called "Inject Node". 65
We could leave it like this. By default, 66
it will send a timestamp, so the actual time. 67
But in my case I will change it to send a string instead. 68
Let's say "hello". 69
Okay, We are done with the publisher part. 70
Now, for the subscriber, I also need to configure the broker. 71
We simply reuse the Mosquitto broker that we registered previously and I specify the topic. 72
In my case, it's LoRaWAN. 73
Great. 74
The subscriber is ready as well. 75
And the last thing is to direct the output of the subscriber to the debug node. 76
Thanks to the debug node, anything coming out of this node will be written in the debug message window 77
and right when the flow will be deployed. 78
So I deploy my flow. 79
I can see that both my publisher and subscriber are connected to the broker. 80
That's a good news. 81
I click on the debug window here and I can try to publish a message. 82
Yes, I can see here that as soon as I've published the message on the LoRaWAN topic, then it's sent 83
to all subscribers. 84
So this one, in that case, great. 85
We are done with this MQTT demonstration with node-red. 86
This is saved already, so I don't need to do anything else. 87
But I'm going to change the tab name in case we need it later. 88
I call it MQTT Mosquitto. 89
Okay, so that's the node-red client demonstration. 90
I'm also going to show you how it works a more user friendly MQTT application. 91
There are a lot available so we could use any of them. 92
But MQTT Explorer is free, open source, and supports many platforms. 93
We can download the installation file from their website and it's very easy to set up. 94
Once we've launched the User Interface, we end up on the connection tab. 95
This is where we enter the broker address and the credential. 96
There is already two brokers registers and the second one is the test mosquitto broker. 97
Everything is already configured properly. 98
If not, then we just add a new connection at the top and fill in this field with the same information. 99
To define the topic we want to subscribe to, 100
we need to go on the advanced section. 101
There we can see that the default configuration subscribes to all topic of the mosquitto public broker. 102
While it would be a good idea when we only use our own broker, using the # wild card on a public broker 103
is complicated because we receive thousands of messages on our clients. Actually we'll receive all mixed 104
messages sent by all publishers around the world. 105
That's huge. 106
Let's try anyway. 107
I go back on the connections section and I connect the client to the broker. 108
Great. 109
I can see all topics and messages used. 110
Now I will disconnect and go back on the advanced section to subscribe only on a topic called LoRaWAN. 111
First, I delete the # topic and I can also delete the $SYS topic. 112
SYS is a specific topic to subscribe to some information on the broker itself, like the number 113
of client connected, the number of topic available and so on. 114
You can leave it if you want, but for this demonstration we don't need it, so I will delete it. 115
Now I can add the LoRaWAN topic. 116
Go back and save my configuration. 117
On the left there is my subscribe section. 118
So if anything arrives on the topic LoRaWAN, it should be here. 119
And on the right, I've got the publisher section. 120
I enter a value in the topic LoRaWAN. 121
Let's say hello. 122
And there we go. 123
We receive it on the subscriber. 124
The nice thing with MQTT Explorer is that we can even plot these values if we send numbers. 125
For example, if I send several data is like 2, then 5, 126
and 4. 127
I can click on the history and have a quick overview of the previous data received on this topic. 128
We are done with the basic functionalities of both MQTT clients. 129
Now, what we would like to do is exactly the same demonstration, but this time we're going to connect 130
the client to our LoRaWAN server. 131
Thanks to that, we'll get the uplink messages of our devices and we'll also be able to send downlink 132
messages.