GPS Logger using SD Card

Dependencies:   SDFileSystem mbed

Tested with a Haicom HI-203E serial GPS mouse and an adafruit MicroSD card breakout board+. The GPS is powered from the "VU" pin and the serial lines are connected via a MAX232 level shifter.

Committer:
mprinke
Date:
Sun Feb 03 16:38:23 2013 +0000
Revision:
0:6a32515035ec
created

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mprinke 0:6a32515035ec 1 /* mbed GPS Logger using SD Card
mprinke 0:6a32515035ec 2 *
mprinke 0:6a32515035ec 3 * Copyright (c) 2013 m.prinke, MIT License
mprinke 0:6a32515035ec 4 *
mprinke 0:6a32515035ec 5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mprinke 0:6a32515035ec 6 * and associated documentation files (the "Software"), to deal in the Software without restriction,
mprinke 0:6a32515035ec 7 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
mprinke 0:6a32515035ec 8 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
mprinke 0:6a32515035ec 9 * furnished to do so, subject to the following conditions:
mprinke 0:6a32515035ec 10 *
mprinke 0:6a32515035ec 11 * The above copyright notice and this permission notice shall be included in all copies or
mprinke 0:6a32515035ec 12 * substantial portions of the Software.
mprinke 0:6a32515035ec 13 *
mprinke 0:6a32515035ec 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mprinke 0:6a32515035ec 15 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mprinke 0:6a32515035ec 16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mprinke 0:6a32515035ec 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mprinke 0:6a32515035ec 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mprinke 0:6a32515035ec 19 *
mprinke 0:6a32515035ec 20 * @file main.cpp
mprinke 0:6a32515035ec 21 * @purpose GPS Logger using SD Card
mprinke 0:6a32515035ec 22 * @version 0.1
mprinke 0:6a32515035ec 23 * @date Feb 2013
mprinke 0:6a32515035ec 24 * @author M. Prinke
mprinke 0:6a32515035ec 25 */
mprinke 0:6a32515035ec 26 /**
mprinke 0:6a32515035ec 27 * Writes any serial data received from a GPS receiver to an logfile on an SD Card.
mprinke 0:6a32515035ec 28 * Logging is started/stopped using a toggle button.
mprinke 0:6a32515035ec 29 * Everytime logging is started, a new file with the filename pattern /sd/gpslog<nnnn>.txt
mprinke 0:6a32515035ec 30 * is created, where <nnnn> is decimal number in the range 0000 to 9999 which is incremented.
mprinke 0:6a32515035ec 31 * Searching for a new unique filename is started from zero.
mprinke 0:6a32515035ec 32 */
mprinke 0:6a32515035ec 33
mprinke 0:6a32515035ec 34 #include "mbed.h"
mprinke 0:6a32515035ec 35 #include "SDFileSystem.h"
mprinke 0:6a32515035ec 36
mprinke 0:6a32515035ec 37 #define GPS_BAUDRATE 4800
mprinke 0:6a32515035ec 38 #define DEBUG
mprinke 0:6a32515035ec 39
mprinke 0:6a32515035ec 40 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs
mprinke 0:6a32515035ec 41
mprinke 0:6a32515035ec 42 Serial pc(USBTX, USBRX); // tx, rx
mprinke 0:6a32515035ec 43 Serial gps(p28, p27); // tx, rx
mprinke 0:6a32515035ec 44 DigitalIn button(p21); // button connects pin to GND
mprinke 0:6a32515035ec 45 DigitalOut led_log(LED1); // logging active
mprinke 0:6a32515035ec 46
mprinke 0:6a32515035ec 47
mprinke 0:6a32515035ec 48 /**
mprinke 0:6a32515035ec 49 * Function to create a unique filename
mprinke 0:6a32515035ec 50 *
mprinke 0:6a32515035ec 51 * @param fn pointer to a filename buffer
mprinke 0:6a32515035ec 52 * @return 0 on success, -1 if no unused filename could be created.
mprinke 0:6a32515035ec 53 */
mprinke 0:6a32515035ec 54 int get_filename(char *fn)
mprinke 0:6a32515035ec 55 {
mprinke 0:6a32515035ec 56 FILE *fp;
mprinke 0:6a32515035ec 57
mprinke 0:6a32515035ec 58 for (int n=0; n < 9999; n++) {
mprinke 0:6a32515035ec 59 sprintf(fn, "/sd/gpslog%04d.txt", n);
mprinke 0:6a32515035ec 60 if ((fp = fopen(fn, "r")) == NULL) {
mprinke 0:6a32515035ec 61 // file does not exist yet
mprinke 0:6a32515035ec 62 return 0;
mprinke 0:6a32515035ec 63 } else {
mprinke 0:6a32515035ec 64 fclose(fp);
mprinke 0:6a32515035ec 65 }
mprinke 0:6a32515035ec 66 }
mprinke 0:6a32515035ec 67
mprinke 0:6a32515035ec 68 // filename pattern exhausted
mprinke 0:6a32515035ec 69 return -1;
mprinke 0:6a32515035ec 70 }
mprinke 0:6a32515035ec 71
mprinke 0:6a32515035ec 72
mprinke 0:6a32515035ec 73 int main() {
mprinke 0:6a32515035ec 74 char ch;
mprinke 0:6a32515035ec 75 char filename[30];
mprinke 0:6a32515035ec 76
mprinke 0:6a32515035ec 77 button.mode(PullUp);
mprinke 0:6a32515035ec 78 gps.baud(GPS_BAUDRATE);
mprinke 0:6a32515035ec 79
mprinke 0:6a32515035ec 80 while (1) {
mprinke 0:6a32515035ec 81 led_log = 0;
mprinke 0:6a32515035ec 82
mprinke 0:6a32515035ec 83 #if defined(DEBUG)
mprinke 0:6a32515035ec 84 pc.printf("\n--------------------------------------------\n");
mprinke 0:6a32515035ec 85 pc.printf("GPS logging stopped, press button to start.\n");
mprinke 0:6a32515035ec 86 pc.printf("--------------------------------------------\n");
mprinke 0:6a32515035ec 87 #endif
mprinke 0:6a32515035ec 88
mprinke 0:6a32515035ec 89 while (button); // wait until button pressed
mprinke 0:6a32515035ec 90 wait_ms(50); // button debounce
mprinke 0:6a32515035ec 91 while (!button); // wait until button released
mprinke 0:6a32515035ec 92 if (get_filename(filename) != 0) {
mprinke 0:6a32515035ec 93 error("Could not create file with unique name\n");
mprinke 0:6a32515035ec 94 }
mprinke 0:6a32515035ec 95
mprinke 0:6a32515035ec 96 #if defined(DEBUG)
mprinke 0:6a32515035ec 97 pc.printf("\n--------------------------------------------\n");
mprinke 0:6a32515035ec 98 pc.printf("GPS logging started, press button to stop.\n");
mprinke 0:6a32515035ec 99 pc.printf("Current logfile: %s\n", filename);
mprinke 0:6a32515035ec 100 pc.printf("--------------------------------------------\n");
mprinke 0:6a32515035ec 101 #endif
mprinke 0:6a32515035ec 102
mprinke 0:6a32515035ec 103 led_log = 1;
mprinke 0:6a32515035ec 104
mprinke 0:6a32515035ec 105 // open file for writing
mprinke 0:6a32515035ec 106 FILE *fp = fopen(filename, "w");
mprinke 0:6a32515035ec 107 if(fp == NULL) {
mprinke 0:6a32515035ec 108 error("Could not open file for write\n");
mprinke 0:6a32515035ec 109 }
mprinke 0:6a32515035ec 110
mprinke 0:6a32515035ec 111 /*
mprinke 0:6a32515035ec 112 * Write GPS data to file until button is pressed or
mprinke 0:6a32515035ec 113 * a write error occurs.
mprinke 0:6a32515035ec 114 */
mprinke 0:6a32515035ec 115 int rv = 0;
mprinke 0:6a32515035ec 116 do {
mprinke 0:6a32515035ec 117 if(gps.readable()) {
mprinke 0:6a32515035ec 118 ch = gps.getc();
mprinke 0:6a32515035ec 119 #if defined(DEBUG)
mprinke 0:6a32515035ec 120 pc.putc(ch);
mprinke 0:6a32515035ec 121 #endif
mprinke 0:6a32515035ec 122 rv = fputc(ch, fp);
mprinke 0:6a32515035ec 123 }
mprinke 0:6a32515035ec 124 } while ((rv != EOF) && button);
mprinke 0:6a32515035ec 125
mprinke 0:6a32515035ec 126 // close file
mprinke 0:6a32515035ec 127 fclose(fp);
mprinke 0:6a32515035ec 128
mprinke 0:6a32515035ec 129 wait_ms(50); // button debounce
mprinke 0:6a32515035ec 130 while (!button); // wait until button released
mprinke 0:6a32515035ec 131 }
mprinke 0:6a32515035ec 132 }