LoRa is a long-range wide-area network technology, like cellular networks, but designed for lower energy consumption. It enables devices to send small amounts of data over long distances running on a small battery. The LoRa signal is received by gateways to offer coverage to specific areas, and the range of the LoRa gateways is 8-10km (5-6 miles) in rural areas and 1km in towns where the density of buildings is greater.
So, the main characteristics of LoRa are:
- Long range (>5 km urban, >10 km suburban)
- Long battery life (>1 year)
- Low cost (<15 Eur/module)
- Secure
The list of characteristics, until LoRa is operating in license-free radio frequency, allows us to set up our gateway (or gateways), have a coverage of a few kilometers and run our own network for a few hundred euros or dollars.
Typical use cases can be found in the IoT (Internet of Things), like network of this project, composed of small sensors (devices) that are battery-operated and communicate information (in our case Humidity, Pressure and Temperature).
A typical LoRa network looks like as in the following picture.
For upstream messages, for example, our sensor that sends information to an application, the flow is from left to right. The sensor value gets transmitted over LoRa radio. Gateway receives the message and forwards it over another network (3G or WiFi) to a Network Server (TTN). The Network Server, through MQTT and Node-RED, routes the message to the end application (Grafana). In the next paragraph, I'll try to describe each component.
The architecture: Components and AppsDEVICE- WisBlock Kit
LoRa devices can be anything that sends or receives information, there is no real definition for them but usually we’re speaking about detectors, actuators or sensors as in our project.
- RAK5005-O WisBlock Base Board
RAK5005-O is a WisBlock Base board that connects all WisBlock modules together and provides the power supply for attached WisBlock modules.
- WisBlock Core LPWAN Module RAK4630
The RAK4630 module delivers a powerful Nordic nRF52840 MCU with BLE 5.0 paired to the latest LoRa® transceivers from Semtech, the SX126x series.
- WisBlock Environmental Sensor RAK1906
RAK1906 is a WisBlock Sensor that is based on the popular BOSCH BME680 environment sensor chip which monitors environmental air quality.
RAK1906 is not only measuring air temperature, humidity, and barometric pressure. It has as well an integrated gas sensor to acquire information about air quality.
RAK1906 combined with the RAK4630 WisBlock Core is ready to go system for air quality and condition system. Its use cases are personal air quality tracker, weather stations, or gas detection, The acquired data can be send using LoRaWan® communication, LoRa® point-to-point connections, and BLE.
- WisBlock RAK Box-B2
RAKBox-B2 is an IP67-rated enclosure for outdoor usage. The enclosure has a top-mounted solar panel for battery charging.
- Li-Po Battery
I use a lithium-polymer battery (LiPo), 200 to 1000 mAh, 3.7 v.
- Antenna
For the project, we need two types of Antenna, one for the device (2dBi gain) and one for the Gateway (3dBi gain fiberglass Antenna). EU Frequency: 868 MHz.
GATEWAY - RAK7243C WisGate
The RAK7243C WisGate Developer D3+ Gateway is a device that consists of Raspberry Pi 3B+, RAK2245 Pi HAT which includes a GPS module and a Heat Sink for better performance and thermal heat dissipation management, and a RAK2013 Cellular Pi HAT and its housing is built with an aluminum casing. Pilot Gateway Pro is ideal for prototyping, proof-of-concept demonstration, or for evaluation. It includes a ready-to-use gateway OS that can be connected to a LoRaWan® server or to TTN The Things Network.
The Setting up: LoRaWAN Network server and hardwareFirst time, we start from the WisGate Developer Gateway Firmware Setup. Flashing an Image to an SD card is an essential process of setting up any gateway device.
1. Plug the SD card into your PC or laptop.
2. Download the balenaEtcher software. There are options for Windows, macOS, and Linux. Clicking the Download button will provide you with the software directly, no installation is required.
3. Open balenaEtcher and click Flash from file. Here, you need to select the file that will be flashed on the SD card. Note that the software allows the user to Flash from URL and Clone drive as well, but in this tutorial, only the Flash from file function will be demonstrated.
4. By clicking on the Select target button, you will have the option to choose the SD card that you want to flash.
5. Click the Flash button to finalize the procedure. Wait until the flashing and validating processes are done. When both are completed, your SD card is flashed and ready to use.
Accessing Your Gateway
After burning the firmware image onto the SD Card, make sure you have inserted the SD Card into the RAK7243C WisGate Developer D3+ Gateway and have the LoRa, GPS, and LTE Antenna connected. By default, the gateway will work in Wi-Fi AP Mode which means that you can find an SSID named Rakwireless_XXXX on your PC Wi-Fi Network List.
Log in to the gateway
SSH (Secure Shell) is typically used to log in to a remote machine and execute commands. For this guide, you will be using Putty.
The IP Address is 192.168.230.1 It will then prompt you to enter the username and password. The default username and password are provided below:
- Username: pi
- Password: raspberry
Accessing the Internet
Assuming you have successfully logged into your gateway using SSH. Enter the following command in the command line:
sudo gateway-config
Then, you will see a page the same as the figure below.
Connect through Wi-Fi
If you want to connect through Wi-Fi, it can easily be done with the wireless capabilities of the Raspberry Pi 3B+ by choosing 5 Configure WIFI. By default, the RAK7243 Developer works in Wi-Fi AP Mode.
For the gateway to connect to the router, it must work in Wi-Fi Client Mode.
Once Wi-Fi AP Mode has been disabled by choosing 2 Enable Client Mode/Disable AP Mode, you can now connect to a new Wi-Fi Network by choosing 4 Add New SSID for Client.
Select your country of residence.
Type the password. If there is none, leave the field empty.
Lastly, reboot the gateway using the command shown below and put it in the command line.
sudo reboot
After reboot, the gateway will connect to the router successfully through Ethernet.
Configuring the Gateway
If you have successfully logged into your gateway using SSH, now you can get your Gateway ID, just enter the command below.
sudo gateway-version
A unique ID will be generated for the gateway. This is also called Gateway EUI and is essential for registering the gateway with any LoRa Network Server (TTN).
Now you can start with the Gateway configuration. Enter the following command:
sudo gateway-config
Then you will see this page:
Setup RAK Gateway Channel Plan
This menu allows you to select your LoRa frequency band and one of the two available Networks Server options by choosing 2 Setup RAK Gateway Channel Plan.
We can choose one of two supported LoRa servers here: TTN or ChirpStack. We choose TTN.
Select Channel-plan n. 5, EU_863_870 (Europe)
Visit LoRa Alliance Regional Parameters for more information on your local frequency plan. This will allow you to choose the correct plan.
When a channel plan is selected, the gateway is configured to connect to the nearest cluster to the region. Europe: eu1.cloud.thethings.network
Register your LoRa gateway to TTN LoRa server
Login to https://www.thethingsnetwork.org/, and open your console panel.
After the registration click on “GATEWAYS” and register the gateway.
Fill in the information on the line and remember to select the check box “I’m using the legacy packet forwarder” and type the Gateway EUI.
After the above operations, click “Create Gateway” in the bottom right corner to register your gateway. If register successfully, you will see the “Status” “Connected”. This means that your gateway has connected with TTN LoRa server successfully!
Add a LoRa node on TTN
Fill in the following items and click “Add application”.
You will see the following page, click on + Add end device
.
Fill in the information for the device and click on “Register end device”.
After clicking “Register”, you will see the following page. Please remember Device EUI, Application EUI, and “App Key”, because are important to configure LoRa node and to fill in the Arduino code.
Now we can connect our board and load the sketch. Firstly remember the previous information: Device EUI, Application EUI, App Key, and insert these in the sketch as you can see in the figure (use copy to clipboard on TTN and paste on the sketch).
Now load the sketch and check TTN!
After the above operations open your TTN console, go to applications, select the application and the end device created before.
If always goes right your message from the device screen in the Live data
dashboard should look something like in the following figure.
As we can see in the next picture, data are not human-readable and so we must convert these data. Payload formatters allow you to process data going to and from end devices. This is useful for converting binary payloads to human readable fields, or for doing any other kind of data conversion on uplinks and downlinks.
Go to the Payload formatters section and enter the following code in Javascript as in the figure.
function Decoder(bytes, port) {
var decoded = {};
if(bytes[0] == 1) {
decoded.TEMP = (bytes[1] << 8 | (bytes[2])) / 100;
decoded.HUM = (bytes[3] << 8 | (bytes[4])) / 100;
decoded.PRES = (bytes[8]| (bytes[7] << 8) | (bytes[6] << 16) | (bytes[5] << 24)) / 100;
decoded.GAS = (bytes[12]| (bytes[11] << 8) | (bytes[10] << 16) | (bytes[9] << 24));
} else if (bytes[0] == 7) { } else if (bytes[0] == xx) {
}
return decoded;
}
After the operations click on Save changes. Now the system converts data and we can read humidity, barometric pressure and air temperature.
MQTT stands for MQ Telemetry Transport, it is a nice lightweight publish and subscribe system where you can publish and receive messages as a client, and it makes really easy to establish communication between multiple devices. It is a simple massaging protocol designed for constrained devices and with low bandwidth so is the perfect solution for Internet Of Things applications.
In our project, we can use MQTT to read data from sensors and publish it through Node-RED in Grafana which is an open-source web application for visualizing data.
So, what we are going to do now we are going to download and install MQTT, go to a search engine and look for MQTT fx Download (my version is MQTTfx 1.7.1).
After the installation, we have to edit the connection profile necessary to connect TTN.
1. Open the applications page in TTN and click on integrations
- MQTT
. In order to connect to the MQTT server you need to create a new API key, which will function as connection password.
2. Click on generate new API key and copy to clipboard. (username and password are the credentials that we will use in MQTT in the next step).
3. Before opening MQTT check the API keys page.
4. Now open MQTT and select Extras - Edit Connection Profile to enter the previously obtained credentials.
5. Enter the connection information on MQTT as shown in the figure below and save thechange.
Now you can open Subscribe section, enter # (allows all incoming messages to be displayed), and click on connect and subscribe.
If the system works properly, messages are displayed on the right of the page.
InfluxDB is an open-source time series database (TSDB).
It is written in the Go programming language for storage and retrieval of time series data in fields such as operations monitoring, application metrics, Internet of Things sensor data, and real-time analytics.
This database is important because allows us to collect data from our sensors and at the same time, to become the data source for Grafana.
After the download, the folder will contain the files described in the next picture.
At this point we proceed as follows:
1. We run command influxd.exe.
2. We run command influx.exe and we create the new database for our application (in our case the name of database is “prova”).
The previous steps are summarized in the following photos.
It’s important to remember that every time you have a restart of the application is necessary to execute the command influx.exe from the prompt.
We can now proceed with the connection of all network components via Node-RED.
The message processing: Node-REDNode-RED is a programming tool for wiring together hardware devices, APIs and online services (in our case TTN and Grafana).
It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single click.
In our project, we use Node-RED to process data from MQTT and publish it through InfluxDB in Grafana, which is our web monitoring platform.
1. Install Node.js. Download the latest 14.x LTS version of Node.js from the official Node.js home page. It will offer you the best version for your system.
2. Install Node-RED. Installing Node-RED as a global module adds the command node-RED to your system path. Execute the following at the command prompt:
npm install -g --unsafe-perm node-red
Once installed, you are ready to run Node-RED.
4. Access the editor
With Node-RED running, open the editor in a web browser.
If you are using a browser on the same computer that is running Node-RED, you can access it with the URL: http://localhost:1880.
If you are using a browser on another computer, you will need to use the IP address of the computer running Node-RED: HTTP://<ip-address>:1880.
The figure below shows the flow that connects TTN to Grafana platform through MQTT and InfluxDB.
The flow consists of three nodes: MQTT node, Function node, and InfluxDB Output node.
5. Add nodes to the palette
Node-RED comes with a core set of useful nodes, but there are many more available from both the Node-RED project as well as the wider community.
You can search for available nodes in the Node-RED library.
Using the editor, you can install nodes (MQTT and InfluxDB nodes) directly within the editor by selecting the Manage Palette option from the main menu to open the Palette Manager.
The ‘Nodes’ tab lists all of the modules you have installed. It shows which you are using and whether updates are available for any of them.
The ‘Install’ tab lets you search the catalog of available node modules and install them.
6. Add an MQTT node
The node allows you to inject messages into a flow, either by clicking the button on the node or setting a time interval between injects.
Drag MQTT node onto the workspace from the palette.
Select the newly added node to set information and properties in the sidebar panel.
- Server: TTN Test application (server created before in MQTT)
- Topic: #
- Username: name of application created in TTN (used also in MQTT before)
- Password: API Key generated before in TTN (used also in MQTT before)
7. Add a Function node
The Function node allows you to pass each message through a JavaScript function.
Double-click on the Function node to bring up the edit dialog. Copy the following code into the function field:
payload.uplink_message.decoded_payload
Now we can complete Node-RED flow with the settings of InfluxDB Output node.
8. Add an InfluxDB Output node
The InfluxDB Output node allows you to inject data into our time series DataBase.
Drag InfluxDB node onto the workspace from the palette.
Enter the following properties on the settings panel.
In our project we insert:
- Server: 127.0.0.1:8086/prova (create before in InfluxDB)
- Measurement: "TTN_App" (needed later in Grafana)
9. Add Debug nodes
The Debug node causes any message to be displayed in the Debug sidebar. By default, it just displays the payload of the message, but it is possible to display the entire message object.
10. Wire them all together
Now we are almost at the end, add wires connecting:
- The MQTT node to the function node
- The function node to the InfluxDB node
- MQTT and function nodes to the Debug nodes.
11. Deploy
At this point, the nodes only exist in the editor and must be deployed to the server.
Click the Deploy button!
The monitoring platform: GrafanaGrafana is a multi-platform open-source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources (InfluxDB in our example).
As previously mentioned, we use Grafana to create our monitoring dashboards where to display our data.
Firstly, download and install Grafana (our version is 7.4.2).
1. Now we have to connect Grafana to our "prova" database. Click on Data Sources.
2. In the configuration section Add InfluxDB data source plug-in
3. After the installation select InfluxDB plug-in and insert the following information:
- url: http://localhost:8086
- Access: Server (default)
- Database: prova
- HTTP method: POST
Now click on Save & Test button, if all is right the system will show the message in the following picture.
Now we can create our dashboard where visualize data from sensors!
1. Click on create on the left of the screen.
2. Select add panel on the top right.
After the steps above we have to connect our dashboard to the right Server (InfluxDB) and the right measurement introduced first in the influxdb node on Node-RED ("TTN_App").
Finally we choose the type of data to display and we create panel for Humidity, Pressure and Temperature.
In every panel you can choose a lot of setting for graph style, legend, measurement, try what you want and save changes!
The final dashboard looks like in the next picture.
Data from sensors are visualized in Grafana!
Alert notifications on TelegramGrafana is a highly potent instrument and allows us to create an Alert system with e-mail, Teams, Discord, and Telegram. In our case, I use Telegram to send alert notifications. This means that you can create differents alerts in your data in Grafana and receive notifications in Telegram.
Now let's see the steps to create an alert system.
1. Go to the Alert section, select Notification channels, and fill in the information in the next picture.
2. To proceed we need Bot API token and Chat ID, so log into Telegram and search the account BotFather, with this Botfather you'll be able to create your own Bot needed to send messages and notifications.
Enter in BotFather's chat and type /start, once you do that BotFather gives you a list of available options. Now we need to create a new Bot, so we click on /newbot. After this, the system asks you for the name of your new bot as in the next figure.
3. Select token to access the HTTP API display on BotFather's chat and paste on Grafana. After doing this create a new channel (channel and not Group) in telegram and insert Contest_Alert_bot as a member of the channel.
4. Now we need the last information: Chat ID. To provide Chat ID type a message in the new channel (e.g. hi @Contest_Alert), go to https://api.telegram.org/bot<token>/getUpdates, paste the right token in the address and press enter. The figure below displays the result.
5. Insert the last information in Grafana, save the change and send a Test. If everything works correctly you will see a message as in the picture
6. Finally we can create Alert rule on Grafana!
In every panel, we have the possibility to create an Alert by selecting the appropriate tab. After clicking Create Alert you can choose the variables to set. Is important to remember the channel to send notifications to (In our project Grafana_Telegram_Alert). Done this Apply the settings and wait for the notifications on Telegram!
Here is an example of Alerts:
Temperature (rule: "IS ABOVE" 25 °C)
Humidity (rule: "IS ABOVE" 45 %H)
Pressure (rule: "IS ABOVE" 1010 hPa)
1 - Install the device in a location and switch it on;
2 - Switch on Gateway;
3 - Control the communication on TTN website;
4 - Open and connect MQTT;
5 - Start InfluxDB and Node-RED from the command prompt;
6 - Open Grafana and visualize Data;
7 - Control Telegram for Alerts notifications.
Join the project!
Comments