esp8266延迟了一段时间,然后更新得非常快

x0fgdtte  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(523)

我的esp和android智能手机之间的连接有问题。
每当esp停止应答或发送串行端口上的任何内容时。在android上调试说“连接被拒绝”。然后,在2-3分钟后,它开始向串行端口发送垃圾邮件连接信息,可能10秒后工作正常。我也得到wifi evt:7-wifi\事件\软件模式\问题根据https://github.com/esp8266/arduino/issues/2735 “[ap]收到请求时出错”
... 每300毫秒更新一次数据。。。错误:19:48:58.765 wifi evt:7 19:49:00.627 wifi evt:7 19:49:00.627 wifi evt:7 19:49:12.179 wifi evt:7。。。然后很快的更新,大约50毫秒。
esp处于ap模式。
最新的图书馆。
esp代码:


# include <ESP8266WiFi.h>

//#include <Ticker.h>
//#include <Wire.h>
//#include "PCF8574.h"
//#include <ESP8266WiFiGratuitous.h>

const char* ssid = "wifitest2";
const char* password = "esp8266";
IPAddress ip(192,168,0,1);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

WiFiServer server(80);
//Ticker timer;

const byte interruptPin = 13;
int czas2 = 0;
int czas = 0;
int a=1;
boolean auto_manual = 0;
unsigned long currentTime=0;
unsigned long previousTime=0;
const long Timeout=200;

//PCF8574 PCF_01(0x20);

void setup()
{
  delay(10);
  //pinMode(interruptPin, INPUT_PULLUP);
  //attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);

  Serial.begin(115200);
  Serial.println();
  //PCF_01.begin();

  WiFi.softAPConfig(ip,gateway,subnet);
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password,10,true,1);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  server.begin();
  //experimental::ESP8266WiFiGratuitous::stationKeepAliveSetIntervalMs();

  //timer.attach_ms(1,zegar);
}

// prepare a web page to be send to a client (web browser)
String prepareAnswer()
{
  String htmlPage;
  htmlPage.reserve(1024);               // prevent ram fragmentation
  htmlPage = F("HTTP/1.1 200 OK\r\n"
               "Content-Type: text/html\r\n"
               "Connection: close\r\n"  // the connection will be closed after completion of the response
               "\r\n"
               "<!DOCTYPE HTML>"
               "<html>");
  htmlPage += F("@"
              "300"
              "#" 
              "200"
              "$");
  htmlPage += F("</html>"
                "\r\n");
  return htmlPage;
}

void loop()
{
  WiFiClient client = server.available();
  // wait for a client (web browser) to connect
  if (client)
  {
    Serial.println("\n[Client connected]");
    while (client.connected())
    {
      yield();
      // read line by line what the client (web browser) is requesting
      if (client.available())
      {
        String line = client.readStringUntil('\r');
        Serial.print(line);
        // wait for end of client's request, that is marked with an empty line
        if (line.length() == 1 && line[0] == '\n')
        {
          client.println(prepareAnswer());
          break;
        }
      }

    }

    while (client.available()) {

      // but first, let client finish its request
      // that's diplomatic compliance to protocols
      // (and otherwise some clients may complain, like curl)
      // (that is an example, prefer using a proper webserver library)
      client.read();
      yield();
    }

    // close the connection:
    client.stop();
    Serial.println("[Client disconnected]");
  }
}

android代码:

new HttpRequestAsyncTask(parameterValue, "192.168.0.1", "80", "").execute();
handler.postDelayed(this,300); //every XXX ms
  }
};

public String sendRequest(String parameterValue, String ipAddress, String portNumber, String parameterName) {
  String serverResponse = "ERROR";
  try {
    HttpClient httpclient = new DefaultHttpClient();
    URI website = new URI("http://" + ipAddress + ":" + portNumber + "/" + parameterName + parameterValue);
    HttpGet getRequest = new HttpGet();
    getRequest.setURI(website);
    HttpResponse response = httpclient.execute(getRequest);

    InputStream content = null;
    content = response.getEntity().getContent();
    BufferedReader in = new BufferedReader(new InputStreamReader(content));
    serverResponse = in.readLine();
    content.close();
  } catch (IOException | URISyntaxException e) {
    serverResponse = e.getMessage();
    e.printStackTrace();
  }
  return serverResponse;
}
qvsjd97n

qvsjd97n1#

那张素描有几处地方不对劲。这个例子(你一定学过)在https://github.com/esp8266/arduino/blob/master/doc/esp8266wifi/server-examples.rst 你做对了。把它作为一个模板,开始做一些小的改变-一次一个。
你的 Serial.println("[Client disconnected]"); 在错误的地方。在房间里 if (client.available()) 阻止但不应该阻止。
你错过了 client.stop() .
您需要使用 if (line.length() == 1 && line[0] == '\n') 或者类似的。
串联响应(使用 s += )导致堆碎片并生成无效的html代码。
我唯一要补充的例子是 yield() 两个人各打一个电话 while -循环。它们有助于避免饥饿的系统进程。这个 WiFiServer 库可能已经在引擎盖下完成了,但是我们不能确定,除非我们检查代码。

相关问题