Saturday, August 19, 2017

Accident Alerting System Using SIM 808 shield

Accident Alerting System Using GSM shield


This was a kind of basic project I have involved with one of my friend who was studying at a engineering education Institute. The target was an accident alerting system for a vehicle. 

When the car getting closer to a vehicle alerting system gives a beep pulse sound to alert the driver. The driver can manually silent the alarm by separated push button switch. If the vehicle gets very closer (7cm as an example) system gives an emergency alarm to indicate an accident. The driver still can silence the alarm and ignore the incidence as a mistake by pressing the same button mentioned above within a minute. 

Unless the driver presses the button within a minute system to consider this as an accident and sends SMS to the family and police with the GPS coordinates of the vehicle, time and information of the accident. Then it automatically makes a missed call to the family as the SMS do not give an urgent alert. however as my thoughts this method has a tedious part with sing an ultra sonic sensor. 


Using SIM 808 GPRS, GSM, GPS  Arduino shield

SIM 808 module
SIM808 module is a GSM/GPS/BT three-in-one function module. It is based on the latest GSM/GPS/BT module SIM808 from 'SIMCOM', supports GSM/GPRS Quad-Band network and combines GPS technology for satellite navigation.It has high GPS receiver sensitivity with 22 tracking and 66 acquisition receiver channels. Besides, it supports A-GPS that available for indoor localization, and it also supports for Bluetooth 4.0.
The module is controlled by AT command via UART and supports 3.3V and 5V logical level.

As the above picture, there are two separated antennas for GSM and GPS. The bigger one is for the GPS and the other one is a GSM antenna. 
We must plug both of them as our application to the dedicated slot labeled on the shield. Some of the major parts of the shield can be understood using the image shown below. But it is another model of shield made by using the same SIM808 module.


Turn on the SIM808  shield

There are two ways to turn on the SIM808 GPRS/GSM+GPS Shield.

1. Turn on through the power button . Press the the 'POWERKEY' for few seconds until Power-on indicator(Green/red) is on.
2. Turn on through the software.To turn on through software run the following code, the SIM808 will POWER on or POWER off after code run.
Here is the code for turn on the device,
int Powerkey = 9;
void setup() {                
  pinMode(Powerkey, OUTPUT);   // initialize the digital pin as an output.  
  power();                     //power on the sim808 or power down the sim808
}
void loop() 
{

}

void power(void)
{
  digitalWrite(Powerkey, LOW); 
  delay(1000);               // wait for 1 second
  digitalWrite(Powerkey, HIGH);
}

Communication with the microcontroller

The SIM808 GPRS/GSM+GPS Shield is used UART (serial)protocol to communicate with an Arduino. Users can use jumpers to connect (RX,TX) of the shield to either Software Serial(D8,D7) or Hardware Serial(D1,D0) of the Arduino

Automatic Power down methods of SIM 808


Apart from normal manual power down through hardware or software, there are some automated procedures for safety and other facts involved the device. They are as follows,

1.  Over-voltage or Under-voltage Power Down: 

The module software monitors VBAT voltage constantly.
           1.If the voltage ≤ 3.5V, the following URC will be reported:
UNDER-VOLTAGE WARNNING
           2.If the voltage ≥ 4.3V, the following URC will be reported:
OVER-VOLTAGE WARNNING
          3.If the voltage < 3.4V, the following URC will be reported,and the module will be automativally powered down.
UNDER-VOLTAGE POWER DOWN
          4.If the voltage > 4.4V, the following URC will be reported,and the module will be automativally powered down.
OVER-VOLTAGE POWER DOWN

2. Over-temperature or Under-temperature Power Down
This will constantly monitor the temperature of the module.
①If the temperature > +80℃, the following URC will be reported:
+CMTE:1
②If the temperature < -30℃, the following URC will be reported:
+CMTE:-1
③If the temperature > +85℃, the following URC will be reported,and the module will be automativally powered down.
+CMTE:2
④If the temperature < -40℃, the following URC will be reported,and the module will be automativally powered down.
+CMTE:-2

Lets' Communicate with The device

Sim 808 is supported with AT commands for communication process with the user. We should send At commands as our purpose to execute it. Here are some important AT commands I found from Elecrow.com.

Back to the project

Here I used a ultrasonic range sensor for detect the distance to the closest vehicle t our vehicle from front side. When the distance is getting lower than predefined alerting limit there is a buzzer for give an alarm. Unless driver bush a separated button for this on the dashboard , alarm sounds as long as distance is low. If the distance reduce to lower than 10cm for two minutes without a button press during this 2 minutes system recognize this as an accident and send a SMS to the predefined phone number .

