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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************
00002 * This is an SMS based GPS tracker using using the    *
00003 * Adafruit Ultimate GPS module and the Seeed studio   *
00004 * GPRS arduino shield [SIM900 chipset].               *
00005 *                                                     *
00006 * The idea of this project is to leverage the free    *
00007 * SMS between Virgin mobile prepaid SIM cards in      *
00008 * Australia for long distance communication.          *
00009 *                                                     *
00010 * NOTE: sendSms() sends the GPS 'extra' data          *
00011 * (eg. speed, heading etc) in one message, and your   *
00012 * location as a google maps URL in another message.   *
00013 * If you are an Iphone user, you can replace the      *
00014 * "https" in the URL with "http" and remove the ".au" *
00015 * to get the URL to open in the native maps app.      *
00016 * however I have found this to be less accurate by    *
00017 * the fact that it doesn't drop a marker on the       *
00018 * real locataion, but looks for the nearest address.  *
00019 * it seems that the internal GPS is the only object   *
00020 * able to be placed unrestricted on the map.          *
00021 *                                                     *  
00022 * October 2012                 Written by: Sam Clarke *
00023 ******************************************************/
00024 /******************************************************
00025 * HelloCylon                                          *
00026 * By Leo Febey Oct 18 2011                            *
00027 * Displays a looping pattern on the built in LEDs on  *
00028 * the mbed which looks somewhat like a Cylon eye      *
00029 * scanner pattern.                                    *
00030 ******************************************************/
00031 
00032 #include "mbed.h"
00033 #include "GPS.h"
00034 #include "rtos.h"
00035 #include "MODSERIAL.h"
00036 #include <string>
00037 
00038 using namespace std;
00039 
00040 const string GoogleChunk = "https://maps.google.com.au/maps?q=";   // URL constant
00041 const string GoogleExtras = "&z=20";                               // Zoom Level (0-20)
00042 char GPRSbuffer[512];
00043 char NUMBER[13];    
00044 string MESSAGE;
00045 int index;
00046 int i = 0;
00047 
00048 GPS gps(p9,p10);
00049 MODSERIAL GPRS(p13,p14);
00050 MODSERIAL pc(USBTX,USBRX);
00051 
00052 DigitalOut myled1(LED1);
00053 DigitalOut myled2(LED2);
00054 DigitalOut myled3(LED3);
00055 DigitalOut myled4(LED4);
00056 
00057 void led_thread(void const *argument)                                  
00058 {
00059     while (true) {
00060         DigitalOut leds[4] = {myled1, myled2, myled3, myled4};
00061         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}};
00062         int i=0;
00063         while(1) {
00064             leds[0] = pattern[i][0];
00065             leds[1] = pattern[i][1];
00066             leds[2] = pattern[i][2];
00067             leds[3] = pattern[i][3];
00068             wait(0.2);
00069             i++;
00070             i == 6? i=0:0;
00071         }
00072     }
00073 }
00074 
00075 void sendSms()
00076 {
00077     // Set message mode to ASCII
00078     GPRS.printf("AT+CMGF=1\r\n");
00079     wait(1);
00080     // Set the phone number
00081     GPRS.printf("AT+CMGS=\"%s\"\r\n", NUMBER);
00082     wait(1);
00083     // Write out the GPS data in a message
00084     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",
00085                 gps.hours, gps.minutes, gps.seconds, gps.altitude, gps.kph, gps.heading, gps.cardinal, gps.validity, gps.satellites, gps.fixtype, gps.fix);
00086     wait(1);
00087     // Send it...
00088     GPRS.putc(0x1A);
00089     wait(4);
00090     GPRS.printf("AT+CMGF=1\r\n");
00091     wait(1);
00092     GPRS.printf("AT+CMGS=\"%s\"\r\n", NUMBER);
00093     wait(1);
00094     GPRS.printf("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras);
00095     wait(1);
00096     GPRS.putc(0x1A);
00097     wait(1);
00098 }
00099 
00100 void parseSms()
00101 {
00102     // If theres a char waiting in the MODSERIAL buffer
00103     while (GPRS.readable()) {
00104         // Assign it to 'c'
00105         char c = GPRS.getc();
00106         // Replace all returns and or line endings with money!
00107         if (c == '\r' || c == '\n') c = '$';
00108         // Put it in the array
00109         GPRSbuffer[i] = c;
00110         // Repeat if possible
00111         i++;
00112     }
00113     // Uncomment the following to debug
00114     // pc.printf("\nbuffer = %s", GPRSbuffer);
00115 
00116     // If we get an SMS notification....
00117     if (sscanf(GPRSbuffer, "$$+CMTI: \"SM\",%d", &index)>0) {
00118         pc.printf("\nSMS recieved @ index [%d]", index);
00119         memset(GPRSbuffer, '0', 511);
00120         i = 0;
00121         pc.printf("\nOpening message...");
00122         // ask GPRS to read the message
00123         GPRS.printf("AT+CMGR=%d\r\n", index);
00124     }
00125 
00126     if (strncmp(GPRSbuffer, "$$+CMGR",7) == 0 ) {
00127         // Get the number out
00128         char *n = strstr(GPRSbuffer,"+6");
00129         strncpy(NUMBER, n, 12);
00130         // Get the message out
00131         char * pch;
00132         pch = strtok (GPRSbuffer, "$$");
00133         pch = strtok (NULL, "$$");
00134         MESSAGE = pch;
00135         pc.printf("\nDone! ");
00136         // Send the location
00137         sendSms();
00138         // Reset the GPRS buffer
00139         memset(GPRSbuffer, '0', 511);
00140         // Reset the char counter
00141         i = 0;
00142     }
00143 
00144     if (strncmp(GPRSbuffer, "$$+CMGS",7) == 0) {
00145         // Reset the GPRS buffer
00146         memset(GPRSbuffer, '0', 511);
00147         // Reset the char counter
00148         i = 0;
00149     }
00150 
00151     if (strncmp(GPRSbuffer, "$$RING",6) == 0) {
00152         GPRS.printf("ATH0\r\n");
00153         pc.printf("\nCall bounced!...");
00154         // Do the send SMS routine...
00155         sendSms();
00156         // Reset the GPRS buffer
00157         memset(GPRSbuffer, '0', 511);
00158         // Reset the char counter
00159         i = 0;
00160     }
00161     pc.printf("\n\n\nWaiting for SMS or call...\n");
00162     pc.printf("\nThe last number was : %s", NUMBER);
00163     pc.printf("\nThe last message was : %s", MESSAGE);
00164     // Reset the GPRS buffer
00165     memset(GPRSbuffer, '0', 511);
00166     // Reset the char counter
00167     i = 0;
00168 }
00169 
00170 int main()
00171 {
00172     pc.baud(115200);
00173     GPRS.baud(19200);
00174     Thread thread(led_thread);
00175     memset(GPRSbuffer, '0', 511);
00176     pc.printf("\nI'm Alive...\n");
00177     // Setup the GPS
00178     gps.Init();
00179     // Set the GPRS AT echo off
00180     GPRS.printf("ATE0\r\n");
00181     pc.printf("\nGPRS echo [OFF]\n");
00182     wait(1);
00183     // Delete all messages on the sim card
00184     GPRS.printf("AT+CMGDA=\"DEL ALL\"\r\n");
00185     wait(1);
00186     pc.printf("\nMessages Cleared...\n");
00187     wait(1);
00188     // Get in a while loop
00189     while (1) {
00190         // Process any recieved GPRS data
00191         parseSms();
00192         // Process / check GPS data
00193         gps.parseData();
00194     }
00195 }