0

So I've got two arduinos Leonardo ETH, both plugged into a local network I host on my computer, and I'm trying to make them communicate through ethernet.

One of my arduinos is collecting data from 2 sensors and sending it on a server, the other one reading. I started by creating an HTTP page with the first and reading it with the second; it worked (more or less).

Then I realised it was overkill and decided to strip down my code to get TCP communication, rather than going through http. The arduino that sends data to the server now works really well; if I connect to the server through chrome the data gets updated each time I connect. But the one that reads it doesn't manage to connect to the server anymore.

Here is the code for the publisher (which seems to work):

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


bool objectDetected=0;
bool objectDetected2=0;


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

IPAddress ip(192, 168, 1, 30);
EthernetServer server(80);




void setup() {

  Ethernet.begin(mac, ip);

  server.begin();


//  Serial.begin(9600);
//  while (!Serial) {
//    ;
//  }
//  Serial.println("Foreign object detection started");

}



void loop() {
  // --- PULSE DETECTION ---
  // This part of the code gets data from the sensors and stores it in objectDetected and objectDetected2. I've removed the code for clarity as it has no impact on the question

  // --- SENDING IT ON THE SERVER ---
  EthernetClient client = server.available();
  if (client) {
    if (client.connected()){
      if (client.available()){
            client.print("Sensor1: ");
            client.print(objectDetected);
            client.println();

            client.print("Sensor2: ");
            client.print(objectDetected2); 
      }
    }



    delay(1);
    client.stop();
  }

}

And here is the code for the receiver (which doesn't seem to work):

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


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

IPAddress ip (192, 168, 1, 40);
IPAddress server(192, 168, 1, 30);

EthernetClient client;
TextFinder finder( client );



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

  Serial.println("Sensational Sensor Detection");

  Ethernet.begin(mac,ip);

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

  while (client.connect(server,80)!=1){
    Serial.println("Connection failed. Trying again...");
    delay(500);
  }
}

void loop() {


  long sensorValue=5, sensor2Value=5;
  int sensor3Value;

  while (client.connected()){
    if (client.available()){

      finder.find ("Sensor1: ");
      sensorValue=finder.getValue();

      finder.find("Sensor2: ");
      sensor2Value=finder.getValue();

      break;
    }
  }


  Serial.print("sensorValue: ");
  Serial.println(sensorValue);

  Serial.print("sensor2Value: ");
  Serial.println(sensor2Value);

  client.stop();

  delay(1000);

}

The code just prints "Connection failed. Trying again..." over and over.

If you can identify the problem... Please shout in my general direction! Thanks

  • you stop() the client and don't reconnect in loop(). and the server stops the connection too. – Juraj Sep 3 at 16:55
  • Yeah, but the client doesn't even get to stop(), it just loops through the while loop in the setup(). But I'll keep that in mind for the future. I tried the code without stopping the connection in the server code, same problem. – Alex Guut Sep 4 at 9:41
  • firewall on PC? – Juraj Sep 12 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.