DEV Community

Cover image for Connect modbus sensor XY-MD02 to NodeRED with balena
Marc Pous
Marc Pous

Posted on

Connect modbus sensor XY-MD02 to NodeRED with balena

Modbus is a widely used protocol in the industrial sector for communication between devices, particularly sensors and controllers. A sensor that utilizes Modbus is the XY-MD02 sensor. This sensor measures temperature and humidity and it uses the Modbus RTU protocol. It can measure temperature from -40°C to 80°C with an accuracy of ±0.5°C and humidity from 0% to 100% relative humidity (RH) with an accuracy of ±3% RH according to their technical documentation.

One tool that can be used to integrate the XY-MD02 sensor with other systems is the Industrial edge gateway made with balena running Node-RED, among others services. Node-RED is a no code / low code programming tool that allows users to create and deploy visual applications for IoT devices. It supports various protocols, including Modbus, making it an ideal tool for creating applications that collect data from Modbus controllers and sensors. By using Node-RED along with balena, users can easily access and integrate data from the XY-MD02 sensor into the edge system, making it a valuable tool in the industrial and automation industries.

The XY-MD02 can be easily installed using a standard RS485 connection and it is compatible with most Modbus master devices, such as programmable logic controllers (PLCs) and industrial PCs. In this case, the XY-MD02 will be connected directly using a USB RS485 converter to the Industrial IoT gateway using balena.

Intel NUC running balena and NodeRED with the XY-MD02 modbus sensor with a RS485 USB converter

Setting up a Modbus gateway using Node-RED and the XY-MD02 sensor is straightforward.

XY-MD02 modbus sensor with a RS485 USB converter connection diagram

In conclusion, the XY-MD02 sensor is a reliable and accurate sensor that can be integrated into various systems using the Modbus protocol. By using Node-RED as a Modbus gateway, users can easily access and integrate data from the sensor into their system, making it a valuable tool in the industrial and automation industries. With the balena Industrial IoT Edge Gateway easy-to-deploy application, industrial developers can create gateways without needing to write any code, making it accessible to a wide range of users.

Hardware needed

  • Intel NUC i7
  • USB stick to flash the operating system of the Intel NUC.

  • USB RS485 converter (for this I used the Greluma ‎‎ZL013LUM)

  • XY-MD02 Modbus sensor

Software needed

  • A free balenaCloud account. Remember: your first 10 devices are free and fully-featured.
  • Software to flash the Intel NUC computer. E.g. balenaEtcher

Deploy the Industrial IoT Gateway

Deploying this application is as simple as clicking the Deploy with balena button to create a fleet on balenaCloud. Find the repository of the application here.

You can follow all the instructions to deploy the Industrial iot edge gateway from this step-by-step tutorial.

balenaCloud with the Industrial IoT Gateway running

Run the XY-MD02 Modbus sensor on NodeRED

First of all, it’s important that the sensor is already connected to a USB port of your Intel NUC once you power up the device.

At this moment, go to NodeRED on the port 80 of your local ip address and use balena and balena as username and passwords.

NodeRED login page

Once there, you should be able to see the Modbus nodes. Select the Modbus Reader node and a Debug node.

Modbus client reader node

To configure the Modbus Reader node, double click on it. Select the FC FC 4: Read Input Registers for this case on Address 1 and Quantity 2. I selected a Poll Rate every 2 seconds (feel free to change the poll rate).

Edit Modbus client Reader node

Click on the Server edit button to Add new modbus-client.

Add new modbus client

Select Type as Serial. The Serial Port is /dev/ttyUSB0. The Serial type is RTU-BUFFERED and the Baud rate is 9600.

Update the Modbus read client node and click Deploy. You should see the Modbus Reader node connected and active.

Parse the data

To parse the data coming from the XY-MD02 modbus sensor we will add a function that will get the data coming from the array resulting from reading the modbus data, and we will divide by 10 the number.

Add a function

The temperature is in celsius degrees and the humidity is in %. This is just an example to encode the temperature and humidity.

Get the data from the modbus array and create a new payload

Troubleshooting

In case this is not existing go to balenaCloud, open the terminal on HostOS and type ls /dev/ttyUSB* and check what you see as a result.

Check the ttyUSB connections of your device

If you don’t see any ttyUSB0 or anything else as a result, you might have a problem with the USB connection. Feel free to write to us on the balena forums to learn more about your case.

Feedback

The balena platform is all about reducing friction for fleet owners. As always, let us know if you run into any issues when following this guide or getting the Industrial IoT gateway reference architecture set up in your factory or industry plant floor.

Feel free to add a comment below or contact us on our forums.

Top comments (0)