Advertisement
soloincc

waspmote

Dec 19th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.94 KB | None | 0 0
  1. int gpsConnected = 0, gpsTimeSet = 0;
  2. int iterationsBeforeUpload = 2;            //The number of loops to loop before checking whether we need to upload the data
  3. long loopTimeBeforeUpload = 0.5 * (60*60);  //the number of hours to save the data before attempting to upload it
  4. uint16_t iterations = 0, iterationsWithGPS = 1;                         //A counter of the number of iterations through the loop
  5. long startTime;                             //The time we start receiving data from the GPS. Forms the start time to start the countdown (in seconds)
  6. char *tmp, *tmp_a, *tmp_b, *tmp_c;                                  //a global variable for various uses
  7. uint8_t uploadCount = 0;                        //The number of times we have uploaded some data
  8. int len, i, j;
  9. uint16_t tiny_i, tiny_j;                            //A temp place holder for small integers
  10. char *loopDate, *loopTime, *curLocation;
  11.  
  12. char *unsentFolder = "unsent";              //The folder where the unsent files will be saved
  13. char *sentFolder = "sent";                  //The folder where the sent files will be saved
  14. uint8_t iterationsPerFile = 5;                //The number of iterations to save in each file
  15. //char *curFile;                            //The name of the current file where we are saving the data  
  16. char *fileTemplate = "raw_data";          //The file name template that we will be using
  17. char *curFile;
  18. char *simPIN = "";
  19.  
  20. void setup()
  21. {
  22.   ACC.ON();
  23.   USB.begin();  // Opening UART to show messages using 'Serial Monitor'
  24.  
  25.   // Power up the Real Time Clock(RTC), init I2C bus and read initial values
  26.   RTC.ON();
  27.  
  28.   //Initialize the SD card
  29.   SD.begin();  // Inits SD pins
  30.   SD.setMode(SD_ON);  // Power SD up
  31.   USB.println(SD.init());
  32.   //the flag SD.flag is not set..... so dont use it.
  33.   //if(SD.flag == 0) USB.println("The SD card could not be initialized.");
  34.  
  35.   //Initialize the GPS module
  36.   GPS.ON();  // Turn GPS on
  37. //  GPS.setMode(GPS_ON);    // set GPS on
  38. //  if(!GPS.pwrMode) USB.println("Was unable to set on the GPS internal power mode.");
  39. //  if(!GPS.setCommMode(GPS_NMEA_GGA)) USB.println("Was unable to set the GPS communication mode.");
  40. //  while( !GPS.check() ){
  41. //    USB.println("Waiting to get a satellite fix...");
  42. //    delay(1000);
  43. //  }
  44. //  USB.println("GPS Connected.");
  45.  
  46.   //check if the 2 folders which we shall be using are there, if not create them
  47.   i = SD.isDir(sentFolder);
  48.   //USB.println(sentFolder);
  49.   USB.println(i);
  50.   if(i == 1) {}    //the folder exists. we are ok
  51.   else if(i == 0){  //something exists, but is not a folder. so delete it and create a new folder
  52.     if( !SD.del(sentFolder) ) USB.println("Something with the sent folder name appears in the SD card and cannot be deleted.");
  53.     i = 0;    //set the flag for creating the folder
  54.   }
  55.   else if(i == -1) i = 0;  //it does not exists
  56.  
  57.   if(i == 0){  //create the folder
  58.     if( !SD.mkdir(sentFolder) ) USB.println("The sent folder doesn't exists and couldn't be created.");
  59.   }
  60.  
  61.   //unsent folder
  62.   i = SD.isDir(unsentFolder);
  63.   //USB.println(i);
  64.   if(i == 1) {}    //the folder exists. we are ok
  65.   else if(i == 0){  //something exists, but is not a folder. so delete it and create a new folder
  66.     if( !SD.del(unsentFolder) ) USB.println("Something with the un-sent folder name appears in the SD card and cannot be deleted.");
  67.     i = 0;    //set the flag for creating the folder
  68.   }
  69.   else if(i == -1) i = 0;  //it does not exists
  70.  
  71.   if(i == 0){  //create the folder
  72.     if( !SD.mkdir(unsentFolder) ) USB.println("The un-sent folder doesn't exists and couldn't be created.");
  73.   }
  74.   GPRS_Pro.ON();
  75.   USB.println("GPRS Pro turned on.");
  76.   while(!GPRS_Pro.setPIN("6785"));
  77.   USB.println("GPRS Pro PIN set.");
  78.   while(!GPRS_Pro.check());
  79.   USB.println("GPRS_Pro connected to the network");
  80.   GPRS_Pro.getCellInfo();
  81.   USB.print("RSSI: ");
  82.   USB.println(GPRS_Pro.RSSI);
  83.   USB.print("Cell ID: ");
  84.   USB.println(GPRS_Pro.cellID);
  85.   if(GPRS_Pro.configureGPRS_HTTP_FTP(1)) USB.println("Configuration OK");
  86.   else USB.println("Configuration failed");
  87. }
  88.  
  89. /**
  90.   Converts the passed date and time to seconds, unix like, ie passed seconds from epoch time, where our epoch time is 20000101 000000
  91.   Returns the seconds which have elapsed since our epoch time
  92. */
  93. long convertTimeToSec(char *xDate, char *xTime){
  94.   char dy[3] = "", mn[3] = "", yr[3] = "", hr[3] = "", mint[3] = "", sc[3] = "";
  95.   long timeInSec = 0;
  96.  
  97.   //get the date
  98.   i = 0;
  99.   len = strlen(xDate);
  100.   for(i = 0; i < len; i++){
  101.     if(i < 2) dy[i] = xDate[i];
  102.     else if(i > 1 && i < 4) mn[i-2] = xDate[i];
  103.     else if(i > 3 && i < 6) yr[i-4] = xDate[i];
  104.   }
  105.   //get the time
  106.   i = 0;
  107.   len = strlen(xTime);
  108.   for(i = 0; i < len; i++){
  109.     if(i < 2) hr[i] = xTime[i];
  110.     else if(i > 1 && i < 4) mint[i-2] = xTime[i];
  111.     else if(i > 3 && i < 6) sc[i-4] = xTime[i];
  112.   }
  113.              
  114.   USB.println("Received Data: loopDate - loopTime:");
  115.   USB.print(GPS.dateGPS); USB.print(" - ");
  116.   USB.print(GPS.timeGPS); USB.print("\n");
  117.  
  118.   USB.print("Broken Down: yr-month-date hr-minutes-seconds: ");
  119.   USB.print(yr); USB.print("-");
  120.   USB.print(mn); USB.print("-");
  121.   USB.print(dy); USB.print(" ");
  122.   USB.print(hr); USB.print("-");
  123.   USB.print(mint); USB.print("-");
  124.   USB.println(sc);
  125.  
  126.   //timeInSec += atol(sc);
  127.   //USB.print("Seconds: ");
  128.   //USB.println(timeInSec);
  129.   timeInSec += atol(mint);
  130.   USB.print("Minutes: ");
  131.   USB.println(timeInSec);
  132.   timeInSec += (atol(hr) * (60) );
  133.   USB.print("Hours: ");
  134.   USB.println(timeInSec);
  135.  
  136.   timeInSec += (atol(dy) * (24*60) );
  137.   USB.print("Days: ");
  138.   USB.println(timeInSec);
  139.   timeInSec += (atol(mn) * (30*24*60) );    //An plain assumption that each month has 30days
  140.   USB.print("Months: ");
  141.   USB.println(timeInSec);
  142.   USB.println(strtod(mn, &tmp) * (30*24*60));
  143.   timeInSec += (atol(yr) * (365*24*60) );    //An plain assumption that each year has 365days
  144.   USB.print("Years: ");
  145.   USB.println(timeInSec);
  146.        
  147.   USB.print("Final: ");
  148.   USB.println(timeInSec);
  149.   return timeInSec;
  150. }
  151.  
  152. void loop(){
  153.   //declare the variables
  154.   //USB.print("5");
  155.   len, i, j = 0;
  156.   char degree[4] = "", minutes[8] = "";
  157.   uint8_t temperature = 0;
  158.   int8_t fileFound = 0;
  159.   byte accOk;
  160.   //USB.print("6");
  161.  
  162.   //check if the GPS has connected to the satellite
  163.   //USB.print("7");
  164.   if(iterations == 0){
  165.     USB.print(RTC.getTime());
  166.   }
  167.   //USB.print("8");
  168.  
  169.  
  170.   USB.println("Try and read the net.");
  171.   USB.println(GPRS_Pro.readURL("www.google.co.ke",1));
  172.   USB.println(GPRS_Pro.data_URL);
  173.  
  174.   if(!gpsConnected){
  175.     USB.println(" Warning: GPS is yet to get a satellite fix...");
  176.   }
  177.   else{
  178.     USB.println(" Info: GPS has a satellite fix....");
  179.     //USB.println(GPS.getRaw(80));  //get the raw NMEA data
  180.     // Getting Date and Time
  181.     GPS.getPosition();
  182.     USB.print("Date and Time: ");
  183.     USB.print(GPS.timeGPS);
  184.     USB.print(" ");
  185.     USB.println(GPS.dateGPS);
  186.  
  187.     // Getting Coordinates
  188.     //convert the latitude to dd.dd
  189. //    i = 0; j = 0; tmp = "";
  190. //    len = strlen(latitude);
  191. //    for(i = 0; i < len; i++){
  192. //      if(i < 2){
  193. //        degree[i] = latitude[i];
  194. //      }
  195. //      else{
  196. //        minutes[j] = latitude[i];
  197. //        j++;
  198. //      }
  199. //    }
  200. //    latitude_dd = strtod(degree, &tmp) + (strtod(minutes, &tmp)/60);
  201. //    
  202. //    //convert the longitude to dd.dd
  203. //    degree[0] = '\0'; minutes[0] = '\0';
  204. //    i = 0; j = 0; tmp = "";
  205. //    len = strlen(longitude);
  206. //    for(i = 0; i < len; i++){
  207. //      if(i < 3){
  208. //        degree[i] = longitude[i];
  209. //      }
  210. //      else{
  211. //        minutes[j] = longitude[i];
  212. //        j++;
  213. //      }
  214. //    }
  215. //    longitude_dd = strtod(degree, &tmp) + (strtod(minutes, &tmp)/60);
  216.    
  217.     USB.print("Raw -- Lat, Long, Alt: ");
  218.     USB.print(GPS.latitude);
  219.     USB.print(", ");
  220.     USB.print(GPS.longitude);
  221.     USB.print(", ");
  222.     USB.println(GPS.altitude);
  223.    
  224. //    USB.print("Converted -- Lat, Long, Alt: ");
  225. //    USB.print(latitude_dd);
  226. //    USB.print(", ");
  227. //    USB.print(longitude_dd);
  228. //    USB.print(", ");
  229. //    USB.println(GPS.altitude);
  230.    
  231.     //Get the speed
  232.     USB.print("Speed: ");
  233.     USB.println(GPS.speed);
  234.  
  235.     // Getting Course
  236.     USB.print("Course: ");
  237.     USB.println(GPS.course);
  238.     iterationsWithGPS++;
  239.   }
  240.    
  241.   //lets set some time in the format YY:MM:DD:dow:hh:mm:ss
  242.   if(gpsTimeSet != 1){
  243.     if(gpsConnected){
  244.       //RTC.setTimeFromGPS();    //Since we have a GPS connection, lets set the time from the satellites!
  245.       //save this time(in seconds) in our startTime variable
  246.       char curTime[6] = "";
  247. //      startTime = convertTimeToSec(loopDate, loopTime);
  248.       gpsTimeSet = 1;
  249.     }
  250.     else{
  251.       if(iterations == 0){
  252.         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
  253.       }
  254.     }
  255.   }
  256.  
  257.   //Check the proper functionality of the accelerometer: should always answer 0x3A if all is ok
  258.   accOk = ACC.check();
  259.   USB.println("");
  260.   USB.print(RTC.getTime());
  261.   int16_t accX, accY, accZ;
  262.   if(accOk != 0x3A){
  263.     USB.print(": Warning: Accelerometer not ok!\n");
  264.     USB.println(accOk, HEX);
  265.   }
  266.   else{
  267.     USB.print(": Info: Accelerometer is ok.\n");
  268.     //good, now lets record the acceleration on the x, y and z axis
  269.     accX = ACC.getX();
  270.     accY = ACC.getY();
  271.     accZ = ACC.getZ();
  272.     USB.print(RTC.getTime());
  273.     USB.print(" Acceleration on the X, Y and Z axis: ");
  274.     USB.print(accX, DEC);
  275.     USB.print(", ");
  276.     USB.print(accY, DEC);
  277.     USB.print(" ");
  278.     USB.println(accZ, DEC);
  279.   }
  280.  
  281.   //lets see if the built in temp sensors are ok
  282.   USB.println("");
  283.   USB.print(GPS.timeGPS);
  284.   USB.print(": Info Temperature: ");
  285.   temperature = RTC.getTemperature();
  286.   USB.println(temperature, DEC);
  287.  
  288.   // Show the remaining battery level
  289.   int battLevel = PWR.getBatteryLevel();
  290.   USB.print("Battery Level: ");
  291.   USB.print(battLevel,DEC);
  292.   USB.println("%\n");
  293.  
  294.   //check if its time to upload
  295.   iterations++;
  296.   USB.print("Iterations, IterationsWithGPS: ");
  297.   USB.print(iterations, DEC);
  298.   USB.print(", ");
  299.   USB.println(iterationsWithGPS, DEC);
  300.  
  301.   if((iterations % iterationsPerFile) == 0 || iterations == 1){
  302.       //USB.println("1");
  303.       tmp_b = (char *)malloc(sizeof(char) * 200);  //for the long messages
  304.       //USB.println("2");
  305.       if(tmp_b == NULL) USB.println("Couldn't allocate memory.");
  306.       if(tmp_c != NULL) free(tmp_c);
  307.       tmp_c = (char *)malloc(sizeof(char) * 30);    //for the file names and short messages
  308.       //USB.println("3");
  309.       if(tmp_c == NULL) USB.println("Couldn't allocate memory.");
  310.       tiny_i = 0;
  311.       //USB.println("4");
  312.      
  313.       if(curLocation != unsentFolder){  //cd into the un-sent folder directory, if we are already not there
  314.         //snprintf(tmp_c, 30, "%s%s", "./", unsentFolder);
  315.         if( !SD.cd(unsentFolder) ) USB.println("Could not cd into the unsent folder.");
  316.         else curLocation = unsentFolder;
  317.         //USB.println( SD.ls() );
  318.       }
  319.      
  320.       if(gpsConnected){    //create a file with the date and time as the name
  321.         snprintf(tmp_c, 30, "%s%s%s%s", GPS.dateGPS, "_", GPS.timeGPS, ".txt");
  322.         tiny_i = 1;
  323.       }
  324.       else{  //create a file with the iterations
  325.         for(i = 0; i < 1000; i++){
  326.           //create the name of the file
  327.           snprintf(tmp_c, 30, "%s%d%s", fileTemplate, i, ".txt");
  328.           //check if the file exists or not
  329.           tiny_j = SD.isFile(tmp_c);
  330.           if(tiny_j == 1){ /*The file already exists*/ }
  331.           else if(tiny_j == -1){    //we need to create the file
  332.             tiny_i = 1;
  333.             break;
  334.           }
  335.           else if(tiny_j == 0){
  336.             USB.print("Found something but is not a file -- ");
  337.             USB.println(tmp_c);
  338.           }
  339.         }
  340.       }
  341.      
  342.       free(tmp_b);
  343.       if(tiny_i == 0) free(tmp_c);
  344.       else if(tiny_i == 1){  //we need to create a file
  345.         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");
  346.         USB.println(tmp_b);
  347.         //USB.println( SD.ls() );
  348.         tiny_j = SD.create(tmp_c);
  349.         if(tiny_j == 1){
  350.           USB.println("The file was created successfully.\n");
  351.           curFile = tmp_c;
  352.         }
  353.         else if(tiny_j == 0) USB.println("The file was not created, we are going to use the old file. Iko shida!\n");
  354.       }
  355.       //if(SD.cd("..")) USB.println("Getting out of the unsent folder.");
  356.   }
  357.   USB.print("Current File: /");
  358.   USB.print(curLocation);
  359.   USB.print("/");
  360.   USB.println(curFile);
  361.  
  362.   tmp_a = (char *)malloc(sizeof(char) * 200);  
  363.   if(gpsConnected){
  364.     //all is good, now lets save the kawaida data to the SD card. We are going to create a string like
  365.     //{dt:date,tm:time,lt:latitude,ln:longitude,al:altitude,cs:course,tp:temperature,ax:accX,ay:accY,az:accZ,bl:batteryLevel,}
  366.     if(tmp_a == NULL) USB.println("Couldn't allocate memory.");
  367.     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");
  368.   }
  369.   else{    //we are gonna save all the other params without the GPS data
  370.     if(tmp_a == NULL) USB.println("Couldn't allocate memory.");
  371.     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");
  372.   }
  373.   USB.println(tmp_a);
  374.   SD.appendln(curFile, tmp_a);
  375.   free(tmp_a);
  376.  
  377.   //USB.print("1");
  378.   if(iterations % iterationsBeforeUpload == 0){  //lets try and upload data to the server
  379.     USB.println("\n\nThis is where we are uploading some data\n");
  380.     //i = SD.numFiles();
  381.     //i++;
  382.     tmp_a = (char *)malloc(sizeof(char) * 30);
  383.     //char *tmp_d = (char *)malloc(sizeof(char) * 30);
  384.     USB.println("Allocated memory.");
  385.    
  386.     //if(i < 0)
  387.     i = 300;   //we have some errors while reading the folder, lets assume the number of files that we might be having
  388.     for(j = 2; j < i; j++){
  389.       tmp = SD.ls(j, 1, NAMES);
  390.       USB.print(tmp);
  391.       tmp_a = "";
  392.       snprintf(tmp_a, 30, "%s%s", "/unsent/", tmp);
  393.       USB.print(tmp_a);
  394.       GPRS_Pro.uploadFile(tmp, tmp, "username", "password", "ip-address", "21",1);
  395.       USB.println("uploaded well");
  396.     }
  397.    
  398.     //GPRS_Pro.OFF();
  399.     //USB.println("GPRS Pro turned off.");
  400.     free(tmp_a);
  401.     //free(tmp_d);
  402.     USB.println("Freed memory.");
  403.     //check if there is need to upload the data
  404.     //    long curTime = convertTimeToSec(loopDate, loopTime);
  405.     //    if(curTime - startTime >= (loopTimeBeforeUpload * (uploadCount+1)) ){  
  406.     //      USB.println("\n\nThis is where we are uploading some data\n");
  407.     //      uploadCount++;
  408.     //    }
  409.   }
  410.   //USB.print("3");
  411.   delay(500);  //sleep for 5secs
  412.   //USB.print("4");
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement