Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #include <Arduino_MKRENV.h>
  2. #include <SPI.h>
  3. #include <SD.h>
  4.  
  5. const String sdFile = "December.dat";
  6. float humidity, lux, pressure, temperature, uva, uvb, uvi;
  7. File filePointer;
  8.  
  9. void setup() {
  10. // initialize serial and wait for port to open
  11. Serial.begin(9600);
  12. while(!Serial) {
  13. ; // wait for serial port to connect for monitoring/debugging
  14. }
  15.  
  16. // connect to environmental shield
  17. if(!ENV.begin()) {
  18. Serial.println("Failed to initialize MKR ENV shield!");
  19. while(1); // this loops setup() instead of going to loop()
  20. }
  21.  
  22. // connect to SD card
  23. if(!SD.begin(SD_CHIP_SELECT_PIN)) {
  24. Serial.println("Failed to mount SD card!");
  25. return;
  26. }
  27. /*
  28. Sd2Card card;
  29. Serial.println("Initializing SD card...");
  30. if(!card.init(SPI_HALF_SPEED, SD_CHIP_SELECT_PIN)) {
  31. Serial.println("Could not connect to SD card. See https://tigoe.github.io/DataloggingExamples/mkr-datalogging.html");
  32. while (1);
  33. }
  34. */
  35. }
  36.  
  37. void loop() {
  38. // get the sensor values
  39. humidity = float(ENV.readHumidity());
  40. lux = float(ENV.readIlluminance());
  41. pressure = float(ENV.readPressure());
  42. temperature = float(ENV.readTemperature());
  43. uva = abs(float(ENV.readUVA()));
  44. uvb = abs(float(ENV.readUVB()));
  45. uvi = float(ENV.readUVIndex());
  46.  
  47. save();
  48. delay(5000);
  49. }
  50.  
  51. void save() {
  52. // datatype cast int->string
  53. String humidityStr = String(humidity);
  54. String luxStr = String(lux);
  55. String pressureStr = String(pressure);
  56. String temperatureStr = String(temperature);
  57. String uvaStr = String(uva);
  58. String uvbStr = String(uvb);
  59. String uviStr = String(uvi);
  60.  
  61. String logData = humidityStr + ", " + luxStr + ", " + pressureStr + ", " + temperatureStr + ", " + uvaStr + ", " + uvbStr + ", " + uvi;
  62. Serial.println(logData);
  63.  
  64. filePointer = SD.open(sdFile, FILE_WRITE);
  65. if(filePointer) {
  66. filePointer.println(logData);
  67. filePointer.close();
  68. blink();
  69. }
  70. else {
  71. Serial.println("Could not open file!");
  72. }
  73.  
  74.  
  75. }
  76.  
  77. void debug() {
  78. // create labels for output
  79. String outHumidity = "Humidity: ";
  80. String outLux = "Lux: ";
  81. String outPressure = "Pressure: ";
  82. String outTemperature = "Temperature: ";
  83. String outUVa = "UVa: ";
  84. String outUVb = "UVb: ";
  85. String outUVi = "UVIndex: ";
  86.  
  87. // report sensor information
  88. Serial.println(outHumidity + humidity + "% rh");
  89. Serial.println(outLux + lux + " lux"); // verify that 1600 is valid for lux/lumens
  90. Serial.println(outPressure + pressure); // 101 what?
  91. Serial.println(outTemperature + temperature + " celcius");
  92. Serial.println(outUVa + uva + " nm");
  93. Serial.println(outUVb + uvb + " nm");
  94. Serial.println(outUVi + uvi);
  95. Serial.println();
  96. }
  97.  
  98. void sdinfo() {
  99. SdVolume volume;
  100. SdFile root;
  101. Sd2Card card;
  102.  
  103. // print the type of card
  104. Serial.println();
  105. Serial.print("Card type: ");
  106. switch (card.type()) {
  107. case SD_CARD_TYPE_SD1:
  108. Serial.println("SD1");
  109. break;
  110. case SD_CARD_TYPE_SD2:
  111. Serial.println("SD2");
  112. break;
  113. case SD_CARD_TYPE_SDHC:
  114. Serial.println("SDHC");
  115. break;
  116. default:
  117. Serial.println("Unknown");
  118. }
  119.  
  120. // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  121. if (!volume.init(card)) {
  122. Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
  123. while (1);
  124. }
  125.  
  126. Serial.print("Clusters: ");
  127. Serial.println(volume.clusterCount());
  128. Serial.print("Blocks x Cluster: ");
  129. Serial.println(volume.blocksPerCluster());
  130.  
  131. Serial.print("Total Blocks: ");
  132. Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  133. Serial.println();
  134.  
  135. // print the type and size of the first FAT-type volume
  136. uint32_t volumesize;
  137. Serial.print("Volume type is: FAT");
  138. Serial.println(volume.fatType(), DEC);
  139.  
  140. volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
  141. volumesize *= volume.clusterCount(); // we'll have a lot of clusters
  142. volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
  143. Serial.print("Volume size (Kb): ");
  144. Serial.println(volumesize);
  145. Serial.print("Volume size (Mb): ");
  146. volumesize /= 1024;
  147. Serial.println(volumesize);
  148. Serial.print("Volume size (Gb): ");
  149. Serial.println((float)volumesize / 1024.0);
  150.  
  151. Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  152. root.openRoot(volume);
  153.  
  154. // list all files in the card with date and size
  155. root.ls(LS_R | LS_DATE | LS_SIZE);
  156. }
  157.  
  158. void blink() {
  159. digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  160. delay(250); // wait for a second
  161. digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement