[WORKING]Get Caller ID Electron -> enable Interrupt on PB12 aka RI_UC

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);
}
4 Likes

41:32: error: too few arguments to function 'int strncmp(const char*, const char*, size_t)'
Serial.print("Callback: ");

not really getting whats wrong here… :confused:

szData is not used but that’s just a warning…

and this here:

/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:35:6: note: declared here
int _EXFUN(strncmp,(const char *, const char *, size_t));

It should be:

if(strncmp(&buf[2], "+CLCC", 5) == 0){

where 5 is equal to the string length of “+CLCC”. strncmp requires the number of characters that must be matched in order to return 0. This is different than strcmp which requires all of the characters in both strings match.

2 Likes

Correct @rickkas7, sorry, that was an unintentional issue after my experimentation to determine SMS. Corrected in the original post.

This strncmp can be avoided because the phoneCallback is only used for AT+CLCC command.

Thanks @rickkas7 for pointing out :smile:

@krvarma Great work on getting the Ring line working!

So can we use this to wake up the Electron from the sleeping modes?

1 Like

@krvarma I came across this code also for waking the Electron using the Ring line. Not sure if it’s helpful but I’ll post it anyway incase it is.

2 Likes

@RWB, I don’t think this can be used in deep sleep because it will shutdown the network system. But can be used in normal sleep because user code runs in that sleep. Not sure, need to experiment :smile:

1 Like

checkout this thread!

I found a finished test program on github for exactly this usage! :wink:
thanks to @technobly

@RWB looks like we found the same code… :smiley: in an commit they said that it would wake up the electron from sort of sleep. Also the current draw in this mode is mentioned(~18mA)!

4 Likes

@RWB, @mrlowalowa, looks like the code should work, wanted to play with it.

1 Like

Thanks for this example @krvarma :smiley:
This will be really beneficial for many people.
Today I do not have time to test this. This is really great I hope that there will soon example for receiving SMS.

1 Like

I managed to receive and process SMS, I started a new thread for that. I have uploaded the code and a demo video.

Hope this helps!

3 Likes

This example does not work for me.
I get the following log:

after termination of the call serial interface continues to display the same data.

@developer_bt, is the call made from the number given at the top of the code?

char szPhone[] = "<phone number to recognize in international format>";

@krvarma Yes the number is entered correctly, for receiving SMS works great.The number is entered in the international format

@developer_bt, can you check what is there in the buf variable. It is printed whenever callback is called.

@krvarma I found the problem, the number I receive in local format, not in international. Why the format of numbers is set for calls to local format?

@developer_bt, I am not sure about that. What we can do is if the number format is 145, then interpret as international format and if it is 129 then interpret as local format.

1 Like

There is a problem when you make a call from an unauthorized number.
The program continuously detects State is high, and no longer detects a new call correctly. How to avoid this?

@developer_bt, I modified the original code and it is working for me, can you check?

2 Likes

@krvarma Excellent.
I tried the updated example now works :smiley:
Thank you.

2 Likes