communication between arduino over ethernet

hi there,
i want to communicate between 3 arduino's over ethernet.

example:

when i press button 1 on arduino #1 ==> LED 1 on arduino #2 will go on, and then disconnect from the arduino (if the connection stay open i cannot send commands)

the same to another arduino

some one can help me?

eyal.

i want to communicate between 3 arduino's over ethernet.

You seem to have a fundamental misunderstanding about how devices communicate over networks.

Each device is either a client, requesting information, or a server, providing information.

Now, picture one of your Arduinos with ethernet shield. Is it going to be a client, asking a server for information (the GET request can contain information needed for the server to fulfill the request)? Or, is it going to be a server, responding to the GET request?

Or, is it going to fulfill both roles, one at a time?

Keep in mind that a server can NOT push information to clients. Clients can ONLY pull information from servers (with the ability to push information to the server).

when i press button 1 on arduino #1 ==> LED 1 on arduino #2 will go on, and then disconnect from the arduino (if the connection stay open i cannot send commands)

the same to another arduino

How does Arduino #1 know which other Arduino to send a request to? How is the LED going to disconnect? How do expect it to stay on if it disconnects?

arduino #1

i want to write a code that when button 1 is pressed
he will make http connection the the other arduino let say
my problem is that i dont know how to tell arduino #1 the browse to this adress and than disconnect after led1 powerd up

if (digitalRead(20) == HIGH)
{
....... http://192.168.1.3/led1on
(192.168.1.3 = arduino #2)
}
arduino #2

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 3);
String readString = String(100); //string for fetching data from address
EthernetServer server(80);

EthernetClient client = server.available();
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
readString = readString + c;
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("");
client.println("");
{
if (readString.indexOf("/led1on")>0)
{
Serial.println ("LED 1 ON");
client.println("Content-Type: text/html");
client.println();
digitalWrite(3,HIGH)
break;
}
client.println("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
readString = "";
start_pos = 0;
stop_pos = 0;
}

}

String readString = String(100); //string for fetching data from address

The readString instance is given an initial value of "100". How useful is that? Why "100"? Why not "banana" or "tuna fish"?

my problem is that i dont know how to tell arduino #1 the browse to this adress and than disconnect after led1 powerd up

Arduino #2 is acting as a server. Arduino #1 needs to act as a client. The GET requests it makes are to a specific IP address. The Arduino as server will drop the connection as soon as it has sent all data. The Arduino as client will disconnect as soon as it has read the whole response.

i know that, but i dont know how to write this code?

i want several buttons that will make http request to several adresses led1 , led2 , led3?

i want several buttons that will make http request to several adresses led1 , led2 , led3?

Don't you know whether you want them?

Which of your three Arduinos is going to be the server? What code is running on that server?

Which of your three Arduinos are going to be the clients? What code is running on those clients?

I've never tried this, but how about using the udp commands to do this. It looks like you could send some little packets between your arduinos that way, set the content however you like to represent commands to turn leds on and off.

I WANT TO CONNECT TWO ARDUINO USING ETHERNETSHIELD I AM NOT ABLE TO MAKE BIDIRECTIONAL COMMUNICATION USING SERVER CLIENT METHOD BETWEEN ARDUINOS.PLEASE SOMEBODY HELP ME

PLEASE SOMEBODY HELP ME

STOP SHOUTING!

What code is running on each Arduino? Are you able to connect to other server from your client(s)? Are you able to connect to your server(s) from other computers? How are the Ethernet shields connected?

i have two arduino mega with ethernet shield
arduino #1 (192.168.2.20)
the code of this arduino is written below
it acts as a server
clients browse to port 80
and they can control him

but if pin 7 is pressed
he will open http get (http://192.168.2.22:94/led1on)
it doesnt work connection fails
and port 80 on this arduino stop working

can someone help me plz?

arduino #2 (192.168.2.22)
this arduino acts as a server
if this arduino get a connection
http://192.168.2.22:94/led1on
it turn led1on
http://192.168.2.22:94/led1off
it turn led1off

this part is working perfect

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBB, 0xAF, 0xFE, 0xED};
String request ="";
IPAddress myDns(8,8,8,8);
EthernetClient client2;
EthernetClient client;
EthernetServer server(80);
IPAddress arduino2(192,168,2,22);
IPAddress ip(192,168,2,20);
String readString = String;
boolean lastConnected = false; // state of the connection last time through the main loop
void setup() {
Serial.begin(9600);
delay(1000);
Ethernet.begin(mac, ip, myDns);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
readString = readString + c;
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("");
client.println("");
if (readString.indexOf("/alive")>0)
{
Serial.println ("i am in alive");
//client.println("Content-Type: text/html");
client.println();
client.println("I Am Alive :- ) ");
client.println("I Am Alive :- ) ");
client.println();
break;
}
client.println("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();

Serial.println("client disonnected");

readString = "";
}

if (client2.available()) {
char c2 = client2.read();
Serial.print(c2);
}

if (!client2.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client2.stop();
}

if (digitalRead(7) == HIGH)
{
Serial.println (digitalRead(7));
httpRequest("/entrencelighton");

}

}

static String httpRequest(String request) {
if (client2.connect(arduino2, 94)) {
Serial.println("connecting...");
client2.println("GET " + request + " HTTP/1.1");
client2.println("User-Agent: arduino-ethernet");
client2.println("Connection: close");
client2.println();
delay(100);

}
else {

Serial.println("connection failed");
Serial.println("disconnecting.");
client2.stop();
}
}

Despite typically confused info being provided by some, below is combined client/server code that will run on an arduino.

//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();

}

hello this sketch allows communication between two Arduino Ethernet?

hello this sketch allows communication between two Arduino Ethernet?

Possibly.

If one of the Arduinos is a server, the other can connect as a client.

Of course, each Arduino can be a server (for the other Arduino) and a client (to get data from the other Arduino).