I'm having some problems getting the Ethernet Shield (R3) communicating with Processing using UDP. I've tried it both on an Arduino Due and an Uno, same result.
The code I'm using is from the UDPSendReceiveString tutorial here, with some minor modifications:
Things I've changed:
- Added the MAC address on the back of my Ethernet Shield
- Tried a version with a manual IP address and with none (using DHCP)
- added a small Processing window so I can tell when the app is in focus
What happens is that the Arduino never receives the strings sent from Processing. Also, if I try sending strings from the Arduino, I never receive them from Processing.
Arduino code is here:
/*
UDPSendReceive.pde:
This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender
A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer.
created 21 Aug 2010
by Michael Margolis
This code is in the public domain.
*/
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x9F, 0x29 };
// This matches IP in Ethernet.localIP
IPAddress ip(10,120,85,19);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Serial.begin(9600);
Serial.println("UDP Ethernet Test");
// Explit IP
Ethernet.begin(mac,ip);
// DHCP CONNECTION
/*
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
*/
delay(1000); // 1 sec delay was suggested in some posts
// prints the Arduino IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
Udp.begin(localPort);
}
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);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
Processing code is here:
/*
Processing sketch to run with this example
=====================================================
// Processing UDP example to send and receive string data from Arduino
// press any key to send the "Hello Arduino" message
*/
import hypermedia.net.*;
UDP udp; // define the UDP object
void setup() {
size(800,600);
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
udp.log( true ); // <-- printout the connection activity
udp.listen( true ); // and wait for incoming message
}
void draw()
{
background(0);
}
void keyPressed() {
String ip = "10.120.85.19"; // the remote IP address
int port = 8888; // the destination port
println("sending Hello World");
udp.send("Hello World", ip, port ); // the message to send
}
void receive( byte[] data ) { // <-- default handler
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
for(int i=0; i < data.length; i++)
print(char(data[i]));
println();
}
Note: using the Ethernet->WebClient example does work on both boards, so the Shield is functioning correctly.
Other stuff I've trued:
- using on a completely different network (home and at work), still not working
- using port numbers other than 8888
- using a different Mac
- using Eclipse instead of Processing for host environment
Running on a Mac OS X, Mtn Lion (both machines).
Both Arduino boards I've tried are R3 boards.
Thanks,
Scott
p.s. Processing console output is:
sending Hello World
-- UDP session started at Tue Aug 06 16:17:38 PDT 2013 --
-- bound socket to host:null, port: 6000 --
[13-08-06 16:17:41.67 -0700] send packet -> address:/10.120.85.19, port:8888, length: 11
sending Hello World
[13-08-06 16:17:44.67 -0700] send packet -> address:/10.120.85.19, port:8888, length: 11
sending Hello World
[13-08-06 16:17:45.535 -0700] send packet -> address:/10.120.85.19, port:8888, length: 11