Conversion from String to Char*, Please!

My sketch received sms from a sender to turn LED on. I would like it to send sms responding back to the sender. I manage to extract the sender 's phone number from to the string received and kept in a variable called “phonenum”. But the library Seeeduino_GPRS-master SIM800L which I used required a char* to send the message back. I have been trying to convert this string to Char* in many ways and searching google for two weeks. Never succeed. Here below is my working sketch. Please tell me what wrong with my sketch. Thank you very much in advanced.

Best Regards,
Parnupong

#include <gprs.h>
#include <softwareserial.h>
#define TIMEOUT    5000
char* phonenumber = "+66995049034";
bool ledStatus;
uint8_t LED1=4;  
uint8_t LED2=5;  
uint8_t LED3=6;  
uint8_t LED4=7;  
uint8_t LED5=8;  
uint8_t LED6=9;  
uint8_t LED7=10;  
uint8_t LED8=11;   
GPRS gprs;
void setup() {
pinMode(LED1,OUTPUT); 
pinMode(LED2,OUTPUT); 
pinMode(LED3,OUTPUT); 
pinMode(LED4,OUTPUT); 
pinMode(LED5,OUTPUT); 
pinMode(LED6,OUTPUT); 
pinMode(LED7,OUTPUT); 
pinMode(LED8,OUTPUT); 
digitalWrite(LED1,0);
digitalWrite(LED2,0);
digitalWrite(LED3,0);
digitalWrite(LED4,0);
digitalWrite(LED5,0);
digitalWrite(LED6,0);
digitalWrite(LED7,0);
digitalWrite(LED8,0);
 Serial.begin(9600);
 while(!Serial);
 Serial.println("Starting SIM800 SMS Command Processor");
 gprs.preInit();
 delay(1000);
 while(0 != gprs.init()) {
     delay(1000);
     Serial.print("init error\r\n");
 }  
 if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
   ERROR("ERROR:CNMI");
   return;
 }
 if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
   ERROR("ERROR:CNMI");
   return;
 }
 Serial.println("Init success");
}
char currentLine[50] = "";
int currentLineIndex = 0;
bool nextLineIsMessage = false;
void loop() {
 if(gprs.serialSIM800.available()){
   char lastCharRead = gprs.serialSIM800.read();
   if(lastCharRead == '\r' || lastCharRead == '\n'){
       String lastLine = String(currentLine);
       if(lastLine.startsWith("+CMT:")){
         Serial.println(lastLine);
         String phonenum = lastLine.substring(6,20);
         Serial.println(phonenum);

unsigned char* phone = (unsigned char*) phonenum.c_str(); 
         
         nextLineIsMessage = true;
       } else if (lastLine.length() > 0) {
         if(nextLineIsMessage) {
           Serial.println(lastLine);
           if(lastLine.indexOf("on1") >= 0){
             digitalWrite(LED1,1);
             gprs.sendSMS(phone,"Device 1 is now ON"); 
           } else if(lastLine.indexOf("off1") >= 0) {
             digitalWrite(LED1,0);
             gprs.sendSMS(phone,"Device 1 is now OFF");
           } if(lastLine.indexOf("on2") >= 0){
             digitalWrite(LED2,1);
             gprs.sendSMS(phone,"Device 2 is now ON"); 
           } else if(lastLine.indexOf("off2") >= 0) {
             digitalWrite(LED2,0);
             gprs.sendSMS(phone,"Device 2 is now OFF");
           } 
           if(lastLine.indexOf("on3") >= 0){
             digitalWrite(LED3,1);
             gprs.sendSMS(phone,"Device 3 is now ON"); 
           } else if(lastLine.indexOf("off3") >= 0) {
             digitalWrite(LED3,0);
             gprs.sendSMS(phone,"Device 3 is now OFF");
           } 
           if(lastLine.indexOf("on4") >= 0){
             digitalWrite(LED4,1);
             gprs.sendSMS(phone,"Device 4 is now ON"); 
           } else if(lastLine.indexOf("off4") >= 0) {
             digitalWrite(LED4,0);
             gprs.sendSMS(phone,"Device 4 is now OFF");
           } 
           if(lastLine.indexOf("on5") >= 0){
             digitalWrite(LED5,1);
             gprs.sendSMS(phone,"Device 5 is now ON"); 
           } else if(lastLine.indexOf("off5") >= 0) {
             digitalWrite(LED5,0);
             gprs.sendSMS(phone,"Device 5 is now OFF");
           }
           if(lastLine.indexOf("on6") >= 0){
             digitalWrite(LED6,1);
             gprs.sendSMS(phone,"Device 6 is now ON"); 
           } else if(lastLine.indexOf("off6") >= 0) {
             digitalWrite(LED6,0);
             gprs.sendSMS(phone,"Device 6 is now OFF");
           }
           if(lastLine.indexOf("on7") >= 0){
             digitalWrite(LED7,1);
             gprs.sendSMS(phone,"Device 7 is now ON"); 
           } else if(lastLine.indexOf("off7") >= 0) {
             digitalWrite(LED7,0);
             gprs.sendSMS(phone,"Device 7 is now OFF");
           } 
           if(lastLine.indexOf("on8") >= 0){
             digitalWrite(LED8,1);
             gprs.sendSMS(phone,"Device 8 is now ON"); 
           } else if(lastLine.indexOf("off8") >= 0) {
             digitalWrite(LED8,0);
             gprs.sendSMS(phone,"Device 8 is now OFF");
           }   
           nextLineIsMessage = false;
         }
       }
       for( int i = 0; i < sizeof(currentLine);  ++i ) {
        currentLine[i] = (char)0;
       }
       currentLineIndex = 0;
   } else {
     currentLine[currentLineIndex++] = lastCharRead;
   }
 }
}

https://www.arduino.cc/en/Reference/StringToCharArray

There is a lot of information in the reference under the learning tab.

Please read the "how to use the forum" stickies to see how to properly format and post code.

Thanks, I 'll read it now.

I have changed the sketch to function toCharArray() as groundfungus kindly suggested. but it still error that ‘phone’ was not declared in this scope. What is still wrong with my sketch. Below is my new code. I use Arduino Nano 3.0 and Sim800L 5V. and Arduino IDE 1.6.8, Windows 10 .x64 and Library Seeeduino SIM800L. Thank you very much again.

#include <gprs.h>
#include <softwareserial.h>
#define TIMEOUT    5000
char* phonenumber = "+66995049034";
bool ledStatus;
uint8_t LED1=4;  
uint8_t LED2=5;  
uint8_t LED3=6;  
uint8_t LED4=7;  
uint8_t LED5=8;  
uint8_t LED6=9;  
uint8_t LED7=10;  
uint8_t LED8=11;   
GPRS gprs;
void setup() {
pinMode(LED1,OUTPUT); 
pinMode(LED2,OUTPUT); 
pinMode(LED3,OUTPUT); 
pinMode(LED4,OUTPUT); 
pinMode(LED5,OUTPUT); 
pinMode(LED6,OUTPUT); 
pinMode(LED7,OUTPUT); 
pinMode(LED8,OUTPUT); 
digitalWrite(LED1,0);
digitalWrite(LED2,0);
digitalWrite(LED3,0);
digitalWrite(LED4,0);
digitalWrite(LED5,0);
digitalWrite(LED6,0);
digitalWrite(LED7,0);
digitalWrite(LED8,0);
  Serial.begin(9600);
  while(!Serial);
  Serial.println("Starting SIM800 SMS Command Processor");
  gprs.preInit();
  delay(1000);
  while(0 != gprs.init()) {
      delay(1000);
      Serial.print("init error\r\n");
  }  
  if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  Serial.println("Init success");
}
char currentLine[50] = "";
int currentLineIndex = 0;
bool nextLineIsMessage = false;
void loop() {
  if(gprs.serialSIM800.available()){
    char lastCharRead = gprs.serialSIM800.read();
    if(lastCharRead == '\r' || lastCharRead == '\n'){
        String lastLine = String(currentLine);
        if(lastLine.startsWith("+CMT:")){
          Serial.println(lastLine);
          String phonenum = lastLine.substring(6,20);
          Serial.println(phonenum);

int str_len = phonenum.length() + 1; 
char phone[str_len];
phonenum.toCharArray(phone, str_len);
       
          nextLineIsMessage = true;
        } else if (lastLine.length() > 0) {
          if(nextLineIsMessage) {
            Serial.println(lastLine);
            if(lastLine.indexOf("on1") >= 0){
              digitalWrite(LED1,1);
              gprs.sendSMS(phone,"Device 1 is now ON"); 
            } else if(lastLine.indexOf("off1") >= 0) {
              digitalWrite(LED1,0);
              gprs.sendSMS(phone,"Device 1 is now OFF");
            } if(lastLine.indexOf("on2") >= 0){
              digitalWrite(LED2,1);
              gprs.sendSMS(phone,"Device 2 is now ON"); 
            } else if(lastLine.indexOf("off2") >= 0) {
              digitalWrite(LED2,0);
              gprs.sendSMS(phone,"Device 2 is now OFF");
            } 
            if(lastLine.indexOf("on3") >= 0){
              digitalWrite(LED3,1);
              gprs.sendSMS(phone,"Device 3 is now ON"); 
            } else if(lastLine.indexOf("off3") >= 0) {
              digitalWrite(LED3,0);
              gprs.sendSMS(phone,"Device 3 is now OFF");
            } 
            if(lastLine.indexOf("on4") >= 0){
              digitalWrite(LED4,1);
              gprs.sendSMS(phone,"Device 4 is now ON"); 
            } else if(lastLine.indexOf("off4") >= 0) {
              digitalWrite(LED4,0);
              gprs.sendSMS(phone,"Device 4 is now OFF");
            } 
            if(lastLine.indexOf("on5") >= 0){
              digitalWrite(LED5,1);
              gprs.sendSMS(phone,"Device 5 is now ON"); 
            } else if(lastLine.indexOf("off5") >= 0) {
              digitalWrite(LED5,0);
              gprs.sendSMS(phone,"Device 5 is now OFF");
            }
            if(lastLine.indexOf("on6") >= 0){
              digitalWrite(LED6,1);
              gprs.sendSMS(phone,"Device 6 is now ON"); 
            } else if(lastLine.indexOf("off6") >= 0) {
              digitalWrite(LED6,0);
              gprs.sendSMS(phone,"Device 6 is now OFF");
            }
            if(lastLine.indexOf("on7") >= 0){
              digitalWrite(LED7,1);
              gprs.sendSMS(phone,"Device 7 is now ON"); 
            } else if(lastLine.indexOf("off7") >= 0) {
              digitalWrite(LED7,0);
              gprs.sendSMS(phone,"Device 7 is now OFF");
            } 
            if(lastLine.indexOf("on8") >= 0){
              digitalWrite(LED8,1);
              gprs.sendSMS(phone,"Device 8 is now ON"); 
            } else if(lastLine.indexOf("off8") >= 0) {
              digitalWrite(LED8,0);
              gprs.sendSMS(phone,"Device 8 is now OFF");
            }   
            nextLineIsMessage = false;
          }
        }
        for( int i = 0; i < sizeof(currentLine);  ++i ) {
         currentLine[i] = (char)0;
        }
        currentLineIndex = 0;
    } else {
      currentLine[currentLineIndex++] = lastCharRead;
    }
  }
}

See comments below. Make phone global or declare right under loop() so that it stays in scope.

if(lastLine.startsWith("+CMT:"))
      {
        Serial.println(lastLine);
        String phonenum = lastLine.substring(6,20);
        Serial.println(phonenum);

        int str_len = phonenum.length() + 1;
        char phone[str_len];                          // here you create the phone array inside this if block
        phonenum.toCharArray(phone, str_len);

        nextLineIsMessage = true;
      }                                            //phone goes out of  scope on exit from if                                                 
      else if (lastLine.length() > 0) 
      {
        if(nextLineIsMessage) 
        {
          Serial.println(lastLine);
          if(lastLine.indexOf("on1") >= 0)
          {
            digitalWrite(LED1,1);
            gprs.sendSMS(phone,"Device 1 is now ON");   // phone out of scope
          }
          else if(lastLine.indexOf("off1") >= 0) {
            digitalWrite(LED1,0);
            gprs.sendSMS(phone,"Device 1 is now OFF");   // phone out of scope
          }
          if(lastLine.indexOf("on2") >= 0){
            digitalWrite(LED2,1);
            gprs.sendSMS(phone,"Device 2 is now ON");   // phone out of scope
          }

Thank you very much groundfungus. I am now out from discouragement. I 'll try as guided. Have a nice day.

I sh like have linked this before.scope tutorial

Why this form of test?

  while(0 != gprs.init()) {
// ...
  if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
// ...
  if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {

Also, it will be a lot easier to read your code if you would use Ctrl-T in the IDE before you post your code.

groundfungus: I sh like have linked this before.scope tutorial

It is very useful for me. I start to understand more after reading the topic. Thanks again.

econjack: Why this form of test?

  while(0 != gprs.init()) {
// ...
  if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
// ...
  if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {

Also, it will be a lot easier to read your code if you would use Ctrl-T in the IDE before you post your code.

I am very new to this programing language. I used the example from the library Seeeduino as a template. May be just to initialize simple text mode and formation the incoming message as I understand. I will put Ctrl-T in IDE as you suggested. Thank to you, econjack

Please help again. Thank to the topic “What is Scope?” written by econjack. I get more understanding about global and local variables. I have managed to eliminate all the errors from compiling the sketch by putting String phonenum; and char* phone; at the beginning to be global variables. But still the variable phone does not pass the sender phone number to the function gprs.sendSMS(). Therefore, the module SIM800L does not send back the message to the sender. I have tried to change the location of function .toCharArray( ); in many ways. But I got errors compiling the sketch. I could not see where the code is still wrong. Please suggest again. Thanks a lot. Here is my new sketch without any errors.

#include <gprs.h>
#include <softwareserial.h>
#define TIMEOUT    5000
char* phonenumber = "+66995049034";
String phonenum;
char* phone;
bool ledStatus;
uint8_t LED1 = 2;
uint8_t LED2 = 3;
uint8_t LED3 = 4;
uint8_t LED4 = 5;
uint8_t LED5 = 6;
uint8_t LED6 = 9;
uint8_t LED7 = 10;
uint8_t LED8 = 11;
GPRS gprs;
void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Starting SIM800 SMS Command Processor");
  gprs.preInit();
  delay(1000);
  while (0 != gprs.init()) {
    delay(1000);
    Serial.print("init error\r\n");
  }
  if (0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  if (0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  Serial.println("Init success");
}
char currentLine[50] = "";
int currentLineIndex = 0;
bool nextLineIsMessage = false;
void loop() {
  if (gprs.serialSIM800.available()) {
    char lastCharRead = gprs.serialSIM800.read();
    if (lastCharRead == '\r' || lastCharRead == '\n') {
      String lastLine = String(currentLine);
      if (lastLine.startsWith("+CMT:")) {
        Serial.println(lastLine);
        String phonenum = lastLine.substring(6, 20);
        Serial.println(phonenum);

        int str_len = phonenum.length() + 1;
        char phone[str_len];                          // here you create the phone array inside this if block
        phonenum.toCharArray(phone, str_len);

        nextLineIsMessage = true;
      }                                            //phone goes out of  scope on exit from if
      else if (lastLine.length() > 0)
      {
        if (nextLineIsMessage)
        {
          Serial.println(lastLine);
          if (lastLine.indexOf("on1") >= 0)
          {
            digitalWrite(LED1, 1); Serial.println(phone);
            gprs.sendSMS(phone, "Device 1 is now ON"); // phone out of scope
          }
          else if (lastLine.indexOf("off1") >= 0) {
            digitalWrite(LED1, 0);
            gprs.sendSMS(phone, "Device 1 is now OFF"); // phone out of scope
          }
          if (lastLine.indexOf("on2") >= 0) {
            digitalWrite(LED2, 1);
            gprs.sendSMS(phone, "Device 2 is now ON");
          }
          else if (lastLine.indexOf("off2") >= 0) {
            digitalWrite(LED2, 0);
            gprs.sendSMS(phone, "Device 2 is now OFF");
          }
          if (lastLine.indexOf("on3") >= 0) {
            digitalWrite(LED3, 1);
            gprs.sendSMS(phonenumber, "Device 3 is now ON");
          }
          else if (lastLine.indexOf("off3") >= 0) {
            digitalWrite(LED3, 0);
            gprs.sendSMS(phone, "Device 3 is now OFF");
          }
          if (lastLine.indexOf("on4") >= 0) {
            digitalWrite(LED4, 1);
            gprs.sendSMS(phone, "Device 4 is now ON");
          }
          else if (lastLine.indexOf("off4") >= 0) {
            digitalWrite(LED4, 0);
            gprs.sendSMS(phonenumber, "Device 4 is now OFF");
          }
          if (lastLine.indexOf("on5") >= 0) {
            digitalWrite(LED5, 1);
            gprs.sendSMS(phone, "Device 5 is now ON");
          }
          else if (lastLine.indexOf("off5") >= 0) {
            digitalWrite(LED5, 0);
            gprs.sendSMS(phone, "Device 5 is now OFF");
          }
          if (lastLine.indexOf("on6") >= 0) {
            digitalWrite(LED6, 1);
            gprs.sendSMS(phone, "Device 6 is now ON");
          }
          else if (lastLine.indexOf("off6") >= 0) {
            digitalWrite(LED6, 0);
            gprs.sendSMS(phone, "Device 6 is now OFF");
          }
          if (lastLine.indexOf("on7") >= 0) {
            digitalWrite(LED7, 1);
            gprs.sendSMS(phone, "Device 7 is now ON");
          } else if (lastLine.indexOf("off7") >= 0) {
            digitalWrite(LED7, 0);
            gprs.sendSMS(phone, "Device 7 is now OFF");
          }
          if (lastLine.indexOf("on8") >= 0) {
            digitalWrite(LED8, 1);
            gprs.sendSMS(phone, "Device 8 is now ON");
          }
          else if (lastLine.indexOf("off8") >= 0) {
            digitalWrite(LED8, 0);
            gprs.sendSMS(phone, "Device 8 is now OFF");
          }
          nextLineIsMessage = false;
        }
      }
      for ( int i = 0; i < sizeof(currentLine);  ++i ) {
        currentLine[i] = (char)0;
      }
      currentLineIndex = 0;
    } else {
      currentLine[currentLineIndex++] = lastCharRead;
    }
  }
}
char phone[str_len];                          // here you create the phone array inside this if block
        phonenum.toCharArray(phone, str_len);

You are creating ANOTHER and different phone array variable here. Then you write to this new array and it goes out of scope at the end of the if block. The global phone array is not updated so when you use phone later on it is not phonenum, the global phone array is empty.

I am not very good with pointers, yet, so I would declare the phone array global with enough elements to hold the longest phonenumber +1.

char phone[21];  //for instance

Then remove the declaration of the local phone array in the code above (char phone[str_len]; ).

I am not sure this will work but it is what I would try.

Insert some serial prints in your code to see what your variables are doing. You can comment or remove the prints once your code works.

groundfungus:
…so I would declare the phone array global with enough elements to hold the longest phonenumber +1.

maybe try that:

#include "gprs.h"
#include <softwareserial.h>
#define TIMEOUT    5000
char* phonenumber = "+66995049034";
String phonenum;
//char* phone;
char phone[21];  // a global buffer to hold phone number
bool ledStatus;
uint8_t LED1 = 2;
uint8_t LED2 = 3;
uint8_t LED3 = 4;
uint8_t LED4 = 5;
uint8_t LED5 = 6;
uint8_t LED6 = 9;
uint8_t LED7 = 10;
uint8_t LED8 = 11;
GPRS gprs;
void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Starting SIM800 SMS Command Processor");
  gprs.preInit();
  delay(1000);
  while (0 != gprs.init()) {
    delay(1000);
    Serial.print("init error\r\n");
  }
  if (0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  if (0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  Serial.println("Init success");
}
char currentLine[50] = "";
int currentLineIndex = 0;
bool nextLineIsMessage = false;
void loop() {
  if (gprs.serialSIM800.available()) {
    char lastCharRead = gprs.serialSIM800.read();
    if (lastCharRead == '\r' || lastCharRead == '\n') {
      String lastLine = String(currentLine);
      if (lastLine.startsWith("+CMT:")) {
        Serial.println(lastLine);
        String phonenum = lastLine.substring(6, 20);
        Serial.println(phonenum);

        //int str_len = phonenum.length() + 1;
        //char phone[str_len];                          
        phonenum.toCharArray(phone, str_len);  // copy String object directly to global buffer

        nextLineIsMessage = true;
      }                                            
      else if (lastLine.length() > 0)
      {
        if (nextLineIsMessage)
        {
          Serial.println(lastLine);
          if (lastLine.indexOf("on1") >= 0)
          {
            digitalWrite(LED1, 1); Serial.println(phone);
            gprs.sendSMS(phone, "Device 1 is now ON"); // phone out of scope
          }
          else if (lastLine.indexOf("off1") >= 0) {
            digitalWrite(LED1, 0);
            gprs.sendSMS(phone, "Device 1 is now OFF"); // phone out of scope
          }
          if (lastLine.indexOf("on2") >= 0) {
            digitalWrite(LED2, 1);
            gprs.sendSMS(phone, "Device 2 is now ON");
          }
          else if (lastLine.indexOf("off2") >= 0) {
            digitalWrite(LED2, 0);
            gprs.sendSMS(phone, "Device 2 is now OFF");
          }
          if (lastLine.indexOf("on3") >= 0) {
            digitalWrite(LED3, 1);
            gprs.sendSMS(phonenumber, "Device 3 is now ON");
          }
          else if (lastLine.indexOf("off3") >= 0) {
            digitalWrite(LED3, 0);
            gprs.sendSMS(phone, "Device 3 is now OFF");
          }
          if (lastLine.indexOf("on4") >= 0) {
            digitalWrite(LED4, 1);
            gprs.sendSMS(phone, "Device 4 is now ON");
          }
          else if (lastLine.indexOf("off4") >= 0) {
            digitalWrite(LED4, 0);
            gprs.sendSMS(phonenumber, "Device 4 is now OFF");
          }
          if (lastLine.indexOf("on5") >= 0) {
            digitalWrite(LED5, 1);
            gprs.sendSMS(phone, "Device 5 is now ON");
          }
          else if (lastLine.indexOf("off5") >= 0) {
            digitalWrite(LED5, 0);
            gprs.sendSMS(phone, "Device 5 is now OFF");
          }
          if (lastLine.indexOf("on6") >= 0) {
            digitalWrite(LED6, 1);
            gprs.sendSMS(phone, "Device 6 is now ON");
          }
          else if (lastLine.indexOf("off6") >= 0) {
            digitalWrite(LED6, 0);
            gprs.sendSMS(phone, "Device 6 is now OFF");
          }
          if (lastLine.indexOf("on7") >= 0) {
            digitalWrite(LED7, 1);
            gprs.sendSMS(phone, "Device 7 is now ON");
          } else if (lastLine.indexOf("off7") >= 0) {
            digitalWrite(LED7, 0);
            gprs.sendSMS(phone, "Device 7 is now OFF");
          }
          if (lastLine.indexOf("on8") >= 0) {
            digitalWrite(LED8, 1);
            gprs.sendSMS(phone, "Device 8 is now ON");
          }
          else if (lastLine.indexOf("off8") >= 0) {
            digitalWrite(LED8, 0);
            gprs.sendSMS(phone, "Device 8 is now OFF");
          }
          nextLineIsMessage = false;
        }
      }
      for ( int i = 0; i < sizeof(currentLine);  ++i ) {
        currentLine[i] = (char)0;
      }
      currentLineIndex = 0;
    } else {
      currentLine[currentLineIndex++] = lastCharRead;
    }
  }
}

Thanks you groundfungus, you help me a lot and thank you BulldogLowell for revising my code. I have a chance to test the revised code above. I found that the phone variable has already held the phone number value extracted from the SMS sender number. But still a little error from the function gprs.sendSMS which I think that it is nearly almost done. But unfortunately for me another problem occurred when I removed SIM Card out of the holder in order to refill the SMS and refill money and when put the SIM card back to the holder. It reported that no SIM Card inserted. This had happened before twice or three times but it used to be recovered by putting SIM in and out to holder several times, But not lucky to day I tried too many times with no success to register the signal from the phone operator. Actually I have three of the SIM800L 5V modules, I tried all of them. None of them can register to the phone network. Thus I suspect it might be my 3g SIM Card. Because I have connected SIM800L to seperate 5V 2A adaptor (measured 5.08 V). I report to you later. Thank you very much for your help. It is very kind of you.

Thank you very much to groundfungus, econjack and BulldogLowell for devoting your precious time to answer my questions. Now, all my problems are resolved. Without your comments and suggestions I couldn’t never make my sketch work as I wish. Now, my sketch can correctly send SMS back to any SMS sender without specifying the sender’s phone number in the sketch. The errors was the excess quotation marks taken from phonenum string when I stripped them out, it works right away. Thanks to you all again. Below is the correct code.

#include "gprs.h"
#include <softwareserial.h>
#define TIMEOUT    5000
String phonenum;
char phone[21];  // a global buffer to hold phone number
bool ledStatus;
uint8_t LED1 = 4;
uint8_t LED2 = 5;
uint8_t LED3 = 6;
uint8_t LED4 = 7;
uint8_t LED5 = 8;
uint8_t LED6 = 9;
uint8_t LED7 = 10;
uint8_t LED8 = 11;
GPRS gprs;
void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Starting SIM800 SMS Command Processor");
  gprs.preInit();
  delay(1000);
  while (0 != gprs.init()) {
    delay(1000);
    Serial.print("init error\r\n");
  }
  if (0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  if (0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
  Serial.println("Init success");
}
char currentLine[50] = "";
int currentLineIndex = 0;
bool nextLineIsMessage = false;
void loop() {
  if (gprs.serialSIM800.available()) {
    char lastCharRead = gprs.serialSIM800.read();
    if (lastCharRead == '\r' || lastCharRead == '\n') {
      String lastLine = String(currentLine);
      if (lastLine.startsWith("+CMT:")) {
        Serial.println(lastLine);
        String phonenum = lastLine.substring(7, 19);
        Serial.println(phonenum);
        phonenum.toCharArray(phone, 21);  // copy String object directly to global buffer
        nextLineIsMessage = true;
      }
      else if (lastLine.length() > 0)
      {
        if (nextLineIsMessage)
        {
          Serial.println(lastLine);
          if (lastLine.indexOf("on1") >= 0)
          {
            digitalWrite(LED1, 1);
            gprs.sendSMS(phone, "Device 1 is now ON"); // phone out of scope
          }
          else if (lastLine.indexOf("off1") >= 0) {
            digitalWrite(LED1, 0);
            gprs.sendSMS(phone, "Device 1 is now OFF"); // phone out of scope
          }
          if (lastLine.indexOf("on2") >= 0) {
            digitalWrite(LED2, 1);
            gprs.sendSMS(phone, "Device 2 is now ON");
          }
          else if (lastLine.indexOf("off2") >= 0) {
            digitalWrite(LED2, 0);
            gprs.sendSMS(phone, "Device 2 is now OFF");
          }
          if (lastLine.indexOf("on3") >= 0) {
            digitalWrite(LED3, 1);
            gprs.sendSMS(phone, "Device 3 is now ON");
          }
          else if (lastLine.indexOf("off3") >= 0) {
            digitalWrite(LED3, 0);
            gprs.sendSMS(phone, "Device 3 is now OFF");
          }
          if (lastLine.indexOf("on4") >= 0) {
            digitalWrite(LED4, 1);
            gprs.sendSMS(phone, "Device 4 is now ON");
          }
          else if (lastLine.indexOf("off4") >= 0) {
            digitalWrite(LED4, 0);
            gprs.sendSMS(phone, "Device 4 is now OFF");
          }
          if (lastLine.indexOf("on5") >= 0) {
            digitalWrite(LED5, 1);
            gprs.sendSMS(phone, "Device 5 is now ON");
          }
          else if (lastLine.indexOf("off5") >= 0) {
            digitalWrite(LED5, 0);
            gprs.sendSMS(phone, "Device 5 is now OFF");
          }
          if (lastLine.indexOf("on6") >= 0) {
            digitalWrite(LED6, 1);
            gprs.sendSMS(phone, "Device 6 is now ON");
          }
          else if (lastLine.indexOf("off6") >= 0) {
            digitalWrite(LED6, 0);
            gprs.sendSMS(phone, "Device 6 is now OFF");
          }
          if (lastLine.indexOf("on7") >= 0) {
            digitalWrite(LED7, 1);
            gprs.sendSMS(phone, "Device 7 is now ON");
          } else if (lastLine.indexOf("off7") >= 0) {
            digitalWrite(LED7, 0);
            gprs.sendSMS(phone, "Device 7 is now OFF");
          }
          if (lastLine.indexOf("on8") >= 0) {
            digitalWrite(LED8, 1);
            gprs.sendSMS(phone, "Device 8 is now ON");
          }
          else if (lastLine.indexOf("off8") >= 0) {
            digitalWrite(LED8, 0);
            gprs.sendSMS(phone, "Device 8 is now OFF");
          }
          nextLineIsMessage = false;
        }
      }
      for ( int i = 0; i < sizeof(currentLine);  ++i ) {
        currentLine[i] = (char)0;
      }
      currentLineIndex = 0;
    } else {
      currentLine[currentLineIndex++] = lastCharRead;
    }
  }
}