Ethernet basics: IP configuration and a first TCP connection
Ethernet is the backbone of most plant networks. This lesson gives the PLC a stable address, confirms it is reachable, and opens a first TCP connection that you can test from your own computer. It is the foundation that Modbus TCP and MQTT build on in later courses.
What you will learn
- Choose between DHCP and a fixed IP address.
- Configure the Ethernet port and print the PLC address.
- Verify reachability with ping.
- Run a TCP echo server and test it from a PC.
You need: an M-Duino Ethernet model, an Ethernet cable, and a PC on the same network.
DHCP versus a fixed IP address
DHCP is the fastest way to get connected during development, the PLC requests an address automatically from your router. For a production deployment, a fixed IP address is almost always the better choice: the PLC's address does not change after a router reboot, and firewall rules and SCADA configuration stay predictable.
The MAC address
Every device on an Ethernet network needs a unique MAC address. Use the one printed on your PLC or given in its documentation. If you ever have to define one yourself, it must be unique on the network: two devices with the same MAC address cause intermittent, hard to diagnose connection problems.
#include <Ethernet.h>
// Replace with the MAC address of your PLC
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 50);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
Serial.print("PLC address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
}
Choose an address that is free on your network and in the same subnet as your PC. For DHCP, call Ethernet.begin(mac) without the address.
Confirming the PLC is reachable
Before writing any application logic, confirm the network layer works. From a PC on the same network, run ping 192.168.1.50 with the address you configured. If it does not respond, check the cable, the switch port, and that the PC and the PLC are in the same subnet before suspecting a code problem.
A first TCP server: an echo test
With the network layer confirmed, a TCP server that sends back whatever it receives is the simplest way to prove that a real connection works. This sketch listens on port 5000.
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 50);
EthernetServer server(5000);
void setup() {
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
EthernetClient client = server.available();
if (client && client.available()) {
client.write(client.read());
}
}
Test it from your PC
Open a terminal and connect with telnet 192.168.1.50 5000 (or nc 192.168.1.50 5000). Type some characters: each one comes back from the PLC. That is your first working TCP connection.
Later in this learning path you will use the same mechanism on port 502, the standard Modbus TCP port.
If the test fails
- No answer to ping: cable, switch port or wrong subnet.
- Ping works but the connection is refused: check the port number in the sketch and in your command, and any firewall on the PC.
- Intermittent behavior: look for a duplicated IP or MAC address on the network.
Next lesson: wireless options, a decision guide to read before ordering hardware. Take the quiz below first to check what you have learned.
There are no comments for now.