I am trying to extract the mobile number from String which is working with attached code. but the issue is random or missing serial until GSM shield start.
I want to get start the execution when match the response. or can say how can I avoid this serial data and print only Number.
Serial monitor :
Number is :
Number is :
Number is : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is :
RDY
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : ⸮4⸮JRP⸮⸮(R⸮⸮EA⸮5⸮AT+CPBF="Arduino"
ERROR
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : AT+CPBF="Arduino"
ERROR
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : AT+CPBF="Arduino"
ERROR
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : AT+CPBF="Arduino"
ERROR
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : AT+CPBF="Arduino"
ERROR
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : AT+CPBF="Arduino"
ERROR
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : AT+CPBF="Arduino"
ERROR
Call Ready
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Number is : 00xxxxxxxxxxxxx
Number is : 00xxxxxxxxxxxxx
Complete Sketch is :
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);//Configuring the serial pins by software
char c[66] ; //array for midi variables
String readString, substring;
String number = "";
void setup()
{
//Serial connection.
Serial.begin(9600);
SIM900.begin(9600);
}
void loop() {
if (SIM900.available() > 0) {
for (int i = 0; i < 63 ; i++)
{
c[i] = SIM900.read();
}
}
SIM900.println("AT+CPBF=\"Arduino\"");
delay(1000);
Serial.print("Number is : ");
// Serial.println(c);
String response = c;
String number = response.substring( response.indexOf("#") + 1 , response.lastIndexOf("#") ) ;
Serial.println(number);
}
// AT command return is : +CPBF: 1,"#00123456789123#",255,"Arduino"
Robin2:
Weren’t you advised in your previous Thread not the use the String class?
…R
Because in my main code some String are there and I didn’t have idea how to convert String to c string.
I can read contact from SIM card, parse, store on EEPROM and read again from EEPROM but still cannot declare as variable that can be use as a mobile number.
What about now ?
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);//Configuring the serial pins by software
const byte numChars = 16;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char mobileNumber[numChars] = {0};
boolean newData = false;
boolean Mnumber = true;
#include <EEPROM.h>
const int EEPROM_MIN_ADDR = 0;
const int EEPROM_MAX_ADDR = 511;
boolean eeprom_is_addr_ok(int addr) {
return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
}
boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {
int i;
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
return false;
}
for (i = 0; i < numBytes; i++) {
EEPROM.write(startAddr + i, array[i]);
}
return true;
}
boolean eeprom_write_string(int addr, const char* string) {
int numBytes; // actual number of bytes to be written
numBytes = strlen(string) + 1;
return eeprom_write_bytes(addr, (const byte*)string, numBytes);
}
boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
byte ch; // byte read from eeprom
int bytesRead; // number of bytes read so far
if (!eeprom_is_addr_ok(addr)) { // check start address
return false;
}
if (bufSize == 0) { // how can we store bytes in an empty buffer ?
return false;
}
// is there is room for the string terminator only, no reason to go further
if (bufSize == 1) {
buffer[0] = 0;
return true;
}
bytesRead = 0; // initialize byte counter
ch = EEPROM.read(addr + bytesRead); // read next byte from eeprom
buffer[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
}
// make sure the user buffer has a string terminator, (0x00) as its last byte
if ((ch != 0x00) && (bytesRead >= 1)) {
buffer[bytesRead - 1] = 0;
}
return true;
}
const int BUFSIZE = 15;
char buf[BUFSIZE];
String myString;
char myStringChar[BUFSIZE];
/////////////////****************//
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '#';
char endMarker = '#';
char rc;
while (SIM900.available()> 0 && newData == false && Mnumber==true) {
rc = SIM900.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
SIM900.println("AT+CPBF=\"Aarduino\"");
delay(1000);
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, "#"); // get the first part - the string
strcpy(mobileNumber, strtokIndx); // copy it to mobileNumber
}
//============
void showParsedData() {
Serial.print("Mobile # is : ");
Serial.println(mobileNumber); // Serial print 00123456789123
Serial.println("Saving variable string to address 15");
myString = mobileNumber;
myString.toCharArray(myStringChar, BUFSIZE); //convert string to char array
strcpy(buf, myStringChar);
eeprom_write_string(15, buf);
Mnumber=false;
}
//============
void setup() {
Serial.begin(9600);
SIM900.begin(9600);
SIM900.print("AT+CMGF=1\r");//AT command to configure the SIM900 in text mode
delay(200);
SIM900.print("AT+CNMI=2,2,0,0,0\r");//configure the module to show the SMS through the serial port.
delay(200);
SIM900.println("AT+CPBS=\"SM\"");
delay(200);
SIM900.println("AT+CPBF=\"Aarduino\"");
delay(200);
Serial.println("This demo Start");
delay(2000);
}
void loop()
{
if (Mnumber == true) {
recvWithStartEndMarkers();
}
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
sendSMS();
newData = false;
}
Serial.print("Reading string from address 15: ");
eeprom_read_string(15, buf, BUFSIZE);
Serial.println(buf);
delay(5000);
}
void sendSMS()
{
SIM900.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
SIM900.println("AT + CMGS = \"+buf+\"");
// SIM900.println("AT + CMGS = \""+buf+"\""); // Also try not working
delay(1000);
SIM900.println("SMS from GSM Module");// The SMS text you want to send
delay(100);
SIM900.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println("MSG Send");
}
Serial monitor is like:
This demo Start
Reading string from address 15: 00xxxxxxxxxxxx
Mobile # is : 00xxxxxxxxxxxx
Saving variable string to address 15
MSG Send
Reading string from address 15: 00xxxxxxxxxxxxx
Reading string from address 15: 00xxxxxxxxxxxxx