将数据从Java服务器发送到ESP8266

时间:2017-04-30 04:50:45

标签: java sockets arduino arduino-esp8266

这是我在这里的第一篇文章,如果我犯了任何错误,请耐心等待......

好的,我只是想在我的计算机上运行一个Java程序,将信息发送到运行arduino软件的ESP8266。首先,这是代码。

ESP8266(Arduino的):

#include <ESP8266WiFi.h>

#define NAME "********"
#define PASS "********"

const char* host = "192.168.1.6";

WiFiClient client;

void setup() {
  Serial.begin(115200);
  Serial.println();
  WiFi.begin(NAME, PASS);
  Serial.print("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  if (client.connect(host, 9090)) {
    Serial.print("Connected to: ");
    Serial.println(host);
    String data = "No Message";
    client.print("ESP8266 connected!");
    if(client.available()) {
      data = client.readStringUntil('\n');
    }
    Serial.print("Host message: ");
    Serial.println(data);
    client.stop();
  } else {
    client.stop();
  }
}

void loop() {
}

Java Server:

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket listener = new ServerSocket(9090);
        try{
            while(true){
                Socket socket = listener.accept();
                socket.setKeepAlive(true);
                try{
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    out.write("Hello from Java!\n");
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }

}

问题是client.available()永远不会返回true,即使Java服务器向客户端发送信息也是如此。我已经尝试将数据从Arduino发送到服务器,并且运行正常。我还制作了一个快速Java程序,可以作为客户端运行,并在网络上的另一台计算机上运行。在这种情况下,Java服务器和客户端通信很好。所以问题出在Arduino的某个地方,但我不知道它是什么。

3 个答案:

答案 0 :(得分:2)

此处的问题似乎与

有关
client.print() 

替换它
client.println()

解决了我的问题。

我相信这在某种程度上阻止了输入缓冲区服务器端,因为它正在等待换行但从未得到换行符。我将发布下面适用于此问题的更新代码。

ESP8266:

#include <ESP8266WiFi.h>

#define NAME "********"
#define PASS "********"

const char* host = "10.0.0.15";

WiFiClient client;

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

  /* Set Client up as station */
  WiFi.mode(WIFI_STA);

  WiFi.begin(NAME, PASS);

  /* Connect to the network */
  Serial.print("Connecting");
  while(WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  if (client.connect(host, 9090))
  {
    Serial.print("Connected to: ");
    Serial.println(host);

    /* Send "connected" to the server so it knows we are ready for data */
    client.println("deviceconnected"); //USE client.println()!!
    Serial.println("Host message: \n");

    /* Wait for data for 5 seconds at most before timing out */
    unsigned long timeout = millis();
    while(client.available() == 0)
    {
      if(millis() - timeout > 5000)
      {
        Serial.println("Timeout to server!");
        break;
      }
    }

    /* Read in the data in the stream */
    while(client.available() > 0)
    {
      Serial.println(client.readStringUntil('\n'));
    }
    client.stop();
  }
  else
  {
    client.stop();
  }
  delay(5000);
}

Java Server:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket listener = new ServerSocket(9090);
        try{
            while(true){
                Socket socket = listener.accept();
                socket.setKeepAlive(true);
                System.out.println("Client Connected");
                try{
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    System.out.println("Client response: " + in.readLine());

                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    System.out.println("Sending Message...");
                    out.write("Hello\n from Java!\n");
                    out.flush();
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }

}

答案 1 :(得分:0)

您的代码部分属于loop()事件:

  if (client.connect(host, 9090)) {
    Serial.print("Connected to: ");
    Serial.println(host);
    String data = "No Message";
    client.print("ESP8266 connected!");
    if(client.available()) {
      data = client.readStringUntil('\n');
    }
    Serial.print("Host message: ");
    Serial.println(data);
    client.stop();
  } else {
    client.stop();
  }

setup()函数仅运行一次。在它移动到loop()并停留在那里之后。你的loop()函数是空的......

答案 2 :(得分:0)

我有同样的问题,这是我的解决方案:
Java只在你添加out.flush()时更新输出流。在out.write();

之后

另外

client.available()

返回0直到你读到了什么。例如

String first_line = client.readStringUntil('\n');

在此之后你可以使用available()方法。


使用字符串测试“你好,\ n这是一个文本\ n”:

client.println(client.available()); // 0

String first_line = client.readStringUntil('\n');

client.println(first_line); // Hello,
client.println(client.available()); // 17

将所有邮件发送回客户端:

client.print("First: ");
client.println(first_line);

while(client.available() > 0) {
  client.println("Next: " + client.readStringUntil('\n'));
}

输出:

First: Hello,
Next: this is a text
Next: