Peter Foden / Mbed 2 deprecated GPS_Logger_01

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // GPS synchronised data logger
00002 // Uses the $GPRMC string for time and date
00003 // synchronisation
00004 // Code derived from Arduino GPS examples
00005 // and the MBED GPS library
00006 // http://www.arduino.cc/
00007 // Thanks Lady Ada and others!
00008 
00009 #include "mbed.h"
00010 // Commands for Globalsat EM-406A GPS
00011 // to turn on or off data strings
00012 #define RMC_ON   "$PSRF103,4,0,1,1*21\r\n"
00013 #define RMC_OFF  "$PSRF103,4,0,0,1*20\r\n"
00014 #define GGA_ON   "$PSRF103,0,0,1,1*25\r\n"
00015 #define GGA_OFF  "$PSRF103,0,0,0,1*24\r\n"
00016 #define GSA_ON   "$PSRF103,2,0,1,1*27\r\n"
00017 #define GSA_OFF  "$PSRF103,2,0,0,1*26\r\n"
00018 #define GSV_ON   "$PSRF103,3,0,1,1*26\r\n"
00019 #define GSV_OFF  "$PSRF103,3,0,0,1*27\r\n"
00020 #define WAAS_ON  "$PSRF151,1*3F\r\n"
00021 #define WAAS_OFF "$PSRF151,0*3E\r\n"
00022 
00023 // Set up serial ports and LEDs
00024 // MBED USB port used for console debug messages
00025 // EM-406A GPS on Pin9 and Pin10
00026 Serial pc(USBTX, USBRX); // tx, rx - Default 9600 baud
00027 Serial gps(p9, p10);     // tx, rx - 4800 baud required
00028 DigitalOut led1(LED1);   // Seconds LED
00029 DigitalOut led4(LED4);   // Data sent LED
00030 
00031 //char buffidx;
00032 char msg[256];           // GPS data buffer
00033 char status;             // GPS status 'V' = Not locked; 'A' = Locked
00034 char *parseptr;          // Pointer for parsing GPS message
00035 char time_buff[7] = "000000";
00036 char date_buff[7] = "000000";
00037 char sens_buff[7] = "123456";
00038 
00039 // Switch off all but $GPRMC string
00040 void setgps() {
00041     gps.printf(RMC_ON);
00042     gps.printf(GGA_OFF);
00043     gps.printf(GSA_OFF);
00044     gps.printf(GSV_OFF);
00045     gps.printf(WAAS_OFF);
00046     return;
00047 }
00048 // Get line of data from GPS
00049 void getline() {
00050     while (gps.getc() != '$'); // Wait for start of line
00051     for (int i=0; i<256; i++) {
00052         msg[i] = gps.getc();
00053 // pc.putc(msg[i]);
00054         if (msg[i] == '\r') {
00055             msg[i] = 0;
00056             return;
00057         }
00058     }
00059 }
00060 // Flash LED 1
00061 void flashled1(void) {
00062     led1 = 1;
00063     wait(0.05);
00064     led1 = 0;
00065     wait(0.05);
00066 }
00067 // Flash LED4
00068 void flashled4(void) {
00069     led4 = 1;
00070     wait(0.1);
00071     led4 = 0;
00072     wait(0.1);
00073 }
00074 void read_sensor() {
00075 // Insert code here to read sensor
00076 // and store data in sens_buff[]
00077 }
00078 void send_data() {
00079 // Insert code to send data
00080     pc.printf(sens_buff);
00081     pc.printf("\r\n");
00082 }
00083 
00084 int main(void) {
00085     gps.baud(4800);         // NB GPS baud rate 4800
00086     pc.printf("\n\rGPS Logger Running!...\n\r");
00087     setgps();
00088     while (1) {
00089         getline();
00090         if (strncmp(msg, "GPRMC",5) == 0) {
00091             // Put the time into the time array
00092             parseptr = msg+6;
00093             for (int i = 0; i < 6; i++) {
00094                 time_buff[i] = parseptr[i];
00095             }
00096             // Skip through commas in the data string
00097             // Firstly to get to the status flag and
00098             // then to pick up the date
00099             parseptr = strchr(parseptr, ',')+1;
00100             status = parseptr[0];
00101             parseptr = strchr(parseptr, ',')+1;
00102             parseptr = strchr(parseptr, ',')+1;
00103             parseptr = strchr(parseptr, ',')+1;
00104             parseptr = strchr(parseptr, ',')+1;
00105             parseptr = strchr(parseptr, ',')+1;
00106             parseptr = strchr(parseptr, ',')+1;
00107             parseptr = strchr(parseptr, ',')+1;
00108             // Put the date into the date array
00109             for (int i = 0; i < 6; i++) {
00110                 date_buff[i] = parseptr[i];
00111             }
00112             // Every second output time, date and lock status
00113             // to the console and flash LED1
00114             pc.printf(time_buff);
00115             pc.printf(",");
00116             pc.printf(date_buff);
00117             pc.printf(",");
00118             pc.putc(status);
00119             pc.printf("\r\n");
00120             flashled1();
00121             // Take a sensor reading every minute
00122             // ie is the time hh:mm:00
00123             if (time_buff[4] == 0x30 && time_buff[5] == 0x30) {
00124                 pc.printf("\r\nSensor Reading!\r\n");
00125                 read_sensor();
00126                 pc.printf("Sending: ");
00127                 send_data();
00128                 pc.printf("\r\n");
00129                 flashled4();
00130 
00131             }
00132         }
00133     }
00134 }
00135