Check out the highlights from Developer Week

IoT Gateway

Chapter 5 of IoT Infrastructure

IoT Gateway

Imagine you’re a tech-savvy farmer with several acres of land. Your smart tractors perform different operations as you sit comfortably at your home several miles away and monitor all the action in the field via a cloud portal.

But wait, how are the tractors sending you all the data? WiFi connectivity is impractical, and cellular connectivity is often poor in rural areas. With a protocol like LoRa, the tractors can send data to a central hub. The hub, in turn, relays data to the cloud. That hub is one example of an IoT gateway.

An IoT gateway is an intelligent hub (either software or hardware) that facilitates IoT device-to-device and device-to-cloud communication. In 2020, a study estimated the global IoT gateway market at USD 1.16 billion, and it is expected to double in size over the next 6 years. Clearly, IoT gateways are an essential component of the IoT ecosystem. 

While facilitating IoT communication is their primary function, there’s a lot IoT gateways can do. In this article, to help you better understand what IoT gateways are and how they work, we’ll take a closer look at the functions of an IoT gateway, the technologies they use, and walk through a practical example of using Python to create an IoT gateway.

Executive summary

Here’s a summary of IoT gateway functions.

IoT Gateway FunctionDescription
Facilitating CommunicationHelps IoT devices communicate with the cloud and with each other
Translating CommunicationFacilitates communication between nodes that follow different communication protocols, encryption technologies, and packet structure
Filtering CommunicationFilters out metadata and keeps communication light-weight and simple
Securing CommunicationAllows encryption of all exchanges with the cloud, can also integrate firewall to further enhance security
Reducing CostsA by-product of all of the above. Reduces cost by reducing the number of connections with the cloud, and keeping the communication lightweight.

In the sections below, we’ll take a closer look at each of these IoT gateway functions.

Functions of an IoT Gateway

IoT diagram

IoT gateway in Action (Source)

Facilitating communication

The primary role of an IoT gateway is facilitating device-to-device and device-to-cloud communication. We saw one example of how this works with the smart tractor example.

Another example is a smart home system. Many smart home appliances, cannot directly support WiFi. Instead, they may communicate to a WiFi-connected hub to receive remote commands.

When it comes to device-to-cloud communication, most gateways support bi-directional data exchange. For example, in the smart home use case, an IoT gateway receives commands from the cloud and passes them on to target appliances. It also gathers status and health information from the smart appliances and passes it on to the cloud.

Some applications, including many Industrial IoT (IIoT) use cases, require IoT devices to talk to each other instead of — or in addition to — the cloud. An IoT gateway can facilitate this device-to-device communication too. IoT gateways can support one-to-one, one-to-many and many-to-one communications.

Translating communication

IoT devices in a system may not follow use the same communication protocols. Some may use Zigbee, some LoRa, and others BLE. Even if they use the same communication protocol, there may be differences in packet structure or the encryption methodology. Incompatibilities like this often occur in heterogeneous environments with IoT devices from different vendors.

An IoT gateway can address this challenge. In these scenarios, an IoT gateway plays the role of a translator. That means the gateway receives data from the sender in one format and converts it to a format compatible with the receiver. Here the sender and receiver can be the cloud or one of the IoT devices connected to the gateway.

For example, consider a smart door lock that provides access using RFID or a fingerprint scanner. Often, these smart locks only support BLE, and for security reasons, communication must be encrypted (e.g., with AES128).

Thus, to enable a cloud portal, an IoT gateway may receive a remote unlock command from the cloud over WiFi, encrypted over HTTPS. It will then decrypt the message, encrypt it again using AES128 (using the key shared between the gateway and the lock), and send it to the lock over BLE.

Filtering communication

Let’s continue with the door lock example from the previous section. In addition to the unlock command, the cloud may also send metadata like a timestamp, user information, and details about the door.

As you would guess, all this metadata makes packets bulky. An IoT gateway can perform some checks on this metadata (like the timestamp validity), discard the metadata, and just send the unlock command to the intended lock, thereby keeping the communication lightweight.

Data filtering is essential in maintaining network performance and reducing congestion. It is particularly beneficial for high-frequency IoT communication.

For another example, consider our earlier smart tractor system. If all the tractors send location coordinates every two seconds, the gateway can streamline communication and save bandwidth by sending data to the cloud only if a location changes.

Securing communication

The IoT devices in a system may not have the computational power to encrypt and decrypt messages. An IoT gateway can fill this gap.

An IoT gateway can be equipped with hardware accelerators, random number generators, cryptographic libraries, and tamper detection. These capabilities help to ensure that the cloud-to-gateway communication is always encrypted and secure.

The gateway-to-device communication often happens over a local network and is less prone to attacks. Other security features, like a firewall, can be integrated into the gateway, to enhance security further.

Reducing costs

Cost reduction is a by-product of the other IoT gateway functions. For example, a gateway can lead to significant cloud communication cost reductions in the long run by filtering data.

Another method of cost reduction is by reducing the number of connections. If you use a service like AWS IoT Core, you are charged for each active connection per minute. A single gateway for multiple devices is more cost-effective than providing cloud connectivity to each device. Finally, the costs saved by preventing security breaches are difficult to quantify, but mitigating even a single incident can significantly impact the bottom line.

Compare

PlatformReal-Time Event ProcessingInternet Scale ThroughputStateful Edge Device ProcessingCross-Region ReplicationGeo-Fencing and Data-Pinning
Azure IoT Edge✔️✔️
AWS IoT Greengrass✔️✔️✔️
Macrometa✔️✔️✔️✔️✔️

The Benefits of an IoT Gateway

