An SMS based GPS tracker using the Adafruit Ultimate GPS module http://www.adafruit.com/products/746 and the Seeed Studio GPRS shield (Sim900 chipset) http://www.seeedstudio.com/depot/gprs-shield-p-779.html?cPath=132_134

Dependencies:   GPS MODSERIAL mbed-rtos mbed

SMS based GPS tracker

This is an SMS based GPS tracker using using the Adafruit Ultimate GPS module and the Seeed studio GPRS arduino shield [SIM900 chipset]. The idea of this project is to leverage the free SMS between Virgin mobile prepaid SIM cards in Australia for long distance communication.

Currently the GPRS shield is switched on manually, and should be done about 10 seconds before powering the mbed/GPS in order to disable echo properly during start up. This could easily be fixed by switching the GPRS shield with the mbed at start up, waiting the 10 seconds, set the echo then flush the buffer.

The mbed can read all 160 ASCII chars sent in a message for control.

http://i47.tinypic.com/35alpue.png http://i48.tinypic.com/2mi3y0z.png http://i45.tinypic.com/jkeyvt.png

The Seeed GPRS shield http://www.seeedstudio.com/depot/gprs-shield-p-779.html?cPath=132_134

The Adafruit Ultimate GPS http://www.adafruit.com/products/746

main.cpp

Committer:
SamClarke
Date:
2012-10-21
Revision:
14:5a33972c09e0
Parent:
13:d8ee6b45bd0a

File content as of revision 14:5a33972c09e0:

/******************************************************
* This is an SMS based GPS tracker using using the    *
* Adafruit Ultimate GPS module and the Seeed studio   *
* GPRS arduino shield [SIM900 chipset].               *
*                                                     *
* The idea of this project is to leverage the free    *
* SMS between Virgin mobile prepaid SIM cards in      *
* Australia for long distance communication.          *
*                                                     *
* NOTE: sendSms() sends the GPS 'extra' data          *
* (eg. speed, heading etc) in one message, and your   *
* location as a google maps URL in another message.   *
* If you are an Iphone user, you can replace the      *
* "https" in the URL with "http" and remove the ".au" *
* to get the URL to open in the native maps app.      *
* however I have found this to be less accurate by    *
* the fact that it doesn't drop a marker on the       *
* real locataion, but looks for the nearest address.  *
* it seems that the internal GPS is the only object   *
* able to be placed unrestricted on the map.          *
*                                                     *  
* October 2012                 Written by: Sam Clarke *
******************************************************/
/******************************************************
* HelloCylon                                          *
* By Leo Febey Oct 18 2011                            *
* Displays a looping pattern on the built in LEDs on  *
* the mbed which looks somewhat like a Cylon eye      *
* scanner pattern.                                    *
******************************************************/

#include "mbed.h"
#include "GPS.h"
#include "rtos.h"
#include "MODSERIAL.h"
#include <string>

using namespace std;

const string GoogleChunk = "https://maps.google.com.au/maps?q=";   // URL constant
const string GoogleExtras = "&z=20";                               // Zoom Level (0-20)
char GPRSbuffer[512];
char NUMBER[13];    
string MESSAGE;
int index;
int i = 0;

GPS gps(p9,p10);
MODSERIAL GPRS(p13,p14);
MODSERIAL pc(USBTX,USBRX);

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);

void led_thread(void const *argument)                                  
{
    while (true) {
        DigitalOut leds[4] = {myled1, myled2, myled3, myled4};
        int pattern[6][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},{0,0,1,0},{0,1,0,0}};
        int i=0;
        while(1) {
            leds[0] = pattern[i][0];
            leds[1] = pattern[i][1];
            leds[2] = pattern[i][2];
            leds[3] = pattern[i][3];
            wait(0.2);
            i++;
            i == 6? i=0:0;
        }
    }
}

void sendSms()
{
    // Set message mode to ASCII
    GPRS.printf("AT+CMGF=1\r\n");
    wait(1);
    // Set the phone number
    GPRS.printf("AT+CMGS=\"%s\"\r\n", NUMBER);
    wait(1);
    // Write out the GPS data in a message
    GPRS.printf("UTC Time: %d:%d:%2.2f \nAltitude: %3.2fm\nSpeed: %3.2f Kph\nHeading: %3.2f %s\nGPS data grade: [%c]\nSatellites in use: [%d]\nSatFix code: [%d] \nFix type: %s",
                gps.hours, gps.minutes, gps.seconds, gps.altitude, gps.kph, gps.heading, gps.cardinal, gps.validity, gps.satellites, gps.fixtype, gps.fix);
    wait(1);
    // Send it...
    GPRS.putc(0x1A);
    wait(4);
    GPRS.printf("AT+CMGF=1\r\n");
    wait(1);
    GPRS.printf("AT+CMGS=\"%s\"\r\n", NUMBER);
    wait(1);
    GPRS.printf("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras);
    wait(1);
    GPRS.putc(0x1A);
    wait(1);
}

void parseSms()
{
    // If theres a char waiting in the MODSERIAL buffer
    while (GPRS.readable()) {
        // Assign it to 'c'
        char c = GPRS.getc();
        // Replace all returns and or line endings with money!
        if (c == '\r' || c == '\n') c = '$';
        // Put it in the array
        GPRSbuffer[i] = c;
        // Repeat if possible
        i++;
    }
    // Uncomment the following to debug
    // pc.printf("\nbuffer = %s", GPRSbuffer);

    // If we get an SMS notification....
    if (sscanf(GPRSbuffer, "$$+CMTI: \"SM\",%d", &index)>0) {
        pc.printf("\nSMS recieved @ index [%d]", index);
        memset(GPRSbuffer, '0', 511);
        i = 0;
        pc.printf("\nOpening message...");
        // ask GPRS to read the message
        GPRS.printf("AT+CMGR=%d\r\n", index);
    }

    if (strncmp(GPRSbuffer, "$$+CMGR",7) == 0 ) {
        // Get the number out
        char *n = strstr(GPRSbuffer,"+6");
        strncpy(NUMBER, n, 12);
        // Get the message out
        char * pch;
        pch = strtok (GPRSbuffer, "$$");
        pch = strtok (NULL, "$$");
        MESSAGE = pch;
        pc.printf("\nDone! ");
        // Send the location
        sendSms();
        // Reset the GPRS buffer
        memset(GPRSbuffer, '0', 511);
        // Reset the char counter
        i = 0;
    }

    if (strncmp(GPRSbuffer, "$$+CMGS",7) == 0) {
        // Reset the GPRS buffer
        memset(GPRSbuffer, '0', 511);
        // Reset the char counter
        i = 0;
    }

    if (strncmp(GPRSbuffer, "$$RING",6) == 0) {
        GPRS.printf("ATH0\r\n");
        pc.printf("\nCall bounced!...");
        // Do the send SMS routine...
        sendSms();
        // Reset the GPRS buffer
        memset(GPRSbuffer, '0', 511);
        // Reset the char counter
        i = 0;
    }
    pc.printf("\n\n\nWaiting for SMS or call...\n");
    pc.printf("\nThe last number was : %s", NUMBER);
    pc.printf("\nThe last message was : %s", MESSAGE);
    // Reset the GPRS buffer
    memset(GPRSbuffer, '0', 511);
    // Reset the char counter
    i = 0;
}

int main()
{
    pc.baud(115200);
    GPRS.baud(19200);
    Thread thread(led_thread);
    memset(GPRSbuffer, '0', 511);
    pc.printf("\nI'm Alive...\n");
    // Setup the GPS
    gps.Init();
    // Set the GPRS AT echo off
    GPRS.printf("ATE0\r\n");
    pc.printf("\nGPRS echo [OFF]\n");
    wait(1);
    // Delete all messages on the sim card
    GPRS.printf("AT+CMGDA=\"DEL ALL\"\r\n");
    wait(1);
    pc.printf("\nMessages Cleared...\n");
    wait(1);
    // Get in a while loop
    while (1) {
        // Process any recieved GPRS data
        parseSms();
        // Process / check GPS data
        gps.parseData();
    }
}