I'm trying to communicate using UDP an Arduino UNO + Ethernet Board with a PC, but can't do it properly.
Facts:
- Arduino code has 2 blocks: first block send a constant message each 5 seconds to the PC. Second block has been implemented as a ECHO (returns what receives)
- UDP is correctly initializated
- At begining, Arduino starts sending the message "Hello PC!" each 5 seconds, but the UDP.beginPacket doesn't return 1 (as it is supposed to do when it works properly).
- in the PC side I have wireshark and PacketSender to check the tx/rx UDP communication.
- Wireshart doesn't detect any incoming packages from Arduino when it has been resetted.
- A UDP package "Hello arduino!" is sent from the PC to Arduino, throught PacketSender. This packed is received correctly in the Arduino and sent back to PC. Wireshark identify tx UDP packet from PC and the same rx UDP packet from Arduino. This works properly.
- Buuuuuuuuuttttttt...... then, after sending a UDP message from the PC and receive the ECHO, the messages from Arduino each 5 seconds start reaching the PC.
- First message received on PC after the ECHO, has 21 bytes (although the message sended is supposed to have 11 bytes). The message received is "C!hello hellhello PC!", so there is some kind of buffer trash with "hello PC!" at the end.
- The weird thing is that after this, each 5 seconds the PC receives an UDP packet, each time 9 bytes longer (hello PC! is 9 bytes message), and as in the first message received from Arduino, at the end of the message you can find "hello PC!". Here is an example of the 9th message received "PC!hello PC!!lhello PC!45hello!llo!helhello PC!hellhello!ohello PC!hello Phello PC!" (95 bytes length).
- I use the standard Arduino Ethernet libraries, so I don't know what is happening and why it doesn't send anything until first message arrives to Arduino (and no idea why it is increasing each time it is sent).
- I tested Arduino with Raspberry with Raspbian to ensure the problem wasn't on the PC side, and the Raspberry received the same erratic UDP behaviour.
Arduino Code:
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: [email protected] 12/30/2008
byte mac\[\] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x3D};
IPAddress ip(192, 168, 1, 6); //IP of the arduino ethernet board
char link_IP\[\] = {192, 168, 1, 3}; //IP of the PC
unsigned int localPort = 51115; // local port to listen on
char message\[\] = "hello PC!"; //message intended to sended from the arduino
char packetBuffer\[UDP_TX_PACKET_MAX_SIZE\]; // UDP_TX_PACKET_MAX_SIZE = 24
unsigned long millis_before = millis(); //variable for delaying 5 seconds the message
EthernetUDP Udp;
void setup()
{
Serial.begin(9600); //serial com for debugging
Ethernet.begin(mac, ip); //starting Ethernet
if (Udp.begin(localPort)==1) Serial.println("port UDP is open"); //starting UDP on 51115
else while(1); //if UDP not open, infinte bucle
}
void loop()
{
if (millis_before + 5000 < millis()) //loop for waiting 5 seconds between messages
{
if (Udp.beginPacket(link_IP, 51115)==1) Serial.print("TX ready - "); // UDP packet to PC port 51115, if beginPacket returns 1 is OK
Serial.print("Lenght:");
Serial.print(Udp.write(message, 9)); //returns the length of the message
if (Udp.endPacket()==1) Serial.println(" - Sent!"); //if endPacket returns 1, is OK
millis_before = millis(); //reset timer
}
delay(10);
int packetSize = Udp.parsePacket(); //check size of UDP packet
if (packetSize)
{
IPAddress remote = Udp.remoteIP(); //save remote IP
for (int i = 0; i < 4; i++) //bucle for printing the remote IP
{
Serial.print(remote\[i\], DEC);
if (i < 3) Serial.print(".");
}
Serial.print(", port ");
Serial.println(Udp.remotePort()); //printing the remote port
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer
// send a echo to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(packetBuffer, packetSize);
Udp.endPacket();
}
}
Here you can find the PacketSender and Wireshark captures after sending the first message to Arduino and receive the ECHO:
I apreciate any help. Thanks in advance