On the things stack community. 1
So on TTN, the MQTT broker is exposed and already available so we can make a demonstration to connect two clients: 2
One publisher and one subscriber. 3
The second architecture we explained where the broker is an independent entity between the LoRaWAN sever 4
and the IoT platform is not available on TTN, at least not at the time of this demonstration. 5
However, it's available if we install our own the things stack server. 6
That's what we'll do later. 7
But for the time being, we'll stick with the community network. 8
So we will only be able to explain the first architecture, the one with the broker exposed in TTN 9
itself. 10
Anyway, it's the simplest architecture, so we won't mind. For this demonstration, 11
my device is going to send a temperature each time I press the user button. 12
On the left, 13
I've got my end device logs, and on the right I've got my application and I can check that everything 14
works well. 15
Okay, so where do I need to connect my subscriber for the uplink and my publisher for the downlink? 16
I need to connect them to the MQTT broker embeded on TTN. 17
What is great is that this broker is already available and enabled so I don't have to do anything to 18
check its configuration. 19
I go in my application > integration > MQTT. 20
1. First, I need to know the server to which we want to connect to. 21
In this case it's TTN, the server address is this one, so I copy it to use it later. 22
2. Secondly, we said that we would need some credentials to connect to this broker. 23
There they are. 24
The username will be this one and the password must be generated. 25
It's an API key specific for the MQTT protocol. 26
Again, I copy it because I will use it later. 27
3. Third, I need a topic, so I will make it easy for the subscriber because I'm going to use the wild card 28
#. 29
What happens if I do that? 30
I will receive all event from all sensors of all applications. 31
Of course, if you want the data from a specific end-device of a specific application, then we need 32
the exact topic and we'll find this information in the documentation. 33
Let's have a look. 34
We go on the things stack website and we look for information about MQTT. 35
Great. 36
In this section, we have everything we need. 37
They explain the credential with the generation of the key. 38
We've done this before, so it's okay for us. 39
And in this paragraph, we see all topics concerning the upstream traffic. 40
Let's say we want the uplink messages. 41
Then we need to use this topic and of course, to replace the application item. 42
In our case, it's training-usmb. And to replace the device ID. 43
In our case, it's device1-abp. 44
And now for the publisher. 45
So to send downlink messages, the information is just below. 46
The topic is this one. 47
Again, we need to replace application ID and device ID with the right values. 48
The topic is just different at the end. 49
It's down/push. 50
... 51
Or 52
down/replace. 53
Depending if you want to erase the downlink queue or not. 54
So again, I copy these topics because I will use them later. 55
4. OK, we're almost done. 56
We need one last information for the publisher. 57
That's the format of the downlink message. 58
The payload of the message has to respect a specific JSON format. 59
Again, if we come back in the documentation, we can find an example of this JSON payload. 60
Basically, we'll assign an application port and of course a frame payload. 61
Just be careful. 62
It's a 64 base format. 63
I also copy this JSON object because I will also use it. 64
Great. 65
We're ready for our demonstration. 66
Let's start with Node-red. 67
... 68
And after I'll show you the same demonstration with MQTT Explorer. 69
We'll need all the information copied previously. 70
So I saved everything here. 71
I come back on my node-red user interface. 72
I will keep this tab with my public mosquitto test so I open a new tab. But the flow I'm going to use 73
is very similar. 74
So I copy all nodes and I pass them on my new flow. 75
> I changed the name of the tab. 76
This test is about an MQTT connection with TTN, so the name will be MQTT TTN. 77
> The broker is now TTN. 78
Again, this is just a common so don't worry about that. 79
But I think it's clearer if we keep in mind that it's TTN the broker here. 80
1. Okay, so now let's configure the subscriber. 81
So I click on the subscriber node and I'm going to add a new broker. 82
I fill in the sever name that I've copied before. 83
And in the security tab, I need to enter the credentials. 84
So username and password. 85
Again, these are all the information I've copied before. 86
Then I add the broker to the list available when the configuration is done. 87
Now I've got the new broker available in the list. 88
Great. 89
2. So now the topic. 90
I said that I would use the # wild card. 91
But if you want, you can specify one. 92
And there we go. 93
We are done with the subscriber. 94
So we can try that part and it should work. 95
I click on deploy. For this test, 96
I'm only interested in the subscriber part. 97
So don't look at the publisher because this is just the one I copied from the previous demonstration. 98
And I hit the push button of my own device. 99
Then I receive the message on my application. 100
This message has traveled all the way from my device, my gateways, the network server, the application 101
server, the TTN broker, and finally has been pushed on my subscriber here. 102
If you want the exact topic for your device, then you can find it here in the debug window. 103
We just need to copy that and change the topic of our subscriber with this one if you wish. 104
So the right part of this flow is working for the upstream traffic. 105
Now on the left I'm going to take care of the publisher part for the downlink traffic. 106
So I click on the publisher here. 107
I choose the broker I want to connect to, which is this one. 108
And for the topic I can paste the topic I took from the documentation. 109
Or I can also take it from here. 110
And if you remember well, I just have to change "up" by down/push. 111
All right. 112
We are done with the publisher configuration. 113
Now I need to select the content of the JSON object to send in the downlink message. 114
So the payload is not a string anymore, but it is an object. 115
And the content is what I picked up from the documentation. 116
So I just open the JSON editor here and I paste the content. 117
In this example, I'm going to send the command 1 on the application Port 15 to turn on the LED of 118
my end device. 119
The text edition is done, and I just changed the name of my node to make things clearer in my flow. 120
(LED on.) 121
Great. 122
So let's deploy and test this application. 123
Both publisher and subscriber are connected. 124
If there were any problem, I would have them reported here. 125
So now everything seems fine. 126
For the moment, nothing happens. 127
And then I press the downlink button on my window. 128
I've got a message from the LoRaWAN server telling me that the message has been queued. 129
It hasn't been sent yet because I remind you that this device is a Class A end device. 130
So I need to send an uplink message before sending a downlink. 131
And for that I need to press my end-device push button. On my application, 132
I receive a new message telling me that an uplink data has been received. 133
So let's go on the device. 134
And yes, indeed, I've received one and I can check that the LED is turned on. 135
If I now want to turn it off, then I just need to modify the downlink frame payload of my injection 136
node or I can have both on the same flow, one for the LED ON, and one for the LED OFF. 137
I copy it and I just modify the name. 138
(LED off.) 139
And the payload with AA== , which is 0 coded in base 64. 140
So we are done with node-red. 141
Now we can try to do the same application with MQTT Explorer. 142
Now that we know the broker address credential and payload to send, I guess it would be much quicker. 143
On my explorer, I create a new connection. 144
I call it TTN and I enter the broker address 145
and credentials. 146
These are exactly the same ones I use previously. 147
For the topic, 148
I leave it like this because if you remember, the default topic is #. 149
So that means that if we don't change this configuration, I will receive all messages sent to all topics. 150
I connect my client to the broker and I can send an uplink message from my device. 151
For that I just need to press the push button. 152
There we go. 153
The content is here and I can see the frame payload in base 64. 154
That was the uplink traffic. 155
So now how do we send a downlink message? 156
We go on the publisher part. 157
We assign the right topic. 158
Again, it's the same one, but I change "up" by "down/push" and I enter the JSON object message with the 159
right payload. 160
My LED is on, so I want to turn it off with AA== payload. 161
As soon as I publish it. 162
A new message is received from the broker telling us that the message is queued. 163
And when I press the push button, I receive a new uplink on my subscriber. 164
And obviously on the device side, I've received the command and the LED is now off. 165
That's that simple. 166
So try these demonstrations, and tell us if you manage to connect your LoRaWAN server with MQTT.