Go Down

Topic: Problem with Ethernet UDP (Read 939 times) previous topic - next topic

pcirillo

Hi,
I am new member of the forum and of Arduino community and I hope you will help me. I'm working on a simple project: I have to read twelve analog channels and send the acquired data using an UDP datagram. I'm using Arduino Due board and the Ethernet Shield rev 3 but I have a problem. When I send too much data in a short time Arduino freezes.

My code is below:

Code: [Select]

#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[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 0, 100);

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

IPAddress RemoteIP(192, 168, 0, 4);
unsigned int RemotePort = 8888;

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

const int numADC_Channels = 12;
const int numIO_Channels = 12;
const int initialIO_Channel = 22;

const int timeDelayUS = 200;

const int numTaxels = numADC_Channels * numIO_Channels;

int sensorValue[numTaxels];        // value read from the pot
uint8_t adcVarHighByte[numTaxels];
uint8_t adcVarLowByte[numTaxels];

char UDP_Packet_Buffer[2 * numTaxels];

void setup() {
 Serial.begin(115200);
   
 // start the Ethernet and UDP:
 Ethernet.begin(mac, ip);
 Udp.begin(localPort);

 analogReadResolution(12);  // Set 12 bit adc resolution
 pinMode(A0, INPUT);
 pinMode(A1, INPUT);
 pinMode(A2, INPUT);
 pinMode(A3, INPUT);
 pinMode(A4, INPUT);
 pinMode(A5, INPUT);
 pinMode(A6, INPUT);
 pinMode(A7, INPUT);
 pinMode(A8, INPUT);
 pinMode(A9, INPUT);
 pinMode(A10, INPUT);
 pinMode(A11, INPUT);

 pinMode(22, OUTPUT);
 pinMode(23, OUTPUT);
 pinMode(24, OUTPUT);
 pinMode(25, OUTPUT);
 pinMode(26, OUTPUT);
 pinMode(27, OUTPUT);
 pinMode(28, OUTPUT);
 pinMode(29, OUTPUT);
 pinMode(30, OUTPUT);
 pinMode(31, OUTPUT);
 pinMode(32, OUTPUT);
 pinMode(33, OUTPUT);

 digitalWrite(22, LOW);
 digitalWrite(23, LOW);
 digitalWrite(24, LOW);
 digitalWrite(25, LOW);
 digitalWrite(26, LOW);
 digitalWrite(27, LOW);
 digitalWrite(28, LOW);
 digitalWrite(29, LOW);
 digitalWrite(30, LOW);
 digitalWrite(31, LOW);
 digitalWrite(32, LOW);
 digitalWrite(33, LOW);
}

void loop() {
 for (int i = 0; i < numIO_Channels; i++) {
   // set on output pin:
   digitalWrite(initialIO_Channel + i, HIGH);

   // pauses to stabilize the input channel
   delayMicroseconds(timeDelayUS);

   // read the analog channels:
   sensorValue[i * numADC_Channels] = analogRead(A0);
   sensorValue[i * numADC_Channels + 1] = analogRead(A1);
   sensorValue[i * numADC_Channels + 2] = analogRead(A2);
   sensorValue[i * numADC_Channels + 3] = analogRead(A3);

   sensorValue[i * numADC_Channels + 4] = analogRead(A4);
   sensorValue[i * numADC_Channels + 5] = analogRead(A5);
   sensorValue[i * numADC_Channels + 6] = analogRead(A6);
   sensorValue[i * numADC_Channels + 7] = analogRead(A7);

   sensorValue[i * numADC_Channels + 8] = analogRead(A8);
   sensorValue[i * numADC_Channels + 9] = analogRead(A9);
   sensorValue[i * numADC_Channels + 10] = analogRead(A10);
   sensorValue[i * numADC_Channels + 11] = analogRead(A11);

   // set off output pin:
   digitalWrite(initialIO_Channel + i, LOW);
 }

 for (int i = 0; i < numTaxels; i++) {        <----------------------------- A
   // split acquired sensorValue into two bytes
   adcVarLowByte[i] = sensorValue[i] & 0xFF;
   adcVarHighByte[i] = (sensorValue[i] >> 8) & 0x0F;

   UDP_Packet_Buffer[i * 2] = adcVarLowByte[i];
   UDP_Packet_Buffer[i * 2 + 1] = adcVarHighByte[i];
 }

 // send UDP packet
 Udp.beginPacket(RemoteIP, RemotePort);
 Udp.write(UDP_Packet_Buffer);
 Udp.endPacket();
 
 delay(20);            <------------------- B

}


In details, I have to send 288 bytes using an UDP datagram. If I remove or decrease the delay indicated as B, Arduino freezes. Instead, removing that delay, Arduino works well only if I reduce the number of the bytes in the UDP packet (reducing the numTaxels value indicated as A in the last for loop).

How can I solve?

Thank you,

Pasquale

Go Up