3

How can I connect and send data between two Arduinos using ENC28J60 ethernet adapters on both of them? Is there any guide or example code for me to take a look from or any library I can use to achieve this communication?

I have one Arduino connected to an ultrasound sensor and the other to a relay module so that one Arduino measures the distance between certain objects and send the distance to the other Arduino, which then processes it to check if it is within a certain limit. If not then it turns on the relay. I could have used two ESP32 with one in AP mode but the range is not enough and serial communication is not possible, as the cables are fragile and the distance is too large. Also I couldn't find another way of sending the data.

I'm open to any suggestion, even changing the method of communication. Or using of any other ethernet module.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Apr 9, 2021 at 17:06

1 Answer 1

0

You can use my EthernetENC library. See the examples of Arduino Ethernet library on how to use Ethernet libraries for Arduino.

Update 2025: Use my EthernetESP32 library which integrates with the ESP32 TCP/IP stack.

EthernetClient object wraps a TCP socket. A normal TCP socket is connected to IP address and port. EthernetServer listens on a port. If server is contacted by a remote client socket, it creates a local socket connected with the remote client socket on a free port and returns a EthernetClient object wrapping the socket. Everything you write or print to a EthernetClient is send to that one remote socket.

If the client board creates a EthernetClient and connects it to IP address and port of the EthernetServer on your 'server' board, then you get there a EthernetClient from server.available() and this two EthernetClient objects are connected. What you write/print on one side you read only from the EthernetClient object on the other side.

client socket

 if (client.connect(serverIP, PORT)) {
 client.print("request\n");
 String response = client.readStringUntil('\n');
 Serial.println(response);
 client.stop();
 }

server side

 EthernetClient client = server.available();
 if (client && client.connected()) {
 String request = client.readStringUntil('\n');
 Serial.println(request);
 client.print("response\n");
 client.stop();
 }
answered Apr 9, 2021 at 18:10
1
  • 1
    You will need a crossover cable. Commented Sep 26, 2023 at 23:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.