-->
Page 1 of 2

File naming in SPIFFS question

PostPosted: Thu Jan 24, 2019 4:29 pm
by kydra1
Hello,
I am trying to do some datalogging with a NodeMCU and SPIFFS. I have a RTC to time stamp each entry which works properly. The next step I am trying to do is create a new file for each day. I found an example for using a variable as the file name on SD card.
SD card example
Code: Select all#include <SD.h>

#include "RTClib.h"

#include <Wire.h>

#include <string.h>



RTC_DS1307 RTC;

char filename[] = "00000000.CSV";

File myFile;



void setup()

{

Serial.begin(9600);

Wire.begin(); //Important for RTClib.h

RTC.begin();


if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");

// following line sets the RTC to the date & time this sketch was compiled

// RTC.adjust(DateTime(__DATE__, __TIME__));

return;

}


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

// On the Ethernet Shield, CS is pin 4. It's set as an output by default.

// Note that even if it's not used as the CS pin, the hardware SS pin

// (10 on most Arduino boards, 53 on the Mega) must be left as an output

// or the SD library functions will not work.

pinMode(10, OUTPUT);



if (!SD.begin(4)) {

Serial.println("initialization failed!");

return;

}

Serial.println("initialization done.");



}



void loop()

{

getFileName();

createFileName();

delay(3000);

}



void getFileName(){

DateTime now = RTC.now();

filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()

filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()

filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()

filename[3] = now.year()%10 + '0'; //To get 4th digit from year()

filename[4] = now.month()/10 + '0'; //To get 1st digit from month()

filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()

filename[6] = now.day()/10 + '0'; //To get 1st digit from day()

filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()

Serial.println(filename);

}



void createFileName(){

//Check file name exist?

if (SD.exists(filename)) {

Serial.println("exists.");

}

else {

Serial.println("doesn't exist.");

Serial.println("Creating new file");

Serial.println(filename);

myFile = SD.open(filename, FILE_WRITE);

myFile.close();

}

}


I have tried to modify it to work in SPIFFS as follows
Code: Select all#define FS_NO_GLOBALS

#include <Wire.h>
#include "Adafruit_HTU21DF.h"
#include "FS.h"
#include "RTClib.h"
#include <string.h>

#ifdef ESP8266
#include <ESP8266WiFi.h>
  #elif defined ESP32
#include <WiFi.h>

#include "SPIFFS.h"
#endif

#include <ESP8266FtpServer.h>

const char* ssid = "*******";
const char* password = "*******";
uint32_t rate = 60;
uint32_t lastRead = 0;
char filename[] = "00000000.txt";

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

RTC_DS3231 rtc;

// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  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());
 
  Serial.println("HTU21D-F");

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }

 if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
 
  /////FTP Setup, ensure SPIFFS is started before ftp;  /////////
 
  /////FTP Setup, ensure SPIFFS is started before ftp;  /////////
 
#ifdef ESP32       //esp32 we send true to format spiffs if cannot mount
  if (SPIFFS.begin(true)) {
#elif defined ESP8266
  if (SPIFFS.begin()) {
#endif
 
 
     Serial.println("SPIFFS opened!");
      ftpSrv.begin("esp8266","esp8266");    //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }   
  }

void loop() {
 getFileName();
 Serial.print (filename);
 createFileName();
 
 DateTime now = rtc.now();
 
 if (now.unixtime() != 0){
  if(now.unixtime() - lastRead > rate) { 
 
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.year(), DEC);
  Serial.print("-");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);

 
  Serial.print("\tTemp:\t"); Serial.print(htu.readTemperature());
  Serial.print("\t\tHum:\t"); Serial.println(htu.readHumidity());

 
  fs::File f = SPIFFS.open("/"filename"", "a");
  f.print(now.month(), DEC);
  f.print('/');
  f.print(now.day(), DEC);
  f.print('/');
  f.print(now.year(), DEC);
  f.print("-");
  f.print(now.hour(), DEC);
  f.print(':');
  f.print(now.minute(), DEC);
  f.print(':');
  f.print(now.second(), DEC);
  f.print("\tTemp:\t"); f.print(htu.readTemperature());
  f.print("\t\tHum:\t "); f.println(htu.readHumidity());
  f.close();

  lastRead = now.unixtime();
  }
 }

  ftpSrv.handleFTP();        //make sure in loop you call handleFTP()!! 
 
}

void getFileName(){
DateTime now = rtc.now();
filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()

filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()

filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()

filename[3] = now.year()%10 + '0'; //To get 4th digit from year()

filename[4] = now.month()/10 + '0'; //To get 1st digit from month()

filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()

filename[6] = now.day()/10 + '0'; //To get 1st digit from day()

filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()

return filename;

}

void createFileName(){


fs::File f = SPIFFS.open("/"filename"", "a");

f.close();

}


When running the compiler I get error: expected ')' before 'filename'

fs::File f = SPIFFS.open("/"filename"", "a");

Am I wrong in how to call the variable "filename"? Any help would be appreciated.
Thanks

Re: File naming in SPIFFS question

PostPosted: Fri Jan 25, 2019 3:24 am
by QuickFix
Shouldn't that be:
Code: Select allfs::File f = SPIFFS.open("/filename", "a");
? :?

Re: File naming in SPIFFS question

PostPosted: Fri Jan 25, 2019 3:55 am
by btidey
Also you should't be using a return statement in the void function getFileName . You are using a global for filename so the attempted return is not needed.

It makes it clearer if you name the functions according to their purpose so getFileName would be a String or char* function that did actually return the filename whereas makeFileName would be a void that did set up the name like your function does.

Re: File naming in SPIFFS question

PostPosted: Fri Jan 25, 2019 8:22 am
by kydra1
QuickFix wrote:Shouldn't that be:
Code: Select allfs::File f = SPIFFS.open("/filename", "a");
? :?

That bit occurs twice within the code so once I got the error figured out for the other one I was going to fix there as well.