Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int gpsConnected = 0, gpsTimeSet = 0;
- int iterationsBeforeUpload = 2; //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, *curLocation;
- 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 = 5; //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;
- char *simPIN = "";
- 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());
- //the flag SD.flag is not set..... so dont use it.
- //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.");
- //check if the 2 folders which we shall be using are there, if not create them
- i = SD.isDir(sentFolder);
- //USB.println(sentFolder);
- USB.println(i);
- if(i == 1) {} //the folder exists. we are ok
- else if(i == 0){ //something exists, but is not a folder. so delete it and create a new folder
- if( !SD.del(sentFolder) ) USB.println("Something with the sent folder name appears in the SD card and cannot be deleted.");
- i = 0; //set the flag for creating the folder
- }
- else if(i == -1) i = 0; //it does not exists
- if(i == 0){ //create the folder
- if( !SD.mkdir(sentFolder) ) USB.println("The sent folder doesn't exists and couldn't be created.");
- }
- //unsent folder
- i = SD.isDir(unsentFolder);
- //USB.println(i);
- if(i == 1) {} //the folder exists. we are ok
- else if(i == 0){ //something exists, but is not a folder. so delete it and create a new folder
- if( !SD.del(unsentFolder) ) USB.println("Something with the un-sent folder name appears in the SD card and cannot be deleted.");
- i = 0; //set the flag for creating the folder
- }
- else if(i == -1) i = 0; //it does not exists
- if(i == 0){ //create the folder
- if( !SD.mkdir(unsentFolder) ) USB.println("The un-sent folder doesn't exists and couldn't be created.");
- }
- GPRS_Pro.ON();
- USB.println("GPRS Pro turned on.");
- while(!GPRS_Pro.setPIN("6785"));
- USB.println("GPRS Pro PIN set.");
- while(!GPRS_Pro.check());
- USB.println("GPRS_Pro connected to the network");
- GPRS_Pro.getCellInfo();
- USB.print("RSSI: ");
- USB.println(GPRS_Pro.RSSI);
- USB.print("Cell ID: ");
- USB.println(GPRS_Pro.cellID);
- if(GPRS_Pro.configureGPRS_HTTP_FTP(1)) USB.println("Configuration OK");
- else USB.println("Configuration failed");
- }
- /**
- 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");
- USB.println("Try and read the net.");
- USB.println(GPRS_Pro.readURL("www.google.co.ke",1));
- USB.println(GPRS_Pro.data_URL);
- 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((iterations % iterationsPerFile) == 0 || iterations == 1){
- //USB.println("1");
- tmp_b = (char *)malloc(sizeof(char) * 200); //for the long messages
- //USB.println("2");
- if(tmp_b == NULL) USB.println("Couldn't allocate memory.");
- if(tmp_c != NULL) free(tmp_c);
- tmp_c = (char *)malloc(sizeof(char) * 30); //for the file names and short messages
- //USB.println("3");
- if(tmp_c == NULL) USB.println("Couldn't allocate memory.");
- tiny_i = 0;
- //USB.println("4");
- if(curLocation != unsentFolder){ //cd into the un-sent folder directory, if we are already not there
- //snprintf(tmp_c, 30, "%s%s", "./", unsentFolder);
- if( !SD.cd(unsentFolder) ) USB.println("Could not cd into the unsent folder.");
- else curLocation = unsentFolder;
- //USB.println( SD.ls() );
- }
- 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){ //we need to create the file
- tiny_i = 1;
- break;
- }
- else if(tiny_j == 0){
- USB.print("Found something but is not a file -- ");
- USB.println(tmp_c);
- }
- }
- }
- 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);
- //USB.println( SD.ls() );
- tiny_j = SD.create(tmp_c);
- if(tiny_j == 1){
- USB.println("The file was created successfully.\n");
- curFile = tmp_c;
- }
- else if(tiny_j == 0) USB.println("The file was not created, we are going to use the old file. Iko shida!\n");
- }
- //if(SD.cd("..")) USB.println("Getting out of the unsent folder.");
- }
- USB.print("Current File: /");
- USB.print(curLocation);
- USB.print("/");
- 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:date,tm: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(iterations % iterationsBeforeUpload == 0){ //lets try and upload data to the server
- USB.println("\n\nThis is where we are uploading some data\n");
- //i = SD.numFiles();
- //i++;
- tmp_a = (char *)malloc(sizeof(char) * 30);
- //char *tmp_d = (char *)malloc(sizeof(char) * 30);
- USB.println("Allocated memory.");
- //if(i < 0)
- i = 300; //we have some errors while reading the folder, lets assume the number of files that we might be having
- for(j = 2; j < i; j++){
- tmp = SD.ls(j, 1, NAMES);
- USB.print(tmp);
- tmp_a = "";
- snprintf(tmp_a, 30, "%s%s", "/unsent/", tmp);
- USB.print(tmp_a);
- GPRS_Pro.uploadFile(tmp, tmp, "username", "password", "ip-address", "21",1);
- USB.println("uploaded well");
- }
- //GPRS_Pro.OFF();
- //USB.println("GPRS Pro turned off.");
- free(tmp_a);
- //free(tmp_d);
- USB.println("Freed memory.");
- //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