Go Down

Topic: 2 arduinos communicating over internet from different LAN's (Read 2884 times) previous topic - next topic

JT1777

Hi All

i have 2 arduinos (1 arduino UNO and 1 arduino MEGA, both have ethernet shields) how could i get them to communicate with each other so that if the MEGA gets a message on its serial port a light comes on on the UNO and visa versa

thanks all for any help given

J

PaulS

Quote
i have 2 arduinos (1 arduino UNO and 1 arduino MEGA, both have ethernet shields) how could i get them to communicate with each other so that if the MEGA gets a message on its serial port a light comes on on the UNO and visa versa

Ethernet and serial are completely separate channels of communication. You would need both Arduinos to act as both client and server, and to use an intermediate server as data repository, so that each client has a place to look for data and each server has a place to put data.

fuh

Using ethernet for that is an overkill..
Use i2c between the two boards.. when board A receives something on the serial interface it forwards an opcode to board B over the i2c bus and board B should react to that opcode by turning a LED on or world domination.. is up to you. :-)
http://creativecommons.org/licenses/by-nc-sa/3.0/

johnwasser

Communicating between different LAN's will require port forwarding on the router on the Server side.  The Client needs to know the WAN IP Address of the router at the Sever side and the port number to connect to.  The router on the Client side makes the outgoing connection.  The router on the Server side receives the connection request, looks the port number up in the port forwarding table to find the LAN address of the Server, and forwards the connection request to the Server.  Responses from the Server go to the Server-side router which forwards them to the Client-side router, which forwards them to the Client that initiated the connection.

See PortForward.com for instructions for many commercial router models if you don't know how to configure your Server-side router.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

johnwasser


Using ethernet for that is an overkill..
Use i2c between the two boards..

If the Arduinos are on separate LANs it's likely they are in separate buildings.  Probably too far for I2C which shouldn't be pushed much more than 10 inches (it's designed for communication between chips on a board).
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

fuh


Probably too far for I2C which shouldn't be pushed much more than 10 inches (it's designed for communication between chips on a board).


Unless you use a i2c buffer.
But in general you're correct if they're on different subnets then is quite probable for them to be far away from each other.
http://creativecommons.org/licenses/by-nc-sa/3.0/

JT1777

thanks for all the help guys!

i will try the port forwarding idea and let you guys know how it goes!

Thanks!

J

P.S. any example code would be great thanks!!!!

fuh

I think you're not understanding the concept..
Port forwarding is something not related with arduino.. it's your router's job to forward a port on the WAN side to the LAN in your arduino board. Unless you want two way communication over the WAN then the port forwarding should only be active on the receiver arduino.
http://creativecommons.org/licenses/by-nc-sa/3.0/

zoomkat

Quote
i have 2 arduinos (1 arduino UNO and 1 arduino MEGA, both have ethernet shields) how could i get them to communicate with each other so that if the MEGA gets a message on its serial port a light comes on on the UNO and visa versa


The below test code might give you a start. It contains both client and server functions. When a "g" is sent to the arduino via the serial monitor, the client part will perform a get request to my server test page and display the returned results in the serial monitor. When a browser makes a request to the arduino server, the server sends a page that allows a pin on the arduino to be controlled via the browser web page. You should be able to modify the code communicate between two arduinos on your lan.

Code: [Select]

//zoomkat 12-08-11, 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 4 high/low
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
IPAddress ip(192,168,1,102); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress myserver(208,104,2,86); // zoomkat web page
EthernetServer server(84); //server port
EthernetClient client;
String readString;

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

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  Ethernet.begin(mac, ip, subnet, gateway);
  server.begin();
  Serial.begin(9600);
  Serial.println("server/client 1.0 test 12/08/11"); // 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 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
            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 src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
            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(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

//////////////////////////
void sendGET() //client function to send/receie GET request data.
{
  if (client.connect(myserver, 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();

}


Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

JT1777

is that example code for use on both arduinos and yes i was aware that port forwarding is to be done on the router and i do know how to do that! (sorry if i sound rude or arrogant)

thanks

J

zoomkat

Quote
is that example code for use on both arduinos


Yes. You would need to modify the code from what it does now to communicate between two arduinos instead of between an arduino and typical server. Below is a possible test setup using a pc and router.

pc<=(serial)=>arduino<==(http via router)==>arduino<=(serial)=>pc
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

lemming

/
Quote
i was aware that port forwarding is to be done on the router and i do know how to do that


try http://portforward.com (don't bother with their software as it is easy to do it manually)

there are also youtube videos and other sites with guides. Google is your friend.

Taher

hi everyone
i am new to this field.
i want my Arduino to communicate with another Arduino using Ethernet. i want to send some command from 1 Arduino to control some application on another Arduino. please somebody tell me how can i use information of this discussion.
taher

PaulS

Quote
please somebody tell me how can i use information of this discussion.

Hijacking threads all over the place is not the way to get answers. Create you own thread.

eyal-h

this is the same thing i am trying to do

did it work for you?

the arduino answered your clients and was able to be a client to the other arduino at the same time?

Go Up