Question

I have several problems trying to send messages between arduino and java. The arduino has an ultrasonic sensor and I want to send the distance detected by the sensor to java.

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

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress serverIP(192,168,1,3);
int serverPort=8887;
EthernetClient client;

int msg;

void setup() 
{  
 Serial.begin(115200); 

if (Ethernet.begin(mac) == 0) 
{
  Serial.println("Error con conexion DHCP");
  Serial.println("Pasamos a direccion estatica");
  IPAddress IP(192,168,1,177);
  Ethernet.begin(mac,IP,gateway,subnet);
}
 delay(1000);
 Serial.println("connecting to Server ...");

// if you get a connection to the Server
if (client.connect(serverIP, serverPort)) {
  Serial.println("Conectado");//report it to the Serial
  String msg="Hola, servidor";//Message to be sent
  Serial.println("sending Message:"+msg);//log to serial
  client.println(msg);//send the message
}
else {
 // if you didn't get a connection to the server:
 Serial.println("connection failed");
}
}
void loop()
{
delay(50);                    
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
int msg=uS / US_ROUNDTRIP_CM;
Serial.print("Ping: ");
Serial.print(msg); 
Serial.println("cm");
client.print("Ping: ");
client.print(msg); 
client.println("cm");


if (client.available()) {
 char c = client.read();
 Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
 Serial.println();//report it to the serial
 Serial.println("disconnected");
 client.stop();
}
}

This is the java code

public static void main(String[] args) {

    ServerSocket s; //Socket servidor
    Socket sc; //Socket cliente

    BufferedReader b; //Canal de Lectura

    String mensaje;

    try {
        //Creo el socket server
        s = new ServerSocket(8887);

        //Invoco el metodo accept del socket servidor, me devuelve una referencia al socket cliente
        sc = s.accept();

        //Obtengo una referencia a los canales de escritura y lectura del socket cliente
        b = new BufferedReader( new InputStreamReader ( sc.getInputStream() ) );


        while ( true ) {
            //Leo lo que escribio el socket cliente en el canal de lectura
            mensaje = b.readLine();
            System.out.println(mensaje);

            //Escribo en canal de escritura el mismo mensaje recibido

            if ( mensaje.equals("by")) { // this dont have sense now
                break;
            }
        }
        b.close();

        sc.close();
        s.close();
    } catch (IOException e) {
        System.out.println(e);
    }       

}

Why does my java code always receive a distance of 0CM?

Was it helpful?

Solution

problem solved.

Here i found the reason why it does not work: Arduino Ethernet UDPSendReceive example disables all pin outputs?

I only changed the pins where ultrasonic is connected:

#define TRIGGER_PIN  8  
#define ECHO_PIN     7
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top