On this part, maybe, is the part that is most often discussed and easily " googled " in various blogs and articles, because the SD Card libraries and ready-to-use SD card reader module for SPI-based Arduino are very cheap in the online market. So I will discuss a little bit further, parsing files and text per line on the SD Card.
Because the shield or module is ready to use, you will be spoiled with direct connections to the MCU or Wemos D1 node module, just look for the appropriate SPI PIN (MOSI, MISO, SCK) and CS then use the libraries available directly on the Arduino sketch, namely SD.h and SPI .h.
And the easiest is the script to test whether the connection is correct or not, as simple as the following:
#include <SPI.h>
#include <SD.h>
const int chipSelect = D8; // use D0 for Wemos D1 Mini
File root;
void setup() {
Serial.begin(9600);
Serial.print("\r\nWaiting for SD card to initialise...");
if (!SD.begin(chipSelect)) { // CS is D8 in this example
Serial.println("SD Card Initialising failed!");
return;
}
Serial.println("SD Card Initialisation completed");
File dataFile = SD.open("key.csv");
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening seq.txt");
}
}
void loop() {
}
The output result as follows:
The file which i use in this example is in CSV format, that I made using excel as the following format:
So, as per line, there are 3 parts of data separated by "commas" and ending with ascii 'CR LF' or if in the coding or C language is known as the character '\r\n'. And i found on the google search, a very powerful script that is useful when you want to find rows with a certain index and then parse it:
String getStringPartByNr(String data, char separator, int index)
{
// spliting a string and return the part nr index
// split by separator
int stringData = 0; //variable to count data part nr
String dataPart = ""; //variable to hole the return text
for(int i = 0; i<data.length()-1; i++) { //Walk through the text one letter at a time
if(data[i]==separator) {
//Count the number of times separator character appears in the text
stringData++;
}else if(stringData==index) {
//get the text when separator is the rignt one
dataPart.concat(data[i]);
}else if(stringData>index) {
//return text and stop if the next separator appears - to save CPU-time
return dataPart;
break;
}
}
//return text if this is the last part
return dataPart;
}
Next, I combine it to get the data on the specified row with the index "no", I use the arduino sketch console to wait for keyboard input from the desired row of data inside an SD card.
#include <SPI.h>
#include <SD.h>
const int chipSelect = D8; // use D0 for Wemos D1 Mini
File root;
String buffer;
String rx_str = ""; //variabel string for stringinput
char rx_byte = 0;
String getStringPartByNr(String data, char separator, int index)
{
// spliting a string and return the part nr index
// split by separator
int stringData = 0; //variable to count data part nr
String dataPart = ""; //variable to hole the return text
for(int i = 0; i<data.length()-1; i++) { //Walk through the text one letter at a time
if(data[i]==separator) {
//Count the number of times separator character appears in the text
stringData++;
}else if(stringData==index) {
//get the text when separator is the rignt one
dataPart.concat(data[i]);
}else if(stringData>index) {
//return text and stop if the next separator appears - to save CPU-time
return dataPart;
break;
}
}
//return text if this is the last part
return dataPart;
}
void bacaSD( String index) // read line from an index
{
File dataFile = SD.open("key.csv");
if (dataFile) {
while (dataFile.available()) {
buffer = dataFile.readStringUntil('\r\n');
//parsing
if(getStringPartByNr(buffer,',',0) == index)
{
Serial.print("no : ");
Serial.print(index);
Serial.print(" - key : ");
Serial.print(getStringPartByNr(buffer,',',1));
Serial.print(" - challenge : ");
Serial.println(getStringPartByNr(buffer,',',2));
}
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening KEY.CSV");
}
}
void setup() {
Serial.begin(9600);
Serial.print("\r\nWaiting for SD card to initialise...");
if (!SD.begin(chipSelect)) { // CS is D8 in this example
Serial.println("SD Card Initialising failed!");
return;
}
Serial.println("SD Card Initialisation completed");
Serial.println("Masukkan index baris: ");
}
void loop() {
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read(); // get the character
if (rx_byte != '\n') {
// a character of the string was received
rx_str += rx_byte;
}
else {
// end of string
Serial.print("Data baris ke: ");
Serial.println(rx_str);
bacaSD(rx_str);
rx_str = ""; // clear the string for reuse
Serial.println("");
Serial.println("Please Input Line Index: ");
}
} // end
}
The results can be seen in the following animation, and if you try it, you can use the "newline" mode on the Arduino Sketch console monitor.
0 komentar:
Posting Komentar