首页 > 解决方案 > ESP8266 在 WIFI 网络中不接收数据包

问题描述

在我的节点 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 调用网站(如谷歌)工作正常

标签: networkingudpwifiesp8266netcat

解决方案


您很可能必须添加 IO 防火墙rules。检查此链接

或者通过使用 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"


推荐阅读