Let’s use a practical example to drive home the benefits of an IoT gateway. Consider a campus security solution with hundreds of smart CCTV cameras distributed across multiple buildings. The cameras generate several GBs of data every minute.

When the cameras are connected to an IoT gateway, the administrators can achieve these benefits:

  1. Enhanced security. The gateway can act as a firewall, and allow ingress/egress traffic only from specific IPs and ports. This way, the sensitive CCTV footage is protected from unauthorized access..
  2. Reduced cloud connections. The gateway can act as the single point of communication with the cloud, reducing the number of connections from hundreds to just one and saving on cloud connectivity costs.
  3. Streamlined communication. The gateway can be equipped with video processing capabilities so that it only sends video to the cloud if it detects movement. This additional processing can greatly reduce cloud bandwidth, storage, and processing costs. 
  4. Cost reduction. All three of the benefits above make for more efficient bandwidth and resource utilization, which can significantly reduce costs. There are also huge indirect cost benefits associated with data protection

Using Python to Create an IoT Gateway

While IoT gateways are often hardware appliances, it is possible to create a software-based IoT gateway on a laptop, tablet, or even a Raspberry P.

In this example, we’ll create a Python script that receives IoT data via BLE and then transmits it to the cloud via HTTPS. This script requires:

For your script to work, you’ll need to the BLE_DEVICE_MAC, the DATA_CHARACTERISTIC_UUID, and the SERVER_URL values to valid values for your implementation. After that, this simple Python script becomes a fully functioning IoT gateway, that takes data from a BLE device and sends it to a server.

Here is the template script:

import asyncio
from bleak import BleakClient
import requests

BLE_DEVICE_MAC = "a0:76:4e:51:ed:9f"
DATA_CHARACTERISTIC_UUID = "e29ee12c-ae3d-14ec-b979-0242ac12def2"

SERVER_URL = "https://example.com"


def notification_handler(sender, data):
    """Print received data and pass it on to the server"""
    print("{0}: {1}".format(sender, data))
    postdata = {"value":data.hex()}
    r = requests.post(url = SERVER_URL, data = postdata)
    print("Request status_code:" + str(r.status_code))
    print("Request text:" + r.text)

async def main(address):
    while True:
        #Try to connect with the client
        async with BleakClient(address) as client:
            print(f"Connected: {client.is_connected}")
            if(client.is_connected):
                #Start listening to client notifications on the data characteristic
                await client.start_notify(DATA_CHARACTERISTIC_UUID, notification_handler)
            while(client.is_connected):    
                #Client is connected, so do nothing
                await asyncio.sleep(5);

asyncio.run(main(BLE_DEVICE_MAC))

You can build on this extend this template as needed. For example, adding server-to-device communication is a good next step.

If you’d like to explore creating IoT gateways in more detail, check out the Web Things Gateway project by Mozilla.

Edge computing needs a next generation database technology

  • Ultra fast distributed writes with Conflict-free Replicated Data Types (CRDTs)
  • Solve scaling constraints due to geo-distributed time-stamping with Version Vectors
  • A unified query language for KV, Docs, Graphs and Search with C8QL

IoT Gateway Recommendations and Best Practices

Now that you know the basics, let’s review some common questions related to IoT gateway best practices and usage recommendations.

Do you need an IoT Gateway?

In some cases, this answer is obvious. If you need cloud connectivity, but your devices are not capable directly connecting to the cloud, you need a gateway. In other cases, the answer is not straightforward. You’ll need to weigh the pros and cons. An IoT gateway comes at an upfront cost, and you need to be able to justify it.

Consider the smart door lock example. If you have users who only want to operate the lock when they are nearby, then they don’t need an IoT gateway.

Similarly, if you have multiple devices that all use the same communication protocol, then you may not need an IoT gateway. For example, multiple BLE devicescan create a BLE mesh and communicate without a gateway.

How to choose an IoT Gateway?

Here are a few criteria you should consider when selecting an IoT gateway:

  • Communication protocols (HTTP, MQTT, BLE, Zigbee, LoRa, cellular, MODBUS, Ethernet, etc.)
  • Encryption technologies (TLS, AES128, SHA256, etc.)
  • Number of devices supported
  • Configurability/Programmability (supported languages and operating systems)
  • Power requirements (most gateways require a DC adapter)
  • Temperature characteristics (these may be important if you are deploying the gateway in harsh ambient conditions)
  • Certifications (depending on your organization and your region, certain certifications like CE/FCC may be mandatory)

If multiple IoT gateways meet your requirements, then consider the cost, quality, and vendor reputation. Often, the vendor of your IoT devices may provide or recommend a compatible gateway. The specifications of that gateway can help you in forming your requirements.

In some cases, especially if the vendor’s IoT devices follow some proprietary communication packet format, you may be left with no choice but to use the gateway provided by the vendor. Thus, the IoT device selection also influences the gateway selection.

How to deploy your IoT Gateway?

Generally, the gateway should be installed in a place that ensures that it is accessible by all your IoT devices, and, at the same time, has sufficient power availability and good network connectivity.

Depending on the criticality of your application, you may power your gateway via UPS (Uninterrupted Power Supply). It helps if the gateway is kept in a climate-controlled environment and is accessible only by authorized staff. For example, if an IoT gateway is used in an Industrial IoT application, you wouldn’t want it to be installed right in the middle of the shopfloor.

Edge computing needs a next generation database technology

  • Stateful geo-replicated stream processing keeps globally distributed data consistent
  • One integrated platform for streams, key values, docs, graphs, and search simplifies development
  • Declarative configuration using JavaScript and SQL avoids the need to learn a new syntax

Like the Article?

Subscribe to our LinkedIn Newsletter to receive more educational content.

Chapters

Platform

Global Data Network
Join the Newsletter