send udp packets from freetronics EtherMega board

Combining the power of the ATmega2560 MCU with onboard Ethernet, a microSD card slot, an efficient switchmode power supply, and a small prototyping area. [Product page]
Post Reply
diofa
Posts: 1
Joined: Wed Oct 30, 2013 7:48 am

send udp packets from freetronics EtherMega board

Post by diofa » Wed Oct 30, 2013 7:50 am

Hy,

i have a EtherMega board from freetronics. I try to send udp packets but i can't.

I use the sample code from aurduino: http://arduino.cc/de/Reference/EthernetUDPWrite

On the arduino, i have a udp server and on the pc, i have a udp client. On the client i don't receive anyone.

I have been tried an other sample code from arduino:

http://arduino.cc/en/Tutorial/UDPSendReceiveString This one work.

In this sample, it is sent and received text strings via UDP. This one work.

Can anybody help?

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: send udp packets from freetronics EtherMega board

Post by angusgr » Wed Oct 30, 2013 11:04 pm

Hi diofa,

I think the example sketch shown for EthernetUDPWrite has a problen. It won't work by itself. This line:

Code: Select all

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Assumes that we've already received a UDP packet from a remote host (with Udp.parsePacket), and therefore we know the remote IP to send one back to. Whereas that hasn't happened here, so there is no valid address for the packet to be sent to. (Unless this defaults to the broadcast address, but that isn't mentioned in the documentation.)

Here's a modified version of the EthernetUDPWrite example sketch that might work better. You'll need to edit the 'ip' and 'remoteIp' and 'remotePort' values.

Code: Select all

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

// 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, 0xED };
IPAddress ip(192, 168, 1, 177);
IPAddress remoteIp(192, 168, 1, 5); // address to send UDP packe to
int remotePort = 8888;  // port to send UDP packet to

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

// 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);
}

void loop() {
  Udp.beginPacket(remoteIP, remotePort);
    Udp.write("hello");
    Udp.endPacket();
}
Please let us know how this works out. If it still doesn't seem to be working, make sure to check all the IP addresses are set correctly on your network (are you able to ping the EtherMega?)

- Angus

Post Reply