My Cart

How to Filter Words within an SMS Using GSM/GPRS Mobile Kit

Difficulty Level: Expert -

Introduction

This tutorial allows the user to filter words within an SMS. An LED indicates if the filter has been used or not, and displays the filted SMS in the LCD.

Ingredients:

    - 1 x GSM/GPRS Mobile Kit:
    • 1x Platform
    • 1x GPRS/GSM QUADBAND MODULE (SIM900)
    • 1x GPRS antenna
    • 1x RGB LED
    • 3x 100Ω resistors
    • 1x Potentiometer
    • 1x Breadboard
    • 1x External power supply
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the GPRS/GSM QUADBAND MODULE (SIM900) Shield, consult the main tutorial.

Step 1: Connection

Connect the GPRS antenna to the shield and then, connect the shield to Arduino or to Raspberry Pi connection bridge. Connect the LED with the resistors, the potentiometer and the LCD in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the VCC supply (5V in Arduino and 3.3V in Raspberry Pi) and ground. A potentiometer will adjust LCD contrast.Connect left terminal of the potentiometer to VCC and the right terminal to GND. Connect the central leg of the potentiometer to the third pin of the LCD display. Follow the wire color diagram to connect all LCD pins. Finally connect pin 9,10 and 11 to the RGB LED with a resistor in each pin and power it with VCC in the longest leg of the RGB LED.

Step 2: The Code

Arduino:

Code:
/*  
 *  GPRS/GSM Quadband Module (SIM900)
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Luis Miguel MartĂ­
 */

#include <LiquidCrystal.h>

#define RED 9
#define BLUE 10
#define GREEN 11

int8_t answer;
int x;
int onModulePin = 2;
char SMS[200];
char SMScopy[200];
char aux_string[30];
char received[200];
byte pos[2] = {0, 0};
//forbidden fruits in our SMS
char message1[] = {"peach"};
char message2[] = {"apple"};
char message3[] = {"orange"};
char message4[] = {"lemon"};
char message5[] = {"strawberry"};
char message6[] = {"kiwi"};
//filtered word
char filtered[] = {"banana"};
boolean changed = false;

LiquidCrystal lcd(12, 8, 6, 5, 4, 3);

void setup()   {

  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  //LED's are controlled with negative logic
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  Serial.println("Starting...");
  power_on();
  delay(3000);
  memset(SMS, 0, sizeof(SMS));//initialize SMS
  Serial.println("Setting SMS mode...");
  sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
  delay(500);
  sendATcommand("AT+CNMI=3,2,0,0", "OK", 1000); //configure new message reception
  lcd.begin(16, 2);
  lcd.print("Banana'o'Matic");
  lcd.setCursor(0, 2);
  lcd.print("SMS Fruit Filter");

}


void loop()
{
  //wait for the SMS
  if (Serial.read() == '+') {

    for (byte i = 0; i < 200; i++) {
      delay(1);
      received[i] = Serial.read();
    }
    //here is where the text starts
    //data before this are time and phone number sent the SMS
    byte z = 47;
    while (char(received[z]) != '\r') {
      SMS[z - 47] = received[z];
      z++;
    }
    byte lenSMS = checkSMS(z - 47);
    printSMS(lenSMS);
    lcd.setCursor(0, 2);
    lcd.print("SMS Fruit Filter");
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, HIGH);
    changed = false;
  }

}


void power_on() {

  uint8_t answer = 0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin, HIGH);
    delay(3000);
    digitalWrite(onModulePin, LOW);

    // waits for an answer from the module
    while (answer == 0) {   // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {

  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialice the string

  delay(100);

  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer

  Serial.println(ATcommand);    // Send the AT command


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do {
    // if there are data in the UART input buffer, reads it and checks for the asnwer
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer) != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  } while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}

byte checkSMS(byte lenght) {

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message1) - 1; j++) {
      if (message1[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message1) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message1) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message1) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message2) - 1; j++) {
      if (message2[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message2) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message2) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message2) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message3) - 1; j++) {
      if (message3[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message3) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message3) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message3) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message4) - 1; j++) {
      if (message4[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message4) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message4) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message4) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message5) - 1; j++) {
      if (message5[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message5) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message5) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message5) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message6) - 1; j++) {
      if (message6[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message6) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message6) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message6) + 1;
        pos[0] = 0;
      }
    }
  }

  return lenght;

}

