Parse details from a char buffer or String

A little side project I'm playing with!
I have a number of GSM based "free call remote controls" I use for various tasks.
They work by matching the caller ID of an incoming call to an "authorised" list and activating a relay.
The incoming call is never answered and hence no call cost apart from $3 /month SIM for the device

My issue is that Telstra is killing off the GSM network here in Australia in the next two years and these devices will need replacing - and currently there is no replacement I can find.
So while I am playing with all things 3G at the moment I thought I'd have a go at replicating this device but on a 3G module.

Digging around I found some code for "BeeGSM" and I have modified it and its libraries to work with my SIM5216a modules and have this very basic code working. I have manually added a couple of numbers to the SIM via AT commands direct to the 3G module and the code works

(I have yet to rename the libraries - it was originally based on a Teltonika module)

#include <TeltonikaTM1Q.h>
#include <SoftwareSerial.h>

#define RELAYPIN 5
#define SYS_PIN ("1234")

char number[20];
char name[20];
char smsbuffer[160];

int index;
boolean auth;


void setup() 
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("Free Call Remote Control");
  //Start configuration.
  gsm.debug(false);
  if (gsm.begin())
    Serial.println("\nstatus=READY");
  else Serial.println("\nstatus=IDLE");
  pinMode(RELAYPIN, OUTPUT);
  digitalWrite(RELAYPIN,HIGH);
  
};

void loop(){
 
  
  if(gsm.readSMS(smsbuffer, 160, number, 20)){
    Serial.print("Number: ");
    Serial.println(number);
    Serial.print("Message: ");
    Serial.println(smsbuffer);
    String SMS_COMMAND = smsbuffer;
    if (SMS_COMMAND.startsWith(SYS_PIN)){
      Serial.println("PIN MATCH!");
      if (SMS_COMMAND.startsWith("#TEL",4)){
        Serial.println("TEL COMMAND");
          if (SMS_COMMAND.startsWith("#",8)){
              Serial.println("Remove number command");
              //remove number from phonebook routine here
          }
          else{
              Serial.println("Add number command");
             //add number to phonebook routine here 
          }      
    }  
    }
  }
  

  if(gsm.readCallAuthPhoneBook(number,20,auth)) {  
      if(auth) {
          Serial.print("Number ");
          Serial.print(number);
          Serial.println(" authorized !");
          Serial.println("Relay ON");
          
          digitalWrite(RELAYPIN, LOW);
          delay(1000);
          digitalWrite(RELAYPIN, HIGH);
          delay(1000);
          Serial.println("Relay OFF");
      }
      else {
          Serial.print("Number ");
          Serial.print(number);
          Serial.println(" is not authorized!");
      }
  }
}

You can see I've just started playing with the receiving SMS portion that allows you to program authorised numbers on the SIM. Recognizing a command prefixed with the command password (SYS_PIN), the #TEL command to add a new number or the #TEL# command to remove one.

My next issue is how to parse out the phone number & index location that will follow on from the #TEL command from either "smsbuffer" or "SMS_COMMAND"

ie the full command may look like this:

1234#TEL0123456789#01

where:

SYS_PIN = 1234
Mobile number to add = 0123456789
#01 at the end = phonebook index location

How do I grab the full phone number including and LEADING zeros and also the index location?

I have this routine to write to the SIM phonebook:

void WRITE_SIM_PHONEBOOK(){ 
 if (gsm.writePhoneBook(number," "));
}

Currently this will just dump the new number into the first available free index location, but I hope to be able to push the number to specific locations.

Hello,

You can use functions strtok or the very useful sccanf.

Thank you - I'll have a look at both of those and see if I can work out how to implement them :slight_smile:

Stocky:
Thank you - I'll have a look at both of those and see if I can work out how to implement them :slight_smile:

There is a short program illustrating their use in this Thread serial input basics

...R

Thanks Robin - That makes their use a little clearer - I'll see what I can do :wink: