Node-RED with Modbus Tutorial (part 1)
Table of Contents
Introduction
This tutorial will guide you through creating a simple example using Node-RED and Modbus via TCP/IP. We will set up a Modbus server, read from and write to registers, and utilize Node-RED for real-time data handling. This is particularly useful for those looking to integrate industrial automation systems or IoT applications.
Step 1: Setting Up Node-RED with Modbus Server
-
Add a Modbus server node to your Node-RED flow:
- Double-click the node to configure it.
- Set the port to the default Modbus TCP port, typically 502.
- Enable a listener to monitor outputs from the server.
-
Inject a trigger:
- Use an inject node to send an empty string to start the server.
- Deploy your flow to activate the server.
-
Check for errors:
- If you encounter issues, verify that the port is not blocked on your system.
- You can troubleshoot the flow files located in the Node-RED directory.
Step 2: Configuring the Modbus Reader
-
Add a Modbus Flex Getter node:
- This is preferred over the standard Getter due to its robustness.
- Connect it to the Modbus server node.
-
Configure the Modbus Flex Getter:
- Set the function code to 3 (read holding registers).
- Specify the unit ID, starting address, and the quantity of registers to read.
- For example, to read 4 registers, your configuration might look like this:
{ "unitid": 1, "fc": 3, "address": 0, "quantity": 4 }
-
Deploy the flow and check the output:
- Use the inject node to trigger a read.
- You should see the values from the specified registers.
Step 3: Writing Values to Modbus Registers
-
Add a Modbus Flex Writer node:
- This node allows you to write values to Modbus registers.
-
Configure the Modbus Flex Writer:
- Set the function code to 16 for writing multiple registers.
- Specify the unit ID and starting address.
- Prepare the payload with integer values to write.
-
Use a Join node to create an array:
- This node can combine multiple values into a single array for writing.
- Ensure the array length matches the number of values you wish to write.
-
Deploy the flow and test writing values:
- Inject an array of values (e.g., [1, 2]) to write to the specified registers.
- Confirm the values by reading the registers again.
Step 4: Implementing Random Number Generation
-
Install the random node:
- Use the terminal to install the node by navigating to the Node-RED directory.
- Run the command:
npm install node-red-contrib-random
-
Create a flow to generate random values:
- Set the random node to generate values from 0 to 65535 (2^16).
- Configure the inject node to trigger the random number generation every second.
-
Deploy the flow to see random values being generated and written to the Modbus registers in real-time.
Step 5: Enhancing Your Flow
-
Add comments and labels:
- Clearly label nodes and add comments to explain their purpose.
- This will make your flow easier to understand for future reference.
-
Test and debug your flow:
- Ensure all nodes are connected correctly and troubleshoot any issues that arise.
Conclusion
You have successfully created a basic Node-RED flow that interacts with a Modbus server, allowing you to read from and write to registers. By implementing random number generation, you can simulate real-world data inputs. For further learning, consider exploring more advanced Modbus features or integrating actual hardware systems into your flows.