How to Configure the Built-in Modbus on IGT-22-DEV

Introduction

In a previous video, a step-by-step procedure has been demonstrated to complete the initial setup of IGT-22-DEV. But the built-in Modbus was not shown in that video. In this video, I’m going to show you how to configure your IGT-22-DEV to enable its Modbus and make the result visible on this demo dashboard.
Actually, after the initial setup procedure, the Modbus has been already enabled via ttyS2, which is a RS-485 port on IGT-22-DEV. If you got an identical sensor as mine, you would only need to make the correct wiring to have it work. However, there might be hundreds of Modbus sensors, and they might have different configuration and register mapping. Therefore, it’s practical to know how to configure it with your own devices or sensors.
It’s recommended that you watch the initial setup video, complete the procedure and prepare a Modbus sensor and its manual before going on.

Wiring

The RS-485 port we are going to use is located at the X3 connector on the top side of IGT-22-DEV. The 7th and 8th pins counted from the front side. Connect these two pins to your Modbus device. And take care of the A and B singles, or sometimes called Data+ and Data-. They have to be well matched. It means A or Data+ of the Modbus device has to be connected to D+ of IGT-22-DEV and B or Data- of the Modbus device to D- of IGT-22-DEV. If your Modbus device is NOT self-powered and required for an external power supply, remember also to connect it to a proper power supply unit.

Modbus Manual

Whatever Modbus devices you’re going to connect to, you have to get its manual of Modbus. There’re some essential information there. The 1st sensor I’ll use as an example is from Carlo Gavazzi. Here is the Modbus manual I got from the distributor.
With this manual, we can find that the interface is RS-485. The pin names of the interface are D+ and D-, which are identical to Data+ and Data- as what I mentioned earlier. And also some parameters for communication, such as “slave address”, “baud rate” and “data format”. The most important thing is to find the default values. Here you can see the default address is 1, and the default data format  9600, N81. And keep them in mind. We will use them later.
Now let’s try to see something about reading temperature and humidity. On page 3, it tells that the temperature is in a holding register at address 564 and the humidity is at 566. These two values are very important for us to get the readings. And in the last column, it shows the definition of the readings. It means once you get the temperature and the humidity you have to divide it by 10 to get the final values. Your device might have different register addresses for the readings and different definition of readings. So you got to find them from the manual of your devices.
I will also show you a tricky thing that sometimes confuses Modbus users. On page 7, it gives an example of a Modbus data frame to read the temperature from this sensor. We can find that the starting address here is 0233 in hex. It is 563 in decimal, and it’s different from the 564 we saw previously on page 3. I don’t want to jump into complex detail. My suggestion is to try to increase or decrease by 1 if you have problem reading the registers.
It’s only possible to connect to the devices if you have the correct “data format”. However, these parameters are usually writable.  It means the data format might not really be the default values upon you get it. Therefore, from my experience, I will usually test the Modbus devices with 3rd party Modbus utilities to ensure the parameters are correct.

Modpoll to Evaluate the Sensor

There’re many choice of Modbus host software. In this article, I’ll use modpoll, which is a light-weighted and pre-compiled freeware supporting various architectures. You can evaluate the sensor with your host computer, and I’ll demonstrate this on IGT-22-DEV.
Modpoll is not pre-installed. After login via the web console, We can download it with wget command and decompress it with tar:
If the Thingsboard-IoT-Gateway has been started, it’s recommended to stop it before the evaluation. This will make the job simpler because it’s configured to use the same serial port and run Modbus in the background. Use this command to stop it:
  • sudo systemctl stop thingsboard-gateway.service
After Thingsboard-IoT-Gateway has been stopped, we can use modpoll to get the readings from the sensor. Let’s try to get temperature first.
  • ./modpoll/arm-linux-gnueabihf/modpoll -m rtu -a1 -b9600 -p none -d8 -s1 -t4 -r564  -1 /dev/ttyS2
