Tracks GPS coordinates of an asset. Uses: GPS GSM Accelerometer mbed

Dependencies:   mbed MODSERIAL ADXL345 MODGPS

Files at this revision

API Documentation at this revision

Comitter:
gsulc
Date:
Fri May 04 14:11:45 2012 +0000
Commit message:
v0.5

Changed in this revision

ADXL345.lib Show annotated file Show diff for this revision Revisions of this file
MODGPS.lib Show annotated file Show diff for this revision Revisions of this file
MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
SecurityMechanism.cpp Show annotated file Show diff for this revision Revisions of this file
SecurityMechanism.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
sms.cpp Show annotated file Show diff for this revision Revisions of this file
sms.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4415987ca08f ADXL345.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL345.lib	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Owen/code/ADXL345/#745335502422
diff -r 000000000000 -r 4415987ca08f MODGPS.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODGPS.lib	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODGPS/#64771e31464e
diff -r 000000000000 -r 4415987ca08f MODSERIAL.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#c11ea36f17f9
diff -r 000000000000 -r 4415987ca08f SecurityMechanism.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SecurityMechanism.cpp	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,39 @@
+#include "SecurityMechanism.h"
+#include "GPS.h"
+#include "sms.h"
+#include <string>
+
+extern Serial pc;
+extern GPS gps;
+extern DigitalOut gps_on;
+extern DigitalOut gps_xstandby;
+
+extern bool command_sent;
+extern bool movement_detected;
+extern string phoneNumber;
+
+
+/*void process_command (string command) {
+}//*/
+
+/*void alert () {
+}//*/
+
+void send_location () {
+    //if gps off, turn it on
+    if(gps_on == 0) {
+        pc.printf("Turning on GPS\r\n");
+        gps_on = 1;
+        gps_xstandby = 1;
+        //wait to turn on
+        wait(45);
+    }
+    //send coordinates
+    char link[64];
+    gps.latitude();
+    gps.longitude();
+    sprintf(link, "%s", "http://maps.google.com/maps?daddr=");
+    sprintf(link, "%s%.4f,%.4f", link, gps.latitude(), gps.longitude());
+    //pc.printf("%s\r\n", link);
+    send_SMS(phoneNumber, link);
+}
\ No newline at end of file
diff -r 000000000000 -r 4415987ca08f SecurityMechanism.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SecurityMechanism.h	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,10 @@
+#ifndef _SECURITYMECHANISM_H_
+#define _SECURITYMECHANISM_H_
+
+#include <string>
+
+//void process_command (string command);
+//void alert ();
+void send_location ();
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 4415987ca08f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,101 @@
+#include "mbed.h"
+#include "stdio.h"
+#include "sms.h"                //custom sms library
+#include "SecurityMechanism.h"  //custom security mechanism library
+#include "MODSERIAL.h"          //buffer library for serial
+#include "GPS.h"                //GPS library
+#include "ADXL345.h"            //Sparkfun ADXL345 accelerometer library
+
+// System
+DigitalOut pulse(LED1); //heartbeat
+Serial pc(USBTX,USBRX); //for debugging
+
+// Peripherals
+MODSERIAL gsm(p28,p27);
+GPS gps(NC, p10);
+ADXL345 accelerometer(p5, p6, p7, p8);
+
+// Power Control
+DigitalOut gps_on(p22);
+DigitalOut gps_xstandby(p21);
+DigitalOut accelerometer_on(p20);
+//DigitalIn security_pwrd(p17);
+AnalogIn voltage_level(p19);
+
+// Global Vars
+int accel_reading[3] = {0, 0, 0};   //x, y, z coordinates of accelerometer
+int prev_accel_reading[3] = {0, 0, 0};   //x, y, z coordinates of accelerometer
+string phoneNumber = "4047849578";
+bool security_enabled = true;
+bool movement_detected = false;
+bool command_sent = false;
+#define ACCEL_THRESH 50
+
+int main() {
+    pc.printf("Initializing Program.\r\n");
+    //****init GSM    
+    GSM_init();
+    
+    //****init GPS
+    pc.printf("Initializing GPS.\r\n");
+    GPS_Time t;
+    gps.baud(4800);
+    
+    //****init accelerometer
+    accelerometer_on = 1;
+    pc.printf("Initializing Accelerometer.\r\n");
+    //Go into standby mode to configure the device.
+    accelerometer.setPowerControl(0x00);
+
+    //Full resolution, +/-16g, 4mg/LSB.
+    accelerometer.setDataFormatControl(0x0B);
+    
+    //3.2kHz data rate.
+    accelerometer.setDataRate(ADXL345_3200HZ);
+
+    //Measurement mode.
+    accelerometer.setPowerControl(0x08);
+    
+    pc.printf("Initializing Complete.\r\n");
+    
+    wait(0.4);
+
+    while(1) {
+        accelerometer.getOutput(accel_reading);
+        //check for movement
+        for (int i = 0; i < 3; i++) {
+            if (((int16_t)prev_accel_reading[i] != 0) && (int16_t)abs(accel_reading[i] - prev_accel_reading[i]) > ACCEL_THRESH) {
+                pc.printf("Movement detected\r\n");
+                movement_detected = true;
+            }
+            prev_accel_reading[i] = accel_reading[i];
+        }
+        
+        //pc.printf("Seurity system = %d\r\n", security_pwrd);
+        //security_enabled = security_pwrd;
+        
+        if (movement_detected) {
+            if (security_enabled) {
+                //turn off accelerometer
+                accelerometer_on = 0;
+                pc.printf("Alert\r\n");
+                send_location();
+                wait(30);
+            }
+        }
+        
+        if (command_sent) {
+            pc.printf("Ping.\r\n");
+            send_location();
+            command_sent = false;
+            gps_on = 0;
+            gps_xstandby = 0;
+        }
+        
+        //heartbeat
+        pulse = 1;
+        wait(0.2);
+        pulse = 0;
+        wait(0.2);
+    }
+}
diff -r 000000000000 -r 4415987ca08f mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
diff -r 000000000000 -r 4415987ca08f sms.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sms.cpp	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,122 @@
+#include "sms.h"
+#include "SecurityMechanism.h"
+
+#define MESSAGE_BUFFER_SIZE 2*1024
+
+// must define these in main.cpp
+extern MODSERIAL gsm;
+extern Serial pc;
+extern bool command_sent;
+char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
+//char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
+//bool messageReceived = false;
+
+void GSM_init() {
+    pc.printf("Setting up GSM Modem\r\n");
+    
+    gsm.baud(115200);                         //Set Baud Rate
+    
+    gsm.puts("AT\r\n");                       //Check Connection
+    messageProcess();                         //Process incoming message
+    
+    // Tune Down Baud
+    pc.printf("Slowing Baud Down....");
+    gsm.puts("AT+IPR=19200\r\n");            //Select Bearer Servr Type autobaud, No name, Non-transparent
+    messageProcess();                         //Process incoming message
+    
+    gsm.baud(19200);
+    gsm.format(8, Serial::None, 1);         //Default format
+    
+    gsm.attach(&messageReceive, MODSERIAL::RxAutoDetect);     //Attaches Interrupts
+    gsm.autoDetectChar('\n');                                 //Set Detection to Line Feed
+    
+    gsm.puts("AT\r\n");                       //Check Connection
+    messageProcess();                         //Process incoming message
+   
+    gsm.puts("AT+IFC=0,0");                   //Disable handshake lines
+    messageProcess();                         //Process incoming message
+    
+    gsm.puts("AT+CSMP=17,167,0,0\r\n");       //Set Text Parameters (default values)
+    messageProcess();                         //Process incoming message
+    
+    gsm.puts("AT+CSCA?\r\n");                 //Check Service Center
+    messageProcess();                         //Process incoming message
+
+    gsm.puts("AT+CMGF=1\r\n");                //Set format to Text Mode
+    
+    messageProcess();                         //Process incoming message
+    /*
+    gsm.printf("AT+CNMI=1,1,0,0,0\r\n");      //Set the new messages indicator
+    wait(0.5);
+    messageProcess();                         //Process incoming message
+
+    gsm.printf("AT+CSAS\r\n");                //Save the Current Setup, REGLED will light SOLID
+    wait(3.0);
+    messageProcess();                         //Process incoming message
+    //*/
+    pc.printf("GSM Setup Done\r\n");
+}
+
+void send_SMS(string phoneNumber, string text) {
+    pc.printf("Sending: %s\r\n", text);
+
+    gsm.printf("AT+CMGS=\"%s\"\r\n", phoneNumber);
+    wait(0.4);
+    gsm.printf("%s", text);
+    wait(0.4);
+    gsm.printf("%c", SUB);
+    wait(0.4);
+    gsm.printf("\r\n");
+    
+    if (messageProcess() == 1) {
+        pc.printf("SMS sent\r\n");
+    } 
+    else {
+        pc.printf("SMS send failed\r\n");
+    }
+}
+
+void check_SMS(){
+    //string text_id;
+    //pc.printf("Checking Messages\r\n");
+    while (!gsm.writeable()) {}
+    //gsm.puts("AT+CMGL=?\r\n");
+    //messageProcess();
+    //while (1) {
+    gsm.puts("AT+CMGL=\"ALL\"\r\n");          //Check ALL messages
+    wait(2);
+    messageProcess();
+    //gsm.puts("AT+CMGR=1\r\n");
+    //messageProcess();
+    //gsm.puts("AT+CMGD=1\r\n");
+    //messageProcess();
+    //}
+    //wait(0.4);
+    /*gsm.scanf("%s", text_id);
+    pc.printf("textID = %s\r\n", text_id);
+    text_id = strtok(text_id, ": ");
+    //messageProcess();                         //Process incoming message
+    gsm.puts("AT+CMGR=%s\r\n", text_id);
+    messageProcess();*/
+}
+
+void messageReceive(MODSERIAL_IRQ_INFO *q) {
+    MODSERIAL *sys = q->serial;
+    sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
+    if (!strncmp(messageBufferIncoming, "+CMTI", sizeof("+CMTI")-1))
+        command_sent = true;
+    //messageReceived = true;
+    return;
+}
+
+int messageProcess() {
+    int mpResult = 0;
+    wait(0.4);
+    if (!strncmp(messageBufferIncoming, "OK", sizeof("OK")-1)) mpResult = 1;
+    else if (!strncmp(messageBufferIncoming, "ERROR", sizeof("ERROR")-1)) mpResult = 2;
+    else mpResult = 1;
+    pc.printf("%s\r\n", messageBufferIncoming);
+    gsm.rxBufferFlush();                            //Flush the Buffer
+    //messageReceived = false;
+    return mpResult;
+}
diff -r 000000000000 -r 4415987ca08f sms.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sms.h	Fri May 04 14:11:45 2012 +0000
@@ -0,0 +1,18 @@
+// Library for sending GSM SMS Messages
+
+#ifndef _SMS_H_
+#define _SMS_H_
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+#include <string>
+
+#define SUB 0x1A    // substition char for Ctrl+Z
+
+void GSM_init();
+void send_SMS(string phoneNumber, string text);
+void check_SMS();
+int messageProcess();
+void messageReceive(MODSERIAL_IRQ_INFO *q);
+
+#endif
\ No newline at end of file