Before you write a TCP client, give the device a fixed IP address, so that the server and the client always find each other. On Raspberry Pi OS Bookworm and Trixie, you do it with nmcli and check it with ip a. The complete method, also for older systems and Wi-Fi, is in the static IP guide.
This is part 1 of a two-part tutorial. Here you plan the network and give a fixed address to a TouchBerry Pi Panel PC or a Raspberry PLC. In part 2 you write the TCP client in Node.js.
The first version of this tutorial used a TouchBerry Pi 3, now discontinued. The steps are the same on the current TouchBerry Pi (Raspberry Pi 4) and on any Raspberry PLC, because both run Raspberry Pi OS (Debian).
- Step 1Plan the networkAddress of the client and of the server
- Step 2Fix the addressnmcli on Bookworm and Trixie
- Step 3Check itip a and ping
- Step 4Write the clientPart 2, with Node.js
Why a TCP client needs a static IP address
By default, the Raspberry Pi gets its address from the router by DHCP, and it can change after a reboot. A server that only accepts known clients, a firewall rule or a SCADA that reads the device by IP stops working when the address changes.
It is like a phone number that changes every day: nobody can call you. A fixed address on each device avoids it.
Step 1: plan the network of the tutorial
The two parts of this tutorial and the Node.js TCP server use these addresses. Change them to match your network:
| Device | Role | Address |
|---|---|---|
| TouchBerry Pi or Raspberry PLC | TCP client | 192.168.1.25/24 |
| TouchBerry Pi, Raspberry PLC or PC | TCP server, port 5566 | 192.168.1.30/24 |
| Router | Gateway and DNS | 192.168.1.1 |
Both devices must be in the same subnet. Choose addresses outside the DHCP range of the router, so that no other device receives them.
Step 2: give the client a fixed address
On Bookworm and Trixie, NetworkManager manages the network. The Ethernet connection of eth0 is usually called Wired connection 1; check it with nmcli connection show. Then set the address and activate the connection:
sudo nmcli connection modify "Wired connection 1" ipv4.method manual ipv4.addresses 192.168.1.25/24 ipv4.gateway 192.168.1.1 ipv4.dns 192.168.1.1
sudo nmcli connection up "Wired connection 1"
Other cases. The nmtui menu, Wi-Fi, dhcpcd.conf on Bullseye and older, going back to DHCP and common errors are in the complete guide: how to set a static IP on an industrial Raspberry Pi.
A Raspberry PLC or a TouchBerry Pi also answers at the factory address 10.10.10.20 on eth0. To change it, see how to change the static IP of a Raspberry PLC.
Step 3: check the address
ip a show eth0
ip route
ping -c 3 192.168.1.1
Look for inet 192.168.1.25/24 in the output of ip a show eth0. ip route must show default via 192.168.1.1, and the ping to the router must get replies. Do the same on the server with its own address.
Next step: the TCP client
- Part 2: create a TCP client with Node.js
- TCP server with Node.js, the other end of the connection
- TCP server on an Arduino PLC, if the server is an M-Duino
The TouchBerry Pi Panel PC comes in 7", 10.1" and 18.5" with Gigabit Ethernet. The Raspberry PLC family has two Ethernet ports and Debian Bookworm 64-bit preinstalled.
Take it further
A fixed address is the first step to connect a panel or a PLC with the rest of the machine. These pages show it in a real project.
Case study
Water bottling with a TouchBerry Pi Panel PC
A TouchBerry Pi 7" runs a water bottling station as PLC and HMI: conveyor, sensors, cylinder and pump on its own I/O.
Read the case study
PLC Solution Library
TCP socket between Python and Node-RED on a Raspberry PLC
A Python control process and a Node-RED dashboard exchange encoder data and start, stop and exit commands over TCP.
See the example