![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
basic lightning detector with gps and sd card logging
Dependencies: AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile
Diff: main.cpp
- Revision:
- 8:f8830b6c6d9b
- Parent:
- 6:96b0dbe76357
- Child:
- 9:15c9bf86d908
--- a/main.cpp Tue Jun 30 16:23:48 2015 +0000 +++ b/main.cpp Tue Jun 30 21:13:18 2015 +0000 @@ -5,6 +5,8 @@ #include "SDFileSystem.h" #include "AS3935.h" #include "ConfigFile.h" +#include "datetime.h" +#include "time.h" #include <string> #define FW_VER 3 @@ -41,7 +43,6 @@ DigitalOut green(LED_GREEN); //DigitalOut blue(LED_BLUE); don't use the blue led, due to a board error, writing to the blue led kills spi bool debug=false; -int day, month,year,hour,minute,seconds; void writeLogFile(int interruptSource, int distance, long energy); char logName[]="lightning_data.csv"; int rdistance, rinterrupt; @@ -49,6 +50,7 @@ int OriginInt=-1; int gDistance=-1; long energy=-1; +struct tm Clock; typedef struct { @@ -109,9 +111,12 @@ header = true; } // write to the current log file - fprintf(fp,"%02d/%02d/20%02d,", gpsd.month, gpsd.day, gpsd.year); - fprintf(fp,"%02d:%02d:%02d,", gpsd.hour, gpsd.minute, gpsd.seconds); - fprintf(fp,"%7.0f,",gpsd.timef); + char time_str[32]; + time_t seconds = time(NULL); + seconds -= 14500; // 14400 = 60*60*4, kludgy way of setting est + struct tm *tminfo = localtime(&seconds); + strftime(time_str, 32, "%m/%d/%Y,%T", tminfo); + fprintf(fp,"%s,%ld,",time_str,seconds); fprintf(fp,"%5.7f,%5.7f,", gpsd.lat_deg, gpsd.lon_deg); fprintf(fp,"%d,",distance); fprintf(fp,"%d,",interruptSource); @@ -122,6 +127,7 @@ fclose(fp); sd.unmount(); pc.printf("Event: "); + pc.printf(" %s - ", time_str); switch (interruptSource) { case 1: @@ -193,6 +199,7 @@ bool readCfgFile(char *paDirectory, sys_cfg_t *paSysCfg) { bool bRetVal = false; + FILE *fp; sys_cfg_t lSysCfg; typedef struct { @@ -223,8 +230,9 @@ fileName = paDirectory; fileName += "/"; fileName += "zeus.cfg"; - + sd.mount(); + fp = fopen(fileName.c_str(), "r"); printf ("\n\rReading configuration file[%s]\n\r", fileName.c_str()); // try to read values from the configuration file @@ -269,6 +277,7 @@ if (plCfgFile) delete plCfgFile; + fclose(fp); sd.unmount(); return bRetVal; } @@ -319,7 +328,6 @@ // read configuration values fro SD file system to allow override of defaults in sysCfg readCfgFile(directory, &sysCfg); - pc.printf("\r\nInitialize lightning detector\r\n"); //initializations for lightning detector ld.init(); @@ -383,6 +391,7 @@ writeCfgFile(regBuff, sizeof(regBuff), FW_VER); bool gpsFix=false; + bool rtcRunning=false; while (1) { if (OriginInt != -1) @@ -413,14 +422,18 @@ } } - // update globals with the lastest gps time stamp - day=gpsd.day; - month=gpsd.month; - year=gpsd.year; - hour=gpsd.hour; - minute=gpsd.minute; - seconds=gpsd.seconds; - + // if the rtc is not running, update the rtc clock with the latest gps time stamp + if ( rtcRunning == false ) + { + // update rtc with the lastest gps time stamp + SetDateTime(gpsd.year+2000, + gpsd.month-1, + gpsd.day, + gpsd.hour, + gpsd.minute, + gpsd.seconds); + } + //check if enough time has passed to warrant printing GPS info to screen //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing if (refresh_Timer.read_ms() >= refresh_Time) @@ -431,6 +444,30 @@ { // first time fix obtained gpsFix = true; + // bug check - rtc may not be running. check if it is incrementing + time_t seconds_a = time(NULL); // get the current rtc second count + wait(2); // wait two seconds + time_t seconds_b = time(NULL); // get the current rtc second count + if (seconds_a != seconds_b) + { + // rtc must be running + rtcRunning = true; + pc.printf("RTC is running\r\n"); + } + else + { + // rtc is not running, we need to update the rtc every pass through the while loop + rtcRunning = false; // the gps time will update the rtc + pc.printf("RTC is not running\r\n"); + } + + // set the rtc with the latest gps time + SetDateTime(gpsd.year+2000, + gpsd.month-1, + gpsd.day, + gpsd.hour, + gpsd.minute, + gpsd.seconds); pc.printf("GPS fix obtained on %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds); //pc.printf("Touch slider to suspend application\r\n"); pc.printf("Waiting for lighting detection...\r\n");