UDP sniffing works on Ethernet but not on WiFi

I’ve got a simple sketch that runs on an Arduino Uno with the 5100 Ethernet header board and it works ie it reliably prints out the UDP messages that come about every 10 seconds. Here it is…

/* This sketch listens for UDP packets and prints them out on the Serial output.
 * It works fine running on the Arduino Uno with the 5100 Ethernet board.
 * Specifically, it prints out all the Owl Intuition UDP messages without any problem.
 */

#include <SPI.h>       
#include <Ethernet.h>
#include <EthernetUdp.h>

#define buffer_size 360

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 160);   // Give the Arduino an IP address
unsigned int localPort = 22600;   // local port to listen on
char packetBuffer[buffer_size]; //buffer to hold incoming packet,

EthernetUDP Udp;

void setup() {
   // start the Ethernet and UDP
   Serial.begin(115200);
   Ethernet.begin(mac,ip);
   Udp.begin(localPort);
}

void loop() {
   int packetSize = Udp.parsePacket();
   if(packetSize){ // The packet has some size
     Udp.read(packetBuffer,packetSize); //buffer_size);
     for(int i=0; i < packetSize; i++) Serial.print(char(packetBuffer[i]));
     Serial.println();
   }
}

However, when I try to do it using WiFi on an ESP8266 I get nothing. It connects to the WiFi OK, but then I get nothing. The sketch for that is here and is very similar…

/* This sketch listens for UDP packets and prints them out on the Serial output.
 * It doesn't work running on an ESP8266 WiFi card.
 * Specifically, it connects to WiFi OK but never prints out any UDP messages.
 */

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

#define buffer_size 360

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 199);   // Give the Arduino an IP address
IPAddress gw(192, 168, 0,1);
IPAddress sn(255, 255, 255, 0);
IPAddress pd(8, 8, 8, 8);
IPAddress sd(8, 8, 8, 4); 
const char* ssid = "myssid";
const char* password = "mypassword";

unsigned int localPort = 22600;   // local port to listen on
char packetBuffer[buffer_size]; //buffer to hold incoming packet,

WiFiUDP Udp;

void setup() {
   Serial.begin(115200);
   WiFiBegin();
   Udp.begin(localPort);
}

void loop() {
   int packetSize = Udp.parsePacket();
   if(packetSize){ // The packet has some size
     Udp.read(packetBuffer,packetSize);
     for(int i=0; i < packetSize; i++) Serial.print(char(packetBuffer[i]));
     Serial.println();
   }
}

void WiFiBegin(){
   delay(200);
   // Connect to the WiFi network
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);
   WiFi.config(ip, gw, sn, pd, sd);
   WiFi.begin(ssid, password) ;
   while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
   }
   Serial.println(" Connected.");
}

I should add that the messages are going out on the WiFi as I have a python program running on my Android tablet that is seeing them.

I’m not a professional coder so most likely I have missed something out so if anyone can help I’d be grateful.

Cheers

It may be that you are using the same MAC address on both devices.

Oh, I see you're not actually using it on the ESP.

I don't actually know why a MAC is even needed; I thought they were hard wired into the network adapters.
I infer they are in the ESP but not in the Arduino.

I can ping the ESP from my PC.

I haven't got any further forward on this issue. I fact I find connecting the ESP (NodeMCU V3) to the Wifi is itself a struggle. If it doesn't connect in a few seconds it will never connect, so I've put an system reboot in the code.

I've solved my issue by getting the sending unit to 'push' the UDP message to the IP of the ESP8266 on a port that is being listened to. I did get some weird effects at first (multiple messages received) but they seem to have gone away now after a bit of code tweaking.
To overcome the WiFi connection issue I've started using a WiFiManager which can be found through the Arduino IDE (Sketch/Include Library/Manage Libraries and search for wifimanager).

The end result is that I now have a dedicated ESP8266 sniffing out my heating and power usage data coming from an Owl Intuition system and sending it to my Home assistant where I can store and visualize it however I like. If any wants to see the Sketch let me know. I now know I could do the same with Node-Red but that's another learning curve.