Mungkin ini adalah bagian yg paling sering dan mudah dicari pembahasannya di berbagai blog dan tulisan, karena fasilitas SD card dan modul siap pakainya untuk arduino yg berbasis SPI sangat murah dipasaran. Jadi saya akan membahas yg lebih lanjut yaitu melakukan parsing perbaris dari sebuah file didalam SD Card.
Untuk melengkapi praktek pada rulisan ini, bisa membaca part 1 disini : https://www.aisi555.com/2021/06/esp8266-kunci-motor-pintu-menggunakan.html
Padahal kalau diingat bagaimana dulu saya sudah berhasil membaca file SD Card secara RAW pada praktek yg saya lakukan bertahun-tahun yg lalu menggunakan ATTINY 2313 yg bisa dibaca disini : https://www.aisi555.com/2012/12/belajar-access-sd-card-cara-simple_3994.html
Karena siap pakai inilah maka anda akan dimanjakan dengan koneksi langsung ke modul node MCU atau Wemoz D1, langsung saja cari PIN SPI ( MOSI, MISO, SCK ) dan CS yg sesuai lalu gunakan library yg tersedia langsung pada sketch Arduino yaitu SD.h dan SPI.h .
Dan yg paling gampang adalah script mengetest apakah koneksi nya sudah benar apa belum yg sederhana seperti berikut :
#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() { }
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;
}
#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 untuk menunggu input
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) //baca line dari 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("Masukkan index baris: ");
}
} // end
}
0 komentar:
Posting Komentar