-1

Im trying yo send wav files between two nodeMCU. My setup is like this uSD Card Module -> Arduino Mega -> Node MCU

So this is my setup, where I have 4 devices like this. One will serve as the central network(where I called it "server") where it will create an access point for the 3 other devices("modules") to connect to. The server will send data by using UDP so it can broadcast to the three modules simultaneously. I'm able to send .txt files which is stored on the uSD module. Pass the .txt file contents to the nodeMCU through serial port, then recieve it on the other NodeMCU which will then send the data to the Arduino Mega also through serial port and will store it on its uSD Shield.

When I would try .wav files. It is able to send the whole .wav file to the NodeMCU from Arduino Mega (since .wav files are too big, I divide the .wav files into sets since the serial port can't handle too large data being sent/recieved. But when it would send the data from the nodeMCU to the other nodeMCU there are parts which are not being sent. When I try to observe what are not being sent, its part of the WAV Header Format, so it needs to be sent. Ill be posting the codes Im using to send and recieve and also a screenshot/example of the wav file as it is being sent.

Server's Arduino Mega Sending data to NodeMCU Serially:

void sendFiletoSerial(char fileSend[12]){
  Serial.println();
  Serial1.println();
  if (!myFile.open(fileSend, O_READ)) {
    sdf.errorHalt("opening test.txt for read failed");
  }
  Serial.print(fileSend);
  Serial.print(" Size:");
  Serial.println(myFile.fileSize());

  Serial1.print(fileSend);
  Serial1.print(" Size:");
  Serial1.println(myFile.fileSize());
  delay(1500);
  fileofSize = myFile.fileSize();
  myFile.close();
  // read from the file until there's nothing else in it:
  char data;
  long int index=0;
    for(int i =0; i<(fileofSize/tempSize);i++){
    if (!myFile.open(fileSend, O_READ)) {
    sdf.errorHalt("opening test.txt for read failed");
  }
    //Serial.println(i);
    myFile.seekSet(index);
  for(int j =0; j<tempSize;j++){
  int temp = myFile.read();
 intTempFile[j]=temp;
  index++;

  }
  for(int k =0;k<tempSize;k++){
  Serial1.write(intTempFile[k]);
  }
    myFile.close();
    delay(1500);
  }
  Serial.println("LAST SET OF DATA");
    if (!myFile.open(fileSend, O_READ)) {
    sdf.errorHalt("opening test.txt for read failed");
  }
    myFile.seekSet(index);
    int lastSetSize=fileofSize-((fileofSize/tempSize)*tempSize);
    Serial.println(fileofSize-((fileofSize/tempSize)*tempSize));
  for(int j =0; j<lastSetSize;j++){
 int temp = myFile.read();

 intTempFile[j]=temp;
  index++;

  }
  Serial.println();
  Serial.println("tempFile");
  for(int k =0;k<lastSetSize;k++){
  Serial1.write(intTempFile[k]);
  }
    myFile.close();

}

Server's code for Sending from NodeMCU to other Node MCU:

    if (Serial.available()) {
          byte readStr[100];
        Serial.readBytes(readStr,100);
       // Serial.write(readStr[i]);
        }

         port.beginPacket("192.168.4.255",2390);
         for(int i =0; i <100; i++){
        port.write(readStr[i]);
         }
         port.endPacket();
      }

Module's Code for NodeMCU to recieve data from other NodeMCU:

void dumpMode(){
  while(1){
    int packetSize = port.parsePacket();
      if (packetSize) {
        port.read(packetBuffer, fileSentSize);
        Serial.write(packetBuffer);

  }
  if(Serial.available()){
    String reply = Serial.readString();
    if(reply == "STOP"){
      break;
    }

    }
  }
}

Server's NodeMCU Sent Data:

RIFFäN  WAVEfmt      À]  À]    dataÀN  €€€€€€€€€€€€€€€€€€€€€€€€

Module's NodeMCU Recieved Data:

RIFFäN€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€

Is there something im doing wrong thats why NodeMCU is not either sending or recieving every content of the wav file?

Thank you for those who will help out. :)

0

You could just program the NodeMCUs and skip the Megas; they have sufficient pins to drive an SD card. There's so much wrong with your code: opening and closing the same file so many times and then having to seek for every chunk because of the previous nastiness, writing and reading bytes singly instead of just passing in a buffer, among others. I also hope you know that you didn't send only the WAV data to the transmitting NodeMCU; there's a bunch of stuff (CRLF, file size, etc) before that.

Your particular problem's most likely the fact that your Mega "server" is just sending chunks continuously to the NodeMCU, never minding if the latter is reading them quickly enough that its serial buffer doesn't overflow, especially considering you're sending data over the network between chunks. You should also note that UDP isn't exactly the most reliable protocol; it's connection-less and so packets will be lost. It's generally used if packet loss isn't a big deal (in your case, it is) or if upper layers implement error control.

What you need to implement is a way for each party to confirm that they've received the next chunk before the their sender transmits the next. The Mega needs to wait for a 'go-ahead' from the NodeMCU before sending the next chunk; for instance, the NodeMCU can reply each chunk with the number of bytes it actually read, so you can know if you should reduce your chunk size to not overwhelm the NodeMCU buffer.

The packets sent over the network also need to be tagged with a sequence number (because of aforementioned error control). The NodeMCU at the receiver needs to acknowledge the packet it received each packet, perhaps by sending that packet's number to the transmitting NodeMCU. This will help ensure all packets are received and in order. Or you could just use TCP which already implements these and more.

  • Actually I also have an RFID Scanner connected to the Mega and also playing some WAV Files with the arduino, thats why I still need to use an Mega + NodeMCU. The "CRLF, file size, etc" you've mentioned is the 44byte Wav Header Right? I am also trying to send that but since its that part thats not being sent, it cant create the wav file completely. I chose UDP over TCP since I need to broadcast the data between the 3 modules. Based from my researches UDP can implement broadcast while TCP can' but if im wrong please do tell me. – boomacoh May 7 '17 at 2:34
  • @boomacoh The stuff I mentioned is not part of the official header unless you mean its your own customized header to help parse the data. TCP isn't built for broadcast yes, but if you use UDP, you'll have to find a way to handle lost packets. – TisteAndii May 7 '17 at 6:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.