arduinosmsgsmsim900

Why does Arduino mega and SIM900 won't send message if I input/read to serial monitor, different phone numbers?


I am new to arduino and gsm sim900, and I currently working with the project when student scan their barcode it will input attendance to database and send sms to their guardians. My issue is when I try to input different numbers like "serial.available" and "readString(phonenumber)" it wont send a message, I get no errors and the sim have e-load/. Sorry for bad grammar. Here is the code:

#include <SoftwareSerial.h>
SoftwareSerial SIM900A(6,7);
String number;
void setup()
{
  SIM900A.begin(9600);
  Serial.begin(9600);
  Serial.println ("SIM900A Ready");
  delay(100);
 Serial.print("Enter PHONE NUMBER: ");
 while(Serial.available()==0){}          //INPUT PHONE NUMBER
 number = Serial.readString();
 delay(500);
  Serial.println ("Type s to send message or r to receive message");

}
void loop()
{
  if (Serial.available()>0)
   switch(Serial.read())
  {
    case 's':
      SendMessage();
      break;
    case 'r':
      RecieveMessage();
      break;
  }
 if (SIM900A.available()>0)
   Serial.write(SIM900A.read());
}
 void SendMessage()
{
  Serial.println ("Sending Message");
  SIM900A.println("AT+CMGF=1");
  delay(1000);
  Serial.println ("Set SMS Number");
  SIM900A.print("AT+CMGS=\""+number+"\"\r");   //send to input phone number
  delay(1000);
  Serial.print(number);
  delay(500);
  Serial.println ("Set SMS Content");
  SIM900A.print("Message from ArduinoGSMSIM900");
  delay(100);
  Serial.println ("Finish");
  SIM900A.println((char)26);
  delay(1000);
  Serial.println ("Message has been sent");
}
 void RecieveMessage()
{
  Serial.println ("SIM900A Membaca SMS");
  delay (1000);
  SIM900A.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
  Serial.write ("Unread Message done");
}

Please help me, Thanks in advance!


Solution

  • Ok after so many research I solved my problem. This is for people who will have the same issue as mine that wasted a lot of time for a simple line of code. When I serial.readString() the output will have a white space like @hcheung said, so to get rid of that white space I use number.Trim(), it will read the string in exact length.

    Final sketch for arduino GSM SIM900:

    #include <SoftwareSerial.h>
    SoftwareSerial SIM900A(10,11);
    String number;
    String message;
    void setup()
    {
      SIM900A.begin(9600);
      Serial.begin(9600);
      Serial.println ("SIM900A Ready");
      delay(100);
     Serial.print("Enter PHONE NUMBER: ");
     while(Serial.available()==0){}
     number = Serial.readString();
     delay(100);
     number.trim();
     delay(200);
     Serial.println(number.length());
     Serial.print("Enter MESSAGE: ");
     while(Serial.available()==0){}
     message = Serial.readString();
     delay(100);
      Serial.println ("Type s to send message or r to receive message");
    
    }
    void loop()
    {
      if (Serial.available()>0)
       switch(Serial.read())
      {
        case 's':
          SendMessage();
          break;
        case 'r':
          RecieveMessage();
          break;
      }
     if (SIM900A.available()>0)
       Serial.write(SIM900A.read());
    }
     void SendMessage()
    {
      Serial.println ("Sending Message");
      SIM900A.println("AT+CMGF=1");
      delay(1000);
      Serial.println ("Set SMS Number");
      SIM900A.print("AT+CMGS=\""+number+"\"\r");
      delay(1000);
      Serial.print(number);
      delay(500);
      Serial.println ("Set SMS Content");
      SIM900A.print(message);
      delay(100);
      Serial.println ("Finish");
      SIM900A.println((char)26);
      delay(1000);
      Serial.println ("Message has been sent");
    }
     void RecieveMessage()
    {
      Serial.println ("SIM900A Membaca SMS");
      delay (1000);
      SIM900A.println("AT+CNMI=2,2,0,0,0");
      delay(1000);
      Serial.write ("Unread Message done");
    }