Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: GPS MODSERIAL mbed-rtos mbed
main.cpp@14:5a33972c09e0, 2012-10-21 (annotated)
- Committer:
- SamClarke
- Date:
- Sun Oct 21 09:30:17 2012 +0000
- Revision:
- 14:5a33972c09e0
- Parent:
- 13:d8ee6b45bd0a
Added cardinal directions (N,NNE,ENE,E,ESE,SSE,S etc)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| SamClarke | 12:3f164c88df87 | 1 | /****************************************************** |
| SamClarke | 12:3f164c88df87 | 2 | * This is an SMS based GPS tracker using using the * |
| SamClarke | 12:3f164c88df87 | 3 | * Adafruit Ultimate GPS module and the Seeed studio * |
| SamClarke | 12:3f164c88df87 | 4 | * GPRS arduino shield [SIM900 chipset]. * |
| SamClarke | 12:3f164c88df87 | 5 | * * |
| SamClarke | 12:3f164c88df87 | 6 | * The idea of this project is to leverage the free * |
| SamClarke | 12:3f164c88df87 | 7 | * SMS between Virgin mobile prepaid SIM cards in * |
| SamClarke | 12:3f164c88df87 | 8 | * Australia for long distance communication. * |
| SamClarke | 12:3f164c88df87 | 9 | * * |
| SamClarke | 12:3f164c88df87 | 10 | * NOTE: sendSms() sends the GPS 'extra' data * |
| SamClarke | 12:3f164c88df87 | 11 | * (eg. speed, heading etc) in one message, and your * |
| SamClarke | 12:3f164c88df87 | 12 | * location as a google maps URL in another message. * |
| SamClarke | 12:3f164c88df87 | 13 | * If you are an Iphone user, you can replace the * |
| SamClarke | 12:3f164c88df87 | 14 | * "https" in the URL with "http" and remove the ".au" * |
| SamClarke | 12:3f164c88df87 | 15 | * to get the URL to open in the native maps app. * |
| SamClarke | 12:3f164c88df87 | 16 | * however I have found this to be less accurate by * |
| SamClarke | 12:3f164c88df87 | 17 | * the fact that it doesn't drop a marker on the * |
| SamClarke | 12:3f164c88df87 | 18 | * real locataion, but looks for the nearest address. * |
| SamClarke | 12:3f164c88df87 | 19 | * it seems that the internal GPS is the only object * |
| SamClarke | 12:3f164c88df87 | 20 | * able to be placed unrestricted on the map. * |
| SamClarke | 12:3f164c88df87 | 21 | * * |
| SamClarke | 12:3f164c88df87 | 22 | * October 2012 Written by: Sam Clarke * |
| SamClarke | 12:3f164c88df87 | 23 | ******************************************************/ |
| SamClarke | 12:3f164c88df87 | 24 | /****************************************************** |
| SamClarke | 12:3f164c88df87 | 25 | * HelloCylon * |
| SamClarke | 12:3f164c88df87 | 26 | * By Leo Febey Oct 18 2011 * |
| SamClarke | 12:3f164c88df87 | 27 | * Displays a looping pattern on the built in LEDs on * |
| SamClarke | 12:3f164c88df87 | 28 | * the mbed which looks somewhat like a Cylon eye * |
| SamClarke | 12:3f164c88df87 | 29 | * scanner pattern. * |
| SamClarke | 12:3f164c88df87 | 30 | ******************************************************/ |
| SamClarke | 12:3f164c88df87 | 31 | |
| SamClarke | 0:5b4bcbd69509 | 32 | #include "mbed.h" |
| SamClarke | 0:5b4bcbd69509 | 33 | #include "GPS.h" |
| SamClarke | 0:5b4bcbd69509 | 34 | #include "rtos.h" |
| SamClarke | 0:5b4bcbd69509 | 35 | #include "MODSERIAL.h" |
| SamClarke | 0:5b4bcbd69509 | 36 | #include <string> |
| SamClarke | 0:5b4bcbd69509 | 37 | |
| SamClarke | 0:5b4bcbd69509 | 38 | using namespace std; |
| SamClarke | 0:5b4bcbd69509 | 39 | |
| SamClarke | 12:3f164c88df87 | 40 | const string GoogleChunk = "https://maps.google.com.au/maps?q="; // URL constant |
| SamClarke | 12:3f164c88df87 | 41 | const string GoogleExtras = "&z=20"; // Zoom Level (0-20) |
| SamClarke | 12:3f164c88df87 | 42 | char GPRSbuffer[512]; |
| SamClarke | 12:3f164c88df87 | 43 | char NUMBER[13]; |
| SamClarke | 12:3f164c88df87 | 44 | string MESSAGE; |
| SamClarke | 0:5b4bcbd69509 | 45 | int index; |
| SamClarke | 0:5b4bcbd69509 | 46 | int i = 0; |
| SamClarke | 0:5b4bcbd69509 | 47 | |
| SamClarke | 0:5b4bcbd69509 | 48 | GPS gps(p9,p10); |
| SamClarke | 12:3f164c88df87 | 49 | MODSERIAL GPRS(p13,p14); |
| SamClarke | 0:5b4bcbd69509 | 50 | MODSERIAL pc(USBTX,USBRX); |
| SamClarke | 0:5b4bcbd69509 | 51 | |
| SamClarke | 0:5b4bcbd69509 | 52 | DigitalOut myled1(LED1); |
| SamClarke | 0:5b4bcbd69509 | 53 | DigitalOut myled2(LED2); |
| SamClarke | 0:5b4bcbd69509 | 54 | DigitalOut myled3(LED3); |
| SamClarke | 0:5b4bcbd69509 | 55 | DigitalOut myled4(LED4); |
| SamClarke | 0:5b4bcbd69509 | 56 | |
| SamClarke | 12:3f164c88df87 | 57 | void led_thread(void const *argument) |
| SamClarke | 0:5b4bcbd69509 | 58 | { |
| SamClarke | 0:5b4bcbd69509 | 59 | while (true) { |
| SamClarke | 0:5b4bcbd69509 | 60 | DigitalOut leds[4] = {myled1, myled2, myled3, myled4}; |
| SamClarke | 0:5b4bcbd69509 | 61 | 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 | 62 | int i=0; |
| SamClarke | 0:5b4bcbd69509 | 63 | while(1) { |
| SamClarke | 0:5b4bcbd69509 | 64 | leds[0] = pattern[i][0]; |
| SamClarke | 0:5b4bcbd69509 | 65 | leds[1] = pattern[i][1]; |
| SamClarke | 0:5b4bcbd69509 | 66 | leds[2] = pattern[i][2]; |
| SamClarke | 0:5b4bcbd69509 | 67 | leds[3] = pattern[i][3]; |
| SamClarke | 0:5b4bcbd69509 | 68 | wait(0.2); |
| SamClarke | 0:5b4bcbd69509 | 69 | i++; |
| SamClarke | 0:5b4bcbd69509 | 70 | i == 6? i=0:0; |
| SamClarke | 0:5b4bcbd69509 | 71 | } |
| SamClarke | 0:5b4bcbd69509 | 72 | } |
| SamClarke | 0:5b4bcbd69509 | 73 | } |
| SamClarke | 0:5b4bcbd69509 | 74 | |
| SamClarke | 0:5b4bcbd69509 | 75 | void sendSms() |
| SamClarke | 0:5b4bcbd69509 | 76 | { |
| SamClarke | 0:5b4bcbd69509 | 77 | // Set message mode to ASCII |
| SamClarke | 0:5b4bcbd69509 | 78 | GPRS.printf("AT+CMGF=1\r\n"); |
| SamClarke | 0:5b4bcbd69509 | 79 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 80 | // Set the phone number |
| SamClarke | 10:059cde049cd6 | 81 | GPRS.printf("AT+CMGS=\"%s\"\r\n", NUMBER); |
| SamClarke | 0:5b4bcbd69509 | 82 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 83 | // Write out the GPS data in a message |
| SamClarke | 14:5a33972c09e0 | 84 | 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", |
| SamClarke | 14:5a33972c09e0 | 85 | gps.hours, gps.minutes, gps.seconds, gps.altitude, gps.kph, gps.heading, gps.cardinal, gps.validity, gps.satellites, gps.fixtype, gps.fix); |
| SamClarke | 0:5b4bcbd69509 | 86 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 87 | // Send it... |
| SamClarke | 0:5b4bcbd69509 | 88 | GPRS.putc(0x1A); |
| SamClarke | 10:059cde049cd6 | 89 | wait(4); |
| SamClarke | 0:5b4bcbd69509 | 90 | GPRS.printf("AT+CMGF=1\r\n"); |
| SamClarke | 0:5b4bcbd69509 | 91 | wait(1); |
| SamClarke | 10:059cde049cd6 | 92 | GPRS.printf("AT+CMGS=\"%s\"\r\n", NUMBER); |
| SamClarke | 0:5b4bcbd69509 | 93 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 94 | GPRS.printf("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras); |
| SamClarke | 0:5b4bcbd69509 | 95 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 96 | GPRS.putc(0x1A); |
| SamClarke | 3:a5df4bd224ad | 97 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 98 | } |
| SamClarke | 0:5b4bcbd69509 | 99 | |
| SamClarke | 0:5b4bcbd69509 | 100 | void parseSms() |
| SamClarke | 0:5b4bcbd69509 | 101 | { |
| SamClarke | 0:5b4bcbd69509 | 102 | // If theres a char waiting in the MODSERIAL buffer |
| SamClarke | 0:5b4bcbd69509 | 103 | while (GPRS.readable()) { |
| SamClarke | 0:5b4bcbd69509 | 104 | // Assign it to 'c' |
| SamClarke | 0:5b4bcbd69509 | 105 | char c = GPRS.getc(); |
| SamClarke | 0:5b4bcbd69509 | 106 | // Replace all returns and or line endings with money! |
| SamClarke | 0:5b4bcbd69509 | 107 | if (c == '\r' || c == '\n') c = '$'; |
| SamClarke | 0:5b4bcbd69509 | 108 | // Put it in the array |
| SamClarke | 0:5b4bcbd69509 | 109 | GPRSbuffer[i] = c; |
| SamClarke | 0:5b4bcbd69509 | 110 | // Repeat if possible |
| SamClarke | 0:5b4bcbd69509 | 111 | i++; |
| SamClarke | 0:5b4bcbd69509 | 112 | } |
| SamClarke | 0:5b4bcbd69509 | 113 | // Uncomment the following to debug |
| SamClarke | 10:059cde049cd6 | 114 | // pc.printf("\nbuffer = %s", GPRSbuffer); |
| SamClarke | 0:5b4bcbd69509 | 115 | |
| SamClarke | 0:5b4bcbd69509 | 116 | // If we get an SMS notification.... |
| SamClarke | 0:5b4bcbd69509 | 117 | if (sscanf(GPRSbuffer, "$$+CMTI: \"SM\",%d", &index)>0) { |
| SamClarke | 1:00c7dc0c1761 | 118 | pc.printf("\nSMS recieved @ index [%d]", index); |
| SamClarke | 12:3f164c88df87 | 119 | memset(GPRSbuffer, '0', 511); |
| SamClarke | 7:db9226b9b037 | 120 | i = 0; |
| SamClarke | 12:3f164c88df87 | 121 | pc.printf("\nOpening message..."); |
| SamClarke | 12:3f164c88df87 | 122 | // ask GPRS to read the message |
| SamClarke | 7:db9226b9b037 | 123 | GPRS.printf("AT+CMGR=%d\r\n", index); |
| SamClarke | 7:db9226b9b037 | 124 | } |
| SamClarke | 7:db9226b9b037 | 125 | |
| SamClarke | 7:db9226b9b037 | 126 | if (strncmp(GPRSbuffer, "$$+CMGR",7) == 0 ) { |
| SamClarke | 10:059cde049cd6 | 127 | // Get the number out |
| SamClarke | 10:059cde049cd6 | 128 | char *n = strstr(GPRSbuffer,"+6"); |
| SamClarke | 10:059cde049cd6 | 129 | strncpy(NUMBER, n, 12); |
| SamClarke | 10:059cde049cd6 | 130 | // Get the message out |
| SamClarke | 12:3f164c88df87 | 131 | char * pch; |
| SamClarke | 12:3f164c88df87 | 132 | pch = strtok (GPRSbuffer, "$$"); |
| SamClarke | 12:3f164c88df87 | 133 | pch = strtok (NULL, "$$"); |
| SamClarke | 12:3f164c88df87 | 134 | MESSAGE = pch; |
| SamClarke | 12:3f164c88df87 | 135 | pc.printf("\nDone! "); |
| SamClarke | 10:059cde049cd6 | 136 | // Send the location |
| SamClarke | 0:5b4bcbd69509 | 137 | sendSms(); |
| SamClarke | 0:5b4bcbd69509 | 138 | // Reset the GPRS buffer |
| SamClarke | 12:3f164c88df87 | 139 | memset(GPRSbuffer, '0', 511); |
| SamClarke | 0:5b4bcbd69509 | 140 | // Reset the char counter |
| SamClarke | 0:5b4bcbd69509 | 141 | i = 0; |
| SamClarke | 7:db9226b9b037 | 142 | } |
| SamClarke | 7:db9226b9b037 | 143 | |
| SamClarke | 7:db9226b9b037 | 144 | if (strncmp(GPRSbuffer, "$$+CMGS",7) == 0) { |
| SamClarke | 7:db9226b9b037 | 145 | // Reset the GPRS buffer |
| SamClarke | 12:3f164c88df87 | 146 | memset(GPRSbuffer, '0', 511); |
| SamClarke | 7:db9226b9b037 | 147 | // Reset the char counter |
| SamClarke | 7:db9226b9b037 | 148 | i = 0; |
| SamClarke | 7:db9226b9b037 | 149 | } |
| SamClarke | 7:db9226b9b037 | 150 | |
| SamClarke | 7:db9226b9b037 | 151 | if (strncmp(GPRSbuffer, "$$RING",6) == 0) { |
| SamClarke | 9:bec6e8f731f2 | 152 | GPRS.printf("ATH0\r\n"); |
| SamClarke | 12:3f164c88df87 | 153 | pc.printf("\nCall bounced!..."); |
| SamClarke | 3:a5df4bd224ad | 154 | // Do the send SMS routine... |
| SamClarke | 3:a5df4bd224ad | 155 | sendSms(); |
| SamClarke | 3:a5df4bd224ad | 156 | // Reset the GPRS buffer |
| SamClarke | 12:3f164c88df87 | 157 | memset(GPRSbuffer, '0', 511); |
| SamClarke | 3:a5df4bd224ad | 158 | // Reset the char counter |
| SamClarke | 4:0e55a4620f5e | 159 | i = 0; |
| SamClarke | 1:00c7dc0c1761 | 160 | } |
| SamClarke | 10:059cde049cd6 | 161 | pc.printf("\n\n\nWaiting for SMS or call...\n"); |
| SamClarke | 10:059cde049cd6 | 162 | pc.printf("\nThe last number was : %s", NUMBER); |
| SamClarke | 10:059cde049cd6 | 163 | pc.printf("\nThe last message was : %s", MESSAGE); |
| SamClarke | 7:db9226b9b037 | 164 | // Reset the GPRS buffer |
| SamClarke | 12:3f164c88df87 | 165 | memset(GPRSbuffer, '0', 511); |
| SamClarke | 7:db9226b9b037 | 166 | // Reset the char counter |
| SamClarke | 7:db9226b9b037 | 167 | i = 0; |
| SamClarke | 0:5b4bcbd69509 | 168 | } |
| SamClarke | 0:5b4bcbd69509 | 169 | |
| SamClarke | 0:5b4bcbd69509 | 170 | int main() |
| SamClarke | 0:5b4bcbd69509 | 171 | { |
| SamClarke | 0:5b4bcbd69509 | 172 | pc.baud(115200); |
| SamClarke | 0:5b4bcbd69509 | 173 | GPRS.baud(19200); |
| SamClarke | 0:5b4bcbd69509 | 174 | Thread thread(led_thread); |
| SamClarke | 12:3f164c88df87 | 175 | memset(GPRSbuffer, '0', 511); |
| SamClarke | 9:bec6e8f731f2 | 176 | pc.printf("\nI'm Alive...\n"); |
| SamClarke | 0:5b4bcbd69509 | 177 | // Setup the GPS |
| SamClarke | 0:5b4bcbd69509 | 178 | gps.Init(); |
| SamClarke | 0:5b4bcbd69509 | 179 | // Set the GPRS AT echo off |
| SamClarke | 0:5b4bcbd69509 | 180 | GPRS.printf("ATE0\r\n"); |
| SamClarke | 9:bec6e8f731f2 | 181 | pc.printf("\nGPRS echo [OFF]\n"); |
| SamClarke | 0:5b4bcbd69509 | 182 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 183 | // Delete all messages on the sim card |
| SamClarke | 0:5b4bcbd69509 | 184 | GPRS.printf("AT+CMGDA=\"DEL ALL\"\r\n"); |
| SamClarke | 0:5b4bcbd69509 | 185 | wait(1); |
| SamClarke | 9:bec6e8f731f2 | 186 | pc.printf("\nMessages Cleared...\n"); |
| SamClarke | 0:5b4bcbd69509 | 187 | wait(1); |
| SamClarke | 0:5b4bcbd69509 | 188 | // Get in a while loop |
| SamClarke | 0:5b4bcbd69509 | 189 | while (1) { |
| SamClarke | 10:059cde049cd6 | 190 | // Process any recieved GPRS data |
| SamClarke | 0:5b4bcbd69509 | 191 | parseSms(); |
| SamClarke | 0:5b4bcbd69509 | 192 | // Process / check GPS data |
| SamClarke | 3:a5df4bd224ad | 193 | gps.parseData(); |
| SamClarke | 0:5b4bcbd69509 | 194 | } |
| SamClarke | 0:5b4bcbd69509 | 195 | } |