The Internet of Things (IoT) has been gaining traction for quite some time now, and everyone wants to connect their devices and push data to the cloud. The challenge lies in the variety of devices and protocols available. How can we integrate all these devices in a complex IoT environment? Don’t worry, today we will discuss ThingsBoard Gateway, an open-source, powerful IoT gateway developed in Python, which is simply a magical tool for integrating these IoT devices.
What is ThingsBoard Gateway? Simply put, it helps you integrate devices with different protocols into the ThingsBoard platform, supporting various protocols like Modbus, MQTT, OPC-UA, and more. Sounds complicated? Don’t worry, we will take it step by step.
1. Environment Preparation
You need to have an environment set up. ThingsBoard Gateway is a Python project, so let’s get the environment sorted out.
PART01 Install Python and Dependencies
You need to have Python installed, preferably version 3.7 or higher. Use pip
to install the dependencies required for ThingsBoard Gateway.
# Check Python version
python3 --version
# Install pip dependencies
pip install thingsboard-gateway
Friendly reminder: If you are using Windows, remember to add Python to your environment variables, otherwise the command might not be found.
2. Configure ThingsBoard Gateway
Once installed, we can start configuring. The configuration file for ThingsBoard Gateway is mainly tb_gateway.yaml
, where all the gateway configurations are stored.
PART02 Configure Gateway Connection
You need to connect the gateway to the ThingsBoard platform.
# tb_gateway.yaml
thingsboard:
host: "localhost" # Your ThingsBoard server address
port: 1883 # MQTT port, default is 1883
remoteShell: false
remoteConfiguration: true
security:
accessToken: "YOUR_ACCESS_TOKEN" # Access Token obtained from the ThingsBoard platform
Friendly reminder: Keep your Access Token safe, it is the “identity card” for your gateway to connect to the server; if lost, you won’t be able to connect.
3. Integrate Different Device Protocols
With the gateway configured, the next step is to integrate devices. ThingsBoard Gateway supports various common IoT protocols, and we will discuss a few commonly used ones.
PART03 Integrate Modbus Devices
Modbus is widely used in industrial IoT, and integrating it with ThingsBoard Gateway is not difficult.
# Add Modbus configuration in tb_gateway.yaml
modbus:
- deviceName: "Modbus Device 1"
type: tcp # Connection method for Modbus, can be tcp or rtu
host: "192.168.0.101"
port: 502
devices:
unitId: 1
attributes:
- name: "temperature"
tag: 1
type: int
functionCode: 4
address: 0
timeseries:
- name: "voltage"
tag: 2
type: float
functionCode: 4
address: 1
The above configuration is an example; we are using the Modbus protocol to capture the device’s temperature and voltage. Note: The Modbus unitId
and device address must match.
PART04 Integrate MQTT Devices
When it comes to IoT protocols, we cannot forget MQTT. It is lightweight, simple, and many devices support it.
# Add MQTT device configuration in tb_gateway.yaml
mqtt:
- topicFilter: "devices/+/data"
converter: mqtt_json
type: json
server:
host: "mqtt://broker.hivemq.com"
port: 1883
accessToken: "YOUR_ACCESS_TOKEN"
Friendly reminder: Pay attention to the MQTT device topics, as different devices may have different topics for sending data. If you want to listen to multiple devices’ data, you can use +/
as a wildcard.
PART05 Integrate OPC-UA Devices
OPC-UA is also quite common in industrial IoT. ThingsBoard Gateway supports this protocol as well.
# Add OPC-UA device configuration in tb_gateway.yaml
opcua:
- deviceName: "OPC-UA Device"
serverUrl: "opc.tcp://localhost:4840"
nodeId: "ns=2;s=Temperature"
name: "temperature"
attributes:
- key: "model"
path: "ns=2;s=Model"
timeseries:
- key: "pressure"
path: "ns=2;s=Pressure"
In the configuration, the nodeId
is the unique identifier for the device in the OPC-UA server, don’t get it wrong, otherwise, the data won’t be readable.
4. Start the Gateway
Once everything is configured, we can directly start the gateway and see the results.
tb_gateway
If you see a bunch of logs output in the terminal, it means the gateway is running. If your configuration is correct, you should also see device data being uploaded to the ThingsBoard platform in real-time.
Friendly reminder: If the gateway fails to start, don’t panic, check your configuration file, especially for the device protocol and server address, as these are common areas where errors occur.
5. Summary
With the code written, our ThingsBoard Gateway is now configured, and device data can be uploaded to the ThingsBoard platform. Isn’t it simple? Remember a few key points:
-
Keep the Access Token safe, it is the credential for the gateway to connect to the server.
-
Configure the corresponding parameters based on the device protocol, ensuring that the Modbus
unitId
, MQTTtopic
, and OPC-UAnodeId
match. -
Log output is very important, check the logs frequently when starting the gateway, as it makes identifying issues much easier.
With this, haven’t we improved our IoT skills significantly? Hurry up and give it a try, and integrate your device data into the ThingsBoard platform!