Node-RED: RS485 Raspberry Pi Tutorial
by Zihatec in Circuits > Raspberry Pi
43331 Views, 13 Favorites, 0 Comments
Node-RED: RS485 Raspberry Pi Tutorial
The flow based visual programming tool Node-RED becomes more and more popular for Raspberry Pi developers. This instructable will show you how to use our isolated RS422 / RS485 Serial HAT under Node-Red for simple RS485 communication and for MODBUS applications too.
Tools and Materials
Materials:
- Raspberry Pi A+, B+, 2B, 3B or 4B
- RS422/RS485 serial HAT
- SD Card
Software:
- Raspbian Stretch or Buster (with desktop and
recommended software)
Free the UART in Raspbian Stretch or Buster
The easiest way is to use the raspi-config tool to switch the UART to the GPIO14/15 pins. take a fresh Raspbian image
- sudo raspi-config
- goto '5 Interfacing Options'
- goto 'P6 Serial'
- 'Would you like a login shell to be accessible over serial?' --> NO
- 'Would you like the serial port hardware to be enabled?' --> YES
- Finish raspi-config
- reboot the Raspberry Pi
Now you can access the UART via /dev/serial0
DIP Switch Setting for RS485 HAT
Our RS422/RS485 HAT comes with 3 DIP switch banks. You have to set these DIP switches for RS485 as shown in the picture above.
- Switch 1: 1-OFF 2-ON 3-ON 4-OFF
- Switch 2: 1-OFF 2-OFF 3-ON 4-ON
- Switch 3: 1-OFF or ON* 2-OFF 3-OFF 4-OFF
*Depending of the position of the RS422/RS485 HAT in the Modbus line you have to switch the terminating resistor ON or OFF. Please switch the resistor to ON position only if the HAT is on one end of the bus line. In all other cases switch the terminating resistor OFF
Start Node-RED
Start Node-RED:
Node-RED is part of Raspbian Stretch and Buster (with desktop and recommended software). You can use the node-red command to run Node-RED in a terminal or on the desktop via the 'Programming' menu.
Open the editor:
Once Node-RED is running you can access the editor in a browser. If you are using the browser on the Pi desktop, you can open the address: http://localhost:1880.
Simple RS485 Communication
In this example flow the Raspberry Pi will send the text 'Hello World' via the RS485 after pressing the inject button. The flow will receive incoming strings (terminated by \d) and show the string in the debugging window on the right side.
The communication will be realised by using the serial in and out nodes, which are pre installed. It is very important to set the properties of the Serial Port to /dev/serial0 as in the picture above.
You can test the flow with a connected PC (via an USB to RS485 adaptor) and a simple terminal program.
Downloads
MODBUS - Configuration 1
In the following steps I want to show you how to implement a simple Modbus RTU communication under Node-RED.
First we have to install additional Modbus nodes node-red-contrib-modbus via the palette manager or on the bash by entering:
npm install node-red-contrib-modbus
Now you can import the flow.
Downloads
Modbus Configuration 2
After importing the flow we can take a look in the configuration of 'Modebus write' and 'Modbus read' nodes. It is imortant to set the 'Server' property to dev/serial0 and configure it as shown in the pictures above.
Modbus Test
For the test I've connected an Arduino with RS485 Shield as Modbus slave (you can check this instructable for more information).
Modbus Read will poll Unit 1 all 2s and read 8 registers of the slave. You can see the result in the status of Modbus Response. Via the 2 injectors you can set the register 6 of the slave to 0 or 255.