SIM800L Sending SMS to a user-defined phone number

Hi,

I am creating a device as a project and one of its function is to allow the users to input a contact number. Now, this contact number will automatically receive an SMS alert on certain conditions. My problem now is that I am having difficulty on using this user-defined phone number. From what I have researched, the AT+CMGS command would need the actual number to be inputted in the actual code. But this is not gonna be the case for me as the users would only be able to input the number through the device.

I am using Arduino Uno, SIM800L Arduino Shield and the library SoftwareSerial. At the moment, I am using 4 pushbuttons to input the numbers and created a code to save the number in the form of +ZZxxxxxxxxxx in a variable called “numberr”.

Now, it is not possible for me to do something like AT+CMGS = numberr. I also tried to simply do numberr = +ZZxxxxxxxxxx and do the same method but neither worked. I have also looked at phonebook entry command but this is still a bit confusing for me.

So is there a way to use “numberr” with +CMGS? Any suggestions? Thank you.

Here is some of the code for you guys to see.

//Library declarations here

//Other variable declarations here

int phonenum[10] = {contactnumber1, contactnumber2, contactnumber3, contactnumber4, contactnumber5, contactnumber6, contactnumber7, contactnumber8, contactnumber9, contactnumber10};
char numberr[13] = " ";


struct areacode
{
  char a = 43; // for "+"
  int b = 44; //UK code for the meantime
};

struct areacode code1;
struct areacode code2;


void setup()
{
  //I have the setup
  
}

void loop()

//Just to start
{
  if (digitalRead(Button1) == LOW)
  {
    Add_Number(&contactnumber1,&contactnumber2,&contactnumber3,&contactnumber4,&contactnumber5,&contactnumber6,&contactnumber7,&contactnumber8,&contactnumber9,&contactnumber10,&numbercount);
  }
}


void Add_Number(int*contactnum1,int*contactnum2,int*contactnum3,int*contactnum4,int*contactnum5,int*contactnum6,int*contactnum7,int*contactnum8,int*contactnum9,int*contactnum10, int*numbercounter)
{

 if (digitalRead(Button1) == LOW) //button for the adding number
  {
  subcount = subcount + 1;
  while (subcount = 1)
  {
    lcd.setCursor(0,0);
    lcd.print("+44");
    lcd.setCursor(4,0);
    lcd.print(contactnumber1);
    lcd.print(contactnumber2);
    lcd.print(contactnumber3);
    lcd.print(contactnumber4);
    lcd.print(contactnumber5);
    lcd.print(contactnumber6);
    lcd.print(contactnumber7);
    lcd.print(contactnumber8);
    lcd.print(contactnumber9);
    lcd.print(contactnumber10);
    lcd.setCursor (0,2);
    lcd.print("B2 Next Digit"); //toggle the digits
    lcd.setCursor(0,3);
    lcd.print("B3 Increment"); //change the digit
    lcd.setCursor(13,3);
    lcd.print("B4 Save"); //save

    if (digitalRead(Button2) == LOW)//Next Digit
    {
      Next_Digit(&numbercount);
    }

      if (digitalRead(Button4) == LOW)//Save
  {
    Save_Number();
  }
    
   if (numbercount == 0)
   {   
    lcd.setCursor(4,0);
    lcd.print(contactnumber1);
    delay(200); //to make the number blink
    lcd.setCursor(4,0);
    lcd.print(" ");
    delay (200);
    
          if (digitalRead(Button3) == LOW) //inc
          {
               if (*contactnum1 == 9)
                  {
                    *contactnum1 = 0;
                  }
               else
                  {
                    *contactnum1 = *contactnum1 + 1;
                  }
          }

          if (digitalRead(Button2) == LOW)
          {
            
            Next_Digit(&numbercount);
          }

//Same principle for the other numbers
 }
}
}



void Next_Digit (int*numbercounter)
{
  if (*numbercounter == 9)
  {
    *numbercounter = 0;
    delay (50);
    Add_Number(&contactnumber1,&contactnumber2,&contactnumber3,&contactnumber4,&contactnumber5,&contactnumber6,&contactnumber7,&contactnumber8,&contactnumber9,&contactnumber10,&numbercount);
  }
  else
  {
    *numbercounter = *numbercounter + 1;
    delay (50);
    Add_Number(&contactnumber1,&contactnumber2,&contactnumber3,&contactnumber4,&contactnumber5,&contactnumber6,&contactnumber7,&contactnumber8,&contactnumber9,&contactnumber10,&numbercount);
  }

  if (digitalRead(Button4) == LOW)
  {
    Save_Number();
  }
}


void Save_Number ()
{
  lcd.clear();

  //int phonenum[10] = {contactnumber1, contactnumber2, contactnumber3, contactnumber4, contactnumber5, contactnumber6, contactnumber7, contactnumber8, contactnumber9, contactnumber10};
   
   lcd.setCursor(7,2);
   lcd.print("Saving...");
   delay(800);
   lcd.clear();

   sprintf(numberr, "%c%d%d%d%d%d%d%d%d%d%d%d",code1.a,code2.b,contactnumber1, contactnumber2, contactnumber3, contactnumber4, contactnumber5, contactnumber6, contactnumber7, contactnumber8, contactnumber9, contactnumber10);
   
/*just to test and output the number in the lcd*/
   while (subcount == 1)
   {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(numberr);

    delay(2000);

   // Press Button4 to send SMS
 
   if (digitalRead(Button4) == LOW)
   {
     GSM_Module.begin(numberr);
     delay(200);
    Send_SMS();
   }
   
   }
   
}


/*Testing Stages*/
void Send_SMS()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Sending...");
  
  GSM_Module.println("AT+CMGF=1");  //setting to SMS text mode
  delay(500);

  GSM_Module.println("AT+CMGS = User"); //"AT+CMGS=\"number here\"" // Error
  delay(500);

  GSM_Module.println("Testttt "); //text content
  delay(500);

  GSM_Module.write(26); //Ctrl+Z to send message
  delay(500);
}

Show all your code. We need to know how you prepared variables code1.a,code2.b,contactnumber1, contactnumber2, etc...

Also did you try printing the result from sprintf() conversion?

Hi. I have edited and added the code. I tried to put as much as I can since the whole message would exceed the limit. As for outputting the sprintf, I have printed it out and showed the number in an LCD. So everything works apart from sending the SMS. If I put AT+CMGS = +ZZxxxxxxxxxx, it works. But I'm wondering if there's a way to use a variable instead, containing that string. Thanks.

You can add code as an attachment if it's too big. What do you mean, "I'm wondering if there's a way to use a variable instead, containing that string"? String variables in C are handled with pointers. You are already doing that. 'numberr' is a pointer variable that points to the memory containing the AT string that you constructed.

I have now attached the code. Also what I meant is to use “numberr” with +CMGS command. Thank you.

Hi. I have used a different library from GitHub. It's called Sim800l library. I have tested this and it works. Thank you.