GSM based Security lock

Posted on at


Project:

                        locker by using avr and gsm

Block Diagram:

Features:         It has three specifications

  • Master key.
  • Message sent by lock to mobile rendomly.

Labs cover

  • Interrupts
  • Timers
  • Serial communication
  • Led interfacing
  • multiplexing
  • Lcd interfacing
  • Keypad interfacing
  • PWM(Optional)

 f

Brief description

In this project we are going to make GSM based security locker.you can open the lock by three diffrent way.one of them is by keypad,By keypad you can enter password directly.and the other way is by using mobile.you can send password to open the lock.

And the last way is that lock send a rendom password to you then you enter password by keypad which is sent to mobile. And you can chang the password on run time and store it permanent until you change the password.

now let start the project.

for this project we need some aquipment.

1) LCD

2) AVR (atmega 16)

3) keypad

4) dp9

5) max232 (for level coverter)

6) one pcb sheet.

7) FECL

8) iron

9) pcb print sheet

etc

code of this  project is.

/*
* GSM_LOCK.c
*
* Created: 11/29/2013 6:35:43 AM
* Author: Babar Adrees
*/

#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>


//Macros definition
#define BitGet(p,m) ((p) & (m))
#define BitSet(p,m) ((p) |= (m))
#define BitFlip(p,m) ((p) ^= (m))
#define Bit(x) (0x01 << (x))
#define BitClear(p, m) ((p) &= ~(m))


unsigned char msg2[]="ENTER PASSWORD";
unsigned char msg3[]="WELCOME BABAR";
unsigned char msg4[]="WRONG PASSWORD";
unsigned char msg5[]="OLD PASSWORD";
unsigned char msg6[]="NEW PASSWORD";
unsigned char msg7[]="PASSWORD CHANGE";
unsigned char msg8[]="NOT CHANGE";
//unsigned char msg5[]="OVER FLOW";
unsigned char cmnd_rec[]="AT+CMGR=";

unsigned char i=0,l=0,j=0,k=0,z,s,d1,d2,d3,x,y=0,s_p[4],r_p[4],m=0,total=0,arr[2],arr2[4];

//////////GSM COMMANDS TO SEND SMS/////////////////////////////////////////////////////////////
unsigned char cmnd1[]="AT\r";
unsigned char cmnd2[]="AT+CMGF=1\r";
unsigned char cmnd3[]="AT+CMGS=+923426143103\r";
unsigned char CtrlZ=0x1A;
//////////////////////////////////////////////////////////////////////////////////////////////

 

void LCDcmd(unsigned char cmdout)
{
PORTA= cmdout;
BitClear(PORTA, Bit(0)); //Clear RS
BitClear(PORTA, Bit(1)); //Clear RW
BitSet(PORTA, Bit(2)); //Set Enable
_delay_us(100);
BitClear(PORTA, Bit(2)); //Clear Enable
}

void cmd_4b(unsigned char Cmd)
{
char Cmd1;
Cmd1 = Cmd & 0xF0; //mask lower nibble because PA4-PA7 pins are used
LCDcmd(Cmd1); // send to LCD
Cmd = Cmd << 4; //shift left 4 bits
LCDcmd(Cmd);
}

void LCDdata(unsigned char dataout)
{
PORTA = dataout;
BitSet(PORTA, Bit(0)); //Set RS
BitClear(PORTA, Bit(1)); //Clear RW
BitSet(PORTA, Bit(2)); //Set Enable
_delay_us(50);
BitClear(PORTA, Bit(2)); //Clear Enable
}

void data_4b(unsigned char data_value)
{
char data_value1;
data_value1 = data_value & 0xF0;
LCDdata(data_value1);
data_value1 = data_value << 4;
LCDdata(data_value1);
}

void LCD_init() // Initialize LCD
{
cmd_4b(0x02); //to initialize LCD in 4-bit mode.
cmd_4b(0x28);
cmd_4b(0x0C); //Display is ON, cursor OFF
}
///////////////////////////////////////////////////////////////////////////////////////////
void display_msg1()
{
cmd_4b(0x80);
for(i=0;msg1[i];i++)
{
data_4b(msg1[i]);
_delay_ms(50);
}
}

void gsm_cmnd()
{
for(i=0;cmnd1[i];i++)
{
UDR=cmnd1[i];
while(!BitGet(UCSRA,Bit(5)));
}

_delay_ms(50);
for(i=0;cmnd2[i];i++)
{
UDR=cmnd2[i];
while(!BitGet(UCSRA,Bit(5)));
}

_delay_ms(50);
for(i=0;cmnd3[i];i++)
{
UDR=cmnd3[i];
while(!BitGet(UCSRA,Bit(5)));
}
_delay_ms(50);
for (i=0;i<4;i++)
{
s=s_p[i];
while(!BitGet(UCSRA,Bit(5)));
switch(s)
{
case 0: UDR='0'; break;
case 1: UDR='1'; break;
case 2: UDR='2'; break;
case 3: UDR='3'; break;
case 4: UDR='4'; break;
case 5: UDR='5'; break;
case 6: UDR='6'; break;
case 7: UDR='7'; break;
case 8: UDR='8'; break;
case 9: UDR='9'; break;
case 10: UDR='A'; break;
case 11: UDR='B'; break;
case 12: UDR='C'; break;
case 13: UDR='D'; break;
case 14: UDR='E'; break;
case 15: UDR='F'; break;
}


}
while(!BitGet(UCSRA,Bit(5)));
UDR='\r';//UDR=13;

_delay_ms(50);
while(!BitGet(UCSRA,Bit(5)));
UDR=0x1A;
_delay_ms(50);
}
///////////////////////////////////////////////////////////////////////////////
void col1()
{
BitSet(PORTC,Bit(0));
BitSet(PORTC,Bit(1));
BitSet(PORTC,Bit(2));
BitSet(PORTC,Bit(3));
BitClear(PORTC,Bit(0));

if (!(BitGet(PINC,Bit(4))))
{
data_4b('7');
r_p[m]=7;m++;
while(!(BitGet(PINC,Bit(4))));
}
BitSet(PORTC,Bit(0));
BitClear(PORTC,Bit(1));

if (!(BitGet(PINC,Bit(4))))
{
data_4b('4');
r_p[m]=4;m++;
while(!(BitGet(PINC,Bit(4))));
}
BitSet(PORTC,Bit(1));
BitClear(PORTC,Bit(2));

if (!(BitGet(PINC,Bit(4))))
{
data_4b('1');
r_p[m]=1;m++;
while(!(BitGet(PINC,Bit(4))));
}
BitSet(PORTC,Bit(2));
BitClear(PORTC,Bit(3));

if (!(BitGet(PINC,Bit(4))))
{
data_4b('F');
r_p[m]=0x0F;m++;
while(!(BitGet(PINC,Bit(4))));
}

BitSet(PORTC,Bit(3));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void col2()
{

BitSet(PORTC,Bit(0));
BitSet(PORTC,Bit(1));
BitSet(PORTC,Bit(2));
BitSet(PORTC,Bit(3));
BitClear(PORTC,Bit(0));

if (!(BitGet(PINC,Bit(5))))
{
data_4b('8');
r_p[m]=8;m++;
while(!(BitGet(PINC,Bit(5))));
}
BitSet(PORTC,Bit(0));
BitClear(PORTC,Bit(1));

if (!(BitGet(PINC,Bit(5))))
{
data_4b('5');
r_p[m]=5;m++;
while(!(BitGet(PINC,Bit(5))));
}

BitSet(PORTC,Bit(1));
BitClear(PORTC,Bit(2));

if (!(BitGet(PINC,Bit(5))))
{
data_4b('2');
r_p[m]=2;m++;
while(!(BitGet(PINC,Bit(5))));
}
BitSet(PORTC,Bit(2));
BitClear(PORTC,Bit(3));

if (!(BitGet(PINC,Bit(5))))
{
data_4b('0');
r_p[m]=0;m++;
while(!(BitGet(PINC,Bit(5))));
}

BitSet(PORTC,Bit(3));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void col3()
{

BitSet(PORTC,Bit(0));
BitSet(PORTC,Bit(1));
BitSet(PORTC,Bit(2));
BitSet(PORTC,Bit(3));
BitClear(PORTC,Bit(0));

if (!(BitGet(PINC,Bit(6))))
{
data_4b('9');
r_p[m]=9;m++;
while(!(BitGet(PINC,Bit(6))));
}
BitSet(PORTC,Bit(0));
BitClear(PORTC,Bit(1));

if (!(BitGet(PINC,Bit(6))))
{
data_4b('6');
r_p[m]=6;m++;
while(!(BitGet(PINC,Bit(6))));
}

BitSet(PORTC,Bit(1));
BitClear(PORTC,Bit(2));

if (!(BitGet(PINC,Bit(6))))
{
data_4b('3');
r_p[m]=3;m++;
while(!(BitGet(PINC,Bit(6))));
}
BitSet(PORTC,Bit(2));
BitClear(PORTC,Bit(3));

if (!(BitGet(PINC,Bit(6))))
{
data_4b('E');
r_p[m]=0x0E;m++;
while(!(BitGet(PINC,Bit(6))));
}

BitSet(PORTC,Bit(3));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void col4()
{

BitSet(PORTC,Bit(0));
BitSet(PORTC,Bit(1));
BitSet(PORTC,Bit(2));
BitSet(PORTC,Bit(3));
BitClear(PORTC,Bit(0));

if (!(BitGet(PINC,Bit(7))))
{
data_4b('A');
r_p[m]=0x0A;m++;
while(!(BitGet(PINC,Bit(7))));
}
BitSet(PORTC,Bit(0));
BitClear(PORTC,Bit(1));

if (!(BitGet(PINC,Bit(7))))
{
data_4b('B');
r_p[m]=0x0B;m++;
while(!(BitGet(PINC,Bit(7))));
}

BitSet(PORTC,Bit(1));
BitClear(PORTC,Bit(2));

if (!(BitGet(PINC,Bit(7))))
{
data_4b('C');
r_p[m]=0x0C;m++;
while(!(BitGet(PINC,Bit(7))));
}
BitSet(PORTC,Bit(2));
BitClear(PORTC,Bit(3));

if (!(BitGet(PINC,Bit(7))))
{
data_4b('D');
r_p[m]=0x0D;m++;
while(!(BitGet(PINC,Bit(7))));
}

BitSet(PORTC,Bit(3));
}

void keypad()
{
TCNT1=0x1B1E;
TCCR1A=0;
TCCR1B=0x05;

DDRC=0x0F;
BitSet(PORTC,Bit(4));
BitSet(PORTC,Bit(5));
BitSet(PORTC,Bit(6));
BitSet(PORTC,Bit(7));

while((m<=3) && (!BitGet(TIFR,Bit(2))))
{
BitClear(PORTC,Bit(0));
BitClear(PORTC,Bit(1));
BitClear(PORTC,Bit(2));
BitClear(PORTC,Bit(3));

if (!(BitGet(PINC,Bit(4))))
{
col1();
}

else if (!(BitGet(PINC,Bit(5))))
{
col2();
}
else if (!(BitGet(PINC,Bit(6))))
{
col3();
}
else if (!(BitGet(PINC,Bit(7))))
{
col4();
}
}
TCCR1B=0;
if (BitGet(TIFR,Bit(2)))
{
TIFR=1<<TOV1;
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg1[i];i++)
{
data_4b(msg1[i]);
_delay_ms(50);
}
}
}

void c_p()
{
if (BitGet(TIFR,Bit(2)))
{
GIFR=0xE0;
GICR=(1<<INT0) | (1<<INT1) | (1<<INT2);
if (k>=2)
{
BitClear(GICR,Bit(7));
}
TCCR1B=0;
TIFR=1<<TOV1;
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
BitClear(PORTB,Bit(0));
for(i=0;msg1[i];i++)
{
data_4b(msg1[i]);
_delay_ms(50);
}
}

if (m>3)
{
GICR=0;
TCNT1=0xD9DA;
TCCR1A=0;
TCCR1B=0x05;
m=0;y=0;j=0;
for (i=0;i<4;i++)
{

if (r_p[i]==s_p[i])
{
total++;
}
}
if (total==4)
{
BitSet(PORTB,Bit(0));
k=0;
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg3[i];i++)
{
data_4b(msg3[i]);
_delay_ms(50);
}
}
else
{
k++;s_p[0]=0;
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg4[i];i++)
{
data_4b(msg4[i]);
_delay_ms(50);
}
}
}
}

int main(void)
{
DDRA=0xFF;
DDRB=0X01;
BitSet(PORTB,Bit(2));
BitClear(DDRD,Bit(2));
BitClear(DDRD,Bit(3));
BitSet(PORTD,Bit(2));
BitSet(PORTD,Bit(3));

TCNT0=0;
TCCR0=0x05;
sei();


UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
//UCSRB=1<<TXEN;
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
UCSRA=1<<U2X;
UBRRL =12;

GICR=(1<<INT0) | (1<<INT1) | (1<<INT2);
MCUCR=(1<<ISC01) | (1<<ISC11);


LCD_init();
display_msg1();
// while(EECR &(1<<EEWE));
// EEAR=0X5F;
// EEDR=1;
// EECR |=(1<<EEMWE);
// EECR |=(1<<EEWE);
//
// while(EECR &(1<<EEWE));
// EEAR=0X6F;
// EEDR=5;
// EECR |=(1<<EEMWE);
// EECR |=(1<<EEWE);
//
// while(EECR &(1<<EEWE));
// EEAR=0X7F;
// EEDR=9;
// EECR |=(1<<EEMWE);
// EECR |=(1<<EEWE);
//
// while(EECR &(1<<EEWE));
// EEAR=0X8F;
// EEDR=5;
// EECR |=(1<<EEMWE);
// EECR |=(1<<EEWE);

while(1)
{
c_p();
}
}

