arduinoesp32sim900phonebook

SIM900L How to read all phonebook entries and store in array?


I'm working on a project where I want to make my own mobile phone with a LILYGO TTGO T-CALL ESP32 and I would like to read all phonebook entries from sim card. I already figured out how to get all entries with an AT command but my code does not work properly, it can read about 5 entries but after the 6th or so it stops working. Here is the code I use for this:

void ReadSimCard() {
  WriteText("Read SIM . . .");
  CMD("AT+CPBS=\"SM\"");
  delay(3000);
  while (!hwSerial.available()) {
    delay(10);
  }
  while (hwSerial.available()) {
    hwSerial.read();
  }
  
  CMD("AT+CPBF");
  delay(10000);
  while (!hwSerial.available()) {
    delay(10);
  }
  while (hwSerial.available()) {
    delay(1000);
    String s = hwSerial.readStringUntil('\n');
    Serial.println("READSIM: " + s);
    if (s.indexOf("CPBF:") > 0) {
      Serial.println();
      AddToContactList(s);
    }
    WriteText("Read SIM . . .", String(NUMBER_AMOUNT));
  }
  if (NUMBER_AMOUNT < 30) { 
    WriteText("Bad read");
    delay(3000);
    NUMBER_AMOUNT = 0;
    for (int i = 0; i < 250; i++) {
      names[i] = "";
    }
    for (int i = 0; i < 250; i++) {
      numbers[i] = "";
    }
    ReadSimCard();
  }
}

void CMD(String c) {
  hwSerial.print(c + "\r\n");
  delay(500);
}

void AddToContactList(String s) { // +CPBF: 1,"+36201234567",145,"NOTME"
  int i1 = 0;
  int i2 = 0;
  int i3 = 0;
  String data1 = "";
  String data2 = "";
  String data3 = "";
  
  i1 = s.indexOf(',');
  i2 = s.indexOf(',', i1 + 1);
  i3 = s.indexOf(',', i2 + 1);
  
  data1 = s.substring(0, i1);
  data1.remove(0, 7);
  data2 = s.substring(i1 + 2, i2 - 1);
  data3 = s.substring(i3 + 2);
  data3.remove(data3.lastIndexOf('"'));
  
  Serial.println("READSIM L1: " + data1);
  Serial.println("READSIM L2: " + data2);
  Serial.println("READSIM L3: " + data3);
  Serial.println();

  numbers[NUMBER_AMOUNT] = data2;
  names[NUMBER_AMOUNT] = data3;
  NUMBER_AMOUNT++;
}

The numbers and names String arrays can contain the maximum amount of elements the sim card can store (the arrays' sizes are 250 each). The WriteText() function shows text on a 1602 LCD display. The NUMBER_AMOUNT contains the number of occupied spaces in the numbers and names arrays.


Solution

  •   const byte maxKontak = 30;
      //String temp_id[maxKontak], temp_name[maxKontak], temp_number[maxKontak];
      int temp_id;
      String temp_name, temp_number;
      struct Contact{ String Name[maxKontak], Number[maxKontak]; int Type[maxKontak], Id[maxKontak], Count; }; 
      Contact kontak;   // Objek Struct variable
      
      HardwareSerial SerialModem(1);
      #define RXD2 27 //16              // GPIO27. merupakan UART2 RXD hardware serial ESP32, jd g perlu library <SoftwareSerial.h>
      #define TXD2 26 //17  
      
      void setup() {
        Serial.begin(115200);
        SerialModem.begin(9600, SERIAL_8N1, RXD2, TXD2); 
        // put your setup code here, to run once:
        _getSimCard(kontak);  // baca semua kontak yang ada pada simcard menggunakan perintah AT+CPBF dan simpan ke global variable struct  
        Serial.println("Menampilkan kontak dari Global Variable Struct:");
        for (int i = 0; i < maxKontak; i++) {
          //Serial.println("no. "+String(i+1)+" -> "+String(kontak.Number[i]));
          Serial.println("no. "+String(i+1)+" -> Id: "+String(kontak.Id[i])+", Nama: "+kontak.Name[i]+", Nomor Kontak: "+kontak.Number[i]);
          delay(10);
        }
        Serial.println("Done");
      
      }
      
      void loop() {
        // put your main code here, to run repeatedly:
      
      }
      
      
      //
      void _getSimCard(Contact &c) {
        Serial.println("\nRead SIM...");
        SerialModem.println("AT+CPBF");                             // menggunakan perintah AT command find
        const int TIMEOUT_AT = 3000;
        long int Start = millis();  
        byte hitung = 0;                                          // indeks mulai dari 0
        while((millis() - Start) < TIMEOUT_AT){
          while (SerialModem.available()) {
            //String response = SerialModem.readString();           // jika menggunakan baris perintah yang tampil hanya index pertama saja
            String response = SerialModem.readStringUntil('\n');    // baca tiap baris
            //Serial.println(response);                           // hasilnya semua yang ada pada simcard ditampilkan
            if (response.indexOf("CPBF:") > 0) {
              //Serial.println(response);                         // hasilnya tiap baris ditampilkan -> +CPBF: 30,"09786483783",129,"namman"
              _addToContactList(response);
              c.Id[hitung]     = temp_id;                         // simpan hasil ekstrak dari global variable sementara ke global variable struct (array)
              c.Name[hitung]   = temp_name;
              c.Number[hitung] = temp_number;
              //Serial.println("no. "+String(hitung+1)+" -> Nama: "+c.Name[hitung]+", Nomor Kontak: "+c.Number[hitung]);    // menampilkan hasil ekstak ke serial
              hitung++;                                           // penambahan 1 sampai dengan kontak terakhir yang ada pada simcard 
              delay(10);
            }
          }
        } 
        Serial.println("Done\n");
      }
      /*======================================================================================================================================================================*/
      // penjelasan tentang manipulasi string http://bf-arduinolab.wikidot.com/docs:string-manipulation
      void _addToContactList(String s) {                          // s = variable penampung dari hasil pembacaan serial modem (SerialModem.readStringUntil('\n')
        //                                                                    +CPBF: 1,"083895555359",129,"dkurnia"
        int i1 = s.indexOf(',');                                  // posisi substring setelah koma pertama, -> "083895555359",129,"dkurnia"        
        int i2 = s.indexOf(',', i1 + 1);                          // posisi substring setelah koma kedua    -> "dkurnia"
        int i3 = s.indexOf(',', i2 + 1);                          // posisi substring setelah koma ketiga,  -> dkurnia"
        
        String id = s.substring(0, i1);                           // ambil substring dari posisi index awal(+) sampai posisi index i1(1) hasilnya +CPBF: 1
        id.remove(0, 7);                                          // kemudian hapus posisi 0 sampai 7 -> +CPBF: 1 hasilnya 1
        //Serial.println(id);                                     // debug serial
        temp_id     = id.toInt();                                 // simpan hasil ekstrak ke global variable sementara
        temp_number = s.substring(i1 + 2, i2 - 1);//.c_str();     // ambil substring dari posisi index (angka 0) sampai posisi index i1(9)  -> 083895555359",129,"dkurnia" hsilnya 083895555359
        temp_name   = s.substring(i3 + 2);                        // ambil substring dari posisi index, -> dkurnia"
        temp_name.remove(temp_name.lastIndexOf('"'));             // kemudian hapus tanda kutip terakhir  -> hasilnya dkurnia
      
        //return data2;
      }
    

    I tried it with a code like this and it worked