UDP wont work over network but works locally to one N switch

Hey everyone, Basically i have two arduinos that are trying to talk to each other via UDP. It works fine over my local switch to my pc and it trips the relay but as soon as i try to put it through multiple switches across a large Local area network it dosent work. here is my code for arduino A num 1 and arduino B num 2

Is it somthing to do with the udp recieve? or is it a network problem, it seems to get lost and i really cant work it out and would love some help

// Arduino A

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

// Motion trip sensitivity:
long debounceDelay = 50;

// the number of the motion sensor input pin:
const int SensorPin = 9;

// Enter a MAC address and IP address for this controller:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 60);

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

// Enter the IP address and Port number for Arduino B:
byte ipDestination[] = {192, 168, 1, 50};

// the destination port:
int port        = 9999;

// Enter the IP address and Port number for the Java Server:
byte ipJava[] = {192, 168, 1, 30};

// external port being listened on
unsigned int JavaPort = 7777;

// Code starts below here, wouldn't change it unless you know what you're doing.

int inputState = LOW;
int lastinputState = LOW;
long lastDebounceTime = 0;

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char  ReplyBuffer[] = "acknowledged";
int Activate = 1;

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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(SensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {

  int reading = digitalRead(SensorPin);

  if (reading == HIGH)
  {
    Serial.println("tripped");
    Udp.beginPacket(ipDestination, port);
    Udp.write("10000");
    Udp.endPacket();
    Udp.beginPacket(ipJava, JavaPort);
    Udp.write("100000");
    Udp.endPacket();
    delay(1000);
  }

}
// Arduino B

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

// Relay active length in milliseconds:
int Timer = 1000;   

// Output to relay pin:
const int relayPin = 2;  

// Enter a MAC address and IP address for this controller:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 50);

// local port to listen on:
unsigned int localPort = 9999;

// Enter the IP address and Port number for Arduino B:
byte ipDestination[] = {192, 168, 1, 60};

// the destination port:
int port        = 8888;               

// Enter the IP address and Port number for the Java Server:
byte ipJava[] = {192, 168, 1, 30};

// external port being listened on
unsigned int JavaPort = 7777;

// Code starts below here, wouldn't change it unless you know what you're doing.

// buffers for receiving and sending data
char packetBuffer[100];
char  ReplyBuffer[] = "acknowledged";


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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(relayPin, OUTPUT);
  Serial.begin(9600); 
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    
      digitalWrite(relayPin, HIGH);
      delay(Timer);
      digitalWrite(relayPin, LOW);
    
}
}

is it the ports thats wrong?

its not the firewall, thats confirmed

does it need a recieve? which im not sure how to do?

If anyone could help me out here id be ever so greatfull

Regards

Lewis

One problem I see right away: You should not have duplicate mac addresses on the local network. Change one.

Thank you, i will change it right away! :slight_smile: can you see any other possible issues so if it is.. we can kill two or more birds with one stone

Regards

The rest looks ok. If your send is localnet, this error check may help. Change your endPacket() calls to this.

    if(!Udp.endPacket()) Serial.println(F("Not sent"));

Bear in mind, this does NOT indicate the packet was delivered to the destination, only that the next network device enroute took the packet. If you are localnet, this usually indicates the packet was received by the destination, but not always.

This is from EthernetUdp.h

// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket();

Thank you tim, i will try that now :slight_smile:

The Network switches that it goes through wont loose the UDP packet will they?

Im guessing there are no settings in network switches that stop or change this?

Regards

lewisjoyce:
The Network switches that it goes through wont loose the UDP packet will they?

Im guessing there are no settings in network switches that stop or change this?

#1 -> If it caches the packet enroute, it could lose the packet.
#2 -> No. It is udp. Use tcp protocol to insure delivery.

edit: If you want to test your switch for udp caching, send a packet to an ip address you know does not exist on your localnet. If the endPacket() doesn't return an error, then your switch is taking the packet.