Pages: [1]   Go Down
Author Topic: problem communicating between arduino's over ethernet  (Read 1626 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Ethernet.begin(mac, arduino2, 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("<!DOCTYPE HTML>");
          client.println("<html>");
      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("</html>");
          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();
  }
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 650
Posts: 50833
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
EthernetServer server(80);
You are running your server on port 80.

Quote
but if pin 7 is pressed
he will open http get (http://192.168.2.22:94/led1on)
and trying to access it on port 94. Of course the connection will fail.

Quote
and port 80 on this arduino stop working
Port 80 on which Arduino? What proof do you have to offer that port 80 stopped working?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

arduino #1 answer clients on port 80

arduino #2 answer clients on port 94
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6173
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
this part is working perfect
I doubt it. Why are you calling Ethernet.begin() twice?
Code:
void setup() {
  Serial.begin(9600);
  delay(1000);
  // any reason you are calling this twice?
  Ethernet.begin(mac, ip, myDns);
  // this second call will be the ip (arduino2 = 192.168.2.22)
  Ethernet.begin(mac, arduino2, myDns);
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}
If that is true, then why are you requesting the page from the same ip?
Code:
static String httpRequest(String request) {
  // This is connecting to 192.168.2.22.
  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);

  }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i dont understand
i use different ip's

arduino #1 ip is: 192.168.2.20 listen to port 80

arduino #2 ip is: 192.168.2.22 listen to port 94
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6173
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In which device is that code you posted above? In the future, please enclose the code in the code tag so we can tell which code is which.

Why are you calling Ethernet.begin() twice? That is my first question. That code ends up with 192.168.2.22 assigned. Then you try to request a webpage from the same IP. The way the ethernet library works, it won't answer a request from itself.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i tried with and without the decleration of "Ethernet.begin(mac, arduino2, myDns);"

--- i have deleted this line from my code

  the 2 arduinos connected to the same network
   i want arduino #1 to act as a server and when button is pressed i want him to create http get request to arduino #2 his ip is 192.168.2.22 and his port is 94
on arduino number two there is a webservercode that will light on led 1 when this url is entered
http://192.168.2.22:94/led1on

the connection always fails

can you understand what is my problem?

Code:
#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("<!DOCTYPE HTML>");
          client.println("<html>");
      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("</html>");
          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();
  }
}

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6173
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK. That is now 192.168.2.20. This is Arduino #1.

Let's see the code from the other device (Arduino #2).

edit: Before you even post that code, insure the mac addresses are different on each device. Otherwise, you will have a real problem with routing.

« Last Edit: August 09, 2012, 06:57:46 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

arduino #2 code
Code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,22);
String readString = String(100); //string for fetching data from address
EthernetServer server(94);
void setup() {

  Serial.begin(9600);

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
     
       pinMode(7, OUTPUT); //led on
}


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

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
             if (readString.indexOf("/led1on")>0)
                        {
                          client.println("Led 1 ON");
                          client.println();
                          digitalWrite(7,HIGH);
                          break;           
                        }                                   
                       else
                       {
                         break;
                       }
          client.println("</html>");
          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 = "";
  }

}

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6173
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you ping both devices from a computer on that localnet? No other duplicate ips on that localnet?
ping 192.168.2.20
ping 192.168.2.22
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ping is working

when i use each code separately on arduino #1 it works perfect

when i upload only the client code to arduino #1 the led on arduino #2 powerd  on

when i upload only the server code to arduino #1 it answeres the requests from the clients

but when i use them together the client does not work and after i press the button the server on arduino #1 stop answering http requests
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i think you helpd me to find my problem
the two arduino's have different mac adress
 0xDE, 0xAD, 0xBB, 0xAF, 0xFE, 0xED};
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }
but when i changed more then one of the letteres in the mac address it started to work

i will try to check my full code on it

thankyou very much





Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In regard to Michael Margolis's book (Arduino Cookbook), there are a couple of programs on page 500 (give or take) of the book.  Does anyone know if these time type sketches will be forward compatible with ipv6 whenever it gets fully implemented?  I'm not sure that there will be a huge snag issue surrounding the upgrade, but I'm not sure that there will NOT be one either???
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, you will need an ethernet shield. There are a few on the market, such as the original version by the Arduino team, and others from the usual retailer.By this you can escape problem communicating between arduino's over ethernet.
Logged

Pages: [1]   Go Up
Jump to: