1

I have a laser security system project with a GSM SIM900 and a few sensors. I'm using an Arduino Mega 2560, and everything is working normally, except the GSM is not sending an SMS to the phone and I don't know why!

this is my code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <SoftwareSerial.h>

SoftwareSerial sim900(0,1);
String  TextForSMS;

int dataldr =0;
int sensorldr = A0;
int ldrState = LOW;            
int ldrthres = 900;

int pir = A1;
int pirState = LOW;            
int val = 0; 

int gas = A8;
int sensorgasThres = 400;

#define Password_Length 8 
#define PINNUMBER ""

int signalPin = 2;
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;
int led6 = 7;
int laser =6;


char Data[Password_Length]; 
char Master[Password_Length] = "123A456"; 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {30,31,32, 33};
byte colPins[COLS] = {34, 35, 36, 37};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x3F, 20, 4);  

void setup(){

  lcd.init(); 
  lcd.backlight();

  pinMode(signalPin, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(laser, OUTPUT);

  pinMode(gas, INPUT);
  pinMode(pir, INPUT);
  pinMode(sensorldr,INPUT);


 randomSeed(analogRead(0));
  Serial.begin(9600);
  sim900.begin (9600);
  Serial.println(" loggin time completed");
delay(5000);

}

void loop(){

     digitalWrite(laser, HIGH);
     digitalWrite(led1, HIGH);

     lcd.setCursor(0,0);
     lcd.print("Enter Password:");

   char customKey = customKeypad.getKey();

  if (customKey){

    Data[data_count] = customKey; 
    lcd.setCursor(data_count,1); 
    lcd.print(Data[data_count]); 
    data_count++; 
    }

  if(data_count == Password_Length-1){
    lcd.clear();

    if(!strcmp(Data, Master)){

      lcd.print("Correct");
      digitalWrite(led3, HIGH);
      tone(signalPin, 150,200);
      delay(5000);
      digitalWrite(signalPin, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(laser, LOW);
      ldrState = LOW;
      delay(5000);
      digitalWrite(laser, HIGH);
      ldrState = HIGH;

      }
    else{
            lcd.print("Incorrect");
            digitalWrite(led4, HIGH); 
            digitalWrite(signalPin, HIGH);

      delay(2000);

            digitalWrite(led4, LOW);
           digitalWrite(signalPin, LOW);
 TextForSMS="\nIntruder password wrong";
  sendSMS(TextForSMS);
 lcd.setCursor(10,2);
  lcd.print("sms2sent");      }

    lcd.clear();
    clearData();  
  }

  int   dataldr =analogRead(sensorldr);
     lcd.setCursor(0,3);
     lcd.print("Laser:");
     lcd.setCursor(7,3);
     lcd.print(dataldr);

  if(dataldr<ldrthres){
    digitalWrite(signalPin, HIGH);
    digitalWrite(led2, HIGH);
  TextForSMS="\nIntruder detected";
  sendSMS(TextForSMS);
  lcd.setCursor(10,2);
  lcd.print("sms1sent");

  }
  else
  {
    digitalWrite(led2, LOW);
    noTone(signalPin);

  }
  delay(100);



 int analogSensor = analogRead(gas);
  lcd.setCursor(0,2);
  lcd.print("Gas:");
  lcd.setCursor(4,2);
  lcd.print(analogSensor);
  // Checks if it has reached the threshold value
  if (analogSensor > sensorgasThres)
  {
            digitalWrite(led5, HIGH);
            digitalWrite(signalPin, HIGH);

     TextForSMS="\nIntruder GAS";
  sendSMS(TextForSMS);
  lcd.setCursor(10,2);
  lcd.print("sms3sent");
  }
  else
  {
    digitalWrite(led5, LOW);
    noTone(signalPin);
  }
  delay(100);


  val = digitalRead(pir); 
  if (val == HIGH) {            
    digitalWrite(led6, HIGH); 

    if (pirState == LOW) {
           Serial.println("Motion detected!");
           pirState = HIGH;
    }
  } else {
    digitalWrite(led6, HIGH);
 delay (5000);
    if (pirState == LOW){
           Serial.println("Motion ended!");
      pirState = LOW;
    }
  }

}


void clearData(){
  while(data_count !=0){
    Data[data_count--] = 0; 
  }
  return;
}


void sendSMS (String message){
    sim900.print("AT+CMGF=1");
    delay(1000);

  sim900.println("AT+CMGS=\"+380633965886\"\r ");
      delay(1000);
sim900.println(message);
    delay(1000);
sim900.println((char)26);
    delay(1000);
sim900.println();
    delay(1000);


  }

Also on Github:

https://github.com/shiyaroo/gsm-sim900/blob/master/laser_security_system__gsm.ino

  • 2
    reduce your code to only send a fixed text message ..... remove all code that is not related to sending the text message ...... please post the result – jsotola Dec 22 '18 at 22:34
  • its all related to each other but i will try to show u the main code about sending sms – Shiyar Dec 22 '18 at 22:51
  • can u see the last part of code void sendSMS (String message) if i should change something ? – Shiyar Dec 22 '18 at 23:06
  • 1
    you are not understanding my comment – jsotola Dec 22 '18 at 23:11
2

It’s not a good approach to put delay function between AT commands.

You have to parse SIM900 responses and check if module replies with OK.

If you don’t receive responses from module, could be different problems:

  1. wrong baudrate
  2. connection or power supply problem
  3. SIM card problem (out of cash, sim lock with pin, etc..)

here you can find a useful guide: https://randomnerdtutorials.com/sim900-gsm-gprs-shield-arduino/

  • the step 1,2,3 are checked and no problem , but i didn;t understood what u mean here (It’s not a good approach to put delay function between AT commands) – Shiyar Dec 23 '18 at 0:04
  • When you send an AT command like this sim900.print("AT+CMGF=1");, you shouldn’t put a delay function like this delay(1000);, but you should reading characters coming from Serial and check if module replies with OK or with some errors. – leoc7 Dec 23 '18 at 8:47
  • 1
    Try to do a fast test, Create a sketch that send only ‘’AT’’ command and check if SIM900 replies with OK. – leoc7 Dec 23 '18 at 8:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.