Connect Modbus Serial Sensor XY-MD02 to n3uron IIoT platform with balena
Published: 2023-04-21 16:59:49

In this blogpost, we are going to showcase how to connect the inexpensive Modbus Serial RTU temperature and humidity sensor, the [XY-MD02](http://www.sah.rs/media/sah/techdocs/xy-md02-manual.pdf). 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amgtgc98jifdidoplbih.png)


We are going to connect the XY-MD02 to an enterprise Industrial IoT platform running on the edge of floor-plants based on [N3uron](https://n3uron.com/) running on [balena](https://balena.io).

## 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 Edge Gateway code

First of all, we are going to deploy the N3uron service along with HiveMQ and MongoDB as an Historian database on balena.

Find [here the Github repository of n3uron with balena](https://github.com/mpous/n3uron-balena/) and click the blue button `Deploy with balena`. Follow the steps and enjoy the magic of the over-the-air updates with balena.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhf361m1jko3zixcbkfu.png)


Open another tab and type the `https://` and your local ip address (copy it from the balenaCloud dashboard). That should redirect you to the login page of n3uron in your local machine.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fi57zc1qvwy7969ifdov.png)


Login using `admin` as User and `n3uron` as a Password.


### Access to the Modbus Data from N3uron

In my case the XY-MD02 is connected to the machine via USB with a RS-485 to USB converter. If you have a similar configuration follow the next steps.

Go to the `Navigation` menu, on `System` and click `Config`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tttv9zvdc50xb4m251um.png)

Click the `Modules`and then click on the tiny blue button next to `Models`. And click `New model`. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn2n6g0oh59aibqnh53j.png)

Add a name such as `Modbus`. And select the `Module Type` as a `Modbus Client`. Click `Save`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok01ovtapkm2oefuowqe.png)

With this you defined a Connection Layer as a Modbus Client. Now it's time to configure the connection from N3uron to the device and specify the data it's going to collect.

On the second menu, you are going to see the `Modbus` module you just created but some elements (`Logging` and `API`), click on them and click the `Save` button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihcbehshpwsc8l8oz5ae.png)

Click on `Modbus` and a new menu will appear, click on the tiny blue button next to `Model` and create a `New Channel`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jo4xiqlk1fdtysf34zw.png)

In this case the XY-MD02 Modbus device is connected through the `/dev/ttyUSB0`. 

Now go to `Tag`on the Explorer Menu and we will create the `Temperature` and `Humidity` tags to gather the data generated by the Modbus sensor. On of the most tricky thing is that the Modbus address to read the Temperature of the XY-MD02 is `30002` and Humidity is the `30003` and they are `Int16`. Have this in mind when you fill the gaps.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb4mvvxpmnko4rvmxf2o.png)

Afterthat you will be able to see data on the `Real Time` option.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwamlu9hy5uja5y6rpxd.png)

I hope this will help you to configure a Modbus Serial device on N3uron using a balena gateway.

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