Here is the Arduino code for send the sms

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 8);

void setup()
{
mySerial.begin(9600); 
mySerial.print("\r");
delay(1000);                    //Wait for a second while the modem sends an "OK"
mySerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
delay(1000);


mySerial.print("AT+CMGS=\"+94123456789\"\r");    //Start accepting the text for the message
//to be sent to the number specified.
//Replace this number with the target mobile number.
delay(1000);
mySerial.print("Hello,World!\r");   //The text for the message
delay(1000);
mySerial.write(0x1A);  //Equivalent to sending Ctrl+Z 
}

void loop()
{
//We just want to send the SMS only once, so there is nothing in this loop.
I used internal GPS feature to get GPS coordinates of the vehicle.

Here is the Arduino code for using GPS

This code for Get the string which is sent from sim808 and decode it to parts as we can separate Latitude, Longitude,Altitude, date, time ,no. of satellites and so on. 
/*
Edited by Chandula N.
   st1,st2,date&time,longitude,latitude,altitude,

*/
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial mySerial(7, 8);
int Powerkey = 9;
void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(Powerkey, OUTPUT);   // initialize the digital pin as an output.
  power();
  getgps();
}

void loop() {
  sendData( "AT+CGNSINF", 1000, DEBUG);
}

void getgps(void) {
  sendData( "AT+CGNSPWR=1", 1000, DEBUG);
  sendData( "AT+CGPSINF=0", 1000, DEBUG);
}

void sendData(String command, const int timeout, boolean debug) {
  String response = "";
  mySerial.println(command);
  delay(5);
  if (debug) {
    long int time = millis();
    while ( (time + timeout) > millis()) {
      while (mySerial.available()) {
        response += char(mySerial.read());
      }
    }

    // Serial.print(response);
    int _1commaIndex = response.indexOf(',');
    int _2commaIndex = response.indexOf(',', _1commaIndex + 1);
    int _3commaIndex = response.indexOf(',', _2commaIndex + 1);
    int _4commaIndex = response.indexOf(',', _3commaIndex + 1);
    int _5commaIndex = response.indexOf(',', _4commaIndex + 1);
    String Lat = response.substring(_3commaIndex + 1, _4commaIndex);
    String Long = response.substring(_4commaIndex + 1, _5commaIndex);
    String Date = response.substring(_2commaIndex + 1, _2commaIndex + 9);
    String Time = response.substring(_2commaIndex + 9 , _3commaIndex );
    Date = Date.substring(0, 4) + "/" + Date.substring(4, 6) + "/" + Date.substring(6, 8);
    Time = (Time.substring(0, 2)) + "." + Time.substring(2, 4) + "." + Time.substring(4, 6);

    Serial.print("Lat:");
    Serial.println(Lat);
    Serial.print("Long:");
    Serial.println(Long);
    Serial.print("Date:");
    Serial.println(Date);
    Serial.print("Time:");
    Serial.println(Time);
    Serial.println();
  }
}
void power(void)            //Turn on thr sim808 
{
  digitalWrite(Powerkey, HIGH); 
  delay(1000);               // wait for 1 second
  digitalWrite(Powerkey, LOW);
}

After separation of GPS date I used sms sending code and wrote a combination for send the GPS data to a specified phone number.
You can download the combined code for sending GPS data to a specified number, which I used for this project. 


After the sms is sent , for the further alerting sysm makes a call to predefined number for a given time. This may be work as a missed call for the receiving party. 

Here is the code for making a call with sim 808

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
void setup()
{
mySerial.begin(19200);               // the GPRS baud rate   
Serial.begin(19200);               // the GPRS baud rate   
delay(2000);
mySerial.println("ATD+94xxxxxxxxx;"); // xxxxxxxxx is the number you want to dial.  

if(mySerial.available())

Serial.print((unsigned char)mySerial.read());


delay(10000); 
delay(10000); 

mySerial.println("ATH"); //End the call.
if(mySerial.available())

Serial.print((unsigned char)mySerial.read());
}


void loop()
{
//Do nothing
}

Here is my final code which I used for this project with all the functions




references-https://www.elecrow.com/wiki/index.php?title=SIM808_GPRS/GSM%2BGPS_Shield_v1.1

The Featured Post

A Deep Dive into Deep Packet Inspection

This blog post aims to unfold the details of DPI, exploring its definition, methods, applications, modern technologies, and the challenges i...