[SOLVED] Arduino Leonardo ETH has problem connecting to server as a client

Hello,

I’m very new to arduino and playing around with the network, although I have some experience in programming.

So I have two arduino leonardo eth, let’s call them arduino 1 and arduino 2.

Arduino 1 posts a string of data on an HTML page it hosts onto a server, with IP 172.16.51.162. This code works - I can connect to the page from my browser and see the data on the page.

Arduino 2 should read the string of data and store it. This is where the problem arises: arduino 2 doesn’t want to connect to the server.

The code for arduino 2 is the following:

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x11, 0x34, 0x86 };

IPAddress ip (172, 16, 49, 195);
IPAddress server(172, 16, 51, 162);

EthernetClient client;


void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Ethernet.begin(mac,ip);

  Serial.println("Connecting...");
  delay(2000);

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: 192.168.5.91");
    client.println("Connection: close");
    client.println();
  } 
  else {
    Serial.println("connection failed");
  }
}

void loop() {

}

The serial outputs “connection failed”.

Anyone knows what’s wrong? :slight_smile:

Show the working code you used for “Arduino 1” complete with the actual IP addresses. Don’t post a simplified version, post the actual version.

Okay, I’ve replaced the IP addresses, although I’m not sure of how it changes anything.

The code for my other arduino, stripped of all the things that I believe are not relevant:

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

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x11, 0x34, 0x5C
};
IPAddress ip(172, 16, 51, 162);
EthernetServer server(80);

void setup() {
  
  Ethernet.begin(mac, ip);

  server.begin();

}


void loop() {
    // --- SENDING IT ON THE SERVER ---
  
  EthernetClient client = server.available();
  if (client) {
    bool currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println("Refresh: 2");
          client.println();
          
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("Here is the string");
          client.println("</html>");
          
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }

}

If you want the full code,here it is (I’m reading data from sensors and publishing it):

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

const int trigPin = 7;
const int echoPin = 8;

const int trigPin2=4;
const int echoPin2=5;

long duration;
long duration2;

int distance;
int distance2;

bool objectDetected=0;
bool objectDetected2=0;


byte mac[] = {
  0x90, 0xA2, 0xDA, 0x11, 0x34, 0x5C
};
IPAddress ip(172, 16, 51, 162);
EthernetServer server(80);


void setup() {

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  
  Ethernet.begin(mac, ip);

  server.begin();
}



void loop() {
  // --- PULSE DETECTION ---
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);

  
  distance= duration*0.034/2;
  distance2= duration2*0.034/2;
  
  if (distance<50){objectDetected=1;} 
  else {objectDetected=0;}
  if (distance2<50){objectDetected2=1;} 
  else {objectDetected2=0;}



  // --- SENDING IT ON THE SERVER ---
  
  EthernetClient client = server.available();
  if (client) {
    bool currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println("Refresh: 2");
          client.println();
          
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");

          if (objectDetected==1 and objectDetected2==1){
            client.println("Object detected in Hookie1 and Hookie2");
          }
          else if (objectDetected==0 and objectDetected2==0) {
            client.println("Nothing to the horizon");
          }
          else if (objectDetected==1 and objectDetected2==0) {
            client.println("Object detected in Hookie1");
          }
          else if (objectDetected==0 and objectDetected2==1){
            client.println("Object detected in Hookie2");
          }


          
          client.println("</html>");
          
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }

}

The IP addresses of the client and server put them on different subnets. Are they supposed to be that way?
If so, you need to set the gateway.

No, there's no restrictions as to which subnet they should be on.

Okay so I checked which gateway/subnet they were affiliated to using the Serial.println(Ethernet.gatewayIP()) and Serial.println(Ethernet.subnetMask()) commands, which came out with the same results for both of the arduinos. So apparently they are on the same subnet, although I don't know anything about all that so I might be wrong. What makes you think they are on different ones?

Just to be sure I declared the gateway/subnet numbers into both programs in the same way that ip was declared, and then in the setup function for both programs, I called:
Ethernet.begin(mac, ip, gateway, subnet);
instead of:
Ethernet.begin(mac, ip);

However the result is exactly the same as above; the serial outputs "connection failed" when I try to run the code for the second arduino.

What would you advise?

Thank you for your help so far btw! Truly appreciated.

Is this a student lab?
Who gave you the IP addresses to use for the client and server?
Do the ethernet cables disappear into the wall or do you have a network switch on your desk?

Hello! I solved the problem so I thought I'd post here.

So the project was hosted on a university network, which made accessing the network pretty tricky and confusing because they put a lot of restrictions on it. After much faffing I ended up just hosting a local network from my computer, using a hub I found in a cupboard. The code worked perfectly!