Friday, 16 September 2016

Accident detect and messaging system


 Accident detect and messaging system:



Code:


#include <avr/io.h>
#include <avr/interrupt.h>
#include<string.h>

#include"lcd_header.h"
#include"serial_header.h"
//function decalaration
void recgps(void);
void delay(int);
void cmden();
void lcdcmd(char);
void lcdinit(void);
void daten(void);
void lcddata(char);
void txdata(char);
void sendsms();
unsigned char rxu0(void);
void serial_init();

static unsigned char lat[10];
static unsigned char lon[11];

void txdata(char ch)
{
UDR=ch;
while(!(UCSRA & (1<<UDRE)));
_delay_ms(10);
}

void sendsms()
{
//txstring("AT\r");
//delay(5000);
//txstring("ATE0\r");
//_delay_ms(100);
//txstring("AT&W\r");
//_delay_ms(100);
txstring("AT+CMGF=1\r");
_delay_ms(500);
txstring("AT+CMGS=");
txdata(0x22);
txstring("+919582848279");
txdata(0x22);
txdata('\r');
_delay_ms(500);
txstring("Accident happen at place....\r\n");
_delay_ms(500);
txstring("Latitude Position Is:\r\n");
_delay_ms(500);
txstring(lat);
txstring("\r");
_delay_ms(500);
txstring("Longitude Position Is:\r\n");
_delay_ms(500);
txstring(lon);
txstring("\r\n");
_delay_ms(500);

_delay_ms(500);
txstring("Please Go For Help ....\r\n");
_delay_ms(500);
txdata(26);
lcdcmd(0x01);
lcdstring("Message Has been");
lcdcmd(0xc0);
lcdstring("Sent To Parents");
_delay_ms(3000);
lcdcmd(0x01);
lcdstring("System Ready... ");
_delay_ms(1000);
return;
}

void recgps()
{
unsigned char p;
int gps=0;
int i;

while(gps==0)
{
p=uart_read();
while(p!='$')
{
p=uart_read();
}
p=uart_read();
if(p=='G')
{
}
else
continue;
p=uart_read();
if(p=='P')
{
}
else
continue;

p=uart_read();
if(p=='G')
{
}
else
continue;

p=uart_read();
if(p=='G')
{
}
else
continue;

p=uart_read();
if(p=='A')
{
while(p!=',')
{
p=uart_read();
}
p=uart_read();
while(p!=',')
{
p=uart_read();
}
for(i=0;i<9;i++)
{
lat[i]=uart_read();
}
p=uart_read();
while(p!=',')
{
p=uart_read();
}
p=uart_read();
while(p!=',')
{
p=uart_read();
}
for(i=0;i<10;i++)
{
lon[i]=uart_read();
}
lat[9]='\0';
lon[10]='\0';
gps=1;
}
else
continue;
}
}

int main()
{
int i;
DDRB=0XFF;
DDRD=0X00;
lcdinit();
serial_init();

lcdcmd(0x80);
lcdstring("Vehicle Accident ");
lcdcmd(0xc0);
lcdstring("Detection system ");

  txstring("AT+CMGF=1\r");
  _delay_ms(500);
  txstring("AT+CMGS=");
  txdata(0x22);
  txstring("+918888888888");
  txdata(0x22);
  txdata('\r');
  _delay_ms(500);
  txstring("System Ready....\r\n");
  _delay_ms(500);
  txdata(26);
  lcdcmd(0x01);
  lcdstring("System Ready... ");
  PORTD|=1<<PB7;
while(1)
{
if((PIND & (1<<PA7)))
{  
lcdcmd(1);
recgps();
lcdstring("Lat:");

lcdstring(lat);
lcdcmd(0xc0);
lcdstring("Lon:");

lcdstring(lon);
_delay_ms(3000);
lcdcmd(0x01);
lcdstring("Sending MSG...");
sendsms();
  }  
 }
}



lcd_header.h




#define F_CPU 8000000UL
#include <util/delay.h>
#define rs 0
#define rw 1
#define en 2
void cmnd()
{
PORTB&=(~(1<<rs));
PORTB&=(~(1<<rw));
PORTB|=(1<<en);
_delay_ms(10);
PORTB&=(~(1<<en));
}
void lcdcmd(char ch)
{
PORTB=ch & 0xf0;
cmnd();
PORTB=(ch<<4) & 0xf0;
cmnd();
}
void lcdinit()
{
lcdcmd(0x02);
lcdcmd(0x28);
lcdcmd(0x01);
lcdcmd(0x0e);
}
void data()
{
PORTB|=(1<<rs);
PORTB&=(~(1<<rw));
PORTB|=(1<<en);
_delay_ms(10);
PORTB&=(~(1<<en));
}
void lcddata(char ch)
{
PORTB=ch & 0xf0;
data();
PORTB=(ch<<4) & 0xf0;
data();
}
void lcdstring(char *str)
{
while(*str)
{
lcddata(*str);
str++;
}
}



serial_header.h



void serial_init()
{
UCSRB=0x18;
UCSRC=0x86;
UBRRL=0X33;
}
char uart_read()
{
while(!(UCSRA & (1<<RXC)));
return UDR;
}

void txstring(char *str)
{
while(*str)
{
txdata(*str);
str++;
_delay_ms(20);
}
}
/*void send_msg(char *msg)
{
_delay_ms(1000);
lcdcmd(0x01);
lcdstring("Sending Message.");
txstring("\rAT\r");
_delay_ms(1000);
txstring("\rAT+CMGF=1\r");
_delay_ms(1000);
txstring("\rAT+CMGS=");
txdata(0x22);
txstring("+918447601173");
txdata(0x22);
txdata(0x0D);
_delay_ms(1000);
txstring(msg);
txdata(0x1A);
_delay_ms(1000);
}*/






0 comments:

Post a Comment

if you have any doubt please let me know