Shareing .txt file .wav file

hi, everyone, i’m working in a project, i want to share .txt and .wav file between two Arduino

at the beginning, i just try to make two Arduino communicate ( First Arduino can control the other Arduino LED) via the network using an Ethernet Shield

anyway the Shield has an SD card, so i was trying to write a text message from the first
Arduino and it’s work perfectly but the problem was at the receiver i just keep receiving an empty message

the Sender Code

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <EthernetUdp.h>

File myFile;


#define MSG_INTERVAL 1000





const int push1 = 2;
const int push2 = 3;
int count = 1;

// network parameters
byte mac[] = 
{
  0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x02
};  // ethernet interface MAC address
IPAddress localIp(10, 131, 41, 72);    // local ip address
IPAddress destIp(10, 131, 41, 74);      // destination ip address
unsigned int port = 9631;               // destination port

// EthernetUDP to send and receive messages.
EthernetUDP Udp;


char message[] = "ON" ;
char message1[] = "OFF" ;

 
// timing
unsigned long previousLedMillis;
unsigned long ledInterval;
unsigned long previousSendMillis;
unsigned long sendInterval;

// setup the arduino and shields
void setup() {

    pinMode(push1, INPUT); 
    pinMode(push2, INPUT);
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600); // 19200
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  }

 
  // start ethernet and udp
  Ethernet.begin(mac, localIp);   // static ip version
  //if(Ethernet.begin(mac) == 0) {     // dhcp version
  // report failure to obtain network parameters
  // Serial.println("DHCP error");

  // no point in carrying on, loop indefinitely:
  // while(true)
  // ;
  //}

  // open UDP port
  Udp.begin(port);

  // show the local ip address (useful for dhcp)
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());


  previousSendMillis = 0;

  // initialize send interval
  sendInterval = MSG_INTERVAL;

//  // initialize led1 state and interval
//  ledOn = false;
//  ledInterval = LED_OFF;
Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

 
}

// do tasks
void loop() {
 
  // check if udp string has to be sent
  if (millis() - previousSendMillis >= sendInterval) {
    sendMessage();
  }
}

// send udp string // 
void sendMessage() {
  
//    Serial.println(message);
    
    while((digitalRead(push1) == HIGH) && (digitalRead(push2) == HIGH) ){
      if(count == 1){
        Serial.println("i'm in the LOOP");
        Serial.println("Press on or off");
        count = 0;
     
      }
    }
    
    if(digitalRead(push1) == LOW){
     
      Serial.println("im ON");
     

       // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test1.txt", FILE_WRITE); // return boolean ,, true if it can open a file ,, 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
     Udp.beginPacket(destIp, port);
     Udp.write(myFile);
      Udp.endPacket();
      Serial.println("Sending UDP message");
    // close the file:
    myFile.close(); // Close the file, and ensure that any data written to it is physically saved to the SD card.
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  //---------------------------------------------------------------------

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
      
     }
     
    else if(digitalRead(push2) == LOW){
     
     Serial.println("im OFF");
    Udp.beginPacket(destIp, port);
     Udp.write(message1);
      Udp.endPacket();
      Serial.println("Sending UDP message");
    
       }
    
    
  // store current millis
  previousSendMillis = millis();

  // send udp message
  
}

The Reciver code

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SD.h>

File myFile;

int LED = 8;

// 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, 0x06
};
IPAddress ip(10, 131, 41, 74);
IPAddress remIp(10, 131, 41, 72);

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

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

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

void setup()
{
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);//change and try
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

}

void loop()
{
  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);


  myFile = SD.open("test1.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
   
    myFile.println(packetBuffer);
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  //--------
    myFile = SD.open("test1.txt");
  if (myFile) {
    Serial.println("test1.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

    
    Serial.println("Contents:");
    if (strcmp(packetBuffer, "ON" ) == 0) {
     Serial.print("packetSize = ");
     Serial.println(packetSize);
     Serial.println("the massge is  = ");
      Serial.println(packetBuffer[0]);
      Serial.println(packetBuffer[1]);
      digitalWrite(LED, HIGH);
      //delay(5000);
      Serial.println("LED is ON");
 for (int x = 0 ; x<UDP_TX_PACKET_MAX_SIZE ; x++){
        packetBuffer[x]= '\0';
      }
      //break;

    }
    else  if (strcmp(packetBuffer, "OFF" ) == 0) {
     Serial.print("packetSize = ");
     Serial.println(packetSize);
     Serial.println("the massge is  = ");
      Serial.println(packetBuffer[0]);
      Serial.println(packetBuffer[1]);
      Serial.println(packetBuffer[2]);
      digitalWrite(LED, LOW);
      // delay(5000);
      Serial.println("LED is OFF");
      //break;
      for (int x = 0 ; x<UDP_TX_PACKET_MAX_SIZE ; x++){
        packetBuffer[x]= '\0';
      }
    }
//    Serial.print("packetBuffer size ");
//       Serial.println(packetBuffer[0]);
//      Serial.println(packetBuffer[1]);
//      Serial.println(packetBuffer[2]);
    Serial.println("*END OF THE msg");//to know the end of message
  }

}

this what i got

Never, never post text output as a screen shot!

anyway the Shield has an SD card, so i was trying to write a text message from the first Arduino and it's work perfectly but the problem was at the receiver i just keep receiving an empty message

How can you tell us it works perfectly if the receiver doesn't receive the message? How do you know the message got transferred (hint: it doesn't)?

Udp.write() writes one byte (in the single parameter version) or a string if the argument is a character array. Interestingly you know how to handle files from the SD card as the code section that writes the content to the serial interface does it correctly. Why don't you use the same way to send it to the network?

Why do you use UDP communication? Use that only if you know what you're doing, in all other cases use standard TCP methods as these are more similar to the familiar serial communication channels.

pylon: Never, never post text output as a screen shot!

sorry but why?

pylon: How can you tell us it works perfectly if the receiver doesn't receive the message? How do you know the message got transferred (hint: it doesn't)?

what i meant if i send the message like this it'll work perfectly

    else if(digitalRead(push2) == LOW){
     
     Serial.println("im OFF");
    Udp.beginPacket(destIp, port);
     Udp.write(message1);
      Udp.endPacket();
      Serial.println("Sending UDP message");
    
       }

but if i try to send something from the SD card it won't work thx.

sorry but why?

Take a look at the picture you posted that should already give enough reason. There are many others: more network bandwith wasted, the picture is on an external hosting service, so it might disappear in a few weeks to months, I cannot copy/paste the content. Do you need more?

what i meant if i send the message like this it'll work perfectly

There you send a string.

but if i try to send something from the SD card it won't work

There you try to send a File object. Don't you see the difference? I already wrote that you know how to handle this as you do it with the serial interface (in the same code).