Connect modbus sensor XY-MD02 to NodeRED with balena
Tags: industrial, iot, nodered, balena
Published: 2023-04-04 16:49:25

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](https://blog.balena.io/industrial-iot-edge-gateway-reference-architecture-using-balena-hivemq/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ho3anky3u7nhujs6u1h.jpeg)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqpmthef4axei716hso2.png)


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](https://www.balena.io/cloud) account. Remember: your first 10 devices are free and fully-featured.
* Software to flash the Intel NUC computer. E.g. [balenaEtcher](https://www.balena.io/etcher)


## 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](https://github.com/mpous/hivemq4-ming).

You can follow all the instructions to deploy the Industrial [iot edge gateway from this step-by-step tutorial](https://blog.balena.io/industrial-iot-edge-gateway-reference-architecture-using-balena-hivemq/).


![balenaCloud with the Industrial IoT Gateway running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmqbhvdsnzm2j92jho3m.png)


### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wny0qkw4er1vudtqnvmw.png)


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

![Modbus client reader node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wemxyqrurv1ejw7kv0yl.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx0bwbahuz9yc5wzac4l.png)


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


![Add new modbus client](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4itf1q10edidvvyt6uk9.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enb2ebtgv4d0xey8an0o.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rwcytw4n5octlpkt9v6.png)



## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1ixoza6eumk8vy5i805.png)


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](https://forums.balena.io) 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](https://forums.balena.io).