Advertisement
soloincc

waspmote

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