Comments (10)
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.
from ch376msc.
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...
from ch376msc.
I have added function writeChar(character)
to test lib.
from ch376msc.
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
from ch376msc.
flashDrive.writeChar('\n');
works for me
from ch376msc.
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.
from ch376msc.
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.
from ch376msc.
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.
from ch376msc.
UR welcome. Thanks for feedback. Then we can close this issue.
from ch376msc.
Thank you!
from ch376msc.
Related Issues (20)
- getChipVer() returns 0xFF, but everything else works HOT 3
- CH376s SPI USB issues
- file name Limitations HOT 7
- ch376s not working with esp32 HOT 4
- readFileUntil() return "false" for both EOF and the record terminator HOT 3
- interfacing ch376s module with build in sd HOT 3
- Accessing USB Pendrive and SD Card consecutively! HOT 6
- Init function hangs the application HOT 2
- Volume Label Information
- Files are not created when called using separate fileOpen, fileWrite and fileClose functions
- i can't write string to flash drive. HOT 2
- "Flash Drive Attached" instability
- Long filename support
- To pair the Ch376msc with the library ESP32-audioI2S
- drive is detached
- readFileUntil() crashing when trying to read more than the buffer size
- FS Struct needed HOT 2
- Reading pictures from USB HOT 2
- Speed on HW Serial and small buffer
- Library update required HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ch376msc.