Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writefile(string) #23

Closed
thquack opened this issue Jan 29, 2020 · 11 comments
Closed

writefile(string) #23

thquack opened this issue Jan 29, 2020 · 11 comments
Labels
enhancement New feature or request

Comments

@thquack
Copy link

thquack commented Jan 29, 2020

I am trying to use the USB as a csv data logger and I am not sure if I am off, but char writing seems a bit cumbersome, vs #variables particular with #number values such as int.

is it possible to directly write an integer?

The ch376msc guide states:
flashDrive.writefile(string, strlen(string)); //string, string length
//but string really this is really a char string

for data logging it is fairly straight forward to add int, floats, etc.

//standard sdFat.h then example is
file.print(float); file.println();

//CH376msc.h requires example
string.toCharArray(charstring,10)
//then
writefile(charstring, strlen(charstring));
`
this all gets quite cumbersome when dealing with multiple data variables including floating points.

@djuseeq
Copy link
Owner

djuseeq commented Jan 29, 2020

Hi, this lib can read and write char type of variables(c type string alias char array), so actually yes is possible converting to integer or vice versa just need to write these routines which make the conversions. If there is a need I can think about it and maybe write these functions.

@djuseeq djuseeq added the enhancement New feature or request label Jan 29, 2020
@djuseeq
Copy link
Owner

djuseeq commented Feb 3, 2020

Hi, if you can, please download and test this version of the library, some new functions and example are available, e.g. writing and reading integer/float numbers. But testing is required.

@thquack
Copy link
Author

thquack commented Feb 3, 2020

thanks!
I have downloaded the example. It compiles, but as a side note, it uses a lot of program storage space memory.
I will work to incorporate it into my data-logger and test it live...results to follow.

@thquack
Copy link
Author

thquack commented Feb 4, 2020

Ok, I integrated this into a generic test for a temperature controller data logger. The update works. Is it possible to /newline after a char string?

File:

#include <SPI.h>            //SPI bus
#include <Ch376msc.h>         //USBFLASH 
#include <SoftwareSerial.h>   //serial debug

//pins
#define USBFLASH_CS   5
#define USBFLASH_INT  8

//---------------------------------------------------------------------
//USBFLASH 
Ch376msc flashDrive(USBFLASH_CS, USBFLASH_INT); // chipSelect, interrupt pin
boolean usbError = false;

//filename creation
#define USB_FILE_BASE_NAME "FDdata"  //base file name up to 6 characters
const uint8_t BASE_NAME_SIZE = sizeof(USB_FILE_BASE_NAME) - 1; 
char USBfileName[13] = USB_FILE_BASE_NAME "00.csv"; //logfile naming

// buffer for reading
char adatBuffer[255];// max length 255 = 254 char + 1 NULL character
boolean readMore;

char headerString[] = "Time(ms),record,Set_Temp,Mode,Temp_Read,PID_percent,P,I,D\0"; //the csv header
char commaString[] = ","; //a comma for CSV

//Sample temperature controller variables
char mode[8] = "Auto";  //mode text for log
float temp_read = 60.0; //the tempature reading
byte set_temp = 153;  //the set tempature
float PID_value = 0; //PID Duty in % 
volatile static uint32_t elapsedTime, timePrev, logTime, Time = 0, now = 0, last_time, elapsedTime_log, timePrev_log; //real time clock
//output values
float PID_p = 0;    float PID_i = 0;    float PID_d = 0;

int dd = 0;  //log record counter

word Ts;  //PID update sampletime in sec (user inputs)
word loop_rate; //main loop rate
word log_data_now;  //lof file sample rate

//---------------------------------------------------------------------------------------------
void setup() {
Serial.begin(57600);  //Serial.println("hello, Serial is up");
//USBflash
flashDrive.init();
if(flashDrive.getDeviceStatus()){
     usbError=false;
    } else {
      usbError=true;
      Serial.print("USB ERROR at Init");
      exit(0);
    }
checkUSB();

now=millis(); // read RTC initial value
elapsedTime = now; // prepare for next RTC delay loop 
loop_rate = 200;  // 5Hz main loop (ms)
Ts = 5; //5 seconds PID loop (sec)
log_data_now = 1;//log to USB sample time (sec)

Serial.print("Starting at File:  ");
Serial.println(USBfileName);
flashDrive.setFileName(USBfileName);

//log to a unique file (add 1 and look)
while (flashDrive.openFile() == ANSW_USB_INT_SUCCESS)   {
    Serial.print("Found File:  ");
    Serial.println(USBfileName);
    if (USBfileName[BASE_NAME_SIZE + 1] != '9') {
      USBfileName[BASE_NAME_SIZE + 1]++;
    } else if (USBfileName[BASE_NAME_SIZE] != '9') {
      USBfileName[BASE_NAME_SIZE + 1] = '0';
      USBfileName[BASE_NAME_SIZE]++;
    } else {
      Serial.print("Can't create file name");
      usbError = true;
    }
    flashDrive.closeFile();
    flashDrive.setFileName(USBfileName);
  }
// put your setup code here, to run once:
flashDrive.closeFile();
flashDrive.setFileName(USBfileName);
Serial.print("Here we go, writing to:  ");
Serial.println(USBfileName);
Serial.println("Write header...");
if(flashDrive.driveReady()){
writeHeaderUSB();
  } else {
     Serial.println(F("Drive error"));
  }//end drive ready 
}

//---------------------------------------------------------------------------------------------
void loop() {
Time = millis();
temp_read = random(33, 55) * (PI);

//  this is where the main program function would reside, to simplify, I have random generated some //PID feedback caculations
elapsedTime = (Time - timePrev) * 0.001;   
if (elapsedTime >= Ts) {  //is it time to recalculate gains?
PID_value = random(0, 100);  //  100% duty cycle, GAMMA
PID_p = random(-10000, 10000)* (PI);  //Controller gain Sim
PID_i = random(-10000, 10000)* (PI);  //time integral gain sim
PID_d = random(-10000, 10000)* (PI);  //time Derivative gain sim
timePrev = Time;  /reset the PID calculation update rate
}

elapsedTime_log = (Time - timePrev_log) * 0.001; 
if (elapsedTime_log >= log_data_now) {
++dd;  //next record
logDataUSB(); // write data record
timePrev_log = Time;  //reset the log timer
}
if (Time * 0.001 >= 40) {   //end test, print file to serial
printfile();
exit(0);
}

do {now=millis();} while (now-last_time<loop_rate);  //a real time delay
last_time=now; // prepare for next loop 
}//end of main loop
//--------------------------------------------------------------------------------------------
void printfile(){
Serial.print("print file: ");
Serial.println(USBfileName);
flashDrive.setFileName(USBfileName);  //set the file name
        flashDrive.openFile();                //open the file
        readMore = true;
                //read data from flash drive until we reach EOF
        while(readMore){ // our temporary buffer where we read data from flash drive and the size of that buffer
          readMore = flashDrive.readFile(adatBuffer, sizeof(adatBuffer));
          Serial.println(adatBuffer);          //print the contents of the temporary buffer
        }
        flashDrive.closeFile();               //at the end, close the file
               
}
void checkUSB(){
if(flashDrive.checkIntMessage()){
    if(flashDrive.getDeviceStatus()){
      Serial.println(F("Flash drive attached!"));
    } else {
      Serial.println(F("Flash drive detached!"));
      exit(0);
    }
}

}
//--------------------------------------------------------------------------------
// Write data header USB.
void writeHeaderUSB() {
  Serial.print("in header, writing: ");
  Serial.println(headerString);
  flashDrive.setFileName(USBfileName);  //set the file name
  flashDrive.openFile();
   if(flashDrive.getFreeSectors()){ //check the free space on the drive
    flashDrive.writeFile(headerString, strlen(headerString)); //string, string length
    flashDrive.writeNumln(0);  //next line?
    Serial.print("Header written: ");
    Serial.println(USBfileName);
   } else {
    usbError=true;
    Serial.println("Error, USB full?");
   }
   flashDrive.closeFile();               //at the end, close the file
}
//-----------------------------------------------------------------------------------------------------
void logDataUSB() {

  flashDrive.setFileName(USBfileName);  //set the file name
  if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){               //open the file
    flashDrive.moveCursor(CURSOREND);     //if the file exist, move the "virtual" cursor at end of the file, with CURSORBEGIN we actually rewrite our old file
    //flashDrive.moveCursor(flashDrive.getFileSize()); // is almost the same as CURSOREND, because we put our cursor at end of the file
   }
   if(flashDrive.getFreeSectors()){ //check the free space on the drive
    flashDrive.writeNum(Time);
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length
    flashDrive.writeNum(Time/1000);
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length
    flashDrive.writeNum(set_temp);
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length
    flashDrive.writeFile(mode, strlen(mode)); //string, string length   
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length
    flashDrive.writeNum(temp_read);
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length
    flashDrive.writeNum(PID_value);
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length 
    flashDrive.writeNum(PID_p);
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length 
    flashDrive.writeNum(PID_i);
        flashDrive.writeFile(commaString, strlen(commaString)); //string, string length 
    flashDrive.writeNumln(PID_d);
  // flashDrive.writeFile(dataString, strlen(dataString)); //string, string length

  Serial.print("log: ");Serial.print(dd);Serial.print(", ");
   } else {
    usbError=true;
    Serial.println("Error, USB full?");
   }
   flashDrive.closeFile();               //at the end, close the file

}  


Output;

Starting at File:  FDdata00.csv
Found File:  FDdata00.csv
Found File:  FDdata01.csv
Found File:  FDdata02.csv
Found File:  FDdata03.csv
Found File:  FDdata04.csv
Found File:  FDdata05.csv
Found File:  FDdata06.csv
Found File:  FDdata07.csv
Found File:  FDdata08.csv
Found File:  FDdata09.csv
Found File:  FDdata10.csv
Found File:  FDdata11.csv
Found File:  FDdata12.csv
Found File:  FDdata13.csv
Found File:  FDdata14.csv
Found File:  FDdata15.csv
Found File:  FDdata16.csv
Found File:  FDdata17.csv
Found File:  FDdata18.csv
Found File:  FDdata19.csv
Found File:  FDdata20.csv
Found File:  FDdata21.csv
Found File:  FDdata22.csv
Found File:  FDdata23.csv
Found File:  FDdata24.csv
Found File:  FDdata25.csv
Found File:  FDdata26.csv
Found File:  FDdata27.csv
Found File:  FDdata28.csv
Found File:  FDdata29.csv
Found File:  FDdata30.csv
Found File:  FDdata31.csv
Found File:  FDdata32.csv
Found File:  FDdata33.csv
Found File:  FDdata34.csv
Found File:  FDdata35.csv
Here we go, writing to:  FDdata36.csv
Write header...
in header, writing: Time(ms),record,Set_Temp,Mode,Temp_Read,PID_percent,P,I,D
Header written: FDdata36.csv
log: 1, log: 2, log: 3, log: 4, log: 5, log: 6, log: 7, log: 8, log: 9, log: 10, log: 11, log: 12, log: 13, log: 14, log: 15, log: 16, log: 17, log: 18, log: 19, log: 20, log: 21, log: 22, log: 23, log: 24, log: 25, log: 26, log: 27, log: 28, log: 29, log: 30, log: 31, log: 32, log: 33, log: 34, log: 35, log: 36, log: 37, log: 38, log: 39, log: 40, print file: FDdata36.csv
Time(ms),record,Set_Temp,Mode,Temp_Read,PID_percent,P,I,D0
1058,1,153,Auto,138.23,0.00,0.00,0.00,0.00
2058,2,153,Auto,103.67,0.00,0.00,0.00,0.00
3059,3,153,Auto,135.09,0.00,0.00,0.00,0.00
4059,4,153,Auto,119.38,0.00,0.00,0.00,0.00
5059,5,153,Auto,10
6.81,57.00,-10440.00,-9067.00,-6901.00
6059,6,153,Auto,153.94,57.00,-10440.00,-9067.00,-6901.00
7059,7,153,Auto,163.36,57.00,-10440.00,-9067.00,-6901.00
8059,8,153,Auto,103.67,57.00,-10440.00,-9067.00,-6901.00
9059,9,153,Auto,116.24,57.00,-10440.00,-
9067.00,-6901.00
10059,10,153,Auto,150.80,8.00,17944.00,15668.00,-8510.00
11059,11,153,Auto,141.37,8.00,17944.00,15668.00,-8510.00
12059,12,153,Auto,125.66,8.00,17944.00,15668.00,-8510.00
13059,13,153,Auto,109.96,8.00,17944.00,15668.00,-8510.00...

@djuseeq
Copy link
Owner

djuseeq commented Feb 4, 2020

It's possible if you add \n character at the end of the string. I can rewrite the writeNum function to have a conditional terminator character, e.g if you call just writeNum(number) then simply write a given number to drive, or you can call writeNum(number, terminatorChar) and concatenate the given character with the number.

@djuseeq
Copy link
Owner

djuseeq commented Feb 5, 2020

I have added function writeChar(character) to test lib.

@thquack
Copy link
Author

thquack commented Feb 5, 2020

adding the \n to the end solved the newline for me, such as:
char csv_single_row[] = "some,csv,header,row,text\n";

some,csv,header,row,text
1,2,3,4,5

The writeChar works as well, such as:

flashDrive.writeNum(value1);  //some
flashDrive.writeChar(','); // a cs
...
flashDrive.writeNumln(value2); //text

Data logger is doing what I need

@thquack
Copy link
Author

thquack commented Feb 5, 2020

flashDrive.writeChar('\n');

works for me

@djuseeq
Copy link
Owner

djuseeq commented Feb 5, 2020

UR welcome. Thanks for feedback. Then we can close this issue.

@thquack
Copy link
Author

thquack commented Feb 5, 2020

Thank you!

@thquack thquack closed this as completed Feb 5, 2020
@Espectrobr
Copy link

It is possible to register a number with three decimal places using flashdrive.writeNum(float); ? tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants