I am having trouble getting my arduino to receive udp packets. All the packets that I am looking to receive are being broadcast to the network, and Wireshark reports that my computer is sending them correctly. However, it seems that there are no packets being found by udp.parsepacket(), or that they are empty. Even the udpsendrecievestring example sketch doesn't work properly. I have tested out the network that I am using for the project using the webserver example sketch, and that works correctly. What could be causing the arduino to not receive udp packets correctly?
Posting your code might help.
I had problems with UDP if the SD SPI interface was not disabled. I would get trash in the packets.
I had problems with the "605 bug" and UDP also. Other have too.
http://arduino.cc/forum/index.php/topic,95889.0.html
Here is the bug/fix reported to Arduino. It has been fixed in v1.0.1.
http://code.google.com/p/arduino/issues/detail?id=605
The example UDPSendReceiveString sketch that comes with the Arduino IDE is not working. My program is exactly that, with just some code after it to process the data received. This is the full code, if it helps.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Ethernet configuration
byte MAC[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // MAC address
IPAddress IP(2, 0, 0, 123); // IP address
unsigned int LOCALPORT = 6454; // local port to listen on
// output pins
char UNIVERSE1PIN = 6;
char UNIVERSE2PIN = 7;
char UNIVERSE3PIN = 8;
char UNIVERSE4PIN = 9;
// buffers for receiving and sending data
char udpData[530];
EthernetUDP UDP;
// artnet opcodes
char OPDMX = 0x5000;
// universe data struct
struct universe {
unsigned char universe, subNet, net;
int dmxDataLength;
unsigned char dmxData[512];
} universe1, universe2, universe3, universe4, tempUniverse;
void setup() {
// set up DMX output pins
pinMode(UNIVERSE1PIN, OUTPUT);
pinMode(UNIVERSE2PIN, OUTPUT);
pinMode(UNIVERSE3PIN, OUTPUT);
pinMode(UNIVERSE4PIN, OUTPUT);
// DMX idle state
digitalWrite(UNIVERSE1PIN, HIGH);
digitalWrite(UNIVERSE2PIN, HIGH);
digitalWrite(UNIVERSE3PIN, HIGH);
digitalWrite(UNIVERSE4PIN, HIGH);
pinMode(4,OUTPUT);
digitalWrite(4, HIGH);
// start ethernet and UDP:
Ethernet.begin(MAC, IP);
UDP.begin(LOCALPORT);
Serial.begin(19200);
}
void loop() {
// if there's data available, read a packet
int packetSize = UDP.parsePacket();
if (packetSize) {
Serial.println("2");
UDP.read(udpData, packetSize); // read in ArtNet packet
Serial.println("3");
Serial.println("UDP");
// packet descriptors
char packetID[7] = {udpData[0], udpData[1], udpData[2], udpData[3], udpData[4], udpData[5], udpData[6]};
int opCode = udpData[2] << 8 + udpData[1];
if (strcmp(packetID, "Art-Net") && opCode == OPDMX) { // ArtDMX data
// parse packet data
Serial.println("artnet");
int artnetAddress = (int) udpData[11] << 8 + (int) udpData[12];
tempUniverse.net = unsigned(artnetAddress & 0x7F00) >> 8;
tempUniverse.subNet = unsigned(artnetAddress & 0xF0) >> 4;
tempUniverse.universe = artnetAddress & 0xF;
if (tempUniverse.net == 0 && tempUniverse.subNet == 0) { // check for correct net and subnet
tempUniverse.dmxDataLength = udpData[15] << 8 + udpData[16];
int j = 0;
for(int i=17; i<530; i++) {
tempUniverse.dmxData[j] = udpData[i];
j++;
}
if (tempUniverse.universe == 0) { // find which universe to modify
universe1 = tempUniverse;
} else if (tempUniverse.universe == 1) {
universe2 = tempUniverse;
} else if (tempUniverse.universe == 2) {
universe3 = tempUniverse;
} else if (tempUniverse.universe == 3) {
universe4 = tempUniverse;
}
}
}
}
dmxSendData(universe1.dmxData, universe2.dmxData, universe3.dmxData, universe4.dmxData);
}
I have made the change suggested by the 605 bug solutions, however it didn't work. I don't think that is what is causing me problems. It is not that I am getting random data, or incorrect data, but that there is no data being received by the Arduino at all.
I could really use some help with this. It seems that the issues are only affecting UDP traffic, and not TCP, because the webserver example works fine. Is the ethernet shield not able to receive broadcast UDP packets? do they need to be unicast only? Any other thoughts as to what might be causing this?