LoRaWAN and MQTT Integration for IoT Application Design
Written by Kudzai Manditereza
Published: February 4, 2022
Are you an IoT developer, or a Solutions Architect trying to find a solution to enhance the capabilities of your LoRa and LoRaWAN enabled IoT applications? You are in the right place. In this blog post, I discuss a solution to integrate LoRaWAN and the MQTT protocol for IoT applications design.
Ever since the rise of the Internet of Things (IoT), connected devices have so far mostly ridden on the back of mobile communications networks such as Cellular, Wi-Fi, and Bluetooth. However, these networks were designed for human consumption of the internet e.g. web browsing, and video streaming, which requires large amounts of bandwidth.
Meanwhile, most IoT sensors rarely need large amounts of bandwidth as they often only need to send small packets of data. To put this into perspective, it is estimated that 75% of IoT connectivity will fall into the category of small data rates. What’s more, most IoT sensors in remote locations barely have enough power at their disposal to sustain Wi-Fi connectivity for long periods of time.
It is for this reason that Low Power Wide Area Networks (LPWAN) have continued to rise in popularity. An LPWAN is a wireless wide area network technology for interconnecting low-bandwidth battery-powered devices with low bit rates over long ranges.
Examples of IoT applications where LPWANs can be used include smart agriculture, smart cities, smart metering, waste management, logistics, and other Industrial IoT domains such as manufacturing. In general, IoT applications where the sensing units are spread apart over a wide geographical area (up to 20km) such that it is not possible to rely on a permanent cloud connection as internet access through 4G, 5G, or WiFi.
At the forefront of the LPWAN revolution is LoRa, a low-frequency modulation technology, in the form of a chip, that can be embedded into IoT sensors to enable them to achieve long-range communication between devices together with low battery consumption. Combined with the LoRaWAN protocol, remote battery-powered IoT devices can be connected to the internet while enhancing battery life.
We’ll discuss more on LoRaWAN in a bit, for now, I want to highlight the fact that LoRaWAN harbors some characteristics that make it not suitable for IoT applications such as Home and Industrial Automation. For example, in LoraWan, you cannot transmit information more often than every few minutes at the most, and downlink messages are discouraged as LoRaWAN platforms such as The Things Network have to think about the load on gateways and backend infrastructure.
It is for this reason that in this article, I present to you an architectural approach that allows IoT developers and Solution Architects to enhance the capabilities of LoRa enabled IoT applications using MQTT. The MQTT protocol is a publish/subscribe message protocol that makes it efficient to integrate IoT applications with advanced cloud services and Internet-connected applications. In particular, we are going to discuss how the HiveMQ MQTT broker is best suited for this kind of integration.
Introducing LoRa and LoRaWAN
Now, to set the foundation for our discussion, let us briefly understand how LoRa and LoRaWAN works.
In industrial use cases, where you find remote installations or production facilities that are spread out, wireless sensor networks face a problem of power and range. Simply stated, if you want to transmit information wirelessly over long distances, you have to either increase signal power or decrease signal bandwidth. This motivated the introduction of a class of wireless-sensor-networks that allow sensors to regularly transmit data, have their battery life prolonged for as long as possible, and still maintain a network connection wherever they are needed. LoRaWAN belongs to that class of networks, and it essentially is a standard protocol built on top of LoRa.
When a LoRa enabled sensor/device wants to send data via the internet, it will broadcast the data packet in all directions using the LoRa technology. This packet will be received by one or more devices with antennas capable of receiving LoRa packets, called LoRaWAN Gateways or Base Stations.
LoRaWAN Gateways provide internet access to the LoRa enabled sensors, called End Devices, by demodulating the LoRa packets and forwarding them to what is called a Network Server via 4G, 5G, and WiFi e.t.c
As can be seen from the diagram above, each gateway is connected to the Network Server. And in addition to performing network management functions such as eliminating duplicate messages and Over-The-Air-Activation of End Devices, the Network Server, like a router of the LoRaWAN protocol, performs some checks on the message sent from a Gateway and then determines the target Application Server to forward the message to.
On the other hand, the Application Server receives the messages from the Network Server, decrypts them, processes the data, and prepares it for consumption using application protocols such as MQTT. The application server also manages and forwards messages that need to be sent back down to End Devices through the Network Server.
LoRaWAN and MQTT Integration
Now that we know the different system components that take part in a LoRa enabled IoT application, and how they interact with each other, let’s look at the possible opportunities for integrating MQTT.
In case you are not familiar with MQTT, it is basically a lightweight open IoT messaging transport protocol that is based on a publish/subscribe pattern of communication. Meaning that, instead of communicating with a server, client devices and applications publish and subscribe to topics handled by a centralized MQTT broker. Although MQTT can also use other bidirectional transports to move messages around, it typically uses Internet Protocol (IP).
Now, before discussing LoRaWAN and MQTT integration points, the question is, why would you want to bring MQTT into a LoRa Based IoT architecture? Well, as it turns out, there is complexity with gathering data from IoT devices on a LoRaWAN network as LoRa packets have to traverse multiple technology stacks and infrastructures, from LoRa sensors to Gateways, Network Servers, Application Servers, and finally third party systems. MQTT can simplify this process since it uses the broker as the single centralized middleware solution connecting your IoT end devices to every other system. What’s more, MQTT has better routing control, improved visibility, and advanced security among other things. In other words, the adoption of MQTT and its publish/subscribe paradigm in LoRaWAN sensor networks makes it possible to simplify the integration of data from End Devices with other distributed applications.
And from an MQTT broker, you can have your cloud applications subscribe to the messages from LoRaWAN End Devices, which fits well with the preferred micro-services architecture for building your IoT application backend functionality. You can read more about Microservices Architecture for IoT in a separate article on this series.
In a nutshell, MQTT exhibits remarkable efficiency and appropriacy in supporting LoRaWAN wireless sensor networks.
MQTT Integration with LoRaWan Gateway
The first opportunity of MQTT Integration with LoRaWAN is the ability to capture wireless sensors traffic from a LoRaWAN Gateway and forward it to a standalone MQTT broker as MQTT messages.
HiveMQ provides an enterprise-grade MQTT Broker that could then be used to manage and forward MQTT messages from your LoRa enabled sensors to third-party enterprise systems and IoT platforms.
Based on this approach, your End Devices use LoRa technology to communicate with a local LoRaWAN Gateway, and most LoRa systems will have an option of translating and forwarding the received messages to your provided MQTT Broker, for example, the Dragino LoRa gateway.
Either way, your LoRaWAN Gateway acts as an MQTT Client and when using general computing hardware such as the Raspberry Pi as your LoRa gateway, you may use custom scripts to translate LoRa messages to MQTT and forward them to a broker hosted on-board the device or in the cloud. A HiveMQ MQTT Broker can be deployed for both scenarios, and if you’ve got your LoRaWAN Gateway connected to your corporate network it makes sense to deploy the HiveMQ MQTT broker on a computing platform that is on the same network, otherwise a cloud-based and fully managed version of HiveMQ MQTT broker would be the best solution.
MQTT Integration with LoRaWAN Backend Server
Combined, the LoRaWAN Network and Application servers form what is called a LoRaWAN Backend Server. And unless you are running your own private LoRaWAN backend, most LoRa-based IoT applications would typically use a cloud-based LoRaWAN Backend Server from providers such as The Things Network.
Now here’s the thing, LoRa systems primarily use UDP to send packets from LoRa enabled sensors to a LoRaWAN backend server. However, they also do use the MQTT protocol for sending the packets, and this presents another opportunity to integrate a standalone enterprise-grade MQTT broker, such as the HiveMQ broker, by creating a bridge between the LoRaWAN Server MQTT broker and your HiveMQ Broker.
Simply put, creating an MQTT bridge basically means that you connect two MQTT brokers so messages to one broker are forwarded to the other one and/or vice-versa. Consequently, all uplink messages from your LoRa sensors are forwarded to your HiveMQ MQTT broker, whilst downlink messages that you publish from your cloud-based IoT applications to the HiveMQ MQTT broker are forwarded to your LoRaWAN Server MQTT broker, where they are eventually pushed down to LoRa End Devices.
This works in scenarios where you have an existing IoT system consisting of an MQTT network of IoT devices and Enterprise Apps and you’d like to bring in information from devices behind a LoRaWAN network. So instead of having your apps subscribe to two brokers, the LoRaWAN MQTT broker and your existing MQTT broker, you let HiveMQ broker pull in that information from the LoRaWAN broker and distribute it to nodes in your IoT system MQTT network.
Now, because the bridge goes both ways, this means that uplink sensor measurements from your LoRaWAN End devices are forwarded to your HiveMQ MQTT broker, and downlink messages that you publish to your HiveMQ MQTT broker are forwarded to the LoRaWAN backend server and then to your LoRa based sensors.
In conclusion, the integration of LoRaWAN with MQTT in IoT application design brings simplicity and visibility to your solution. And above anything else, it enables your IoT system to provide the crucial interoperability needed to communicate with the many third-party Cloud and Edge-based IoT components that talk MQTT.