我在作为服务器的计算机上有一个java程序,在充当客户机的esp上有一个程序。它们都连接到本地wifi,然后彼此连接。其目的是让计算机向esp发送一个号码。我曾设法让它们工作,但突然esp已停止连接到服务器。我花了相当长的时间试图弄清楚发生了什么,但我认为我没有改变什么。前一天他们都在一起工作,第二天就没有了。
这是我的java代码的主要部分,我相信它可以工作,尽管它可能并不理想(除此之外,还有一些函数可以用来制作一个小型gui和执行一些操作):
createGUI();
//looking for the arduino port, to get data from it
SerialPort comPort = findArduinoPort();
if (comPort != null) {
System.out.println("Arduino port found: " + comPort.getSystemPortName());
} else {
System.out.println("Arduino port not found");
System.exit(0);
}
comPort.openPort();
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
InputStream inComPort = comPort.getInputStream();
ServerSocket listener = new ServerSocket(8020);
char lastRead;
String data = new String("0");
try{
while(true){
Thread.sleep(0);
if(myStatus == status.ON) {
//read data from aduino serial
if (inComPort.available() > 0) {
data = "";
while (true) {
lastRead = (char)inComPort.read();
if (lastRead == '\r') {
lastRead = (char)inComPort.read();
break;
} else {
data += lastRead;
}
}
}
System.out.println("Waiting for client");
//from my understanding the next line waits for the client to connect
Socket socket = listener.accept();
socket.setKeepAlive(true);
System.out.println("Client Connected");
try{
//i read data from the client, to make sure it's ready to receive
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Client response: " + in.readLine());
//I send the data to esp8266
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("Sending Message: " + data);
out.write(data);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
socket.close();
}
}
}
} finally {
try {
inComPort.close();
} catch (Exception e) {
e.printStackTrace();
}
comPort.closePort();
listener.close();
}
}
在写入控制台“waitingforclient”之后,它会等待一个客户端连接(据我所知,它工作正常)。这个问题与esp8266中的代码有关
# include <ESP8266WiFi.h>
# include <LiquidCrystal.h>
//wifi data
# define NAME "i_put_my_wifi_here"
# define PASSWORD "i_put_my_wifi_pw_here"
String host = "i_put_my_computer_ip_here";
const int port = 8020;
WiFiClient client;
//lcd display to write the data to
LiquidCrystal lcd(D2, D3, D5, D6, D7, D8);
void setup() {
Serial.begin(115200);
//lcd setup
lcd.begin(16, 2);
lcd.print("Puntamenti:");
lcd.setCursor(0, 1);
lcd.print("0");
WiFi.mode(WIFI_STA);
WiFi.begin(NAME, PASSWORD);
Serial.print("\nConnecting to wifi");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected\n");
}
void loop() {
//i try to conect to the server
if (client.connect(host, port))
{
Serial.print("Connected to host: ");
Serial.println(host);
//i tell the server i'm ready for data
client.println("Connected");
Serial.print("Wating for data");
int i = 0;
while(true){
if(client.available() == 0){
Serial.print(".");
i++;
if(i >= 10){
break;
}
delay(1000);
} else {
lcd.setCursor(0, 1);
String streamData = client.readStringUntil('\n');
lcd.print(streamData);
Serial.print("\nData received: ");
Serial.println(streamData);
break;
}
}
client.stop();
} else {
Serial.println("Connection to host failed");
}
delay(1000);
}
所以我的问题来自client.connect(主机,端口)。上次我试过这些程序,效果很好。结果是真的,剩下的就发生了。然而,从今天起,它总是错误的,所以esp连接到wifi,就像计算机一样,但是它总是无法连接到等待它的服务器。我不明白问题出在哪里,尤其是它以前工作得很好,现在已经不工作了。
1条答案
按热度按时间f87krz0w1#
好吧,这很尴尬。花了一天的时间后,我重新启动了我的调制解调器,现在它工作正常,就像以前一样。好吧,谢谢大家