Sending data between two arduino yun via wifi

I am new with the arduino yun and i have a problem: I want to send data from on Yun to an other (via wifi). Unfortunately it doesn't work.. I would be happy if you could help me :slight_smile: My code looks like this:
1st Yun:

#include <YunServer.h> 
YunServer server;
void setup() {
  Bridge.begin();
  server.begin();
}

void loop() {
  server.write(analogRead(A0)); // server should write this data to all connected clients
  delay(10000);
}

2nd Yun:

include <YunClient.h>
IPAddress server(192,168,0,101);  //Because Yun1 has this IP adress
void setup() {
  pinMode(13,OUTPUT);
  Bridge.begin();
}

void loop() {
  YunClient client;
  client.connect(server,5555));
  if(client.connected()){
    if(client.available()>0){
      int value = client.read();
      if(value> 25){
        digitalWrite(13,HIGH);
      }
      client.stop();
    }
  }
  delay(30);
}

It doesn't work how i want, but i hope you understand what i am trying to do. Is something like that even possible?
Would be great if you can give me some tipps!

Take a look at the TemperatureWebPanel example for the server part of your communication. You need to get a client object (yes, a client object on the server) using the server.accept() method.

Thank you a lot! your hint really helped me :slight_smile: i managed it to connect with the server, but an other problem popped up.
How can i write and read data from an client? it just doesn't work with client.write() and client.read()
This is my code:
Client:

void loop() {
  YunClient client;
  if(client.connect(server,5555)){
      //Console.println(analogRead(A0));
      client.write(42);
  }
  client.stop();
  delay(500);
}

Server:

YunClient client = server.accept();
  if(client){
   [color=red] Console.println(client.read()); // this should print out 42[/color]
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    client.stop();
  }

The problem is the red line in the code. It should print out 42, but it only prints -1 (no byte available). Can somebody tell me what i did wrong?

Thanks in advance

You didn't wait for the data to be transmitted. client.available() returns the number of bytes available in the input buffer. Try with a short delay and a while(client.available()) loop afterwards. Note that client.read() returns exactly one byte of data.

Thank you guys very much! now it works :slight_smile: here is the code for people with the same problem:
Server:

#include <YunServer.h> 
#include <YunClient.h>
#include <Console.h>
YunServer server;
void setup() {
  Bridge.begin();
  server.begin();
  Console.begin();
}

void loop() {
  YunClient client = server.accept();
  if(client){
    while(client.available()<1);
    Console.println(client.read()); // this should print out 42[/color]
    client.stop();
  }
}

Client:

#include <YunClient.h>
IPAddress server(192,168,0,100);  //Because Yun1 has this IP adress
void setup() {
  pinMode(13,OUTPUT);
  Bridge.begin();
}

void loop() {
  YunClient client;
  if(client.connect(server,5555)){
      client.write(42);
  }
  client.stop();
  delay(500);
}

How have you configured your wifi for Yun1 and 2?

  • ad-Hoc?
  • Host/Client?
  • ...?