Connect devices through IoT Gateway
Devices with industrial protocols, like Modbus, don’t send data to ThingsBoard themselves. ThingsBoard IoT Gateway, a program on a computer near the devices, reads them and sends their values to ThingsBoard. This page shows how your AI agent connects devices through IoT Gateway, and how to do the same from the terminal.
Devices ──► IoT Gateway ──► ThingsBoard (on a computer near the devices)The agent creates a gateway with a connector after your yes and writes the files that run the gateway. On the computer where the agent runs, it can also start the gateway. On another computer, you run one command there. Then the agent checks that the values arrive and that they make sense.
Before you start
Section titled “Before you start”- ThingsBoard CLI, connected to ThingsBoard Cloud or your own ThingsBoard server.
- A computer near the devices for the gateway: the computer that runs the agent, or another one, like a Raspberry Pi or a PC. It runs Linux, macOS, or Windows, reaches the devices, and reaches your ThingsBoard server.
- Docker or Python 3 on the gateway computer.
- For Modbus RTU devices, a USB-to-RS-485 adapter on the gateway computer.
- The register map of each device model, if you have it: the table in the manual that says which value sits at which register. Without it, the agent finds the manual.
To try the steps without devices, use ThingsBoard Modbus Demo Server, as Connect with your AI agent shows.
Connect with your AI agent
Section titled “Connect with your AI agent”The steps below connect Modbus devices. The agent asks one question at a time, and each question says what it asks for and where you find the value.
To try the steps without devices, run ThingsBoard Modbus Demo Server on your computer. It simulates a temperature and humidity sensor:
docker run --rm -it -p 5021:5021 thingsboard/tb-gw-modbus-server:latestWhen the agent asks about the sensor, answer that it runs on this computer, on port 5021, with unit ID 1. Holding register 0 is the temperature and holding register 1 is the humidity, both 16-bit signed integers divided by 10.
-
Ask the agent, like “Connect my Modbus temperature and humidity sensor in the boiler room”. After the agent deploys a solution, you can also select Yes when it asks Connect a real device to it?, then Modbus sensor or meter when it asks what device it is. The first question opens with a one-line diagram of how your devices connect, like
Boiler room sensor → gateway → ThingsBoard. -
Tell the agent about your devices. The agent asks about these details, and only about what it doesn’t know yet:
- Model and count: Name the model of each device and how many devices of each model you have. The model is printed on the device or its box.
- Register map: Give your file, or let the agent find the map in ThingsBoard IoT Hub or in the manual of the model on the web. The agent doesn’t invent registers: without a model or a map, it asks for something to find them from, like a photo of the board.
- Connection: Answer what the manual doesn’t say, like how the device is wired, its address and port, its serial settings and unit ID, where the gateway runs, and the serial port of the RS-485 adapter. With several devices in a solution, the agent also asks which solution device is which real device.
You can give several answers at once, like the answer for the demo server below.
-
Check the register map. The agent shows each register with its key, data type, scale, and unit, and asks Use it or Change something.
The map also sets the byte and word order of each device, the order in which the device sends the bytes and registers of a value. With a wrong order, the values are wrong, and the agent catches it when the values arrive. The agent takes the order from the manual. If you give the registers without a manual, the agent asks for the order after you select Use it.
-
Select Create when the agent asks to create the gateway and its connector. The agent names the gateway after the place in your words, like Boiler Room Gateway. To use other names, type them. The agent creates a gateway device in ThingsBoard with a Modbus connector for your devices. ThingsBoard delivers the connector to the gateway, so it also shows on the gateway’s page in Entities > Gateways.
-
Select how the gateway runs. The agent offers the ways that fit the gateway computer and recommends one. For details, see Choose how the gateway runs.
-
Start the gateway.
- This computer: The agent asks whether to start the gateway now. Select Start, and the agent starts the gateway and checks it right away.
- Another computer: The agent writes the files and gives you the steps in one message: the link to the gateway, the path of its files, what to install first, how to move the files to the other computer, and the commands to run there. When the gateway runs, say “check the gateway”.
-
Wait for the check. The agent checks each step from the gateway to the data, and it judges the values against the units of the register map. If a value is off, the agent fixes the scale or the byte order and checks again. For details, see Check the gateway.
-
Choose the next step. When the data arrives, the agent shows the values that it received and asks what to do next, like build a dashboard for the devices. With a solution, the data is already on its dashboard. If the dashboard does not show some of the keys that arrived, the agent offers to add them.
If a demo data stream of your solution is still running, the agent stops it before it starts the gateway, so that the demo data does not mix with the data of your devices.
Choose how the gateway runs
Section titled “Choose how the gateway runs”The gateway runs in one of three ways:
- Docker: A Docker Compose file that runs the gateway in a container. You start it with one command. Docker on Linux reaches network devices and RS-485 adapters.
- Python: Run scripts for a gateway that you install from pip into a Python virtual environment,
~/tb-gateway-env. No Docker needed. - Remote Agent: You run one script on the gateway computer, and it starts Remote Agent there in Docker. When you say “check the gateway”, the agent installs the gateway on Remote Agent. After that, you update and restart the gateway from ThingsBoard. Remote Agent is available in ThingsBoard Cloud and in ThingsBoard 4.4 and later. For details, see Remote Agent.
Which ways the agent offers depends on the gateway computer and on how the devices are connected:
| Gateway computer | Ways | Recommended |
|---|---|---|
| This computer | Docker or Python. Remote Agent too, on Linux, if your ThingsBoard has it | Docker |
| Another Linux computer, like a Raspberry Pi | Remote Agent, if your ThingsBoard has it, Docker, or Python | Remote Agent, else Docker |
| macOS or Windows, with Modbus TCP devices | Docker or Python | Docker |
| macOS or Windows, with Modbus RTU devices | Docker can’t reach a USB adapter there. The agent offers Python on that computer, the gateway on a Linux computer near the devices, or an RS-485-to-Ethernet converter that makes the devices Modbus TCP devices | Python |
The agent writes the files to the gateway folder of your project. <gateway-name> is the name of the gateway in lowercase with dashes, like boiler-room-gateway:
| Way | Files | Start command |
|---|---|---|
| Docker | gateway/<gateway-name>/docker-compose.yml |
docker compose up -d in the folder |
| Python | run-gateway.sh for Linux and macOS, run-gateway.ps1 for Windows, and config/tb_gateway.json, in gateway/<gateway-name>/ |
The commands that install the gateway in ~/tb-gateway-env, then sh run-gateway.sh, or powershell -ExecutionPolicy Bypass -File run-gateway.ps1 on Windows |
| Remote Agent | gateway/remote-agent/start-remote-agent.sh |
sh start-remote-agent.sh |
The files hold the access token of the gateway or the provisioning secret of Remote Agent. On Linux and macOS, only your user can read them, and git does not commit the gateway folder. Don’t share the files.
On another computer, run the Docker commands with sudo if your user there is not in the docker group. Python on Debian and Ubuntu needs the python3-venv package: if it is missing, run sudo apt install python3-venv first. Python on Windows needs Python 3 from python.org, installed with Add Python to PATH. The terminal that runs a Python gateway shows “deprecated” warnings from pymodbus on every read. They are harmless.
Run the gateway on another computer
Section titled “Run the gateway on another computer”The agent writes the files on the computer where it runs. To run the gateway on another computer, move the folder of the gateway there. The agent gives you the steps with your folder and the address of the other computer filled in:
- Linux or macOS: Copy the folder with
scp -r, or create the files in an SSH session and paste their text. - Windows: Copy the folder with a USB stick, or with
scp -rafter you turn on OpenSSH Server in Settings > System > Optional features.
Then run the commands from the agent’s message in that folder.
If your ThingsBoard runs on localhost, a gateway on another computer can’t reach it. The agent asks for the address of the ThingsBoard computer on your network, like 192.168.1.20, and writes the files with it.
With Remote Agent, run the script on the other computer, then say “check the gateway”. Your AI agent then installs the gateway on the Remote Agent that the script started. The command docker ps --filter name=tb-agent on that computer shows whether Remote Agent runs.
Check the gateway
Section titled “Check the gateway”Say “check the gateway”. The agent runs tb gateway check --wait, which checks these steps in order:
| Step | It passes when |
|---|---|
| Installed | Remote Agent installed the gateway. Only for a gateway that Remote Agent runs |
| Online | The gateway is connected to ThingsBoard |
| Connector accepted | The gateway applied the current Modbus connector |
| Devices created | The gateway connected each device of the connector |
| Data arriving | Values arrive from the devices |
The check waits up to two minutes for the gateway to start. When only the data is missing, it stops after three poll periods without data, but not before 30 seconds. It counts only the values that the gateway read after it applied the current connector.
When all steps pass, the agent judges each value against the unit of the register map:
- A value is off by a power of ten, like 235 °C from a room sensor: The agent fixes the scale of the register.
- A value is off otherwise, like -281.6 instead of 24.5 °C: The agent switches the byte order, or the word order for a value of two or more registers, and tells you what it changed.
After a fix, the agent saves the connector again and checks again. If no order gives plausible values, the agent asks you to compare the register map with the documentation of the device.
Logs of the gateway
Section titled “Logs of the gateway”When a step fails, the agent reads the logs of the gateway to find the cause, like a device that the gateway can’t reach or a read error. For a running gateway on any computer, it runs tb gateway logs, which collects the gateway’s log lines through ThingsBoard for 30 seconds. To read the logs yourself:
| Way | Logs |
|---|---|
| Docker | docker compose logs -f in the gateway’s folder |
| Python | The terminal that runs the gateway, or gateway.log in the gateway’s folder when the agent started it |
| Remote Agent | In ThingsBoard, go to Edge management > Agents, open the agent, then the gateway’s application |
Stop the gateway
Section titled “Stop the gateway”| Way | Command |
|---|---|
| Docker | docker compose down in the gateway’s folder |
| Python | pkill -f tb-gateway-env/bin/thingsboard-gateway, or Stop-Process -Name thingsboard-gateway in PowerShell on Windows |
| Remote Agent | Delete the gateway’s application from Remote Agent in ThingsBoard, in Edge management > Agents |
Connect from the terminal
Section titled “Connect from the terminal”You can create the gateway and its connector, write the launch files, and check the gateway yourself:
tb gateway create --name "Boiler Room Gateway" # create a gateway devicetb gateway connector set "Boiler Room Gateway" --file modbus.json # check the Modbus config and deliver ittb gateway launch "Boiler Room Gateway" --way docker # write the Docker Compose file, or --way piptb gateway check "Boiler Room Gateway" --wait # wait until the values arrivetb gateway logs "Boiler Room Gateway" # collect the gateway's log linestb gateway createexits with code 2 and suggests another name if a device with the name exists.tb gateway connector setsaves nothing and exits with code 2 if the config misses a field that the gateway needs, likebyteOrderorpollPeriod, or has a value out of range. It also exits with code 2 if the gateway runs other connectors. For the format of the config, see Modbus connector.tb gateway launchwrites the files togateway/<gateway-name>/, which git ignores. Use--serial-port /dev/ttyUSB0to give the Docker container an RS-485 adapter, once for each adapter. Use--hostto set the address of ThingsBoard that the gateway connects to, and-oto write the files to another folder. The output hides the access token of the gateway unless you add--show-secrets.tb gateway check --waitwaits up to two minutes. Use--timeout <seconds>to change the time.tb gateway logsturns on the remote logging of the gateway for 30 seconds and turns it off again, also when you press Ctrl+C. Use--duration <seconds>to collect from 10 to 600 seconds, and--levelto set the lowest level:DEBUG,INFO,WARNING, orERROR. The gateway must be online.- Add
--format jsonto any of these commands for scripts and AI agents.
To run the gateway through Remote Agent, write the script that starts Remote Agent, run it on the gateway computer, and install the gateway on Remote Agent:
tb gateway agent provision-command --write # write gateway/remote-agent/start-remote-agent.shtb gateway agent list # list the agents and whether they are onlinetb gateway agent install "Boiler Room Gateway" --agent <agent> # install the gateway on Remote AgentFor Modbus RTU devices, add --serial-port to tb gateway agent install, as for launch. The agent commands exit with code 3 if your ThingsBoard has no Remote Agent.
Troubleshooting
Section titled “Troubleshooting”The gateway is not online
The gateway does not run, or it can’t reach ThingsBoard. On the computer that runs the agent, the agent looks at the gateway itself. On another computer, check there:
- Docker: Run
docker compose psanddocker compose logs --tail 50in the gateway’s folder. - Remote Agent: Run
docker ps. - Python: Look at the terminal that runs the gateway.
To check that the computer reaches ThingsBoard, run one of these commands there, with the host and the MQTT port from the agent’s message:
- Linux: Run
timeout 5 bash -c '</dev/tcp/<host>/<port>' && echo reachable. - macOS: Run
nc -z -w 5 <host> <port> && echo reachable. - Windows: Run
Test-NetConnection <host> -Port <port>in PowerShell.
Don’t use ping: many servers don’t answer it.
The connector is not accepted
The gateway runs, but it has not applied the connector yet. Wait a minute, or restart the gateway, then say “check the gateway”.
Some devices are not created
The gateway can’t read these devices. The agent reads the gateway’s logs, which name the device and the reason.
- Modbus TCP: Check the IP address and the port of the device in its settings.
- Modbus RTU: Check the serial port, the baud rate, the parity, and the wiring.
- Both: Check the unit ID of each device.
For a program or a simulator on the computer that runs the agent, the agent finds the port that the program really listens on and offers to use it. A gateway in Docker can’t reach a program that listens only on 127.0.0.1: the program must listen on all addresses, 0.0.0.0.
The devices are created, but no data arrives
The gateway connects to the devices, but it can’t read the registers. Check the register addresses and the function code: 3 for holding registers, 4 for input registers. If other devices with the same register map send data, the cause is the device itself: its power, address, port, or unit ID.
Remote Agent did not install the gateway
The check shows how far the install got and what failed, like a missing serial port. Fix the cause, then delete the gateway’s application from Remote Agent in ThingsBoard, in Edge management > Agents. The next “check the gateway” installs the gateway again.
If you move the gateway to another computer, Remote Agent on the first computer still runs it. Delete the gateway’s application from that Remote Agent first.
The gateway on Docker can’t read an RS-485 adapter on macOS or Windows
Docker on macOS and Windows can’t reach a USB adapter. Run the gateway with Python on that computer, run it on a Linux computer near the devices, or connect the devices through an RS-485-to-Ethernet converter.
Not supported yet
Section titled “Not supported yet”The agent always creates a new gateway with a new Modbus connector. Adding devices to a gateway that you already have comes in a later release.
Was this helpful?