byte rewriteSMS(byte init, byte finish, byte len1, byte len2) {

  for (byte i = 0; i < len2 + 1; i++) {
    if (i == init) {
      for (byte j = 0; j < sizeof(filtered) - 1; j++) {
        SMScopy[i + j] = filtered[j];
      }
      i = i + len1;
    }
    if (i >= finish) SMScopy[i - (finish - init) + sizeof(filtered) - 1] = SMS[i];
    else SMScopy[i] = SMS[i];

  }
  len2 = len2 + sizeof(filtered) - 1 - len1;
  for (byte i = 0; i <= len2 + 1; i++) {
    SMS[i] = SMScopy[i];
  }
  return len2;
}

void printSMS(byte len) {

  lcd.setCursor(0, 2);
  if (changed) {
    digitalWrite(RED, LOW);
  } else {
    digitalWrite(GREEN, LOW);
  }
  //clean lcd second line
  for (byte i = 0; i < 16; i++) {
    lcd.print(" ");
  }
  for (byte i = 0; i < 16; i++) {
    lcd.print(SMS[i]);
  }
  delay(1000);
  for (byte j = 0; j <= len - 16; j++) {
    lcd.setCursor(0, 2);
    for (byte i = j; i < j + 16; i++) {
      lcd.print(SMS[i]);
    }
    delay(150);
  }
  delay(3000);
}
        

Raspberry Pi:

Code:
/*  
 *  GPRS/GSM Quadband Module (SIM900)
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos Martinez
 */
 
#include "arduPi.h"

#define RED 9
#define BLUE 10
#define GREEN 11

int8_t answer;
int x;
int onModulePin = 2;
char SMS[200];
char SMScopy[200];
char aux_string[30];
char received[200];
int pos[2] = {-1, -1};
//forbidden fruits in our SMS
char message1[] = {"peach"};
char message2[] = {"apple"};
char message3[] = {"orange"};
char message4[] = {"lemon"};
char message5[] = {"strawberry"};
char message6[] = {"kiwi"};
//filtered word
char filtered[] = {"banana"};
boolean changed = false;

void power_on();
int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout);
void printSMS();
byte rewriteSMS(byte init, byte finish, byte len1, byte len2);
byte checkSMS(byte lenght);

void setup()   {

  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  //LED's are controlled with negative logic
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  printf("Starting...\n");
  power_on();
  delay(3000);
  memset(SMS, 0, sizeof(SMS));//initialize SMS
  printf("Setting SMS mode...\n");
  sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
  delay(500);
  sendATcommand("AT+CNMI=3,2,0,0", "OK", 1000); //configure new message reception
  printf("Banana'o'Matic\n");
  printf("SMS Fruit Filter\n");

}


void loop()
{
  //wait for the SMS
  if (Serial.read() == '+') {
	byte i=0;
    while(Serial.available()>0) {
      delay(1);
      received[i] = Serial.read();
      i++;
    }
    //here is where the text starts
    //data before this are time and phone number sent the SMS
    int z = 47;
    while (char(received[z]) != '\n') {
      SMS[z - 47] = received[z];
      z++;
    }

    byte lenSMS = checkSMS(z - 47);
    printSMS();
    printf("SMS Fruit Filter\n");
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, HIGH);
    changed = false;
  }

}


void power_on() {

  uint8_t answer = 0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin, HIGH);
    delay(3000);
    digitalWrite(onModulePin, LOW);

    // waits for an answer from the module
    while (answer == 0) {   // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}

int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout) {

  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialice the string

  delay(100);

  Serial.println(ATcommand);    // Send the AT command

  x = 0;
  previous = millis();

  // this loop waits for the answer
  do {
    // if there are data in the UART input buffer, reads it and checks for the asnwer
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      printf("%c", response[x]);
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer) != NULL)
      {
        printf("\n");
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  } while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}

