ESP8266未在WIFI网络中接收数据包

时间:2019-11-28 22:42:25

标签: networking udp wifi esp8266 netcat

在我的节点mcu v3.0上运行UDP示例时,遇到以下奇怪的问题。

如果我将计算机和节点mcu都连接到手机托管的WIFI热点,则一切运行正常(我可以通过netcat将来自计算机的udp数据包发送到节点mcu并获得答案)。

但是,如果我切换到我居住的学生宿舍提供的WiFi网络(我们必须像使用任何其他常规wifi网络一样,使用相应的ssid和密码连接到下一个路由器),则节点mcu不会收到任何信息数据包(它仍然会打印出IP地址,因此我想它可以连接到网络)。我还尝试了通过netcat将udp数据包从计算机发送到笔记本电脑,但仍然可以正常工作。 这是怎么回事?

代码如下:

/*
  UDPSendReceive.pde:
  This sketch receives UDP message strings, prints them to the serial port
  and sends an "acknowledge" string back to the sender

  A Processing sketch is included at the end of file that can be used to send
  and received messages for testing with a computer.

  created 21 Aug 2010
  by Michael Margolis

  This code is in the public domain.

  adapted from Ethernet library examples
*/


#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#ifndef STASSID
#define STASSID ".."
#define STAPSK  ".."
#endif

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged\r\n";       // a string to send back

WiFiUDP Udp;

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());
  Serial.printf("UDP server on port %d\n", localPort);
  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.printf("Received packet of size %d from %s:%d\n    (to %s:%d, free heap = %d B)\n",
                  packetSize,
                  Udp.remoteIP().toString().c_str(), Udp.remotePort(),
                  Udp.destinationIP().toString().c_str(), Udp.localPort(),
                  ESP.getFreeHeap());

    // read the packet into packetBufffer
    int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    packetBuffer[n] = 0;
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }

}

/*
  test (shell/netcat):
  --------------------
      nc -u 192.168.esp.address 8888
*/

编辑:从节点mcu调用网站(例如google)很好

1 个答案:

答案 0 :(得分:0)

您很有可能必须添加IO防火墙rules。 选中This link

或使用PowerShell:

PS C:\> New-NetFirewallRule -DisplayName "Allow UDP 8888 over Teredo" -Direction Inbound -Action Allow -EdgeTraversalPolicy Allow -Protocol UDP -LocalPort 8888 -Program "C:\Program Files (x86)\TestIPv6App.exe"