Hi,
I'm having problem to connect directly to my laptop an ethernet shield, with a crossed RJ45.
I have created a client on my leonardo, and a java server, on which it connects, in order to get the signal of a sensor.
What is odd, is that when I connect the Ethernet Shield on my box, it works correctly.
I have have also created a Java client on my laptop, and a server on my leonardo, and this works in all cases.
Here is my Java server :
import java.io.*;
import java.net.*;
public class Serveur {
public static void main(String args[]) throws IOException{
int port=24000;
ServerSocket sersoc = new ServerSocket(port);
System.out.println("Server listening port : "+port);
Socket soc=sersoc.accept();
InputStream stream=soc.getInputStream();
BufferedReader entree = new BufferedReader(new InputStreamReader(stream));
String message;
while(true){
message=entree.readLine();
System.out.println("Message received : "+message);
}
}
}
My Arduino client
#include <SPI.h>
#include <Ethernet.h>
/* Détails technique de la connexion ethernet */
byte mac[] = {0x00, 0x90, 0xF5, 0xE5, 0x2E, 0x42 };
//byte ip[] = {192,168,1,200 }; // Here are the addresses to connect on wifi box, it works perfectly
//byte server[] = {192,168,1,42};
byte ip[] = {169,254,192,124}; // The adresses used to connect directly with RJ45 on laptop
byte server[] = {169,254,192,123};
const int capteurIR = 3;
const int ledV = 9;
const int ledR = 8;
int etat=HIGH;
boolean b=false;
EthernetClient client;
void setup(){
pinMode(ledV, OUTPUT);
pinMode(ledR, OUTPUT);
digitalWrite(ledV,HIGH);
digitalWrite(ledR,HIGH);
attachInterrupt(0, react, FALLING); // Utilisation interruption 0, ie. la broche 3
Ethernet.begin(mac, ip);
client.connect(server, 24000);
delay(5000);
if (client.connected()) {
client.println("Arduino Connecte");
etat=LOW;
digitalWrite(ledV,etat);
}
else{
digitalWrite(ledR,LOW);
}
}
void loop(){
digitalWrite(ledV,etat);
if(b){
b=false;
client.println("Objet détecté");
}
}
void react(){
etat=!etat;
b=true;
}
And also my client on Java and server for Arduino
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class Client {
public static void main(String args[]) throws IOException{
String hote = "169.254.192.124";
int port =18001;
Socket soc =new Socket(hote, port);
PrintStream writer = new PrintStream(soc.getOutputStream());
while(true){
Byte c;
Scanner s=new Scanner(System.in);
System.out.println("Order?");
System.out.println("1-Led on");
System.out.println("2-Led off");
System.out.println("Else-Quit");
c=s.nextByte();
if(c==1){
writer.write(0x31);
writer.flush();
}
else if(c==2){
writer.write(0x32);
writer.flush();
}
else{
System.out.println("Disconnection");
break;
}
System.out.println("Message sent to server");
}
}
}
#include <Ethernet.h>
/* Détails technique de la connexion ethernet */
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0xC0, 0x54 };
//byte mac[] = {0x00, 0x90, 0xF5, 0xE5, 0x2E, 0x42 };
byte ip[] = {169,254,192, 124 };
//byte gateway[] = {192,168,1, 1 };
// Attachement d'un objet "server" sur le port 1337
EthernetServer server(18001);
void setup()
{
// Configuration de la ethernet shield et du server
//Ethernet.begin(mac, ip, gateway);
Ethernet.begin(mac, ip);
server.begin();
// Mise en sortie de la broche avec notre led (par défaut allumé)
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
}
void loop()
{
EthernetClient client = server.available();
if (client && client.connected()) {
if (client.available() > 0) {
switch(client.read()){
case '1': // led on
digitalWrite(9, LOW);
break;
case '2': // led off
digitalWrite(9, HIGH);
break;
}
}
}
}
Has I said, everything works correctly, except when I use a client on Arduino, a server on my laptop, and the arduino and laptop are directly connected by RJ45. It must be an IP adress problem, but I tried many things by modifying IP addresses, and nothing worked.