Site icon Renzo Mischianti

Use SD file system defined for EmailSender to create new files in SD

Hello, and many thanks to Renzo, for this wonderful project!! I have successfully installed EmailSender.h v3.0.13 in a d1Mini ESP8266 board with a SD card, using Arduino IDE V2.2.1. I only had to set the option “#define DEFAULT_EXTERNAL_ESP8266_STORAGE STORAGE_SD” in EmailSenderKey.h, all other options are left by default, I had to write a very short squetch and voila! it is sending to my email messages with file attachments from the SD! I am trying to do a data logger that should capture some data, append it to a file in the SD, and send the file periodically by email. It is a plain text file. This works very well, but only when the files are previously loaded in the SD. Until now I can not imagine how to access the SD to create or modify files with my own code in the same squetch. The SD file system defined by EmailSender is fine, it is working, because it reads files to attach and send them OK. The problem is that I don´t know which instance of SD FS should I use, which name give to my own file objects or how to access and use the same SD file system for my own purposes, to reference, open, read, write or close files in the SD. My squetch is as simple as that, and the code I don't know how to do is in loop() section: —————

#include "Arduino.h"

#include <EMailSender.h> // tested with version 3.0.13 (see https://github.com/xreef/EMailSender)

const char * ssid = " ** ** "; // edit to your own value
const char * password = " ** ** "; // edit to your own value

// global variables for this squetch
uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
const size_t txtsize = 60; // lenght for txt buffer including final {0}
char txt[txtsize] = ""; // auxiliary buffer for text formatting

// constructor for EMailSender
/EMailSender emailSend("***@gmail.com", "password", "myDev"); / / edit to your own values

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

  // connect to WiFi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // create message, subject is chip_id (int32) as hex string
  EMailSender::EMailMessage message;
  snprintf(txt, txtsize, " % s % X", "Message from", system_get_chip_id());
  message.subject = txt;
  message.mime = "text / html";
  message.message = "Ciao come stai < br > io bene. < br > www.mischianti.org";

  // define attachments
  EMailSender::FileDescriptior fileDescriptor[1]; // define only 1 attachment
  fileDescriptor[0].filename = "test.txt";
  fileDescriptor[0].url = "/test.txt";
  fileDescriptor[0].mime = "text / plain";
  fileDescriptor[0].encode64 = false;
  fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
  EMailSender::Attachments attachs = {
    1,
    fileDescriptor
  };

  // send message
  EMailSender::Response resp = emailSend.send("test @mundobiji.com > ", message, attachs);

  // print sending result
  Serial.println("Sending status: ");
  Serial.println(resp.status);
  Serial.println(resp.code);
  Serial.println(resp.desc);

} // end of setup()

void loop() {

  // This section compiles without errors but does not work !!!!!
  FsFile myfile;
  myfile.open("test.txt", O_RDWR | O_CREAT | O_AT_END); // open to append: this fails
  txt = "some new data";
  myfile.write(txt, strlen(txt)); // this should append new data
  myfile.close();

  // send file test.txt again by email with EmailSender
  // …

  // wait and repeat
  delay(60000);

} // end of loop()
————— Please, could you give me any ideas about that? How can I reference the SD File system created by EmailSender to create and use a new file? Thank you in advance!
Exit mobile version