And remember, by definition, we have to divide the reading by 10 to get the final values. Let me briefly explain the parameters. “a” means slave address. a1 indicates that we are going to talk to slave 1. b is baud rate, p d s together provide the data format and means N81 here. “t4” is for reading holding registers. r is for register address. We use “-1” to force to read once only.  The last part is /dev/ttyS2. It indicates which serial port to use.
Then we can change the register address from 564 to 566 to get the humidity:
  • ./modpoll/arm-linux-gnueabihf/modpoll -m rtu -a1 -b9600 -p none -d8 -s1 -t4 -r566  -1 /dev/ttyS2
If we can get the readings correctly, we have confirmed the data format is correct and the sensor works well.

Modify Thingsboard Gateway configuration

After the initial setup, a specific configuration file of Modbus RTU has been installed. We can edit the configuration file with the following command.
  • sudo nano /etc/thingsboard-gateway/config/NT_IGT22_TTYS2.json
Since we have already confirmed the register address and data format of the sensor, it’s very easy to apply them to the configuration file.
If you are going to use Modbus RTU via the built-in RS-485 on IGT-22-DEV. You don’t need to change the first 4 items. What you have to check are Baud rate, unitID, it’s previously called slave address, functionCode, 3 means reading holding register. And the last item is address. We know the temperature address should be 564. But I have to use 563 instead. It’s the mentioned trick of increasing or decreasing by 1.
After the modification, use Ctrl-X to leave the editor and go back to the command line. We need to start the Thingsboard-IoT-Gateway using this command:
  • sudo systemctl start thingsboard-gateway.service
Now you should see the new readings every 5 seconds. You can modify configuration file to get a different sampling rate. For more detail of Thingsboard-IoT-Gateway configuration, you can browse the official website of Thingsboard-IoT-Gateway.

One more different sensor

There is a 2nd sensor I bought from an on-line store. I would like to show that configurations vary from sensors to sensors. The yellow wire is notated as 485-A and the white as 485-B. These two pins are identical to the pins D+ and D- of the 1st sensor.
And here is the datasheet I captured on-line. And I put my translation for better readability.
First, we find that the temperature is in input registers instead of holding registers as the 1st sensor is. And the register address of temperature is 1 and that of humidity is 2. From somewhere on the web, its default Baud Rate is 9600 and the data format is N81. These two values are identical to the 1st sensor. And actually they are commonly used among devices. The default slave address is 1, but I changed it from 1 to 2 so that I can connect them together in one Modbus RTU network.
Again. We stop the Thingsboard-IoT-Gateway to check the 2nd sensor manually.
  • sudo systemctl stop thingsboard-gateway.service
According to the default values, this is the command to get the readings
  • ./modpoll/arm-linux-gnueabihf/modpoll -m rtu -a2 -b9600 -p none -d8 -s1 -t3 -r1  -1 /dev/ttyS2
You will find it fails to get the result.  So Let’s try the increasing by 1 trick. Change the register address from 1 to 2.
  • ./modpoll/arm-linux-gnueabihf/modpoll -m rtu -a2 -b9600 -p none -d8 -s1 -t3 -r2  -1 /dev/ttyS2
  • ./modpoll/arm-linux-gnueabihf/modpoll -m rtu -a2 -b9600 -p none -d8 -s1 -t3 -r3  -1 /dev/ttyS2
Now it works.
It’s time to change the configuration file and make Thingboard-IoT-Gateway to read the 2nd sensor instead of the 1st sensor.
Use this command to open the editor:
  • sudo nano /etc/thingsboard-gateway/config/NT_IGT22_TTYS2.json
The first item to change is unitID because I have changed the slave address of the 2nd sensor from 1 to 2. The next item is functionCode. The temperature is in input registers rather then in holding registers. We got to have 4 for this item. And the last one is address. The register address of the temperature reading is 1. And let’s do the same to humidity. Leave the editor and save the configuration file file by Ctrl-x.
Restart the service to make it effective.
  • sudo systemctl start thingsboard-gateway.service
Conclousion
In this article, some brief introduction of Modbus was described. Also I have demonstrated how to configure two different modbus sensors on IGT-22-DEV and make the result visible on the demo dashboard. Hope you can work well with your own sensors. And actually, the configuration file originally installed by the initial setup is compatible with all the default values of the 2nd sensor. If you got something alike, it would be completely plug and play. You don’t need to modify anything.