Bi-directional communication over ethernet.

So I’m fairly new to playing with Arduinos. I have played around enought to have a clue of some of the capabilities of these little gems but now I’m stuck.

Ok so I’m trying to get some direction on how to proceed getting this working. I have 2 Arduinos set up. Arduino A we will call the control station has an ethernet shield & 3 buttons and 3 LEDs attached to it. Arduino B is the panel side, it has an ethernet shield and is connected to a relay shield. It also has pins connected to an external device that act as inputs on the Arduino.

When button 1 is pushed on the control station I want it to send a signal to Arduino B to fire one of the relays. The external panel will then send back a signal to Arduino B as an input. Arduino B needs to send this back to the control station to light up the correct LED.

I have the basic sketches working with xbee radios and xbee shields but I can’t seem to get this converted to ethernet. Because of the distance and building construction the xbee’s are not a viable solution. What I’m stuck on is what ethernet library should I base the communication off of. Since I need to have bi-directional communication established, because the panel side doesn’t happen instantly it has to fire relays and then detect if the relays open before it sends the input back to the Arduino B. To also add a little bit to this the software that controls the panel may also fire the inputs so Arduino B needs to send that info to the control station without the control station starting the communication.

So I’m not sure I can do this with ethernet or not but any help or guidance in the right direction would be great. I can’t seem to find anybody that has used or has a good example of 2 Arduinos having bi-directional communication over ethernet.

Do you have original Arduino Ethernet Shields (I'm getting cautious about that by experience)?

I have the basic sketches working with xbee radios and xbee shields

Show us that code.

Did you look at the ChatServer example of the Ethernet library?

I have the Arduino Ethernet Shield R3. I'm not where I can test this again but I believe this was the last version of the code that I had working with the xbee radios & shields.

Serial Catchers Relay shield side

unsigned char relayPin[4] = {4,5,6,7};

#define LED 13   // the pin for the LED
#define Input1 8 // the input pin where the
                 // pushbutton is connected
int valIn = 0;     // val will be used to store the state
                 // of the input pin 

void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(Input1, INPUT);
pinMode(relayPin[0],OUTPUT);
pinMode(relayPin[1],OUTPUT);
pinMode(relayPin[2],OUTPUT);
pinMode(relayPin[3],OUTPUT);
}

void loop() {
  if (Serial.available()) {
    byte val = Serial.read(); // this will read from the xbee
    if (val == 'x') {
      digitalWrite(relayPin[3],HIGH);
      delay(1000);
      digitalWrite(relayPin[3],LOW);
    }
  
    if (val == 'c') {
      digitalWrite(relayPin[2],HIGH);
      delay(1000);
      digitalWrite(relayPin[2],LOW);
    }
  
    if (val == 'v') {
      digitalWrite(relayPin[1],HIGH);
      digitalWrite(relayPin[0],HIGH);
      delay(1000);
      digitalWrite(relayPin[1],LOW);
      digitalWrite(relayPin[0],LOW);
    }
  }
  valIn = digitalRead(Input1); // read input value and store it
  
  // check whether the input is HIGH (button pressed)
  if (valIn == HIGH) { 
    digitalWrite(LED, HIGH); // turn LED ON
    Serial.print('l'); //send back to the Pitcher
  } else {
    digitalWrite(LED, LOW);
    Serial.print('k'); //send back to the Pitcher
  }
}

Serial Pitcher Push Button

#define LED1 12   // the pin for the open LED
#define LED2 11   // the pin for the closed LED
#define BUTTON1 7 // the input pin where the
                 // pushbutton is connected
#define BUTTON2 8 // the input pin where the
                 // pushbutton is connected
#define BUTTON3 9 // the input pin where the
                 // pushbutton is connected
int val = 0;     // val will be used to store the state
                 // of the input pin 


void setup() {
Serial.begin(9600);
pinMode(LED1, OUTPUT);   // tell Arduino LED is an output
pinMode(LED2, OUTPUT);   // tell Arduino LED is an output
pinMode(BUTTON1, INPUT); // and BUTTON is an input
pinMode(BUTTON2, INPUT); // and BUTTON is an input
pinMode(BUTTON3, INPUT); // and BUTTON is an input
}

void loop() {
  if (Serial.available()) {
    byte valR = Serial.read(); // this will read from the xbee
    if (valR == 'l') {
      digitalWrite(LED1, HIGH); // turn LED ON
      digitalWrite(LED2, LOW); // turn LED OFF
      }
    if (valR == 'k') {
      digitalWrite(LED2, HIGH); // turn LED ON
      digitalWrite(LED1, LOW); // turn LED OFF
      }
    }
  
  val = digitalRead(BUTTON1); // read input value and store it

  // check whether the input is HIGH (button pressed)
  if (val == HIGH) { 
    Serial.print('x'); // send the value x to catcher 
    delay(500);
    }
  val = digitalRead(BUTTON2); // read input value and store it

  // check whether the input is HIGH (button pressed)
  if (val == HIGH) { 
    Serial.print('c'); // send the value c to catcher 
    delay(500);
    }
  val = digitalRead(BUTTON3); // read input value and store it

  // check whether the input is HIGH (button pressed)
  if (val == HIGH) { 
    Serial.print('v'); // send the value v to catcher 
    delay(500);
    }
}

I would make the catcher the server and the pitcher the client. Let the client establish a connection in the setup() and check it at every loop, reconnect if the connection drops. Once you have a connection you can use the EthernetClient objects mostly the same way as you use the Serial object in your current code.

Use the ChatServer and TelnetClient examples (of the Ethernet library) as templates to create your sketches.

Combined client/server test code.

//zoomkat 7-03-12, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 5 high/low
//use the ' in html instead of " to prevent having to escape the "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "web.comporium.net"; // (DNS) zoomkat's test web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address

String readString; //used by server to capture GET request 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin 5 selected to control
  Ethernet.begin(mac,ip,gateway,gateway,subnet); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("server/client 1.0 test 7/03/12"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) 
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call client sendGET function
    }
  }  

  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') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat");
            client.println();
            client.println();  
          }
          else {   
            client.println("HTTP/1.1 200 OK"); //send new page on browser request
            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>Zoomkat's simple Arduino 1.0 button</H1>");

            client.println("<a href='/?on' target='inlineframe'>ON</a>"); 
            client.println("<a href='/?off' target='inlineframe'>OFF</a>"); 

            client.println("<IFRAME name=inlineframe style='display:none'>");          
            client.println("</IFRAME>");

            client.println("</BODY>");
            client.println("</HTML>");
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
} 

//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}

Hi,

you can get it running easily using the vNet library. It build connection between nodes easily and in peer-to-peer, there are some example that will do what are you requiring.

This library is included into the Souliss project, but you can use the vNet library as standalone.

If you need more, please ask.

Regards,
Dario.