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