Sunday, August 25, 2013

Remote Door Open by SMS using Arduino



GSM Module used:

SIM900
Wiki
Testing the module


Video:


Code:
// this code was tested using the modem SIM900

/*

this code turn "On" and "Off" an LED by SMS.

Commands:

$ON$
$OFF$

Note: the commands are case sensitive.

*/


#include <SoftwareSerial.h>

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

byte led = 13;   // use "byte" instead of "int" to save memory.
byte led_on = 0;
byte led_off = 0;
byte dollar_count = 0;

long last_millis = 0;
const int delay_time = 5000;

const int buff_size = 10;
char buffer[buff_size];

void setup()  
{
  pinMode(led, OUTPUT);
  mySerial.begin(9600);
  Serial.begin(9600);
 
   mySerial.println("AT\r\n"); // check communication with the modem.
   delay(500);
   mySerial.println("AT\r\n");
   
   //delete all read SMS
   
   for(int x=1; x<=20; x++){
   mySerial.print("AT+CMGD=");
   mySerial.print(x);
   mySerial.print(",");
   mySerial.print("3");
   mySerial.print("\r\n");  
   delay(1000);
   }
   
  
 
}

void loop(){

     
     if ((millis() - last_millis) > delay_time ){
       mySerial.print("AT+cmgl=\"REC UNREAD\"\r\n");
       last_millis = millis();
     }
  

  if (mySerial.available() > 0){

    char inchar = mySerial.read(); // print in data to  software serial port for debug perpose.
    
    Serial.print(inchar);

    if(inchar == '\r' || inchar == '\n'){
      dollar_count = 0;
      led_on = 0;
      led_off = 0;
    }

    if(inchar == '$'){
      dollar_count++; 
     } 
    

      switch (dollar_count){
      case 1:
      
      if(inchar == 'O' || inchar == 'N'){
        led_on++;
      }
      
      if(inchar == 'O' || inchar == 'F'){
        led_off++;
      }
      
      if(led_on == 2){
        digitalWrite(led, HIGH);
      }
      
      if(led_off == 3){
        digitalWrite(led, LOW);
      }
      
      break;
        
      }  


  }


}




                                                                                           By: Elimeléc