communication Arduino2Arduino by ethernet

Hi it´s practicly my first post ,

Is it possible to connect by ethernet two arduinos? for example one arduino has a motion sensor and at the other end the other arduino has a LED, and when the motion sensor is triggered the LED turns on.

Well something like that... the only examples are by android or webpage but i was kind of trying to do it more direct... what would be the proggram?

Cumps

If you make a combination of the ChatServer/TelnetClient examples you more or less get what you're looking for. You might have to define a simple protocol for your task. A webserver/webclient setup is also possible but that might be a bit over-complex for your task.

but if i'm using an arduino YUN the chatserver code and telnet code would be very different right?

Please describe your setup (be as complete as possible)!

well the server would be something like this to start:

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;

void setup() {
Serial.begin(9600);
Bridge.begin();

server.listenOnLocalhost();
server.begin();

}

void loop() {
YunClient client = server.accept();

if (client) {
Serial.println(“Client Connected”);

but the client i can´t understand how to start it because the WEBClient Examples its different…for example how do i set the client server?

There's still no description of your setup. Is the second Arduino also a Yun?

Why do you call server.listenOnLocalhost()? That way you cannot connect from the other Arduino. Please use code tags for posting code and post complete sketches!

yes the second one is also a YUN...i used it because it tells the server to begin listening for incoming connections, in this cause the YUN client.

Maibe i'm not understanding the explanation...

listenOnLocalhost() make the server listening just on 127.0.0.1 (loopback interface) but not on the other IP it has. That doesn't make sense in your case. Just remove that line.

YunClient is the implementation of a simple TCP client. Just call connect with the hostname/IP address and the port you want to connect. In your case (you didn't specify the port for the server) you have to use Yun's default port 5555.

thanks… it would be something like this right?

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

IPAddress server(192,168,1,1);
//IPAddress ip(192, 168, 1, 2);
YunServer server;

void setup() {
Serial.begin(9600);
Bridge.begin();
server.begin();

}

void loop() {
YunClient client = server.accept();

if (client) {
Serial.println(“Client Connected”);

client.stop();
}

delay(50);

it would be something like this right?

No. That doesn't make sense. First of all you have two server variables with different meanings. Then you still have a server object and call accept on it.

It looks more like:

IPAddress server_ip(192.168.1.14); // must be the IP of the other Yun
YumClient client(Bridge);
if (client.connect(server_ip, 5555)) {
  // send some stuff to the server by client.write()
} else {
  // handle error
}

So the client code should be?
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

IPAddress server_ip(192,168,1,1);
YunClient client(Bridge); -------WHY THIS AND NOT “YunClient client”?

void setup() {
Serial.begin(9600);
Bridge.begin();
}

void loop()
{
if (client.connect(server_ip, 5555))
{
Serial.println(“connected”);-----I COULD SEND SOMETHING BUT FOR KNOW I JUST
} WANT TO SEE IF IT´S CONNECTED
else {
Serial.println(“connection failed”);
}
client.stop();
delay(500);
}

what i don´t understand is in CAPS on the description of the code…

Please learn how to use code tags. It's the button with the blue less than and greater than signs.

YunClient client(Bridge); -------WHY THIS AND NOT "YunClient client"?

You can leave the Bridge parameter away as it's the default anyway.

Make the delay bigger because that way it will connect every half a second. Apart from that your code looks OK. Are you sure that the other Yun got the IP 192.168.1.1?

So this code that looks OK is the client right?
And on the other YUN i got this code working as server:

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;

void setup() {
Serial.begin(9600);
Bridge.begin();
//Server.listenOnLocalhost(); make the server listening just on 127.0.0.1 (loopback interface)
server.begin();
}

void loop() {
YunClient client = server.accept();
if (client) {
Serial.println(“Client Connected”);
//Console.println(request);
//process(client);
client.stop();
}
delay(50);

That might work if the IP is correct.

You still didn't format the code with code tags. Please learn how to do that!

I would insert a client.println("Hello"); line to be able to check the server with your PC (that way you see something when you connected correctly).

Thanks!! it´s connecting now...

I can see on the server when the client is connected now...that line client.println("Hello") i put it in the client?

void loop() { if (client.connect(server, 5555)) { Serial.println("connected"); client.println("Hello") } else { Serial.println("connection failed"); }

that line client.println("Hello") i put it in the client?

No, it was meant for the server to be able to test with a PC. But if they connect, so that's dispensable now.

my question is... right now the code it´s always on a loop, but what if i have a montion sensor on the client and when its detected some movement it sends the signal to the server and this one turn a LED on

Is it possible to send that only when the system detects movement instead of always running on a loop?

Is it possible to send that only when the system detects movement instead of always running on a loop?

Of course this is possible. Just rename the loop routine to something different (p.e. "informServer") and create a new loop() that checks the sensor and in case of some movement calls that routine.

Is it possible to send that only when the system detects movement instead of always running on a loop?

You will probably need to add code to detect when the sensor is reset so the next new alert can be sent. If the sensor stays in alarm, then a new alert will be sent every time the sensor is checked, which could be many times a second.

thanks!! it´s working but the problem is that know i have a problem with the libraries because i´m using this:

#include <VirtualWire.h>
#include <Bridge.h>
#include <YunClient.h>

and this is the message error:

Bridge\Bridge.cpp.o: In function BridgeClass::crcCheck(unsigned int)': C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:50: multiple definition of crc16_update(unsigned int, unsigned char)’
VirtualWire\VirtualWire.cpp.o:C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:50: first defined here
Bridge\Bridge.cpp.o: In function String::operator+=(char const*)': C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:66: multiple definition of crc_xmodem_update(unsigned int, unsigned char)’
VirtualWire\VirtualWire.cpp.o:C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:66: first defined here
Bridge\Bridge.cpp.o: In function BridgeClass::transfer(unsigned char const*, unsigned int, unsigned char const*, unsigned int, unsigned char*, unsigned int)': C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:79: multiple definition of _crc_ccitt_update(unsigned int, unsigned char)’
VirtualWire\VirtualWire.cpp.o:C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:79: first defined here
Bridge\Bridge.cpp.o: In function BridgeClass::begin()': C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:90: multiple definition of _crc_ibutton_update(unsigned char, unsigned char)’
VirtualWire\VirtualWire.cpp.o:C:\Program Files (x86)\Arduino\libraries\VirtualWire/util/crc16.h:90: first defined here
collect2: error: ld returned 1 exit status