ISR(INT0_vect)
{
z=TCNT0;
s_p[0]=z/100;
x=z%100;
s_p[1]=x/10;
s_p[2]=x%10;
s_p[3]=s_p[2]+6;

//serial_tranmition();
gsm_cmnd();

cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg2[i];i++)
{
data_4b(msg2[i]);
_delay_ms(50);
}

cmd_4b(0xC0);
m=0;total=0;
keypad();
}
////////////////////////////////////////////////////////////
ISR(INT1_vect)
{
while(EECR &(1<<EEWE));
EEAR=0X5F;
EECR |=(1<<EERE);
s_p[0]=EEDR;


while(EECR &(1<<EEWE));
EEAR=0X6F;
EECR |=(1<<EERE);
s_p[1]=EEDR;


while(EECR &(1<<EEWE));
EEAR=0X7F;
EECR |=(1<<EERE);
s_p[2]=EEDR;


while(EECR &(1<<EEWE));
EEAR=0X8F;
EECR |=(1<<EERE);
s_p[3]=EEDR;

cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg2[i];i++)
{
data_4b(msg2[i]);
_delay_ms(50);
}
cmd_4b(0xC0);
m=0;total=0;
keypad();
}

ISR(INT2_vect)
{
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg5[i];i++)
{
data_4b(msg5[i]);
_delay_ms(50);
}

cmd_4b(0xC0);
m=0;total=0;
keypad();

while(EECR &(1<<EEWE));
EEAR=0X5F;
EECR |=(1<<EERE);
s_p[0]=EEDR;


while(EECR &(1<<EEWE));
EEAR=0X6F;
EECR |=(1<<EERE);
s_p[1]=EEDR;

while(EECR &(1<<EEWE));
EEAR=0X7F;
EECR |=(1<<EERE);
s_p[2]=EEDR;

while(EECR &(1<<EEWE));
EEAR=0X8F;
EECR |=(1<<EERE);
s_p[3]=EEDR;


for (i=0;i<4;i++)
{

if (r_p[i]==s_p[i])
{
total++;
}
}
if (total==4)
{
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg6[i];i++)
{
data_4b(msg6[i]);
_delay_ms(50);
}

cmd_4b(0xC0);
m=0;
keypad();

while(EECR &(1<<EEWE));
EEAR=0X5F;
EEDR=r_p[0];
EECR |=(1<<EEMWE);
EECR |=(1<<EEWE);

while(EECR &(1<<EEWE));
EEAR=0X6F;
EEDR=r_p[1];
EECR |=(1<<EEMWE);
EECR |=(1<<EEWE);

while(EECR &(1<<EEWE));
EEAR=0X7F;
EEDR=r_p[2];
EECR |=(1<<EEMWE);
EECR |=(1<<EEWE);

while(EECR &(1<<EEWE));
EEAR=0X8F;
EEDR=r_p[3];
EECR |=(1<<EEMWE);
EECR |=(1<<EEWE);

}


if ((m>3) && (total==4))
{
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg7[i];i++)
{
data_4b(msg7[i]);
_delay_ms(50);
}
}

else
{
cmd_4b(0x01);
cmd_4b(0x80);
_delay_ms(50);
for(i=0;msg8[i];i++)
{
data_4b(msg8[i]);
_delay_ms(50);
}
}
m=0;total=0;
TCNT1=0xD9DA;
TCCR1A=0;
TCCR1B=0x05;
}
ISR(USART_RXC_vect)
{
x=UDR;
y++;
//PORTC++;

if (y>12)
{
if ((j==0) || (j==1))
{
arr[j]=x;
j++;
if (j>1)
{
for (j=0;cmnd_rec[j];j++)
{
UDR=cmnd_rec[j];
while(!BitGet(UCSRA,Bit(5)));

}
UDR=arr[0];
while(!BitGet(UCSRA,Bit(5)));
UDR=arr[1];
while(!BitGet(UCSRA,Bit(5)));
UDR=13;
}
}

}
if (y>20)
{
if ((m==0) || (m==1) || (m==2) || (m==3))
{
r_p[m]=x;
m++;
if (m>3)
{
s_p[0]=0x31;
s_p[1]=0x35;
s_p[2]=0x39;
s_p[3]=0x35;
//PORTC=r_p[3];
total=0;
}
}
}

}



About the author

Ihtasham-Zahid

I am student of Electronics Engineering in International Islamic University Islamabad.

Subscribe 0
160