Arduino以太网客户端在发送TCP时遇到问题

时间:2013-12-31 14:26:58

标签: java tcp arduino ethernet

我在尝试在arduino和java之间发送消息时遇到了一些问题。 arduino有一个超声波传感器,我想将传感器检测到的距离发送到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();
}
}

这是java代码

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);
    }       

}

为什么我的java代码总是收到0CM的距离?

1 个答案:

答案 0 :(得分:2)

问题解决了。

我在这里找到了它不起作用的原因:Arduino Ethernet UDPSendReceive example disables all pin outputs?

我只更换了连接超声波的引脚:

#define TRIGGER_PIN  8  
#define ECHO_PIN     7