1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
#include <Wire.h> #include "Sodaq_DS3231.h" #include <SPI.h> #include <SD.h> #include "TSL2561.h" TSL2561 tsl(TSL2561_ADDR_LOW); #define SD_SS_PIN 12 #define FILE_NAME "Datafile.txt" //Data header #define LOGGERNAME "Mayfly Data Logger test file" #define DATA_HEADER "DateTime_EST,Loggertime,BoardTempC,BatteryV,Lum_IR,Lum_Full,Lum_Vis" String dataRec = ""; int currentminute; long currentepochtime = 0; float boardtemp = 0.0; int batteryPin = A6; // select the input pin for the potentiometer int batterysenseValue = 0; // variable to store the value coming from the sensor float batteryvoltage; void setup () { Serial.begin(57600); Serial.println("Mayfly Workshop Multi-Sensor & microSD Logger"); pinMode(8, OUTPUT); pinMode(9, OUTPUT); greenred4flash(); //blink the LEDs to show the board is on pinMode(22, OUTPUT); digitalWrite(22, HIGH); Wire.begin(); rtc.begin(); setupLogFile(); if (tsl.begin()) { } else { while (1); } tsl.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations) tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light) } void loop () { dataRec = createDataRecord(); sensormeasurements(); logData(dataRec); Serial.print("Data Record: "); Serial.println(dataRec); String dataRec = ""; delay(3000); } void setupLogFile() { //Initialise the SD card if (!SD.begin(SD_SS_PIN)) { Serial.println("Error: SD card failed to initialise or is missing."); //Hang // while (true); } //Check if the file already exists bool oldFile = SD.exists(FILE_NAME); //Open the file in write mode File logFile = SD.open(FILE_NAME, FILE_WRITE); //Add header information if the file did not already exist if (!oldFile) { logFile.println(LOGGERNAME); logFile.println(DATA_HEADER); } //Close the file to save it logFile.close(); } void logData(String rec) { //Re-open the file File logFile = SD.open(FILE_NAME, FILE_WRITE); //Write the CSV data logFile.println(rec); //Close the file to save it logFile.close(); } String createDataRecord() { //Create a String type data record in csv format String data = getDateTime(); data += ","; rtc.convertTemperature(); //convert current temperature into registers boardtemp = rtc.getTemperature(); //Read temperature sensor value batterysenseValue = analogRead(batteryPin); batteryvoltage = (3.3/1023.) * 1.47 * batterysenseValue; data += currentepochtime; data += ","; addFloatToString(data, boardtemp, 3, 1); //float data += ","; addFloatToString(data, batteryvoltage, 4, 2); // Serial.print("Data Record: "); // Serial.println(data); return data; } static void addFloatToString(String & str, float val, char width, unsigned char precision) { char buffer[10]; dtostrf(val, width, precision, buffer); str += buffer; } void sensormeasurements() { uint32_t lum = tsl.getFullLuminosity(); uint16_t ir, full, vis; ir = lum >> 16; full = lum & 0xFFFF; vis = full - ir; dataRec += ","; dataRec += ir; dataRec += ","; dataRec += full; dataRec += ","; dataRec += vis; } String getDateTime() { String dateTimeStr; //Create a DateTime object from the current time DateTime dt(rtc.makeDateTime(rtc.now().getEpoch())); currentepochtime = (dt.get()); //Unix time in seconds currentminute = (dt.minute()); //Convert it to a String dt.addToString(dateTimeStr); return dateTimeStr; } uint32_t getNow() { currentepochtime = rtc.now().getEpoch(); return currentepochtime; } void greenred4flash() { for (int i=1; i <= 4; i++){ digitalWrite(8, HIGH); digitalWrite(9, LOW); delay(50); digitalWrite(8, LOW); digitalWrite(9, HIGH); delay(50); } digitalWrite(9, LOW); } |