[Solved] Client - Server Interface

I have two Arduino Ethernets that I need to speak with each other.

Unit 1, the server, is the user interface. It will be connected to a buzzer, LEDs, and some buttons.

Unit 2, the client, detects the state of some equipment and notifies the server of the equipment's status. Depending on the user input from the buttons on the server it will also need to send an output to reset the equipment. (There will actually be two of these, but I will start with 1:1 programming and then adapt for 1:2.)

I need to know:

  • How to establish the connection between the two devices (and automatically re-establish the connection if it is lost)
  • How to send/read commands between devices. These devices will only ever speak with each other, so binary or hex commands is fine.

Can you point me in the right direction?

if you look at the Ethernet examples, you have both a server and client sketches. If you connect the client to the servers IP address, you're good to go.

You can check the connection status with the connected() method.

If you make a direct ethernet connection between the two ethernet shields, you may need a crossover ethernet cable.

I was actually more interested in how to (efficiently) transfer data between the two devices. All the examples are geared towards the Controller<->Human interface (such as displaying info on a browser).

Do I just need to use .write() on the transmitter and .read() on the the reciever, or is there a better way of doing it?

Do I just need to use .write() on the transmitter and .read() on the the reciever

You could try that, and see that yes that is the correct approach.

Ok, so I'm trying to make the "Fade" example work across two boards over an ethernet connection. One board will do the math to calculate and send the value, the other board will recieve the value and do an analogWrite to it's LED.

I'm not 100% sure but it would seem that the units are not establishing a connection [LED 9 is not turning on after boot]. Can anyone offer input as to why that is?

Device 1

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

byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0xF5, 0xB2 };
IPAddress ip(10,0,5,121);
IPAddress subnet(255, 255, 252, 0);
int led = 9;
byte brightness = 0;    // how bright the LED is
byte fadeAmount = 5;    // how many points to fade the LED by

IPAddress server(10,0,5,120); // CCSO Controller Unit

// Initialize the Ethernet client library
// with the IP address and port of the server 
EthernetClient client;

void setup() {
  pinMode(led, OUTPUT);
  client.connect(server, 80);
  
  if(client.connected()){
    digitalWrite(led, HIGH);
  }
  else{
    digitalWrite(led,LOW);
  }
}

void loop() {

  client.write(brightness);
  
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  
  // wait for 30 milliseconds to see the dimming effect    
  delay(100); 
  
  
}

Device 2

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

byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0xF5, 0x98 };
IPAddress ip(10,0,5,120);
IPAddress subnet(255, 255, 252, 0);
int led = 9;

EthernetServer server = EthernetServer(80);

void setup() {
  Ethernet.begin(mac, ip, subnet);
  pinMode(led, OUTPUT); 
  
  server.begin();
}

void loop() {
  
  // wait for a new client:
  EthernetClient client = server.available();

    
   if (client == true) {
      analogWrite(led, client.read());  
    }
}

Could the problem have anything to do with my ethernet.begin order?

Syntax
Ethernet.begin(mac);
Ethernet.begin(mac, ip);
Ethernet.begin(mac, ip, dns);
Ethernet.begin(mac, ip, dns, gateway);
Ethernet.begin(mac, ip, dns, gateway, subnet);

Since I'm on a private network, I have no need for a dns or gateway; however I'm on an usual subnet. Do I need to include the dns and gateway in the Ethernet.begin sequence?

Update. I have my server working in an acceptable (though probably not opitimal) fashion. The code is below:

Server:

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

byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0xF5, 0x98 };
IPAddress ip(10,0,5,120);
IPAddress subnet(255, 255, 252, 0);
int led = 9;

EthernetServer server = EthernetServer(80);
String readString;

void setup() {
  Ethernet.begin(mac, ip, subnet);
  pinMode(led, OUTPUT); 
  
  server.begin();
}

void loop() {
  
  // wait for a new client:
  EthernetClient client = server.available();
 
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 
 
 //if HTTP request has ended
        if (c == '\n') {

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
        
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
        
          client.println("<H1>Arduino Audio Server is Working</H1>");     
         
          client.println("</BODY>");
          client.println("</HTML>");
         
          delay(1);
          //stopping client
          client.stop();
          
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(led, HIGH);
          }
          
          if(readString.indexOf("off") >0)//checks for on
          {
            digitalWrite(led, LOW);
          }
          
        }    //End c == '\n')
      }      //End if(client.available)
    }        //End While Client Connected
  }          //End if(client)
}            //End Loop

The long and short of it is from a browser I can do the following:
10.0.5.120 [displays webpage]
10.0.5.120/?on [turns LED on]
10.0.5.120/?off [turns LED off]

However my remote unit is getting stuck on the client.connect line. Is it wanting the server to send it back some information?

What do I need to do on my remote to have it send simple http commands?

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

byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0xF5, 0xB2 };
IPAddress ip(10,0,5,121);
IPAddress subnet(255, 255, 252, 0);
int led = 9;
byte brightness = 0;    // how bright the LED is
byte fadeAmount = 5;    // how many points to fade the LED by

IPAddress server(10,0,5,120); // CCSO Controller Unit

// Initialize the Ethernet client library
// with the IP address and port of the server 
EthernetClient client;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
 
  delay(1000);
 
  Serial.println("connecting...");
 
  client.connect(server, 80);
  Serial.println("connected, sending");
  client.println("10.0.5.120/?on");
  
 }

void loop() {
//loop code
}

Turns out the reason the client wasn't connecting was being I didn't start off with an Ethernet.begin() statement.

The device didn't know what network it was on so of course it couldn't connect.