This tutorial aims to show how i was able to integrate my Node-red workflows to be triggered by uplink and downlink messages from the RAK811 lora node. The gateway i used here is the RAK831 LoRA 868 MHz gateway. The backhaul is Wifi and is being handled by our venerable Raspberry Pi 3.
By the end of this turtorial you will know the following:
- 1) A brief outlook on the inner working of the TTN network
- 2) How to setup your rak 831 gateway and make your rak811 to connect to it.
- 3) How to integrate the TTN MQTT APIs to subscribe for messages whenever your RAK811 is sending or receiving uplink/downlink messages.
Lets Dive DEEPERRRR....!!!!
TTN Web Service:According to the TTN web services site:
The Things Network is building a network for the Internet of Things by creating abundant data connectivity, so applications and businesses can flourish. The technology we use is called LoRaWAN and it allows for things to talk to the internet without 3G or WiFi. So no WiFi codes and no mobile subscriptions. It features low battery usage, long range and low bandwidth. Perfect for the internet of things.
It is a network that spans across the globe in a number of countries and provides the means to create Gateway, provision them into the network, allow LoRA nodes to talk to the applications via the gateways. There is a whole bunch of APIs that we will be seeing in this tutorial. The most important one being the MQTT APIs. We will come to that shortly.
RAK Wireless is becoming the pioneer in provising makers and developers with cutting edge tools, dev boards in order to enable them to work on the latest wireless technology stacks available in the market. Along with their awesome enginneering team and a stratergic location (Shenzen, China), They are proving to be a formidable name in the global IoT space
Two of their hadrware revolves around the LoRaWAN protocol. The RAK831 Gateway and the RAK811 node:
Together with the TTN gateway APIs and the TTN application and network server interfaces, RAK products can be used n a variety of applications and under various environmental requirements for fulfilling long range communication.
Setup your RAK 831 gateway:In order to keep this tutorial considerably short, Ill request you to proceed to the tutorial below to setup your RAK831 gateway:
https://www.hackster.io/naresh-krish/getting-started-with-the-rak-831-lora-gateway-and-rpi3-e3351d
Setup your RAK 811 and test communication with TTN:To enable the RAK811 to talk to the RAK831 gateway follow the tutorial below:
https://www.hackster.io/naresh-krish/getting-started-with-the-rak811-lora-node-67f157
Note: It is VERY IMPORTANT that you complete the two tutorials above before proceeding further and get your data coming up on the TTN device console. The tutorials cover a lot of the setup in depth and provide you with notes of where one can go wrong while configuring the nodes.A look at the TTN MQTT APIS and its use in our project:
So you maybe wondering how do we get messages when up link and downlink messages are sent from a node, or when a node enters a particular state. Well its pretty straightforward.
1) You register a gateway in particular location by specifying the router to use with the gateways configuration. This also binds any App that gets information from the router to the region. Each region in turn has something called a MQTT broker that one can listen to for messages. Each time a action is triggered in the TTN backend (new message, device activation etc), a subsequent messageis sent to this Broker.
Now ncomes the node-red TTN nodes into the picture. These nodes are nothing but MQTT clients connecting to a particular topic being published from the TTN backend via a MQTT broker. You see that the nodes would require the following information
- 1) Geolocation of the handler (this is in line with the fact that each location has its own broker endpoints and message subscription)
- 2) Topic to listen to: This is in line with the fact that you need to listen for things like, new device activation, messages sent from nodes etc
Lets see some of the MQTT APIs exposed via the message topics
Uplink MessagesTopic: <AppID>/devices/<DevID>/up
{
"app_id": "my-app-id",
Uplink FieldsEach uplink field will be published to its own topic my-app-id/devices/my-dev-id/up/<field>
The payload will be a string with the value in a JSON-style encoding.
{
"water":
Downlink MessagesTopic: <AppID>/devices/<DevID>/down
{
"port": 1,
Downlink FieldsInstead of payload_raw
you can also use payload_fields
with an object of fields. This requires the application to be configured with an Encoder Payload Function which encodes the fields into a Buffer.
{
"port": 1,
Downlink SchedulingBy default, the downlink will replace the currently scheduled downlink, if any. It is also possible to schedule the downlink as the first or last item in a the downlink queue.
{
"port": 1,
"confirmed":
Device ActivationsTopic: <AppID>/devices/<DevID>/events/activations
{
"app_eui": "0102030405060708",
Device EventsManagement Events
- Created:
<AppID>/devices/<DevID>/events/create
Updated:<AppID>/devices/<DevID>/events/update
Deleted:<AppID>/devices/<DevID>/events/delete
Downlink Events
- Downlink Scheduled:
<AppID>/devices/<DevID>/events/down/scheduled
payload: null
- Downlink Sent:
<AppID>/devices/<DevID>/events/down/sent
{
"payload": "Base64 encoded LoRaWAN packet",
"gateway_id": "some-gateway",
"config": {
"modulation": "LORA",
"data_rate": "SF7BW125",
"counter": 123,
"frequency": 868300000,
"power": 14
}
}
Error EventsThe payload of error events is a JSON object with the error’s description.
- Uplink Errors:
<AppID>/devices/<DevID>/events/up/errors
Downlink Errors:<AppID>/devices/<DevID>/events/down/errors
Activation Errors:<AppID>/devices/<DevID>/events/activations/errors
Example: {"error":"Activation DevNonce not valid: already used"}
Now that you have a hang of what is goin on in the background, lets fire up our node installation.
Node red installation:Take out your raspberry pi 3, and follow the instructions below:
If you have the minimal version of Stretch, or other Debian based install, such as Ubuntu, that doesn’t have Node-RED already installed, you can install or upgrade using the Node-RED upgrade script command
bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
The script will perform the following steps
- check if you are connected to the internet
- save a list of any globally installed node-red- nodes found in /usr/lib/node_modules
- apt-get remove nodered
- remove any node-red binaries from /usr/bin and /usr/local/bin
- remove any node-red modules from /usr/lib/node_modules and /usr/local/lib/node_modules
- detect if nodejs was installed from nodejs package or Debian
- if not v6 or newer - remove as appropriate and install latest v6 LTS (not using apt)
- clean out npm cache and .node-gyp cache to remove any previous versions of code
- install Node-RED latest version
- re-install under the user account any nodes that had previously been installed globally
- re-install the extra Pi nodes if required
- rebuild all nodes - to recompile any binaries to match latest nodejs version
- add node-red-start, stop and log commands to /usr/bin
- add menu shortcut and icon
- add systemd script and set user
- if on a Pi add a CPU temperature -> IoT example
- update the local update script
Manually installed nodejs and node-red
We can try and install node-red manually by the following steps:Install nodejs:
wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
Install node-redsudo npm install -g --unsafe-perm node-red
Note: For user in China, i have got help from RAK engineers on how to solve the issue where npm node-red TTN palette doesn't install properly.When installing the ttn palette node, you may face an error where it says the process-nextick-args is not installed properly or any other dependency error. In such cases try installing the TTN node manually by the following shell commands:
cd $HOME/.node-red
npm --registry https://registry.npm.taobao.org install node-red-contrib-ttnRunning Node-RED
To start Node-RED, you can either:
- on the Desktop, select
Menu -> Programming -> Node-RED
- or run
node-red-start
in a new terminal window.
Note: closing the window (or ctrl-c) does not stop Node-RED running. It will continue running in the background.
To stop Node-RED, run the command node-red-stop
To see the log, run the command node-red-log
If you want Node-RED to run when the Pi boots up you can use
sudo systemctl enable nodered.service
Once the setup is done node-red will be running on port 1880. HURAYYYYY !!!!
Node-red workflows and palettes:Once installed we would require the Node-red TTN pallete installed:
Setup Your workflow:- Install the TTN pallete. It’s the second one in the list below (node-red contrib-ttn):
- Once installed, this will show up as two nodes in the left pane like so:
- Select the TTN device node. This node listens for specific TTN MQTT messages raised when a device status/operation enters a particular state some examples are “activation”, “up” etc
- Also drag a debug node into the workflow and connect the output of the TTN device node to the debug node input
- Now double click the TTN node1 node, You will be shown this menu:
- Give a name to this node.
- Now from the TTN console chose your app id and note it down;
- Click on the pencil icon near the App, you will be shows a UI like this:
- This UI you can fill the app id with the Application id from TTN console
- Set the broker to asia. (it can be eu or us as well, check your Handler test in the previous message to know what is your app region)
- In the same app conole page scroll down till you reach the Access key section, copy the acces key and paste it in the Access key test box. And click Update.
- Now you will return to the original config menu. The device ID is the device name you gave to the rak811 node. Put it in the device ID section
- Finally register for an event. An event would usually be either activation or an uplink message. For now you can test with activation by putting the word “activation” inside your event section
- Next click done
- Now you need to complete the previous menu:
- Now connect your rak811 to your computer and try to connect it to the rak831 gateway either by OTAA or ABP.
- Once connected, send a message to the TTN app. You can test with a simple byte like 0 or 1.
- When your message reaches the TTN app, the TTN MQTT service will send a message to the Asia broker which in turn will send a message to this node as it is registered to that broker
Usually when you send message from a LoRA object you will only send some limited bytes. TTN payload decoder service can help you decode this information and store it in a meaningful way:
On how to use the payload functionality, Check out this excellent example provided by TTN:
https://www.thethingsnetwork.org/docs/devices/uno/quick-start.html#monitor--decode-messages
Troubleshooting:1) Make sure both the RAK831 and RAK811 show up in the TTN console under gateways and devices respectively.
2) Make sure RAK831 gateway shows up as online in the TTN gateways console
3) Make sure that the region you provide in theTTN node config is the same as the Handler region of the device (asia, or eu, or us etc)
4) Configure the rak811 node to connect to the same app key as you input in the node-red TTN node config.
Comments