Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void setup()
- {
- ACC.ON();
- USB.begin(); // Opening UART to show messages using 'Serial Monitor'
- // Power up the Real Time Clock(RTC), init I2C bus and read initial values
- RTC.ON();
- //Initialize the SD card
- SD.begin(); // Inits SD pins
- SD.setMode(SD_ON); // Power SD up
- USB.println(SD.init());
- if(SD.flag == 0) USB.println("The SD card could not be initialized.");
- //Initialize the GPS module
- GPS.ON(); // Turn GPS on
- // GPS.setMode(GPS_ON); // set GPS on
- // if(!GPS.pwrMode) USB.println("Was unable to set on the GPS internal power mode.");
- // if(!GPS.setCommMode(GPS_NMEA_GGA)) USB.println("Was unable to set the GPS communication mode.");
- while( !GPS.check() ){
- USB.println("Waiting to get a satellite fix...");
- delay(1000);
- }
- USB.println("GPS Connected.");
- }
- int gpsConnected = 1, gpsTimeSet = 0;
- int iterationsBeforeUpload = 4; //The number of loops to loop before checking whether we need to upload the data
- long loopTimeBeforeUpload = 0.5 * (60*60); //the number of hours to save the data before attempting to upload it
- uint16_t iterations = 0, iterationsWithGPS = 1; //A counter of the number of iterations through the loop
- long startTime; //The time we start receiving data from the GPS. Forms the start time to start the countdown (in seconds)
- char *tmp, *tmp_a, *tmp_b, *tmp_c; //a global variable for various uses
- uint8_t uploadCount = 0; //The number of times we have uploaded some data
- int len, i, j;
- uint16_t tiny_i, tiny_j; //A temp place holder for small integers
- char *loopDate, *loopTime;
- char *unsentFolder = "unsent"; //The folder where the unsent files will be saved
- char *sentFolder = "sent"; //The folder where the sent files will be saved
- uint8_t iterationsPerFile = 20; //The number of iterations to save in each file
- //char *curFile; //The name of the current file where we are saving the data
- char *fileTemplate = "raw_data"; //The file name template that we will be using
- char *curFile;
- /**
- Converts the passed date and time to seconds, unix like, ie passed seconds from epoch time, where our epoch time is 20000101 000000
- Returns the seconds which have elapsed since our epoch time
- */
- long convertTimeToSec(char *xDate, char *xTime){
- char dy[3] = "", mn[3] = "", yr[3] = "", hr[3] = "", mint[3] = "", sc[3] = "";
- long timeInSec = 0;
- //get the date
- i = 0;
- len = strlen(xDate);
- for(i = 0; i < len; i++){
- if(i < 2) dy[i] = xDate[i];
- else if(i > 1 && i < 4) mn[i-2] = xDate[i];
- else if(i > 3 && i < 6) yr[i-4] = xDate[i];
- }
- //get the time
- i = 0;
- len = strlen(xTime);
- for(i = 0; i < len; i++){
- if(i < 2) hr[i] = xTime[i];
- else if(i > 1 && i < 4) mint[i-2] = xTime[i];
- else if(i > 3 && i < 6) sc[i-4] = xTime[i];
- }
- USB.println("Received Data: loopDate - loopTime:");
- USB.print(GPS.dateGPS); USB.print(" - ");
- USB.print(GPS.timeGPS); USB.print("\n");
- USB.print("Broken Down: yr-month-date hr-minutes-seconds: ");
- USB.print(yr); USB.print("-");
- USB.print(mn); USB.print("-");
- USB.print(dy); USB.print(" ");
- USB.print(hr); USB.print("-");
- USB.print(mint); USB.print("-");
- USB.println(sc);
- //timeInSec += atol(sc);
- //USB.print("Seconds: ");
- //USB.println(timeInSec);
- timeInSec += atol(mint);
- USB.print("Minutes: ");
- USB.println(timeInSec);
- timeInSec += (atol(hr) * (60) );
- USB.print("Hours: ");
- USB.println(timeInSec);
- timeInSec += (atol(dy) * (24*60) );
- USB.print("Days: ");
- USB.println(timeInSec);
- timeInSec += (atol(mn) * (30*24*60) ); //An plain assumption that each month has 30days
- USB.print("Months: ");
- USB.println(timeInSec);
- USB.println(strtod(mn, &tmp) * (30*24*60));
- timeInSec += (atol(yr) * (365*24*60) ); //An plain assumption that each year has 365days
- USB.print("Years: ");
- USB.println(timeInSec);
- USB.print("Final: ");
- USB.println(timeInSec);
- return timeInSec;
- }
- void loop(){
- //declare the variables
- USB.print("5");
- len, i, j = 0;
- char degree[4] = "", minutes[8] = "";
- uint8_t temperature = 0;
- int8_t fileFound = 0;
- byte accOk;
- USB.print("6");
- //check if the GPS has connected to the satellite
- USB.print("7");
- if(iterations == 0){
- USB.print(RTC.getTime());
- }
- USB.print("8");
- if(!gpsConnected){
- USB.println(" Warning: GPS is yet to get a satellite fix...");
- }
- else{
- USB.println(" Info: GPS has a satellite fix....");
- //USB.println(GPS.getRaw(80)); //get the raw NMEA data
- // Getting Date and Time
- GPS.getPosition();
- USB.print("Date and Time: ");
- USB.print(GPS.timeGPS);
- USB.print(" ");
- USB.println(GPS.dateGPS);
- // Getting Coordinates
- //convert the latitude to dd.dd
- // i = 0; j = 0; tmp = "";
- // len = strlen(latitude);
- // for(i = 0; i < len; i++){
- // if(i < 2){
- // degree[i] = latitude[i];
- // }
- // else{
- // minutes[j] = latitude[i];
- // j++;
- // }
- // }
- // latitude_dd = strtod(degree, &tmp) + (strtod(minutes, &tmp)/60);
- //
- // //convert the longitude to dd.dd
- // degree[0] = '\0'; minutes[0] = '\0';
- // i = 0; j = 0; tmp = "";
- // len = strlen(longitude);
- // for(i = 0; i < len; i++){
- // if(i < 3){
- // degree[i] = longitude[i];
- // }
- // else{
- // minutes[j] = longitude[i];
- // j++;
- // }
- // }
- // longitude_dd = strtod(degree, &tmp) + (strtod(minutes, &tmp)/60);
- USB.print("Raw -- Lat, Long, Alt: ");
- USB.print(GPS.latitude);
- USB.print(", ");
- USB.print(GPS.longitude);
- USB.print(", ");
- USB.println(GPS.altitude);
- // USB.print("Converted -- Lat, Long, Alt: ");
- // USB.print(latitude_dd);
- // USB.print(", ");
- // USB.print(longitude_dd);
- // USB.print(", ");
- // USB.println(GPS.altitude);
- //Get the speed
- USB.print("Speed: ");
- USB.println(GPS.speed);
- // Getting Course
- USB.print("Course: ");
- USB.println(GPS.course);
- iterationsWithGPS++;
- }
- //lets set some time in the format YY:MM:DD:dow:hh:mm:ss
- if(gpsTimeSet != 1){
- if(gpsConnected){
- RTC.setTimeFromGPS(); //Since we have a GPS connection, lets set the time from the satellites!
- //save this time(in seconds) in our startTime variable
- char curTime[6] = "";
- // startTime = convertTimeToSec(loopDate, loopTime);
- gpsTimeSet = 1;
- }
- else{
- if(iterations == 0){
- RTC.setTime("12:01:01:01:00:00:01"); //Set an arbitrary time since we dont have the time from the GPS. Defaults on January 1st 2012, which is a Sunday
- }
- }
- }
- //Check the proper functionality of the accelerometer: should always answer 0x3A if all is ok
- accOk = ACC.check();
- USB.println("");
- USB.print(RTC.getTime());
- int16_t accX, accY, accZ;
- if(accOk != 0x3A){
- USB.print(": Warning: Accelerometer not ok!\n");
- USB.println(accOk, HEX);
- }
- else{
- USB.print(": Info: Accelerometer is ok.\n");
- //good, now lets record the acceleration on the x, y and z axis
- accX = ACC.getX();
- accY = ACC.getY();
- accZ = ACC.getZ();
- USB.print(RTC.getTime());
- USB.print(" Acceleration on the X, Y and Z axis: ");
- USB.print(accX, DEC);
- USB.print(", ");
- USB.print(accY, DEC);
- USB.print(" ");
- USB.println(accZ, DEC);
- }
- //lets see if the built in temp sensors are ok
- USB.println("");
- USB.print(GPS.timeGPS);
- USB.print(": Info Temperature: ");
- temperature = RTC.getTemperature();
- USB.println(temperature, DEC);
- // Show the remaining battery level
- int battLevel = PWR.getBatteryLevel();
- USB.print("Battery Level: ");
- USB.print(battLevel,DEC);
- USB.println("%\n");
- //check if its time to upload
- iterations++;
- USB.print("Iterations, IterationsWithGPS: ");
- USB.print(iterations, DEC);
- USB.print(", ");
- USB.println(iterationsWithGPS, DEC);
- if((iterationsWithGPS % iterationsPerFile) == 0 || iterations == 1){
- //USB.println(SD.ls());
- tmp_b = (char *)malloc(sizeof(char) * 200); //for the long messages
- if(tmp_b == NULL) USB.println("Couldn't allocate memory.");
- tmp_c = (char *)malloc(sizeof(char) * 30); //for the file names and short messages
- if(tmp_c == NULL) USB.println("Couldn't allocate memory.");
- tiny_i = 0;
- if(gpsConnected){ //create a file with the date and time as the name
- snprintf(tmp_c, 30, "%s%s%s%s", GPS.dateGPS, "_", GPS.timeGPS, ".txt");
- tiny_i = 1;
- }
- else{ //create a file with the iterations
- for(i = 0; i < 1000; i++){
- //create the name of the file
- snprintf(tmp_c, 30, "%s%d%s", fileTemplate, i, ".txt");
- //check if the file exists or not
- tiny_j = SD.isFile(tmp_c);
- if(tiny_j == 1){ /*The file already exists*/ }
- else if(tiny_j == -1){
- tiny_i = 1;
- break;
- }
- else if(tiny_j == 0) USB.println("Something found but is not a file.\n");
- }
- }
- free(tmp_b);
- if(tiny_i == 0) free(tmp_c);
- else if(tiny_i == 1){ //we need to create a file
- snprintf(tmp_b, 200, "%s%s%s", "The file doesn't exist, so we are going to create it. Current file will be '", tmp_c, "'.\n");
- USB.println(tmp_b);
- tiny_j = SD.create(tmp_c);
- if(tiny_j == 1) USB.println("The file was created successfully.\n");
- else if(tiny_j == 0) USB.println("The file was not created. Iko shida!\n");
- curFile = tmp_c;
- }
- }
- USB.print("Current File: ");
- USB.println(curFile);
- tmp_a = (char *)malloc(sizeof(char) * 200);
- if(gpsConnected){
- //all is good, now lets save the kawaida data to the SD card. We are going to create a string like
- //{dt:,datetm:time,lt:latitude,ln:longitude,al:altitude,cs:course,tp:temperature,ax:accX,ay:accY,az:accZ,bl:batteryLevel,}
- if(tmp_a == NULL) USB.println("Couldn't allocate memory.");
- snprintf(tmp_a, 200, "%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%d%s%d%s%d%s%d%s", "{dt:", GPS.dateGPS, ",tm:", GPS.timeGPS, ",lt:", GPS.latitude, ",ln:", GPS.longitude, ",al:", GPS.altitude, ",cs:", GPS.course, ",tp:", temperature, ",ax:", accX, ",ay:", accY, ",az:", accZ, ",bl:", battLevel,"}\0");
- }
- else{ //we are gonna save all the other params without the GPS data
- if(tmp_a == NULL) USB.println("Couldn't allocate memory.");
- snprintf(tmp_a, 200, "%s%d%s%d%s%d%s%d%s%d%s%d%s", "{it:", iterations, ",tp:", temperature, ",ax:", accX, ",ay:", accY, ",az:", accZ, ",bl:", battLevel,"}\0");
- }
- USB.println(tmp_a);
- SD.appendln(curFile, tmp_a);
- free(tmp_a);
- USB.print("1");
- if(iterationsWithGPS % iterationsBeforeUpload == 0){
- USB.print("2");
- USB.println("\n\nThis is where we are uploading some data\n");
- //check if there is need to upload the data
- // long curTime = convertTimeToSec(loopDate, loopTime);
- // if(curTime - startTime >= (loopTimeBeforeUpload * (uploadCount+1)) ){
- // USB.println("\n\nThis is where we are uploading some data\n");
- // uploadCount++;
- // }
- }
- USB.print("3");
- delay(500); //sleep for 5secs
- USB.print("4");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement