Writing an AT code for sms message on sim800l module

I have a telephone number that is saved to the sd card, I am able to assign this number to a string variable called “number” and I have tested this by using Serial.print(number) and this returns the number saved on the sd card.
What I am trying to do now is to use “number” and insert it into the AT command that holds the number where the sms message is to be sent, here is the code I’m talking about

  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\"+447853557568\"\r\n");

So what I need to do is replace the telephone number “+447853557568” with the string variable “number”

So far I have tried the following

  serialSIM800.write("AT+CMGS=\""+number+"\"\r\n");

Breaking the command up as follows:

  sim800.print("AT+CMGS=\"");
  sim800.print(number);
  sim800.print("\"");

but nothing I have tried works and I dont seem to find any reference anywhere how this command is put together with the string variable.

Im pretty new to Arduino and C++ so my technical explanation maybe explained poorly

Here is the full Sletch I’m using this code within

#include <SoftwareSerial.h>
#include <String.h>
//#include <SD.h>
#define SD_CS_PIN SS
#include <SPI.h>
#include "SdFat.h"

SdFat SD;

File myFile;

SoftwareSerial sim800(8, 7); // TX, RX

//const int ledPin3 = 11;//Define the Bluetooth led pin
const int ledPin2 = 2;//Define the interupt pin to signify bluetooth connect or disconnect

String readString = "";
String telNoString = "";
String number = "";
String telNo = "";

volatile int ledonState = 0;
int lastLedonState = 0;


const int thresholdvalue=680;//The threshold to turn the led on


void setup() {
  pinMode (ledPin2, INPUT);//set input for interupt pin
  //pinMode (ledPin3, OUTPUT);//set output for bluetooth pin

  attachInterrupt(0, pin_ISR, CHANGE);
  
  Serial.begin(9600);
  
  while(!Serial);

  sim800.begin(9600);
      
  delay(1000);
    
  Serial.println("Setup Complete!");

}


void pin_ISR() {
  ledonState = digitalRead(ledPin2);

if (ledonState != lastLedonState) {

  if (ledonState == HIGH) {
        //digitalWrite(ledPin3, HIGH);//turn led on
            Serial.println("HC-05 is now connected");
            //Serial.println();
      }else{
         //digitalWrite(ledPin3, LOW);//turn led off
             Serial.println("HC-05 is now Disconnected");
             //Serial.println();
  }
lastLedonState = ledonState;
}
}


void sound_detect(){
  
  int sensorValue = analogRead(A0);//use A0 to read the electrical signal
  
  if(sensorValue > thresholdvalue) {
    
//  digitalWrite(ledPin1,HIGH);//if the value read from A0 is larger than 400,then light the LED
//  delay(10);
//  digitalWrite(ledPin1,LOW);

 Serial.print("Initializing SD card...");
  
  pinMode(10, OUTPUT);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  
  Serial.println("initialization done.");  

  if (!SD.exists("CONFIG.DAT")) {

    Serial.println("No Number Exists! Please go to Setup Device to add Number for Alert");
    
  }else{

  // open the file for reading:
  myFile = SD.open("CONFIG.DAT");
  
  if (myFile) {
    
    Serial.println("CONFIG.DAT:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {

                char ltr = myFile.read();
                number += ltr;      
    }  
    
    // close the file:
    myFile.close();
    
  } else {
    
    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");
    
  }
 
  }
  
Serial.println("this is the phone number: ");
Serial.print(number);

  if (sim800.available()){ 
    Serial.write(sim800.read()); 
  }

    //Set SMS format to ASCII
  sim800.print("AT+CMGF=1\r");
  delay(1000);
 
  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\""+number+"\"\r\n");

  sim800.print("AT+CMGS=\"");
  sim800.print(number);
  sim800.print("\"");  
  delay(1000);
   
  //Send SMS content
  sim800.print("TEST SMS NOISE DETECT");
  delay(1000);
   
  //Send Ctrl+Z / ESC to denote SMS message is complete
  sim800.print((char)26);
  delay(1000);
  
  sim800.println();
    
  Serial.println("SMS Sent!");
}

}



void insertNo(){

        while(Serial.available()==0) { // Wait for User to Input Data  
  }

telNoString = Serial.readString();


  Serial.print("Initializing SD card...");
  
  pinMode(10, OUTPUT);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  
  Serial.println("initialization done.");

  if (SD.exists("CONFIG.DAT")) {
    
  SD.remove("CONFIG.DAT");
  }

  if (!SD.exists("CONFIG.DAT")) {
    
  myFile = SD.open("CONFIG.DAT", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to CONFIG.DAT...");
    
    myFile.println(telNoString);
    
    // close the file:
    myFile.close();
    
    Serial.println("done.");
    
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");
  } 

  // re-open the file for reading:
  myFile = SD.open("CONFIG.DAT");
  
  if (myFile) {
    
    Serial.println("CONFIG.DAT:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      
      Serial.write(myFile.read());
      
    }
    
    // close the file:
    myFile.close();
    
  } else {
    
    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");
    
  }     

  }
   return;    
}


void loop() {

 while (Serial.available()) {
          delay(10);       
          char c = Serial.read();
          readString  += c;
 }
 
    if(readString == "setup device") {

      Serial.println(readString);
      readString = "";



insertNo();
      
    }else{
      if(readString == "start device") {
        
      Serial.println(readString);
      readString = "";


    }
 sound_detect();   
    }       
}
void pin_ISR() {
. . . 
. . . 
            Serial.println("HC-05 is now connected");

Just . . . don't.

Try:

 serialSIM800.println("AT + CMGS = \"" + number +"\"");

Write a small sketch that reads the number from the SD card, display it to screen and send a text to it?

I have tried that code before as stated in the question, I know the command works when the number is hard coded sim800.write("AT+CMGS=\"+447853557568\"\r\n"); and as I said I can get the "number" to display correctly in the serial monitor, I just cannot get the sms to send using the string variable.

This is the serial monitor response to the code which shows the "number" variable is being read correctly

Initializing SD card...initialization done.
CONFIG.DAT:
this is the phone number:
+447853557568

but nothing I have tried works

The code exactly what you tell it to do. So, clearly it works. It is your expectations that are wrong.

Explain/show exactly what the code is and does, and what your expectations are, and we can explain why they are wrong.

AWOL:

void pin_ISR() {

. . .
. . .
            Serial.println("HC-05 is now connected");



Just . . . don't.

The OP seems to have missed this one. Using Serial inside of an ISR is like making lead balloons.

I can only tell if code works by seeing what the serial monitor returns and or by the fact the expected result is actioned, which in this case is an sms message sent to the encoded number.

This code will send the sms

sim800.write("AT+CMGS=\"+447853557568\"\r\n");

this code will show the telephone number from the sd card CONFIG.BAT file that is assigned to the string variable "number"

  myFile = SD.open("CONFIG.DAT");
 
  if (myFile) {
   
    Serial.println("CONFIG.DAT:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {

                char ltr = myFile.read();
                number += ltr;     
    } 
   
    // close the file:
    myFile.close();
   
  } else {
   
    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");
   
  }
 
  }
 
Serial.println("this is the phone number: ");
Serial.print(number);

this is what shows in the serial monitor

Initializing SD card...initialization done.
CONFIG.DAT:
this is the phone number:
+447853557568

As the telephone number is saved to the card by a user, then the number on occasion can be changed by the user, in which case having the telephone number hard coded is unsuitable for this project, so by assigning the telephone number to the string variable number and replacing the hard coded number with the string variable if the sms number needs to be changed the software will not need to be edited and recompiled.

So the hard coded AT command is

sim800.write("AT+CMGS=\"+447853557568\"\r\n");

I want to remove the hard coded number "+447853557568" and replace it with the string variable "number", and the code I have tried so far fails in as much as the following code

sim800.println("AT + CMGS = \"" + number +"\"");

doesnt result in the sms being sent

Those guys commenting on the ISR Code, why do you make these comments without explaining why it shouldn't be done, there are many new users that are experimenting with Arduino and IOT, and a lot of users like me that have never coded in c or c++, and it is becoming obvious there are many different ideas and ways to achieve the same action with many different codes. For long standing members of a forum that has a purpose of educating best practice as well as promoting these technologies, these comments come over as extremely unhelpful and at worse smack of elitism from those who have more knowledge and experience in these matters, the fact that one of these comments may have been made by a moderator makes it worse.

If your here to help, educate and advise then more power to you, but if your just here to berate and belittle those who are trying there level best to learn then what is the point of you being here?

and the code I have tried so far fails in as much as the following code...doesnt result in the sms being sent

So, change sim800 to Serial in that line and figure out what the AT command looks like. I'm sure that you'll have an "Oh, duh" moment.

there are many new users that are experimenting with Arduino and IOT

New users and interrupts just do not go together.

When an ISR is called, all interrupts are disabled. Serial.print() writes data to a buffer and returns immediately, IF there is room in the buffer.

If there is not, then Serial.print() blocks until there IS room.

Room is made in the buffer by sending data out the serial port. THAT is done by other ISRs being called, which can’t happen because your ISR is running. So, if the buffer gets full, it can never be emptied, and the Arduino will appear to hang.

gptgeoff:
Those guys commenting on the ISR Code, why do you make these comments without explaining why it shouldn't be done, there are many new users that are experimenting with Arduino and IOT, and a lot of users like me that have never coded in c or c++, and it is becoming obvious there are many different ideas and ways to achieve the same action with many different codes. For long standing members of a forum that has a purpose of educating best practice as well as promoting these technologies, these comments come over as extremely unhelpful and at worse smack of elitism from those who have more knowledge and experience in these matters, the fact that one of these comments may have been made by a moderator makes it worse.

If your here to help, educate and advise then more power to you, but if your just here to berate and belittle those who are trying there level best to learn then what is the point of you being here?

Or, you could simply RTFM

If your here to help,

sp. "you're". HTH

Thank you Paul for explaining that, The reason I have done that was so I could see that the interrupt was working and also to be able to demonstrate that fact to my lecturer at uni.

To the other issue, I took your advice with changing sim800 to Serial, I had been wondering how I could see what was being created as the AT command, by comparing the hard code and the "number" inserted code I think I have identified that a double quote is being returned on a newline in the "number" code, so I'm assuming the code is being read as

AT + CMGS = "+447853557568

instead of

AT + CMGS = "+447853557568"

I tried to add a double quote but without success, so how do I make sure the double quote is in the right place/

AWOL pointing out my grammar is just being pedantic and silly, just leave me alone and let me learn something from those on here who want to pass on their knowledge freely

You asked for help/education.
Imagine what your lecturers would saymark if you presented your results looking like that.

People can do without the kind of so called help you have offered so far, If you think by trying to belittle me you are achieving anything I can assure you you are wrong, So again I respectfully request you leave me alone

OK.

As it happens, I'm down your neck-of-the-woods next week to talk to some of the uni staff about STEM initiatives.
Maybe you could come down the pub with us.

gptgeoff:
Those guys commenting on the ISR Code, why do you make these comments without explaining why it shouldn't be done, there are many new users that are experimenting with Arduino and IOT, and a lot of users like me that have never coded in c or c++, and it is becoming obvious there are many different ideas and ways to achieve the same action with many different codes.

That's true as far as it goes but there is no Magic.

For long standing members of a forum that has a purpose of educating best practice as well as promoting these technologies, these comments come over as extremely unhelpful and at worse smack of elitism from those who have more knowledge and experience in these matters, the fact that one of these comments may have been made by a moderator makes it worse.

So drink bleach, those elitist doctors are full of themselves, you'll be okay.

If your here to help, educate and advise then more power to you, but if your just here to berate and belittle those who are trying there level best to learn then what is the point of you being here?

Help! Help! I need help getting out of the car!
"Open the door."
i tried that, it didn't work!
"Unlock the door."
The door won't open! Help! Help!
"Unlock the door."
Somebody help! The door won't open!
"Unlock the door."
STFU with your elitist talk! You're not helping! You just think you know everything!
WHY WON'T ANYONE HELP ME? WHAAAA!

News for anyone like you: you come asking for help from people giving their free time just for those who might learn, don't WASTE THEIR TIME and expect to get your butt kissed. Hire someone for that.

gptgeoff:
Thank you Paul for explaining that, The reason I have done that was so I could see that the interrupt was working and also to be able to demonstrate that fact to my lecturer at uni.

Flash a led. Store the value in a volatile variable and write a task in loop to print it outside of the IRQ.

I think I have identified that a double quote is being returned on a newline in the "number" code

It appears that you are stuffing everything you read from the file into the String instance. You could do some sanity checking. After all, the only things that belong in a phone number are digits. The + should not be stored on the SD card as part of the phone number.

Or, you could investigate the methods in the String class, like trim() that get rid of stuff you put in the instance but don't want.

By the way, I hate anonymous printing. I always prefix what I print, and I always surround strings with brackets, so I can see things like carriage returns and line feeds in the string.

Serial.print("This is the AT command: [");
Serial.print("AT + CMGS = \"" + number +"\"");
Serial.println("]");

This is the AT command: [AT + CMGS = "+447853557568"
]

and

This is the AT command: [AT + CMGS = "+447853557568
"
]

tell me two very different things.