1

I've a code which records sound to SD, then starts an FTP connection for a limited time. I want to make an automatic system which records then sends the sound file continuously. Now I'm recording sound for 3s, then opening a FTP connection for 3s. In that 3s interval, my python code (PC) is trying to download the data in ESP (it also runs continuously. However, sometimes if ftp time is not enough, my connection is interrupting and ESP is resetting itself. So, I dont want to fetch this file via my computer, ESP32 should send this file after the recording, then it should start recording after the successful connection. I couldn't configured the code in the Arduino Playground I pasted my code below, waiting for suggestions for valid examples. You can find the libraries here

#include <WiFi.h>
#include <WiFiClient.h>
#include "ESP32FtpServer.h"
#include <FS.h>
#include <SD.h>
#include "Wav.h"
#include "I2S.h"

const int record_time = 3;  // second
const char filename[] = "/sound.wav";
const int headerSize = 44;
const int waveDataSize = record_time * 88000;
const int numCommunicationData = 8000;
const int numPartWavData = numCommunicationData / 4;
byte header[headerSize];
char communicationData[numCommunicationData];
char partWavData[numPartWavData];
unsigned long starttime;
unsigned long endtime;
File file;

const char* ssid = "*****";
const char* password = "*****";

FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP32FtpServer.h to see ftp verbose on serial

void setup(void) {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  /////FTP Setup, ensure SD is started before ftp;  /////////
  if (SD.begin()) {
    Serial.println("SD opened");
    ftpSrv.begin("esp32", "esp32");   //username, password for ftp.  set ports in ESP32FtpServer.h  (default 21, 50009 for PASV)
  }
  CreateWavHeader(header, waveDataSize);

}

void loop(void) {

  SD.remove(filename);
  file = SD.open(filename, FILE_WRITE);
  if (!file) return;
  Serial.println("recording");
  file.write(header, headerSize);
  I2S_Init(I2S_MODE_RX, I2S_BITS_PER_SAMPLE_32BIT);
  for (int j = 0; j < waveDataSize / numPartWavData; ++j) {
    I2S_Read(communicationData, numCommunicationData);
    for (int i = 0; i < numCommunicationData / 8; ++i) {
      partWavData[2 * i] = communicationData[8 * i + 2];
      partWavData[2 * i + 1] = communicationData[8 * i + 3];
    }
    file.write((const byte*)partWavData, numPartWavData);
  }
  file.close();

  Serial.println("recording finished, ftp starting");

  starttime = millis();
  endtime = starttime;
  while ((endtime - starttime) <= 3000) // do this loop for up to 1000mS
  {
    ftpSrv.handleFTP();        //make sure in loop you call handleFTP()!!
    endtime = millis();
  }
  Serial.println("ftp finished");

}
  • Your code does not compile. Please edit your question and ensure all of the relevant includes are in the sketch. – VE7JRO May 10 at 1:52
  • @VE7JRO edited the question – Selim Turkoglu May 10 at 13:15
  • you need to wait on the FTP to finish before moving on – dandavis May 10 at 17:58
  • @dandavis ftp transfer is handling by pc and its time changes in every connection, if connection is slow, it could take 15s or more, however esp opens tunnel without knowing this time. how can I adjust it? best option is sending file from esp with a code – Selim Turkoglu May 10 at 19:30
  • What protocols are available to you on the server? Only FTP? is there HTTP for example? any others? Seems FTP isn't suited to the solution – Jaromanda X May 11 at 2:29

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.