Post topics, source code that relate to the Arduino Platform

User avatar
By Twoaster
#48576 Hi All, I am developing a program that downloads an audio file from a website and save it into a SD card on my DUE platform. I am using the Tx port os the ESP8266 and the RX port of the DUE to transfer the data.
I am having issues with the audio transfer.
Everytime that I run my sketch, I receive a different combination of characters that does not match the original file. I thought it could be the serial buffer overflow but I don´t think that is the problem because I can transfer .TXT files with no problem.
Do you guys have any thoughts of what can be happening? There is any way that I can check if each character received match the original?
The code is bellow:
Code: Select all#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

// I only use Firebase to get the URL
#define FIREBASE_HOST "***.firebaseio.com"
#define FIREBASE_AUTH "****"
#define WIFI_SSID "**"
#define WIFI_PASSWORD "***"
const char* host = "textfiles.com"; //"cyanu.com";
String URL;

WiFiClient client;

void setup() {
  Serial.begin(115200);

  // connect to wifi.
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  Serial.println(WiFi.localIP());

  // CONNECT TO FIREBASE
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  delay(1000);
  //RETRIEVE URL
  Serial.println("URL: ");
  URL = Firebase.getString("/mensagem/url");
  Serial.println(URL);
  delay(1000);
  // CONNECT TO CYANU FOR DOWNLOADING
 
  Serial.print("connecting to ");
  Serial.println(host);
 
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }else{Serial.println("Connection Succeeded");}

    // REQUEST DOWNLOAD OF THE FILE
  client.print(String("GET ")  + URL +" HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
               
          unsigned long timeout = millis();
          while (client.available() == 0) {
            if (millis() - timeout > 5000) {
              Serial.println(">>> Client Timeout !");
              client.stop();
              return;
              }
        }
        // Read all the lines of the reply from server and print them to Serial
        Serial.println('+');
        String line;
        while(client.connected()){
        line = client.readStringUntil('\r');
        Serial.print(line); 
        }
     
      Serial.println("");
      Serial.println("");
      Serial.println("closing connection");
     
int n = 0;
}
void loop() {
}


And here is the DUE code

Code: Select all#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <SdFat.h>
#include <SdFatUtil.h>

 
//////////////////////////////
// SD CARD DEFINITIONS      //
//////////////////////////////
#define CARDCS 4           // Card chip select pin
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

File fh;
File root;
char filename[15];
unsigned long timeOUT;
void setup()
{
  Serial3.begin(115200);
  Serial.begin(115200);
  Serial.println("iniciou");
 
  initializeSD();
  timeOUT = millis()+ 10000;
}
bool Write;
bool runn = true;
void loop()
{
  while(runn)
  {
    if(Serial3.available())
  {   
     timeOUT = millis()+ 6000;
     char rx = Serial3.read();
     Serial.print(rx);
     fh.print(rx);
  }else{
        if(millis() > timeOUT){     
                        fh.close();
                        Serial.println("file is closed");
                        runn = false;
                        break;
                       }
       }
  }
}

void initializeSD()
{
  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));
  if(!SD.begin(CARDCS)){
    Serial.println("SD initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  root = SD.open("/");
    strcpy(filename, "RECORD00.txt");
    for (uint8_t i = 0; i < 100; i++) {
      filename[6] = '0' + i/10;
      filename[7] = '0' + i%10;
      if (! SD.exists(filename)) {
        break;
      }
    }
    fh = SD.open(filename, FILE_WRITE);
            if(!fh)
          {
          Serial.println(F("SD file oppening fail")); 
          }
    printDirectory(root, 0);
}

// errorLoop prints an error code, then loops forever.
void errorLoop(int error)
{
  Serial.print(F("Error: ")); Serial.println(error);
  Serial.println(F("Looping forever."));
  for (;;)
    ;
}

// serialTrigger prints a message, then waits for something
// to come in from the serial port.
void serialTrigger(String message)
{
  Serial.println();
  Serial.println(message);
  Serial.println();
  while (!Serial.available());
  while (Serial.available())
    Serial.read();
}
 
void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      Serial.println("no more files");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}


Thanks for the help!