0

I wrote a code that can send a file. If I send a text file it will work perfectly, but when I try to send voice it will send the whole file form the Sender side, but in the receiver side he won't understand some data or he won't read it i don't know what happened i tried to send 50k byte and the receiver only receive 20K . I tried to send .wav .mp3 .m4p.

the Sender code

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






const int push1 = 2;


File myFile;
char a[168];
int c= 0; 
int counter = 0 ;
unsigned long data; 
unsigned long result;
unsigned long remaining ; 


// 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;


// timing



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

    pinMode(push1, 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
  }

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

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


  // 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());



}

// do tasks
void loop() {

if(digitalRead(push1) == LOW){


    myFile = SD.open("SV12.wav");
     data = myFile.size();

//     result = myFile.position();


  if (myFile) {
    Serial.println("inside the file there's");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      a[c] = myFile.read();  
       Serial.print(a[c]);
       c++;
      if ( c==168){
        Serial.println("");
      Serial.println("i will send now");
      Udp.beginPacket(destIp, port);
      Udp.write(a,168);    
      Udp.endPacket();
      delay(250); 
      Serial.println("Sending UDP message");
       counter++;
       Serial.print("Sending packet#");
       Serial.println(counter);
//         for (int x = 0 ; x<c ; x++){
// 
//         a[x] = '\0';}
         c=0;

  Serial.println("clearing");}



//  Serial.println(result);
    }

          if ( c<168 ){ // if (remaining)
      Serial.println("i will send now");
      Udp.beginPacket(destIp, port);
      Udp.write(a,168);
      Udp.endPacket();
      Serial.println("Sending UDP message Last segment");
//      counter++;
//         for (int x = 0 ; x<c ; x++){
// 
//         a[x] = '\0';}
         c=0;}


    // close the file:
    myFile.close();
     Serial.println("im here");
    Serial.println(counter);
    Serial.println("END");
  } else {
   // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
 }


}
}

the Reciver code

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


int LED = 8;
int x=0;

// 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 // 123456

File myFile;
char y[100];
int c= 0; 

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

char packetBuffer[168]; //buffer to hold incoming packet,
//UDP_TX_PACKET_MAX_SIZE
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());


    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, 168);
    Serial.println("Contents:");



    RandS();
    Serial.println(x); //to check the message


  }

}

void RandS(){

   myFile = SD.open("Last123.mp3", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.println("i'm Writing what i got from UDP");
    myFile.print(packetBuffer);
    x++;
    // close the file:
    myFile.close();

    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

}

Can someone help please?

  • Could you show the serial output? – Gerben Mar 7 at 16:21
  • Check: Does Udp.write(a) terminate on zero? Shouldn't it be something like Udp.write(a, c)? – Mikael Patel Mar 7 at 19:45
  • how do you know that the problem is caused by the type of file? ..... have you verified that the problem is not related to file size? – jsotola Mar 8 at 0:52
  • I kind of solved the old problem and other problem showed up, i modify the question above hope you can see it and help, thx. – khalid Mar 10 at 14:01
  • Hint: Add a sequence number and you might be able to detect dropped UDP packages. You might want to use TCP instead. – Mikael Patel Mar 10 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.