byte checkSMS(byte lenght) {

byte i=0;
byte j=0;

while(i < lenght){
    byte last_i = i;
    
    while(j < sizeof(message1) - 1){
		
		
      if (message1[j] == SMS[i]) {
        i++;
        if (pos[0] == -1) pos[0] = i-1;
        if (j == sizeof(message1) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message1) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message1) + 1;
        pos[0] = -1;
      }
      j++;
    }
    j=0;
    i++;	
  }

i=0;
j=0;
  while(i < lenght){
	
    byte last_i = i;
    
    while(j < sizeof(message2) - 1){
		
      if (message2[j] == SMS[i]) {
        i++;
        if (pos[0] == -1)  pos[0] = i - 1;
        if (j == sizeof(message2) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message2) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message2) + 1;
        pos[0] = -1;
      }
      j++;
    }
    j=0;
    i++;	
  }

i=0;
j=0;
  while(i < lenght){
	
    byte last_i = i;
    
    while(j < sizeof(message3) - 1){
			
      if (message3[j] == SMS[i]) {
        i++;
        if (pos[0] == -1)  pos[0] = i - 1;
        if (j == sizeof(message3) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message3) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message3) + 1;
        pos[0] = -1;
      }
      j++;
    }
    j=0;
    i++;	
  }

i=0;
j=0;
  while(i < lenght){
	
    byte last_i = i;
    
    while(j < sizeof(message4) - 1){
			
      if (message4[j] == SMS[i]) {
        i++;
        if (pos[0] == -1)  pos[0] = i - 1;
        if (j == sizeof(message4) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message4) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message4) + 1;
        pos[0] = -1;
      }
      j++;
    }
    j=0;
    i++;	
  }

i=0;
j=0;
  while(i < lenght){
	
    byte last_i = i;
    
    while(j < sizeof(message5) - 1){
			
      if (message5[j] == SMS[i]) {
        i++;
        if (pos[0] == -1) pos[0] = i - 1;
        if (j == sizeof(message5) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message5) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message5) + 1;
        pos[0] = -1;
      }
      j++;
    }
    j=0;
    i++;	
  }
  
i=0;
j=0;
 while(i < lenght){
	
    byte last_i = i;
    
    while(j < sizeof(message6) - 1){
			
      if (message6[j] == SMS[i]) {
        i++;
        if (pos[0] == -1)  pos[0] = i - 1;
        if (j == sizeof(message6) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message6) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message6) + 1;
        pos[0] = -1;
      }
      j++;
    }
    j=0;
    i++;	
  }

  return lenght;

}

byte rewriteSMS(byte init, byte finish, byte len1, byte len2) {

byte i =0;

  while (i < len2 + 1){
    if (i == init) {
		byte j = 0;
		while(j < sizeof(filtered) - 1){
        SMScopy[i + j] = filtered[j];
        j++;
      }
      i = i + len1;
    }



    if (i >= finish) SMScopy[i - (finish - init) + sizeof(filtered) - 1] = SMS[i];
    else SMScopy[i] = SMS[i];
  i++;
  }

  len2 = len2 + sizeof(filtered) - 1 - len1;
  i=0;
  while( i <= len2 + 1){
    SMS[i] = SMScopy[i];
    i++;
  }
  return len2;
}

void printSMS() {

  if (changed) {
    digitalWrite(RED, LOW);
  } else {
    digitalWrite(GREEN, LOW);
  }
 
printf("SMS: %s\n",SMS);
delay(3000);
memset(SMS, 0, sizeof(SMS)); 
}

int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

        

Intel Galileo:

