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.