Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino_MKRENV.h>
- #include <SPI.h>
- #include <SD.h>
- const String sdFile = "December.dat";
- float humidity, lux, pressure, temperature, uva, uvb, uvi;
- File filePointer;
- void setup() {
- // initialize serial and wait for port to open
- Serial.begin(9600);
- while(!Serial) {
- ; // wait for serial port to connect for monitoring/debugging
- }
- // connect to environmental shield
- if(!ENV.begin()) {
- Serial.println("Failed to initialize MKR ENV shield!");
- while(1); // this loops setup() instead of going to loop()
- }
- // connect to SD card
- if(!SD.begin(SD_CHIP_SELECT_PIN)) {
- Serial.println("Failed to mount SD card!");
- return;
- }
- /*
- Sd2Card card;
- Serial.println("Initializing SD card...");
- if(!card.init(SPI_HALF_SPEED, SD_CHIP_SELECT_PIN)) {
- Serial.println("Could not connect to SD card. See https://tigoe.github.io/DataloggingExamples/mkr-datalogging.html");
- while (1);
- }
- */
- }
- void loop() {
- // get the sensor values
- humidity = float(ENV.readHumidity());
- lux = float(ENV.readIlluminance());
- pressure = float(ENV.readPressure());
- temperature = float(ENV.readTemperature());
- uva = abs(float(ENV.readUVA()));
- uvb = abs(float(ENV.readUVB()));
- uvi = float(ENV.readUVIndex());
- save();
- delay(5000);
- }
- void save() {
- // datatype cast int->string
- String humidityStr = String(humidity);
- String luxStr = String(lux);
- String pressureStr = String(pressure);
- String temperatureStr = String(temperature);
- String uvaStr = String(uva);
- String uvbStr = String(uvb);
- String uviStr = String(uvi);
- String logData = humidityStr + ", " + luxStr + ", " + pressureStr + ", " + temperatureStr + ", " + uvaStr + ", " + uvbStr + ", " + uvi;
- Serial.println(logData);
- filePointer = SD.open(sdFile, FILE_WRITE);
- if(filePointer) {
- filePointer.println(logData);
- filePointer.close();
- blink();
- }
- else {
- Serial.println("Could not open file!");
- }
- }
- void debug() {
- // create labels for output
- String outHumidity = "Humidity: ";
- String outLux = "Lux: ";
- String outPressure = "Pressure: ";
- String outTemperature = "Temperature: ";
- String outUVa = "UVa: ";
- String outUVb = "UVb: ";
- String outUVi = "UVIndex: ";
- // report sensor information
- Serial.println(outHumidity + humidity + "% rh");
- Serial.println(outLux + lux + " lux"); // verify that 1600 is valid for lux/lumens
- Serial.println(outPressure + pressure); // 101 what?
- Serial.println(outTemperature + temperature + " celcius");
- Serial.println(outUVa + uva + " nm");
- Serial.println(outUVb + uvb + " nm");
- Serial.println(outUVi + uvi);
- Serial.println();
- }
- void sdinfo() {
- SdVolume volume;
- SdFile root;
- Sd2Card card;
- // print the type of card
- Serial.println();
- Serial.print("Card type: ");
- switch (card.type()) {
- case SD_CARD_TYPE_SD1:
- Serial.println("SD1");
- break;
- case SD_CARD_TYPE_SD2:
- Serial.println("SD2");
- break;
- case SD_CARD_TYPE_SDHC:
- Serial.println("SDHC");
- break;
- default:
- Serial.println("Unknown");
- }
- // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
- if (!volume.init(card)) {
- Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
- while (1);
- }
- Serial.print("Clusters: ");
- Serial.println(volume.clusterCount());
- Serial.print("Blocks x Cluster: ");
- Serial.println(volume.blocksPerCluster());
- Serial.print("Total Blocks: ");
- Serial.println(volume.blocksPerCluster() * volume.clusterCount());
- Serial.println();
- // print the type and size of the first FAT-type volume
- uint32_t volumesize;
- Serial.print("Volume type is: FAT");
- Serial.println(volume.fatType(), DEC);
- volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
- volumesize *= volume.clusterCount(); // we'll have a lot of clusters
- volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
- Serial.print("Volume size (Kb): ");
- Serial.println(volumesize);
- Serial.print("Volume size (Mb): ");
- volumesize /= 1024;
- Serial.println(volumesize);
- Serial.print("Volume size (Gb): ");
- Serial.println((float)volumesize / 1024.0);
- Serial.println("\nFiles found on the card (name, date and size in bytes): ");
- root.openRoot(volume);
- // list all files in the card with date and size
- root.ls(LS_R | LS_DATE | LS_SIZE);
- }
- void blink() {
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(250); // wait for a second
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement