The RI is getting triggered for call and SMS received. I am able to retrieve the incoming phone number during a call. Following is a sample code written for testing it. This code turn on the LED on D7 when an incoming call is received from predefined phone number. Note that you have to change the szPhone to your number.
When an incoming call is received then send the AT+CLCC command returns the list of current call. I am using AT+CPAS command to get phone activity state.This command will return a value from 0 to 5. The u-Blox AT Command Manual describes this command in detail.
One thing pending is to retrieve the incoming phone number when SMS is received. Any thoughts?
#define TIMEOUT 10000
void riInterrupt(void);
int ledPin = D7;
volatile int state = LOW;
char szIncomingPhone[16];
bool isCallRecognized = false;
bool isWaitingForCLCC = false;
char szPhone[] = "+919847237452";
int statusCallback(int type, const char* buf, int len, char* param){
Serial.print("Callback: ");
Serial.write((const uint8_t*)&buf[2], len - 2);
Serial.println();
char szData[32];
int status = -1;
sscanf(&buf[2], "%s%d", szData, &status);
if(strcmp(szData, "+CPAS:") == 0 && status == 0){
state = LOW;
szIncomingPhone[0] = '\0';
isCallRecognized = false;
}
return WAIT;
}
int phoneCallback(int type, const char* buf, int len, char* param){
Serial.print("Callback: ");
Serial.write((const uint8_t*)&buf[2], len - 2);
Serial.println();
char szData[32];
int p1, p2, p3, p4, p5;
int format;
if(strncmp(&buf[2], "+CLCC", 5) == 0){
sscanf(&buf[8], "%d, %d, %d, %d, %d, %[^/,]s, %d", &p1, &p2, &p3, &p4, &p5, szIncomingPhone, &format);
strcpy(szIncomingPhone, &szIncomingPhone[1]);
szIncomingPhone[strlen(szIncomingPhone) - 1] = 0;
Serial.println(szIncomingPhone);
Serial.println(szPhone);
if(strcmp(szIncomingPhone, szPhone) == 0){
Serial.println("Call detected");
isCallRecognized = true;
isWaitingForCLCC = false;
}
else{
state = LOW;
szIncomingPhone[0] = '\0';
isCallRecognized = false;
}
}
else if(isWaitingForCLCC && !isCallRecognized){
state = LOW;
szIncomingPhone[0] = '\0';
isCallRecognized = false;
isWaitingForCLCC = false;
}
return WAIT;
}
void setup(){
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(RI_UC, INPUT_PULLUP);
attachInterrupt(RI_UC, riInterrupt, CHANGE);
}
void loop(){
if(isCallRecognized)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
if(state == HIGH){
char szReturn[32] = "";
Serial.println("State is high");
Serial.print("Phone Detected: ");
Serial.println(isCallRecognized);
if(strlen(szIncomingPhone) == 0){
Serial.println("Detecting incoming phone number");
isWaitingForCLCC = true;
Cellular.command(phoneCallback, szReturn, TIMEOUT, "AT+CLCC\r\n");
} else if(isCallRecognized == true){
Serial.println("Getting status");
isWaitingForCLCC = false;
Cellular.command(statusCallback, szReturn, TIMEOUT, "AT+CPAS\r\n");
}
}
}
void riInterrupt(){
state = digitalRead(RI_UC);
}