Hello,
I've made a code to send data from one arduino to another one with Ethernet shields.
here is the code
The one that sends: #include <SPI.h> #include <Ethernet.h> #include <EthernetUdp.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x80, 0x0A, 0x9B
};
IPAddress ip(192, 168, 0, 77);
IPAddress IP_Remote(192, 168, 0, 98);
unsigned int localPort = 6101;
unsigned int Port_Remote = 12345;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char ReplyBuffer[] = "acknowledged";
EthernetUDP Udp;
void setup() {
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void loop() {
int test = analogRead(A2);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(test);
Udp.endPacket();
delay(10);
}
the one that receive:
#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
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0x14, 0x50
};
IPAddress ip(192, 168, 0, 98);
IPAddress IP_Remote(192, 168, 0, 77);
unsigned int localPort = 12345;
unsigned int Port_Remote = 6101;// local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
EthernetUDP Udp;
void setup() {
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);
int a = atoi(packetBuffer);
Serial.println(a);
}
delay(10);
}
My problem is that at the begining it's almost send instantly but if i let the Arduinos work for like 10 min the delay increases a lot and I reach something like 14s of delay.
Is it because of the arduino's that aren't powerful enough or is something else?
Thank you.
I think I know why you are using delay() with the sender (to avoid sending more than 100 packets per second).
But why is there a delay() also with the receiver()?
Using delay() is bad programming practice in both cases.
But using delay() with the receiver could fill up the receive buffers (sender and receiver are not synchronized in any way) and therefore must be avoided to avoid outbraking the receiver.
Get rid of the delay in your receiver sketch. This is probably what's causing the issues.
Put a '\0' at the end of the buffer, so that the string functions work properly.
You are reserving 65k of arduino memory to read packets that you know are only going to be a few bytes. Use a smaller buffer, and discard packets that are too big.
Add a longer delay in your sender sketch. If it works with a longer delay, reduce the delay until it starts to lock up again.
char packetBuffer[20]; //buffer to hold incoming packet, enough room for a number
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize > 0 && packetSize < sizeof(packetBuffer)) {
Udp.read(packetBuffer, sizeof(packetBuffer)-1); // leave room for the '\0'
packetBuffer[packetSize] = '\0';
int a = atoi(packetBuffer);
Serial.println(a);
}
}
Consider sending your packets as binary. Just be sure you are getting "endian-ness" right. This will work ok if both boards are arduinos, but it's best to be explicit:
Sender:
void loop() {
int test = analogRead(A2);
byte b[2];
Udp.beginPacket(IP_Remote, Port_Remote);
b[0] = (test>>8) & 0xFF;
b[1] = test & 0xFF;
Udp.write(b, 2);
Udp.endPacket();
delay(10);
}
Reciever:
char packetBuffer[2]; //buffer to hold incoming packet, enough room for a number
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize ==2) {
Udp.read(packetBuffer, 2);
int a = (((int)packetBuffer[0]) << 8) | (packetBuffer[1]);
Serial.println(a);
}
}
If you decide to cheat and rely on the fact that both boards are AVR, then the code is simpler:
Sender:
void loop() {
int test = analogRead(A2);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write((byte*)&test, sizeof(int));
Udp.endPacket();
delay(10);
}
Reciever:
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize ==2) {
int a;
Udp.read((byte*)&a, sizeof(int));
Serial.println(a);
}
}