Ethernet

Hey all!

So, I have two Arduino Megas both equipped with an ethernet shield 2. With that code I want to send a UDP Packet over the ethernet from the first Arduino, to tell the other Arduino it should or shouldnt switch on an LED if I push a button (I need to keep it pushed).
That works, but the problem I have is that it takes very very long...
When I upload the code and push the button it takes about a minute for the first Arduino to even register that I pushed the button. And it takes another thirty seconds or so to switch on or switch on the LED.
How can I speed that up?

Thank you very much!

Here`s the code:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte taster = A4;
byte power = 44;
byte tasterWert;
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //dest mac address
IPAddress ip(); //Local
IPAddress destIp(); //dest

unsigned int localPort = 5000; //Local Port to listen on
unsigned int destPort = 5005; //dest port

EthernetUDP Udp; //Instance
char high [] = "H";
char low [] = "L";

void setup(){
pinMode(taster,INPUT);
pinMode(power,OUTPUT);
Ethernet.begin(9600);
Udp.begin(localPort);
Serial.begin(9600);

}

void loop() {
digitalWrite(power,HIGH);
int tasterWert = digitalRead(taster);
Serial.println(tasterWert);
Udp.beginPacket(destIp, destPort);

if(tasterWert == 1){
Udp.write(high);
}
Udp.endPacket();

Udp.beginPacket(destIp, destPort);

if(tasterWert == 0){
Udp.write(low);
}
Udp.endPacket();

}

Ethernet.begin(9600); ???

@Juraj

Yeah I found that one!
But the problem continues...

Now it just prints a 0 and after this, nothing...

But thanks for the reply

sevizimmi:
@Juraj

Yeah I found that one!
But the problem continues...

Now it just prints a 0 and after this, nothing...

But thanks for the reply

this is a setup with a cable between two shields? or you have a router which assignes IP addresses.

@Juraj

Yes, I have a router.

char high [] = "H";
char low [] = "L";

It's rather pointless to use char arrays to hold one letter.

Yeah I found that one!

And, you did what? If you changed the code, you can't expect us to guess how.

 Udp.beginPacket(destIp, destPort);

 if(tasterWert == 1){
   Udp.write(high);
 }
 Udp.endPacket();

 Udp.beginPacket(destIp, destPort);

 if(tasterWert == 0){
   Udp.write(low);
 }
 Udp.endPacket();

Regardless of what came in, send two packets, one of which is empty. That is NOT the right thing to do.

is the router connected to Internet? can you try the WebClient example?
do you use the latest version of the Ethernet iibrary?

@Juraj

Yes it is connected. The example works perfect :slight_smile:

do you use the same MAC address on both shields?

@Juraj

No they are different, this is the other one btw:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define W5200_CS_ 10
byte LED = 6;
byte mac[] = {0x90,0xA2,0xDA,0x10,0x5F,0x92};
IPAddress ip();

unsigned int localPort = 5005;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char ReplyBuffer []= "has agstellt";

EthernetUDP Udp;

void setup(){
pinMode(LED,OUTPUT);
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
}

void loop(){
int packetSize = Udp.parsePacket();
if(packetSize){
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
packetBuffer[packetSize] = '\0';
packetSize -= 8;
Serial.print("Received packet data size = ");
Serial.print(packetSize);
Serial.print(" from ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++) { // Print IP address xxx.xxx.xxx.xxx
Serial.print(remote*, DEC);*

  • if (i < 3) {*

  • Serial.print(".");*

  • }*

  • }*

  • Serial.print(", port");*

  • Serial.println(Udp.remotePort());*

  • // Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);*

  • Serial.println("contents");*

  • Serial.println(packetBuffer);*

  • Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());*

  • Udp.write(ReplyBuffer);*

  • Udp.endPacket();*

  • switch (packetBuffer[0]) {*

  • case 'H':*

  • digitalWrite(LED,HIGH);*

  • break;*

  • case 'L':*

  • digitalWrite(LED,LOW);*

  • break;*

  • }*

  • delay(10);*
    }
    }

do you set the IP or is it set by DHCP? do the default gateway and subnetmask match your network?

@Juraj

Its over IP and I didnt have defined a subnet mask or gateway.

sevizimmi:
@Juraj

Its over IP and I didnt have defined a subnet mask or gateway.

do the default gateway and subnetmask match your network?

the library uses first 3 numbers of IP address with 1 as 4 number for the address of the gateway and dns server and network mask 255.255.255.0

@Juraj

Yes they are :slight_smile:

then see the comment of PaulS. you send empty packets. and too many packets.

@PaulS

Hey there, I just changed it from Ethernet.begin to Udp.begin(localPort);

And what do you mean with empty packet? How should I change that?

Thank you

And what do you mean with empty packet?

YOU need to research what Udp.beginPacket(), Udp.write(), and Udp.endPacket() do.

How should I change that?

Do NOT call beginPacket() and endPacket() unconditionally, while the write()(s) are conditional.