Semua Tentang Belajar Teknologi Digital Dalam Kehidupan Sehari - Hari

Sabtu, 03 Juli 2021

[ ESP 8266 ] Wireless Key Via Wifi - Part 2 - SD Card Access Read File Line by Line

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.





Share:

0 komentar:

Posting Komentar

Kontak Penulis



12179018.png (60×60)
+628155737755

Mail : ahocool@gmail.com

Site View

Categories

555 (8) 7 segmen (3) adc (4) amplifier (2) analog (19) android (12) antares (11) arduino (27) artikel (11) attiny (3) attiny2313 (19) audio (5) baterai (5) blog (1) bluetooth (1) chatgpt (2) cmos (2) crypto (2) dasar (46) digital (11) dimmer (5) display (3) esp8266 (26) euro2020 (13) gcc (1) gsm (1) iklan (1) infrared (2) Input Output (3) iot (74) jam (7) jualan (12) kereta api (1) keyboard (1) keypad (3) kios pulsa (2) kit (6) komponen (17) komputer (3) komunikasi (1) kontrol (8) lain-lain (8) lcd (2) led (14) led matrix (6) line tracer (1) lm35 (1) lora (11) lorawan (2) MATV (1) memory (1) metal detector (4) microcontroller (70) micropython (6) mikrokontroler (1) mikrokontroller (14) mikrotik (5) modbus (9) mqtt (3) ninmedia (5) ntp (1) paket belajar (19) palang pintu otomatis (1) parabola (88) pcb (2) power (1) praktek (2) project (33) proyek (1) python (8) radio (28) raspberry pi (9) remote (1) revisi (1) rfid (1) robot (1) rpm (2) rs232 (1) script break down (3) sdcard (3) sensor (2) sharing (3) signage (1) sinyal (1) sms (6) software (18) solar (1) solusi (1) tachometer (2) technology (1) teknologi (2) telegram (2) telepon (9) televisi (167) television (28) telkomiot (5) transistor (2) troubleshoot (3) tulisan (94) tutorial (108) tv digital (6) tvri (2) vu meter (2) vumeter (2) wav player (3) wayang (1) wifi (3) yolo (7)

Arsip Blog

Diskusi


kaskus
Forum Hobby Elektronika