I was working with Sim800l and the SD card module where there are a number of contact details whose names with phone numbers are stored on the SD card
[example: 9860xxxxxx-Kapalik].
And when Sim800L gets a call, it first compares a number from the SD card and if the contact details are from that list, it sends the callername to the server. Everything works just fine when I run these functions separately, but when I use both functions at the same time on a loop(), the function name compareCaller() does not get executed. Please help.
Codes:
void loop() {
char phone[32] = "98xxxxxxxx";
compareCaller(phone);
delay(5000);
sendDataToServer();
delay(20000);
}
void compareCaller(char number[32]) {
myFile = SD.open("numbers.txt");
if (myFile) {
Serial.println("numbers.txt:");
while (myFile.available()) {
buffer = myFile.readStringUntil('\n');
char charBuf[50];
buffer.toCharArray(charBuf, 50);
char *callerNumber = strtok(charBuf, "-");
char *Name = strtok(NULL, '\n');
if (strcmp(callerNumber, number) == 0) {
callerName = Name;
Serial.println(callerName);
break;
}
delay(10);
}
Serial.println(callerName);
myFile.close();
}
else {
Serial.println("error opening test.txt");
}
}
int sendDataToServer()
{
String latitude, longitude;
latitude = callerName;
longitude = random(2, 20);
Serial.print("Latitude= ");
Serial.print(latitude);
Serial.print(" Longitude= ");
Serial.println(longitude);
String url, temp;
url = "http://debilitative-tacks.000webhostapp.com/gpsdata.php?lat=";
url += latitude;
url += "&lng=";
url += longitude;
//https://debilitative-tacks.000webhos
//url = "http://ahmadssd.000webhostapp.com/gpsdata.php?lat=222&lng=222";
Serial.println(url);
delay(300);
sendATcommand("AT+CFUN=1", "OK", 2000);
sendATcommand("AT+CGATT=1", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"web\"", "OK", 2000);
sendATcommand("AT+SAPBR=1,1", "OK", 2000);
sendATcommand("AT+HTTPINIT", "OK", 2000);
sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 1000);
fona.print("AT+HTTPPARA=\"URL\",\"");
fona.print(url);
sendATcommand("\"", "OK", 1000);
sendATcommand("AT+HTTPACTION=0", "0,200", 1000);
sendATcommand("AT+HTTPTERM", "OK", 1000);
sendATcommand("AT+CIPSHUT", "SHUT OK", 1000);
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {
uint8_t x = 0, answer = 0;
char response[100];
unsigned long previous;
memset(response, '\0', 100);
delay(100);
while ( fona.available() > 0) fona.read();
if (ATcommand[0] != '\0') {
fona.println(ATcommand);
}
x = 0;
previous = millis();
do {
if (fona.available() != 0) {
response[x] = fona.read();
x++;
if (strstr(response, expected_answer) != NULL) {
answer = 1;
}
}
} while ((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}
sketch_aug09d.ino (3.8 KB)