Nodemcu doesn't receive UDP from Nano's

I have 2 Nano's and 2 ESP's sending small UDP packets to a Due using IP 192.168.1.180 and port 8881, it receives all UDP packets from all 4 senders.

I then changed the Due to a Nodemcu using the WIFI libraries, with the same IP 192.168.1.180 and port 8881.
Problem is that the Nodemcu only receives the packets from the 2 ESP senders and nothing comes in from the Nano's.

Senders:
Nano1 IP 192.168.1.178 local port 8889
Nano2 IP 192.168.1.177 local port 8888
ESP1 IP 192.168.1.200 local port 8886
ESP2 IP 192.168.1.201 local port 8887

Code for the Nodemcu, receive only no acknowledgments send:


#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
#include "TFT_eSPI.h"
#include "Free_Fonts.h"                 // Include the Adafruit fonts from the header file attached to this sketch
//#define UDP_TX_PACKET_MAX_SIZE 80

/*Put your SSID & Password*/
const char* ssid = "";  // Enter SSID here
const char* password = "";  //Enter Password here

IPAddress ipNTP(5,45,111,220);
unsigned int localPort2 = 8885;
WiFiUDP Udp2;
NTPClient timeClient(Udp2, ipNTP, 3600, 60000); //3600 = timeoffset, 60000 = get it every minute

WiFiUDP Udp;
unsigned int localUdpPort = 8881;         // local port for UDP to listen on, Must be the same for all receiving units?
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
//char ReplyBuffer[40] = "Testing 1 2 3";                // not used

void setup() {
  
  Serial.begin(9600);
    
  // Connect to Wi-Fi network with SSID and password
  Serial.println(" ");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);       
  IPAddress ip(192,168,1,180);                // Set fixed ip
  IPAddress gateway(192,168,1,1);   
  IPAddress subnet(255,255,255,0);   
  WiFi.config(ip, gateway, subnet);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  // Print local IP address
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Udp.begin(localUdpPort);
  displayIPaddress(ip, localUdpPort);
//  displayMACaddress(mac);
  Serial.println("Listening for UDP");

  Udp2.begin(localPort2);     // for timeserver
  timeClient.begin(); 
}

void loop(){
  
  // if there's UDP data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.println("");
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i=0; i < 4; i++) {             // print ip address
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.println(", port ");
    Serial.println(Udp.remotePort());
    
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);   // read the packet into packetBufffer
      Serial.println("Contents:");
      Serial.println(packetBuffer);
  }
}

// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
  Serial.print(" IP ");
  for (int i = 0; i < 4; i++) {
    Serial.print(address[i], DEC);
    if (i < 3) Serial.print(".");
  }
  Serial.print(" port ");
  Serial.println(port);
}

void displayMACaddress(byte address[]) {
  Serial.print("MAC address ");
  for (int i = 0; i < 6; i++) {
    Serial.print("0x");
    Serial.print(address[i], HEX);
    if (i < 5) Serial.print(".");
  }
  Serial.println();
}

Code for Nano:

#include <EthernetENC.h>
#include <EthernetUdp.h>
#include <NTPClient.h>            // Required to convert leap years and other exceptions
#include <TimeLib.h>
#define UDP_TX_PACKET_MAX_SIZE 80

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE1 
};
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;      // local port for UDP

IPAddress remoteIP(192, 168, 1, 180);
unsigned int remotePort(8880);

char ReplyBuffer[] = "UDP test";        // a string to send back

EthernetUDP Udp;    // An EthernetUDP instance to let us send and receive packets over UDP

unsigned int localPort2 = 8885;
EthernetUDP Udp2;
NTPClient timeClient(Udp2, "europe.pool.ntp.org", 3600, 60000); //3600 = timeoffset, 60000 = get it every minute

void setup() {
  Ethernet.init(10);  // default pin for nano
  mac[5] = ip[3]; // change default MAC address 
  Ethernet.begin(mac, ip);    // start the Ethernet
  Serial.begin(9600);
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {    // Check for Ethernet hardware present
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }  
  }
  else
  {
    Serial.println("Ethernet shield detected");
  }
  // start UDP
  Udp.begin(localPort);
  Serial.println("Send UDP packet from:");
  displayIPaddress(ip, localPort);
  displayMACaddress(mac);

  Udp2.begin(localPort2);
  timeClient.begin();
}


void loop() {
  Serial.print("Send UDP packet to:");
  displayIPaddress(remoteIP, remotePort);  
  Serial.println();
  Udp.beginPacket(remoteIP, remotePort);
  Udp.write(ReplyBuffer);
  Udp.endPacket();
  delay(1000);

  timeClient.update();
  Serial.println(timeClient.getFormattedTime());
}

// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
  Serial.print(" IP ");
  for (int i = 0; i < 4; i++) {
    Serial.print(address[i], DEC);
    if (i < 3) Serial.print(".");
  }
  Serial.print(" port ");
  Serial.println(port);
}

void displayMACaddress(byte address[]) {
  Serial.print("MAC address ");
  for (int i = 0; i < 6; i++) {
    Serial.print("0x");
    Serial.print(address[i], HEX);
    if (i < 5) Serial.print(".");
  }
  Serial.println();
}

Output from Due:

08:55:19.983 -> Received packet of size 5
08:55:20.017 -> From 192.168.1.178, port 
08:55:20.017 -> 8889
08:55:20.594 -> 
08:55:20.594 -> Received packet of size 8
08:55:20.628 -> From 192.168.1.177, port 
08:55:20.662 -> 8882
08:55:20.662 -> Contents:
08:55:20.696 -> UDP test
08:54:59.458 -> Received packet of size 32
08:54:59.458 -> From 192.168.1.200, port 
08:54:59.492 -> 8886
08:54:59.492 -> Sensor1   22.06   19.56   22.81    5.05
08:54:39.524 -> Received packet of size 24
08:54:39.558 -> From 192.168.1.201, port 
08:54:39.558 -> 8887
08:54:39.592 -> Sensor2   20.38   17.13   32.00    5.06

Output from Nodemcu:

14:30:25.339 -> Received packet of size 24
14:30:25.339 -> From 192.168.1.201, port 
14:30:25.385 -> 8887
14:30:25.385 -> Contents:
14:30:25.385 ->    22.75   17.13   32.00    5.06
14:31:02.040 -> 
14:31:02.040 -> Received packet of size 32
14:31:02.040 -> From 192.168.1.200, port 
14:31:02.040 -> 8886
14:31:02.040 -> Contents:
14:31:02.087 ->    21.75   19.56   23.13    5.06
14:31:25.874 -> 
14:31:25.874 -> Received packet of size 24
14:31:25.874 -> From 192.168.1.201, port 
14:31:25.920 -> 8887
14:31:25.920 -> Contents:
14:31:25.967 ->    22.88   17.13   32.00    5.06
14:32:02.525 -> 
14:32:02.525 -> Received packet of size 32
14:32:02.525 -> From 192.168.1.200, port 
14:32:02.572 -> 8886
14:32:02.572 -> Contents:
14:32:02.619 ->    21.88   19.56   23.13    5.07

Thanks,
Luc.

could you give more details regarding the WiFi modules, etc being used, e.g.what module were you using on the DUE to receive the packets

Hello Horace,

this is a continuation on this topic:

I have added some ESP's as senders and everything is working fine with the Due, I can process the data. But then needed to switch to a NodeMCU because I didn't have a working display for the Due.
The WIFI modules are simple ESP8266MOD types from Aliexpress.
The Due uses a 3V3 Ethernet shield connected to the ICSP pins.

you had a Due which used Ethernet and the other modules used WiFi?
you replaced the Due/Ethernet with a NodeMCU which used WiFi but kept the same IP?
could the router have a had a problem with this?
try changing the NodeMCU IP address?

I still have both the Due and the NodeMCU.
The current situation is like this:
4 senders sending UDP packets for several days now.
When I power up the Due everything works, but not when I use the NodeMCU.
The Due and NodeMCU are never on at the same because of the same IP address.
I'm no expert but don't see why the router would block the UDP's from the Nano's when the NodeMCU is on.

the router could be associating an IP with a MAC address (don't see why it would) so when you switch devices with different MAC addresses you have problems - can you make the Due and NodeMCU the same MAC address?

Tried to change the Mac address using this method and variations that I found on Internet:

But it didn't work (maybe because it's a clone?) and gave up after a few hours.

Using the Nodemcu was only a quick temporary solution until the LCD for the Due arrives and no changes to the senders would be needed.
So I decided to try and send the UDP packet twice, once to the Due and a second to the Nodemcu.
This worked from the first time (to my surprise) and I have done it on all 4 senders, it took some time but now I will be able to work on both units and finally have 2 working displays which is great.

Made the following changes in the senders:

IPAddress remoteIP(192, 168, 1, 180);     //Display on Due
unsigned int remotePort(8881);
IPAddress remoteIP1(192, 168, 1, 205);     //Display on Nodemcu
unsigned int remotePort1(8870);    

in the code:
    Udp.beginPacket(remoteIP, remotePort);
    Udp.write(ReplyBuffer,5);               //Send the buffer including bytes = 0
    Udp.endPacket();
    delay(50);
    Udp.beginPacket(remoteIP1, remotePort1);
    Udp.write(ReplyBuffer,5);               //Send the buffer including bytes = 0
    Udp.endPacket();

The problem must be a bug in my router as you suggested before.
Thanks for the help.