【问题标题】:ESP8266 lags for some time and then updates very fastESP8266 滞后一段时间,然后更新非常快
【发布时间】:2021-04-09 05:00:43
【问题描述】:

我在 ESP 和 Android 智能手机之间的连接出现问题。

ESP 每隔 X 次停止在串行端口上响应或发送任何内容。在 Android 上调试时说“连接被拒绝”。然后,在 2-3 分钟后,它开始向串口发送垃圾邮件连接信息,并且可能在 10 秒后正常工作。 我也得到 wifi evt: 7 - WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED 根据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]");
  }
}

安卓代码:

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

【问题讨论】:

  • 你的安卓设备是如何连接到ESP的串口的?
  • 我的华为通过wifi连接到esp。我正在通过 arduino ide 在 pc 上读取串口

标签: java android arduino wifi esp8266


【解决方案1】:

该草图有几处错误。 https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/server-examples.rst 的示例(您必须遵循)确实正确。以它为模板,开始做一些小的改动——一次一个。

  1. 您的Serial.println("[Client disconnected]"); 放错地方了。它在 if (client.available()) 块内,但不应该。
  2. 您缺少client.stop()
  3. 您需要使用if (line.length() == 1 &amp;&amp; line[0] == '\n') 或类似方式完整读取客户端请求。
  4. 串联响应(使用s +=)会导致堆碎片并产生无效的 HTML 代码。

我要添加到示例中的唯一内容是两个 while 循环中的每一个中的 yield() 调用。它们有助于避免使系统进程挨饿。 WiFiServer 库可能已经在后台执行此操作,但除非我们检查代码,否则我们无法确定。

【讨论】:

  • 还是同样的问题,有时候卡住1分钟,之后连接和断开非常快
猜你喜欢
  • 2016-12-05
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 2014-07-27
  • 1970-01-01
  • 2012-02-22
相关资源
最近更新 更多