微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ESP8266 突然停止连接到 java 服务器

如何解决ESP8266 突然停止连接到 java 服务器

我在作为服务器的计算机上有一个 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);

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

写入控制台“等待客户端”后,它等待客户端连接(据我所知,它工作正常)。问题出在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(host,port)。上次我尝试程序时它运行良好。它返回true,其余的都会发生。从今天开始,然而它总是错误的,所以esp连接到wifi,就像计算机一样,但它总是无法连接到等待它的服务器。我不明白问题出在哪里,尤其是它以前运行良好,现在却无法运行了。

解决方法

好吧,这很尴尬。花了一天的时间后,我重新启动了调制解调器,现在它工作正常,就像以前一样。 好的,还是谢谢大家

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。