【问题标题】:Esp8266 - MDNS stop responding for some time after I send dataEsp8266 - MDNS 在我发送数据后停止响应一段时间
【发布时间】:2018-03-22 18:19:16
【问题描述】:

我的代码非常简单,我使用我的 esp8266 作为服务器,它应该等待客户端连接从中读取数据并通过串口将其传输到 Arduino。 我还使用 mDNS 来检测网络内 ESP8266 的 IP 地址,代码如下:

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>


.....
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10); 

  if (!MDNS.begin("Device 1")) { //
    while (1) {  delay(1000); }
  }
  MDNS.addService("SmartDimmerx2", "tcp", 80);


  WiFi.mode(WIFI_STA);
  delay(4000);
  // Check if WiFi is already connected and if not, 
  if (WiFi.status() != WL_CONNECTED){
    Serial.println("Disconnected");
    ConnecStat_Toarduino = false;
  }
  else {
    Serial.println("Connected");
    ConnecStat_Toarduino = true;
  }
  // Start the server
  server.begin();

}



void loop() {
  MDNS.update();

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected");
    ConnecStat_Toarduino = false;
    delay(100);
  }
  else if ((WiFi.status() == WL_CONNECTED) && (ConnecStat_Toarduino = false)) {
    Serial.println("Connected");
    ConnecStat_Toarduino = true;
  }


  if (Serial.available() > 0) {
    String incomingChars = Serial.readStringUntil('\n');
    if(incomingChars.indexOf("WPS") != -1) {
     WPS_fnct();
    }
  }
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    time_cnx = millis();
    return;
  }

  // Wait until the client sends some data

  while (!client.available()) {
    if ((millis() - time_cnx)>2000) 
    {return;}
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');

  client.flush();
  client.stop();

  // Match the request
  int val;
  if (req.indexOf("V=") != -1)
  {
    Serial.print(req.substring(2,5));
    Serial.println(req.substring(7,11));
     //Serial.println(char(req.substring(8,10).toInt()));
  }

  else {
    client.stop();
    return;
  }
}

我使用我的手机应用程序作为客户端,代码运行良好,我扫描“_SmartDimmerx2._tcp.local”。手机会使用其 IP 地址检测 esp。 我能够将数据发送到 ESP,它会正确接收,然后通过串行将其发送回 arduino,

问题是在我连续向 ESP 发送一些数据之后,我再次尝试扫描 ESP 没有响应的 mDNS。 ESP 仍然没有响应 mDNS 扫描大约 30 秒/1 分钟,之后它再次变得可发现。

与此同时,当 mDNS 没有响应时,ESP 仍然能够从手机接收数据并通过串口发送数据而没有任何问题,并显示正确的数据。所以esp没有被窃听。

当我从手机发送大量数据时,我感觉有东西阻止了 mDNS 服务。我的代码有问题吗? 当 mDns 没有响应时,我尝试使用 Zeroconf android 应用程序进行扫描,但直到大约一分钟后它才响应。

【问题讨论】:

    标签: esp8266 arduino-esp8266 mdns


    【解决方案1】:

    所以我尝试激活 DEBUG 以查看问题所在, 所以我在文件的开头添加了#define DEBUG_ESP_MDNS_ERR 并激活IDE内的串行调试,这就是我得到的。

    1- 当我第一次启动 ESP 时,我得到了这种每隔 2 秒就会弹出一次的线条:

    :urn 33
    :urd 4, 33, 13
    :urd 4, 33, 18
    :urd 5, 33, 23
    :urn 283
    

    2- 每次我尝试使用手机搜索 esp 时,都会弹出这些行(ZeroconfResolver/mdns),并且我的手机能够找到 esp8266

    :urn 43
    :urd 14, 43, 13
    :urd 4, 43, 28
    :urd 5, 43, 33
    

    3- 然后当我用手机向 esp 发送一系列数据时,我得到了这样的线路:

    WS:ac
    :rn 12
    :ref 1
    WS:av
    :ref 2
    :ur 2
    :c0 1, 12
    :ur 1
    :close
    WS:dis
    :del
    WS:ac
    :rn 12
    :ref 1
    WS:av
    :ref 2
    :ur 2
    :c0 1, 12
    :ur 1
    :close
    WS:dis
    :del
    .........
    

    所以我在手机中使用了一个滑块,每次我更改它的值时,它都应该将此值发送到 ESP

    4.发送一些数据后,我再次尝试使用 (ZeroconfResolver/mdns) 搜索 esp8266,但 esp 没有响应,手机无法检测到它,这就是我在调试中得到的:

    :urn 43
    :urd 14, 43, 13
    :urd 4, 43, 28
    :urd 5, 43, 33
    failed _reservefailed _reservefailed _reservefailed _reservefailed _reservefailed _reservefailed _reservefailed _reservefailed _reservefailed _reservefailed _reservefailed .........
    

    所以要让 esp 再次被发现,我可以重新启动 esp,或者我可以等待 30 秒到 2 分钟,然后 ESP 将再次被发现。

    我在 UdpContext.h 中搜索此错误“失败 _reserve”并在此处找到它

    https://github.com/esp8266/Arduino/blob/461c922586564b8b8b1ece7e5591531b38dbfb87/libraries/ESP8266WiFi/src/include/UdpContext.h#L270

    谁能解释一下为什么会触发这个问题?看起来 tx 缓冲区已满?那我该怎么办? 对不起,我是网络菜鸟,我只是一个电子ing。

    谢谢

    【讨论】:

    • 这个@Tizana 有什么更新吗?我正在体验 3 中的日志,不知道该怎么办。
    • 我试图运行 mdns 更新,很少像每秒 1 次,但又一次失败了
    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2011-04-08
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多