Connection between 2 Arduino UNO+Ws5100 over Router line

I have the following project.

TWO Arduino's + Ws5100 Ethernet shields.

They are 2 routers in the forest 2km away, which have a connection established between them, but they are not connected to the outside world.

How can I communicate(send sensor data), or any example Arduino + shield on one end to connect to the Arduino+Shield on the other.

Thank you in advance.

If it is direct link, like you are still in LAN network (not connected via internet), you can connect to Arduino's private IP address inside of LAN network. If these routers are connected via internet, you can connect to its public IP, and then you can do port forwarding of that port - for instance 80 that will make available that Arduino webserver accessable from internet.

You can send datas like:

  • Client --> Server via HTTP POST or HTTP GET method. It is using TCP mechanism.
  • or you can use some UDP Send and Receive String examples to a specific port, where Arduino is listening.

EthernetClient object wraps a TCP socket. A normal TCP socket is connected to IP address and port. EthernetServer starts a listening socket on a port. If server on listening socket 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 one of your client boards 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();
   }

Thx for the quick response!

BTW is that the proper way of initializing the Client and Server?

CLIENT:

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress server(192, 168, 0, 50);            // IP address of the server
IPAddress gateway(192,168,0,1);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network
IPAddress dns(192,168,1,26);

EthernetClient client;


....
void Setup()
{
...
Ethernet.begin(mac, server, dns, gateway, subnet);
}

SERVER:

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress server(192, 168, 0, 50);            // IP address of the server
IPAddress gateway(192,168,0,1);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network
IPAddress dns(192,168,1,26);

EthernetServer server(80);

....
void Setup()
{
...
Ethernet.begin(mac, server, dns, gateway, subnet);
}

VS_HVAC:
Thx for the quick response!

BTW is that the proper way of initializing the Client and Server?

CLIENT:

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress server(192, 168, 0, 50);            // IP address of the server
IPAddress gateway(192,168,0,1);          // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network
IPAddress dns(192,168,1,26);

EthernetClient client;




SERVER:



byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress server(192, 168, 0, 50);            // IP address of the server
IPAddress gateway(192,168,0,1);          // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network
IPAddress dns(192,168,1,26);

EthernetServer server(80);

the library has examples in IDE examples menu.