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.
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.
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).
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.