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.
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@2:2b2c785c6627, 2012-10-07 (annotated)
- Committer:
- SamClarke
- Date:
- Sun Oct 07 02:05:15 2012 +0000
- Revision:
- 2:2b2c785c6627
- Parent:
- 1:00c7dc0c1761
- Child:
- 3:a5df4bd224ad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SamClarke | 0:5b4bcbd69509 | 1 | #include "mbed.h" |
SamClarke | 0:5b4bcbd69509 | 2 | #include "GPS.h" |
SamClarke | 0:5b4bcbd69509 | 3 | #include "rtos.h" |
SamClarke | 0:5b4bcbd69509 | 4 | #include "MODSERIAL.h" |
SamClarke | 0:5b4bcbd69509 | 5 | #include <string> |
SamClarke | 0:5b4bcbd69509 | 6 | #include <iostream> |
SamClarke | 0:5b4bcbd69509 | 7 | |
SamClarke | 0:5b4bcbd69509 | 8 | using namespace std; |
SamClarke | 0:5b4bcbd69509 | 9 | |
SamClarke | 0:5b4bcbd69509 | 10 | const string GoogleChunk = "https://maps.google.com.au/maps?q="; |
SamClarke | 0:5b4bcbd69509 | 11 | const string GoogleExtras = "&z=20"; |
SamClarke | 0:5b4bcbd69509 | 12 | char GPRSbuffer[512]; |
SamClarke | 0:5b4bcbd69509 | 13 | int index; |
SamClarke | 0:5b4bcbd69509 | 14 | int i = 0; |
SamClarke | 0:5b4bcbd69509 | 15 | |
SamClarke | 0:5b4bcbd69509 | 16 | GPS gps(p9,p10); |
SamClarke | 0:5b4bcbd69509 | 17 | MODSERIAL GPRS(p13,p14); // MODSERIAL for rxBufferFlush() |
SamClarke | 0:5b4bcbd69509 | 18 | MODSERIAL pc(USBTX,USBRX); |
SamClarke | 0:5b4bcbd69509 | 19 | |
SamClarke | 0:5b4bcbd69509 | 20 | DigitalOut myled1(LED1); |
SamClarke | 0:5b4bcbd69509 | 21 | DigitalOut myled2(LED2); |
SamClarke | 0:5b4bcbd69509 | 22 | DigitalOut myled3(LED3); |
SamClarke | 0:5b4bcbd69509 | 23 | DigitalOut myled4(LED4); |
SamClarke | 0:5b4bcbd69509 | 24 | |
SamClarke | 0:5b4bcbd69509 | 25 | void led_thread(void const *argument) // Some light jazz in a thread |
SamClarke | 0:5b4bcbd69509 | 26 | { |
SamClarke | 0:5b4bcbd69509 | 27 | while (true) { |
SamClarke | 0:5b4bcbd69509 | 28 | DigitalOut leds[4] = {myled1, myled2, myled3, myled4}; |
SamClarke | 0:5b4bcbd69509 | 29 | 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}}; |
SamClarke | 0:5b4bcbd69509 | 30 | int i=0; |
SamClarke | 0:5b4bcbd69509 | 31 | while(1) { |
SamClarke | 0:5b4bcbd69509 | 32 | leds[0] = pattern[i][0]; |
SamClarke | 0:5b4bcbd69509 | 33 | leds[1] = pattern[i][1]; |
SamClarke | 0:5b4bcbd69509 | 34 | leds[2] = pattern[i][2]; |
SamClarke | 0:5b4bcbd69509 | 35 | leds[3] = pattern[i][3]; |
SamClarke | 0:5b4bcbd69509 | 36 | wait(0.2); |
SamClarke | 0:5b4bcbd69509 | 37 | i++; |
SamClarke | 0:5b4bcbd69509 | 38 | i == 6? i=0:0; |
SamClarke | 0:5b4bcbd69509 | 39 | } |
SamClarke | 0:5b4bcbd69509 | 40 | } |
SamClarke | 0:5b4bcbd69509 | 41 | } |
SamClarke | 0:5b4bcbd69509 | 42 | |
SamClarke | 0:5b4bcbd69509 | 43 | void sendSms() |
SamClarke | 0:5b4bcbd69509 | 44 | { |
SamClarke | 0:5b4bcbd69509 | 45 | // Set message mode to ASCII |
SamClarke | 0:5b4bcbd69509 | 46 | GPRS.printf("AT+CMGF=1\r\n"); |
SamClarke | 0:5b4bcbd69509 | 47 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 48 | // Set the phone number |
SamClarke | 2:2b2c785c6627 | 49 | GPRS.printf("AT+CMGS=\"+614<<NUMBER>>\"\r\n"); |
SamClarke | 0:5b4bcbd69509 | 50 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 51 | // Write out the GPS data in a message |
SamClarke | 0:5b4bcbd69509 | 52 | GPRS.printf("Time: %4.2f \nAltitude: %3.2fm\nSpeed: %3.2f Kn\nHeading: %3.2f Deg\nValidity: %c\nFix: %s\nSatellites: %d", gps.time, gps.altitude, gps.speed, gps.heading,gps.validity,gps.fixtype,gps.satellites); |
SamClarke | 0:5b4bcbd69509 | 53 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 54 | // Send it... |
SamClarke | 0:5b4bcbd69509 | 55 | GPRS.putc(0x1A); |
SamClarke | 0:5b4bcbd69509 | 56 | wait(3); |
SamClarke | 0:5b4bcbd69509 | 57 | GPRS.printf("AT+CMGF=1\r\n"); |
SamClarke | 0:5b4bcbd69509 | 58 | wait(1); |
SamClarke | 2:2b2c785c6627 | 59 | GPRS.printf("AT+CMGS=\"+614<<NUMBER>>\"\r\n"); |
SamClarke | 0:5b4bcbd69509 | 60 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 61 | GPRS.printf("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras); |
SamClarke | 0:5b4bcbd69509 | 62 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 63 | GPRS.putc(0x1A); |
SamClarke | 0:5b4bcbd69509 | 64 | } |
SamClarke | 0:5b4bcbd69509 | 65 | |
SamClarke | 0:5b4bcbd69509 | 66 | void parseSms() |
SamClarke | 0:5b4bcbd69509 | 67 | { |
SamClarke | 0:5b4bcbd69509 | 68 | // If theres a char waiting in the MODSERIAL buffer |
SamClarke | 0:5b4bcbd69509 | 69 | while (GPRS.readable()) { |
SamClarke | 0:5b4bcbd69509 | 70 | // Assign it to 'c' |
SamClarke | 0:5b4bcbd69509 | 71 | char c = GPRS.getc(); |
SamClarke | 0:5b4bcbd69509 | 72 | // Replace all returns and or line endings with money! |
SamClarke | 0:5b4bcbd69509 | 73 | if (c == '\r' || c == '\n') c = '$'; |
SamClarke | 0:5b4bcbd69509 | 74 | // Put it in the array |
SamClarke | 0:5b4bcbd69509 | 75 | GPRSbuffer[i] = c; |
SamClarke | 0:5b4bcbd69509 | 76 | // Repeat if possible |
SamClarke | 0:5b4bcbd69509 | 77 | i++; |
SamClarke | 0:5b4bcbd69509 | 78 | } |
SamClarke | 0:5b4bcbd69509 | 79 | // Uncomment the following to debug |
SamClarke | 0:5b4bcbd69509 | 80 | |
SamClarke | 1:00c7dc0c1761 | 81 | pc.printf("buffer = %s", GPRSbuffer); |
SamClarke | 1:00c7dc0c1761 | 82 | |
SamClarke | 0:5b4bcbd69509 | 83 | |
SamClarke | 0:5b4bcbd69509 | 84 | // If we get an SMS notification.... |
SamClarke | 0:5b4bcbd69509 | 85 | if (sscanf(GPRSbuffer, "$$+CMTI: \"SM\",%d", &index)>0) { |
SamClarke | 1:00c7dc0c1761 | 86 | pc.printf("\nSMS recieved @ index [%d]", index); |
SamClarke | 0:5b4bcbd69509 | 87 | // Do the send SMS routine... |
SamClarke | 0:5b4bcbd69509 | 88 | sendSms(); |
SamClarke | 0:5b4bcbd69509 | 89 | // Wait for the cascade of \r\nOK\r\n\r\nOK\r\n's from the SMS's to finish |
SamClarke | 0:5b4bcbd69509 | 90 | wait(4); |
SamClarke | 0:5b4bcbd69509 | 91 | // Flush out any left in the serial buffer |
SamClarke | 0:5b4bcbd69509 | 92 | GPRS.rxBufferFlush(); |
SamClarke | 0:5b4bcbd69509 | 93 | // Reset the GPRS buffer |
SamClarke | 0:5b4bcbd69509 | 94 | memset(GPRSbuffer, '0', 512); |
SamClarke | 0:5b4bcbd69509 | 95 | // Reset the char counter |
SamClarke | 0:5b4bcbd69509 | 96 | i = 0; |
SamClarke | 1:00c7dc0c1761 | 97 | } else { |
SamClarke | 1:00c7dc0c1761 | 98 | GPRS.rxBufferFlush(); |
SamClarke | 1:00c7dc0c1761 | 99 | // Reset the GPRS buffer |
SamClarke | 1:00c7dc0c1761 | 100 | memset(GPRSbuffer, '0', 512); |
SamClarke | 1:00c7dc0c1761 | 101 | // Reset the char counter |
SamClarke | 1:00c7dc0c1761 | 102 | i = 0; |
SamClarke | 1:00c7dc0c1761 | 103 | pc.printf("\nWaiting for SMS...\n"); |
SamClarke | 1:00c7dc0c1761 | 104 | } |
SamClarke | 0:5b4bcbd69509 | 105 | } |
SamClarke | 0:5b4bcbd69509 | 106 | |
SamClarke | 0:5b4bcbd69509 | 107 | int main() |
SamClarke | 0:5b4bcbd69509 | 108 | { |
SamClarke | 0:5b4bcbd69509 | 109 | pc.baud(115200); |
SamClarke | 0:5b4bcbd69509 | 110 | GPRS.baud(19200); |
SamClarke | 0:5b4bcbd69509 | 111 | Thread thread(led_thread); |
SamClarke | 0:5b4bcbd69509 | 112 | pc.printf("I'm Alive...\n"); |
SamClarke | 0:5b4bcbd69509 | 113 | // Setup the GPS |
SamClarke | 0:5b4bcbd69509 | 114 | gps.Init(); |
SamClarke | 0:5b4bcbd69509 | 115 | // Set the GPRS AT echo off |
SamClarke | 0:5b4bcbd69509 | 116 | GPRS.printf("ATE0\r\n"); |
SamClarke | 0:5b4bcbd69509 | 117 | pc.printf("GPRS Echo OFF\n"); |
SamClarke | 0:5b4bcbd69509 | 118 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 119 | // Delete all messages on the sim card |
SamClarke | 0:5b4bcbd69509 | 120 | GPRS.printf("AT+CMGDA=\"DEL ALL\"\r\n"); |
SamClarke | 0:5b4bcbd69509 | 121 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 122 | pc.printf("Messages Cleared...\n"); |
SamClarke | 0:5b4bcbd69509 | 123 | wait(1); |
SamClarke | 0:5b4bcbd69509 | 124 | // Flush out any \r\nOK\r\n's |
SamClarke | 0:5b4bcbd69509 | 125 | GPRS.rxBufferFlush(); |
SamClarke | 0:5b4bcbd69509 | 126 | // Get in a while loop |
SamClarke | 0:5b4bcbd69509 | 127 | while (1) { |
SamClarke | 0:5b4bcbd69509 | 128 | // Process any recieved data |
SamClarke | 0:5b4bcbd69509 | 129 | parseSms(); |
SamClarke | 0:5b4bcbd69509 | 130 | // Process / check GPS data |
SamClarke | 0:5b4bcbd69509 | 131 | if(gps.parseData()) { |
SamClarke | 0:5b4bcbd69509 | 132 | pc.printf("\nGo to %s%f,%f%s\n", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras); |
SamClarke | 0:5b4bcbd69509 | 133 | pc.printf("\n\nLocation: %f%c, %f%c \nTime: %4.2f \nSatellites: %d \nAltitude: %3.2f\nSpeed: %3.2f\nHeading: %3.2f\nValidity: %c\n",gps.latitude, gps.ns, gps.longitude, gps.ew, gps.time, gps.satellites, gps.altitude, gps.speed, gps.heading,gps.validity); |
SamClarke | 0:5b4bcbd69509 | 134 | } else pc.printf("Aquiring Satellites...\n"); |
SamClarke | 0:5b4bcbd69509 | 135 | } |
SamClarke | 0:5b4bcbd69509 | 136 | } |