Code:
/*  
 *  GPRS/GSM Quadband Module (SIM900)
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
#include <LiquidCrystal.h>

#define RED 9
#define BLUE 10
#define GREEN 11

int8_t answer;
int x;
int onModulePin = 2;
char SMS[200];
char SMScopy[200];
char aux_string[30];
char received[200];
byte pos[2] = {0, 0};
//forbidden fruits in our SMS
char message1[] = {"peach"};
char message2[] = {"apple"};
char message3[] = {"orange"};
char message4[] = {"lemon"};
char message5[] = {"strawberry"};
char message6[] = {"kiwi"};
//filtered word
char filtered[] = {"banana"};
boolean changed = false;

LiquidCrystal lcd(12, 8, 6, 5, 4, 3);

void setup()   {

  lcd.begin(16, 2);
  lcd.print("Banana'o'Matic");
  lcd.setCursor(0, 2);
  lcd.print("SMS Fruit Filter");
  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);
  Serial1.begin(115200);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  //LED's are controlled with negative logic
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  Serial.println("Starting...");
  power_on();
  delay(3000);
  memset(SMS, 0, sizeof(SMS));//initialize SMS
  Serial.println("Setting SMS mode...");
  sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
  delay(500);
  sendATcommand("AT+CNMI=3,2,0,0", "OK", 1000); //configure new message reception

}


void loop()
{
  //wait for the SMS
  if (Serial1.read() == '+') {

    for (byte i = 0; i < 200; i++) {
      delay(1);
      received[i] = Serial1.read();
    }
    //here is where the text starts
    //data before this are time and phone number sent the SMS
    byte z = 47;
    while (char(received[z]) != '\r') {
      SMS[z - 47] = received[z];
      z++;
    }
    byte lenSMS = checkSMS(z - 47);
    printSMS(lenSMS);
    lcd.setCursor(0, 2);
    lcd.print("SMS Fruit Filter");
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, HIGH);
    changed = false;
  }

}


void power_on() {

  uint8_t answer = 0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin, HIGH);
    delay(3000);
    digitalWrite(onModulePin, LOW);

    // waits for an answer from the module
    while (answer == 0) {   // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {

  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialice the string

  delay(100);

  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer

  Serial1.println(ATcommand);    // Send the AT command


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do {
    // if there are data in the UART input buffer, reads it and checks for the asnwer
    if (Serial1.available() != 0) {
      response[x] = Serial1.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer) != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  } while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}

byte checkSMS(byte lenght) {

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message1) - 1; j++) {
      if (message1[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message1) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message1) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message1) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message2) - 1; j++) {
      if (message2[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message2) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message2) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message2) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message3) - 1; j++) {
      if (message3[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message3) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message3) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message3) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message4) - 1; j++) {
      if (message4[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message4) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message4) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message4) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message5) - 1; j++) {
      if (message5[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message5) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message5) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message5) + 1;
        pos[0] = 0;
      }
    }
  }

  for (byte i = 0; i < lenght; i++) {
    byte last_i = i;
    for (byte j = 0; j < sizeof(message6) - 1; j++) {
      if (message6[j] == SMS[i]) {
        i++;
        if (pos[0] == 0) pos[0] = i - 1;
        if (j == sizeof(message6) - 2) {
          pos[1] = i;
          lenght = rewriteSMS(pos[0], pos[1], sizeof(message6) - 1, lenght);
          changed = true;
          i = 0;
        }
      } else {
        i = last_i;
        j = sizeof(message6) + 1;
        pos[0] = 0;
      }
    }
  }

  return lenght;

}

byte rewriteSMS(byte init, byte finish, byte len1, byte len2) {

  for (byte i = 0; i < len2 + 1; i++) {
    if (i == init) {
      for (byte j = 0; j < sizeof(filtered) - 1; j++) {
        SMScopy[i + j] = filtered[j];
      }
      i = i + len1;
    }
    if (i >= finish) SMScopy[i - (finish - init) + sizeof(filtered) - 1] = SMS[i];
    else SMScopy[i] = SMS[i];

  }
  len2 = len2 + sizeof(filtered) - 1 - len1;
  for (byte i = 0; i <= len2 + 1; i++) {
    SMS[i] = SMScopy[i];
  }
  return len2;
}

void printSMS(byte len) {

  lcd.setCursor(0, 2);
  if (changed) {
    digitalWrite(RED, LOW);
  } else {
    digitalWrite(GREEN, LOW);
  }
  //clean lcd second line
  for (byte i = 0; i < 16; i++) {
    lcd.print(" ");
  }
  for (byte i = 0; i < 16; i++) {
    lcd.print(SMS[i]);
  }
  delay(1000);
  for (byte j = 0; j <= len - 16; j++) {
    lcd.setCursor(0, 2);
    for (byte i = j; i < j + 16; i++) {
      lcd.print(SMS[i]);
    }
    delay(150);
  }
  delay(3000);
}