Raspberry Pi - arduino uno Ethernet networking issues

I am working on automating a greenhouse. My plan is to use a Raspberry Pi 4 to interface with one or more Arduinos placed around the building to read temperature, humidity, and control fan motors.

EDIT: I am using an arduino uno and an ethernet shield 2

My problem is that I can't figure out how to set up the Arduino to connect to my raspberry pi through using TCP ethernet. The raspberry is my server and I have tested it communicating with my desktop, so I know that the RPi side is working.

Raspberry Pi Server python code:

import socket

HOST = '169.254.80.147' # server IP
PORT = 1024

msg = b'Welcome!'

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  s.bind((HOST, PORT))
  s.listen()
  conn, addr = s.accept()
  with conn:
    print('Connected by', addr)
    while True:
      data = conn.recv(1024)
      if not data:
        break
      print("Receved: ", data)
      print("Sent: ", msg)
      conn.sendall(msg)


Any help developing an Arduino client would be greatly appreciated.

have a look at Arduino ethernet reference and Client Connect which includes example code for a TCP client

I tried that example and it isn't connecting. How do I need to set up my server to communicate with the Arduino?

I have been trying to get the Arduino to connect to my raspberry pi, this is the program that I based on the Telnet client example.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xA6, 0x61, 0x0A, 0xAE, 0x74, 0x86 };
int port = 1024;
IPAddress ip(169,254,25,177);
IPAddress server(169,254,80,147);

EthernetClient client;

void setup() {
  Ethernet.init(10);
  Ethernet.begin(mac, ip);
  
  Serial.begin(115200);
  while(!Serial) {
    ;
  }

  Serial.print("client IP: ");
  Serial.println(ip);
  Serial.print("server IP: ");
  Serial.println(server);
  
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield not found.");
    while (true) {
      ; // do nothing, forever...
    }
  }
  while (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("No ethernet cable connected");
    delay(500);
  }
  delay(1000);
  Serial.println("Connecting");

  int c=client.connect(server, port);
  if (c) {
    Serial.println("connected");
  } else {
    
    Serial.println("Connection failed");
  }


  Serial.print("local IP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("remote IP: ");
  Serial.println(client.remoteIP());
  Serial.print("remote port: ");
  Serial.println(client.localPort());
  Serial.print("local port: ");
  Serial.println(client.localPort());
  Serial.print("connect feed back: ");
  Serial.println(client.connect(server, port));
}

void loop() {
  if (client.available()) {
    client.print("Hello World");
    char c = client.read();
    Serial.print(c);
  }

 if (!client.connected()) {
  Serial.println();
  Serial.println("Disconnecting");
  client.stop();
  while (true) {
    delay(1);
  }
 }
}

Here is the serial monitor output.

client IP: 169.254.25.177
server IP: 169.254.80.147
No ethernet cable connected
No ethernet cable connected
Connecting
Connection failed
local IP: 169.254.25.177
remote IP: 0.0.0.0
remote port: 0
local port: 0
connect feed back: 0

Disconnecting

I don't understand why the local and remote ports are 0 and the remote IP is 0.0.0.0 when I set it to 169.254.80.147.

Can anyone explain what I am doing wrong?

How did you choose those IP addresses?

The server IP is the one that the raspberry Pi already had, I think it was through DNS. I just chose one simmer to my desktop's ethernet IP for the arduino.

The 169.254.0.0/16 network is reserved for autoconfiguration uses. You shouldn't use these addresses as predefined values in a fixed network!

Setting the static IP on the Raspberry Pi to 192.168.0.220/24 didn't change what the Arduino does.

What is the ip of your router?

I don't have a router in the network. The Arduino is connected directly to the Raspberry Pi with an ethernet cable.

I have the router IP set to the same IP as the raspberry ethernet IP right now.

Shirley, you can't be serious!

How do I need to set up the IP addresses? I don't really know much about networking and ethernet.

Hard to say. One thing to note is that an ethernet cable won't connect two devices. You would need a patch cable, which you likely don't have.

A switch or a router would make your life easier.

I did have the Raspberry PI connected to my desktop. I am planning to get a switch.

How does your desktop connect to the internet? What is its IP?

It's connected with wifi, the ethernet IP is 169.254.25.128

Did you change the IP on the Arduino too?

You desktop PC probably supports Auto-MDIX so you can connect it to a switch/router and to another computer directly, tha network hardware will swap the pins accordingly. The Raspberry Pi doesn't support that feature, so you must use a cross-over cable to connect the Arduino (which also doesn't support Auto-MDIX) to the Raspberry Pi. You can get such a cable in your computer store but you probably get a used switch on ebay for less money.

That's most probably the IP of the wired interface which you didn't configure. As the operating system didn't find a DHCP server on that connection it configured the autoconfiguration IP.
Networking isn't easy. If you do exactly what the manual tells you and the manual situation matches your setup you're fine. But if not, you have to know quite a bunch to get networking to do what your intention were.

You'll make your setup much easier if you get a switch to connect the devices as you don't have to fiddle with special cable types and connect by simple patch cables (standard Ethernet cables).

Yes, I did change the Arduino IP address.

I have a switch coming today or tomorrow.

If it doesn't work with the switch, post as much details about the network you span with the switch as possible. List all connected devices, the IP address of every device as well as the rest of the network configuration (netmask, gateway, dns, etc.).

BTW: The autoconfiguration network (169.254.x.x) is a class B network (/16), but the Arduino libraries default to class C networks (/8) so you must provide a network mask to the constructor to use these addresses.