Hello i have a gsm/gps/gprs shield connected to arduino and i would like to store the phone number from a new message that the arduino receives to use it later.
my code on loop is this:
void loop()
{
if(GSM.available()>0) //If there is stuff in the buffer
{
textMessage = GSM.readString(); // Get the new message
Serial.print(textMessage); // Print the new message
}
if(textMessage.indexOf("+3069")>=0) // if it has phone number info
{
Serial.println("Number found");
textMessage.toCharArray(findingNumber, 80); //convert the string to char array so i can search it and store the phone number
i=0;
Serial.println(findingNumber); //print the char array to check if it is the same as the string
while(i<80 || NumberFound == true) // start scanning every char of the array
{
if ((findingNumber[i] == "+") && (findingNumber[i+1] == "3") && (findingNumber[i+2] == "0")) // if it finds +30 which is how the phone number starts
{
j=0;
while (j<13) // (phone number has 13 digits)
{
Number[j] = findingNumber[i]; // store the phone number in a seperated char array
j++;
i++;
}
NumberFound = true; // boolean to stop the outside loop if phone number is found
Serial.print("here is ur number ");
Serial.print(Number); // print the seperated phone number
}
i++;
}
}
}
i attached a pic with serial monitor results.
basically the "if ((findingNumber == "+") && ..."
doesnt return true to store the number .
why though???


Serial.println("Number found");
textMessage.toCharArray(findingNumber, 80); //convert the string to char array so i can search it and store the phone number
i=0;
Serial.println(findingNumber);
I don't see findingNumber being printed, so there is something wrong with the conversion of the String to the char array.
Why are you reading the message as a String? Why not read it as a char array to begin with.
I don't see findingNumber being printed
Why are you reading the message as a String? Why not read it as a char array to begin with.
I guess the tochararray doesn't work as I expected?
I'm kinda noob to these stuff .
Can you show me how to read it as a char array from the beginning so I don't have to do this conversion ?
Its wise to avoid String object on small microprocessors like Arduino. See The Evils of Arduino Strings.
It is better to use c-strings which are null terminated character arrays and the c-string character array functions designed to manipulate them.
This uses .readBytes to put the gsm message into a character array and null terminates it. Then, strstr to set a pointer to any match, and then strncpy to grab the number after the match.
if (GSM.available() > 0) //If there is stuff in the buffer
{
char textMessage[100] = {};
char number[14] = {}; //13 chars + null
//textMessage = GSM.readString(); // Get the new message
byte numChars = GSM.readBytes(textMessage, 100);//.readBytes returns number read
textMessage[numChars] = '\0';//null Terminator
Serial.println(textMessage); // Print the new message
char* pch = (strstr(textMessage, "+3069"));
if (pch)
{
Serial.println("Number found");
strncpy(number, pch, 13);//copy 13 characters after pointer into number
Serial.println(number);
}
else
Serial.println("Number not found");
}
If this snippet does not work as expected, post your entire code.
Thanks for everything !
It works perfectly!
I even adjusted it to my whole code to do other several actions where i was using the same string and all work fine!