Home › Forums › Mayfly Data Logger › Calculated variable pointers preventing logging to SD › Reply To: Calculated variable pointers preventing logging to SD
2019-02-24 at 12:42 AM
#12820
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
// =============================================================================================================================== // Include the base required libraries // =============================================================================================================================== #include <Arduino.h> // The base Arduino library #include <EnableInterrupt.h> // for external and pin change interrupts #include <SensorBase.h> #include <OneWire.h> // =============================================================================================================================== // Data Logger Settings // =============================================================================================================================== // The library version this example was written for const char *libraryVersion = "0.19.6"; // The name of this file const char *sketchName = "tbshtnocell.ino"; // Logger ID, also becomes the prefix for the name of the data file on SD card const char *LoggerID = "nocell4"; // How frequently (in minutes) to log data const uint8_t loggingInterval = 2; // Your logger's timezone. const int8_t timeZone = -5; // Eastern Standard Time // NOTE: Daylight savings time will not be applied! Please use standard time! // ================================================================================================================================ // Primary Arduino-Based Board and Processor // ================================================================================================================================ #include <sensors/ProcessorStats.h> const long serialBaud = 115200; // Baud rate for the primary serial port for debugging const int8_t greenLED = 8; // MCU pin for the green LED (-1 if not applicable) const int8_t redLED = 9; // MCU pin for the red LED (-1 if not applicable) const int8_t buttonPin = 21; // MCU pin for a button to use to enter debugging mode (-1 if not applicable) const int8_t wakePin = A7; // MCU interrupt/alarm pin to wake from sleep // Set the wake pin to -1 if you do not want the main processor to sleep. // In a SAMD system where you are using the built-in rtc, set wakePin to 1 const int8_t sdCardPin = 12; // MCU SD card chip select/slave select pin (must be given!) const int8_t sensorPowerPin = 22; // MCU pin controlling main sensor power (-1 if not applicable) // Create and return the main processor chip "sensor" - for general metadata const char *mcuBoardVersion = "v0.5b"; ProcessorStats mcuBoard(mcuBoardVersion); // ================================================================================================================================ // Maxim DS3231 RTC (Real Time Clock) // ================================================================================================================================ #include <sensors/MaximDS3231.h> // Create and return the DS3231 sensor object MaximDS3231 ds3231(1); // ================================================================================================================================ // Bosch BME280 Environmental Sensor (Temperature, Humidity, Pressure) // ================================================================================================================================ #include <sensors/BoschBME280.h> const int8_t I2CPower = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected) uint8_t BMEi2c_addr = 0x77; // The BME280 can be addressed either as 0x77 (Adafruit default) or 0x76 (Grove default) // Either can be physically mofidied for the other address const uint8_t bme280ReadingsToAvg = 33; // Create and return the Bosch BME280 sensor object BoschBME280 bme280(I2CPower, BMEi2c_addr, bme280ReadingsToAvg); // Create the four variable objects for the BME280 and return variable-type pointers to them // Use these to create variable pointers with names to use in multiple arrays or any calculated variables. Variable *bme280Humid = new BoschBME280_Humidity(&bme280); Variable *bme280Temp = new BoschBME280_Temp(&bme280); Variable *bme280Press = new BoschBME280_Pressure(&bme280); // ================================================================================================================================ // Maxim DS18 One Wire Temperature Sensor // ================================================================================================================================ #include <sensors/MaximDS18.h> // OneWire Address [array of 8 hex characters] const int8_t OneWireBus = 4; // Pin attached to the OneWire Bus (-1 if unconnected) const int8_t OneWirePower = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected) const int8_t MaxDS18ReadingsToAvg = 33; // Create and return the Maxim DS18 sensor object (use this form for a single sensor on bus with an unknown address) MaximDS18 ds18_u(OneWirePower, OneWireBus, MaxDS18ReadingsToAvg); // Create the temperature variable object for the DS18 and return a variable-type pointer to it // Use this to create a variable pointer with a name to use in multiple arrays or any calculated variables. Variable *ds18Temp = new MaximDS18_Temp(&ds18_u); // ================================================================================================================================ // MeaSpecMS5803 (Pressure, Temperature) // ================================================================================================================================ #include <sensors/MeaSpecMS5803.h> //const int8_t I2CPower = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected) const uint8_t MS5803i2c_addr = 0x76; // The MS5803 can be addressed either as 0x76 (default) or 0x77 const int16_t MS5803maxPressure = 14; // The maximum pressure measurable by the specific MS5803 model const uint8_t MS5803ReadingsToAvg = 33; // Create and return the MeaSpec MS5803 pressure and temperature sensor object MeaSpecMS5803 ms5803(I2CPower, MS5803i2c_addr, MS5803maxPressure, MS5803ReadingsToAvg); // Create the conductivity and temperature variable objects for the ES2 and return variable-type pointers to them // Use these to create variable pointers with names to use in multiple arrays or any calculated variables. Variable *ms5803Press = new MeaSpecMS5803_Pressure(&ms5803); Variable *ms5803Temp = new MeaSpecMS5803_Temp(&ms5803); // ================================================================================================================================ // Maxbotix HRXL Ultrasonic Range Finder // ================================================================================================================================ //#include <sensors/MaxBotixSonar.h> // Create a reference to the serial port for the sonar // A Maxbotix sonar with the trigger pin disconnect CANNOT share the serial port // A Maxbotix sonar using the trigger may be able to share but YMMV // Extra hardware and software serial ports are created in the "Settings for Additional Serial Ports" section //AltSoftSerial &sonarSerial = altSoftSerial; // For software serial if needed //const int8_t SonarPower = sensorPowerPin; // Excite (power) pin (-1 if unconnected) //const int8_t Sonar1Trigger = 6; // Trigger pin (a unique negative number if unconnected) // Create and return the MaxBotix Sonar sensor object //MaxBotixSonar sonar1(sonarSerial, SonarPower, Sonar1Trigger); // Create the voltage variable object and return a variable-type pointer to it //Variable *sonar1Range = new MaxBotixSonar_Range(&sonar1); // ========================================================================== // Calculated Variables // ========================================================================== // Create any calculated variables you want here // Create the function to calculate the water pressure // Water pressure = pressure from MS5803 (water+baro) - pressure from BME280 (baro) // The MS5803 reports pressure in millibar, the BME280 in pascal // 1 pascal = 0.01 mbar float calculateWaterPressure(void) { float totalPressureFromMS5803 = ms5803Press->getValue(); float baroPressureFromBME280 = bme280Press->getValue(); float waterPressure = totalPressureFromMS5803 - (baroPressureFromBME280)*0.01; if (totalPressureFromMS5803 == -9999 || baroPressureFromBME280 == -9999) waterPressure = -9999; // Serial.print(F("Water pressure is ")); // for debugging // Serial.println(waterPressure); // for debugging return waterPressure; } // Properties of the calculated water pressure variable const char *waterPressureVarName = "pressureGauge"; // This must be a value from http://vocabulary.odm2.org/variablename/ const char *waterPressureVarUnit = "millibar"; // This must be a value from http://vocabulary.odm2.org/units/ int waterPressureVarResolution = 3; const char *waterPressureUUID = "12345678-abcd-1234-efgh-1234567890ab"; const char *waterPressureVarCode = "CorrectedPressure"; // Create the calculated water pressure variable objects and return a variable pointer to it Variable *calcWaterPress = new Variable(calculateWaterPressure, waterPressureVarName, waterPressureVarUnit, waterPressureVarResolution, waterPressureUUID, waterPressureVarCode); // Create the function to calculate the "raw" water depth // For this, we're using the conversion between mbar and mm pure water at 4°C // This calculation gives a final result in mm of water float calculateWaterDepthRaw(void) { float waterDepth = calculateWaterPressure()*10.1972; if (calculateWaterPressure() == -9999) waterDepth = -9999; // Serial.print(F("'Raw' water depth is ")); // for debugging // Serial.println(waterDepth); // for debugging return waterDepth; } // Properties of the calculated water depth variable const char *waterDepthVarName = "waterDepth"; // This must be a value from http://vocabulary.odm2.org/variablename/ const char *waterDepthVarUnit = "millimeter"; // This must be a value from http://vocabulary.odm2.org/units/ int waterDepthVarResolution = 3; const char *waterDepthUUID = "12345678-abcd-1234-efgh-1234567890ab"; const char *waterDepthVarCode = "CalcDepth"; // Create the calculated raw water depth variable objects and return a variable pointer to it Variable *calcRawDepth = new Variable(calculateWaterDepthRaw, waterDepthVarName, waterDepthVarUnit, waterDepthVarResolution, waterDepthUUID, waterDepthVarCode); // Create the function to calculate the water depth after correcting water density for temperature // This calculation gives a final result in mm of water float calculateWaterDepthTempCorrected(void) { const float gravitationalConstant = 9.80665; // m/s2, meters per second squared // First get water pressure in Pa for the calculation: 1 mbar = 100 Pa float waterPressurePa = 100 * calculateWaterPressure(); float waterTempertureC = ms5803Temp->getValue(); // Converting water depth for the changes of pressure with depth // Water density (kg/m3) from equation 6 from JonesHarris1992-NIST-DensityWater.pdf float waterDensity = + 999.84847 + 6.337563e-2 * waterTempertureC - 8.523829e-3 * pow(waterTempertureC,2) + 6.943248e-5 * pow(waterTempertureC,3) - 3.821216e-7 * pow(waterTempertureC,4) ; // This calculation gives a final result in mm of water // from P = rho * g * h float rhoDepth = 1000 * waterPressurePa/(waterDensity * gravitationalConstant); if (calculateWaterPressure() == -9999 || waterTempertureC == -9999) rhoDepth = -9999; // Serial.print(F("Temperature corrected water depth is ")); // for debugging // Serial.println(rhoDepth); // for debugging return rhoDepth; } // Properties of the calculated temperature corrected water depth variable const char *rhoDepthVarName = "waterDepth"; // This must be a value from http://vocabulary.odm2.org/variablename/ const char *rhoDepthVarUnit = "millimeter"; // This must be a value from http://vocabulary.odm2.org/units/ int rhoDepthVarResolution = 3; const char *rhoDepthUUID = "12345678-abcd-1234-efgh-1234567890ab"; const char *rhoDepthVarCode = "DensityDepth"; // Create the temperature corrected water depth variable objects and return a variable pointer to it Variable *calcCorrDepth = new Variable(calculateWaterDepthTempCorrected, rhoDepthVarName, rhoDepthVarUnit, rhoDepthVarResolution, rhoDepthUUID, rhoDepthVarCode); // =============================================================================================================================== // Creating the Variable Array and filling with Variable Objects // =============================================================================================================================== #include <VariableArray.h> // FORM1: Create pointers for all of the variables from the sensors, // at the same time putting them into an array Variable *variableList[] = { new ProcessorStats_SampleNumber(&mcuBoard), new ProcessorStats_FreeRam(&mcuBoard), new ProcessorStats_Batt(&mcuBoard), new MaximDS3231_Temp(&ds3231), bme280Humid, bme280Temp, bme280Press, ds18Temp, ms5803Press, ms5803Temp, calcWaterPress // calcRawDepth, // calcCorrDepth //final temp and baro corrected pressure depth }; // Count up the number of pointers in the array int variableCount = sizeof(variableList) / sizeof(variableList[0]); // Create the VariableArray object VariableArray varArray(variableCount, variableList); // ========================================================================== // The Logger Object[s] // ========================================================================== #include <LoggerBase.h> // Create a new logger instance Logger dataLogger(LoggerID, loggingInterval, sdCardPin, wakePin, &varArray); // ========================================================================== // Working Functions // ========================================================================== // Flashes the LED's on the primary board void greenredflash(uint8_t numFlash = 4, uint8_t rate = 75) { for (uint8_t i = 0; i < numFlash; i++) { digitalWrite(greenLED, HIGH); digitalWrite(redLED, LOW); delay(rate); digitalWrite(greenLED, LOW); digitalWrite(redLED, HIGH); delay(rate); } digitalWrite(redLED, LOW); } // ========================================================================== // Main setup function // ========================================================================== void setup() { // Start the primary serial connection Serial.begin(serialBaud); // Print a start-up note to the first serial port Serial.print(F("Now running ")); Serial.print(sketchName); Serial.print(F(" on Logger ")); Serial.println(LoggerID); Serial.println(); Serial.print(F("Using ModularSensors Library version ")); Serial.println(MODULAR_SENSORS_VERSION); if (String(MODULAR_SENSORS_VERSION) != String(libraryVersion)) Serial.println(F( "WARNING: THIS EXAMPLE WAS WRITTEN FOR A DIFFERENT VERSION OF MODULAR SENSORS!!")); // Set up pins for the LED's pinMode(greenLED, OUTPUT); digitalWrite(greenLED, LOW); pinMode(redLED, OUTPUT); digitalWrite(redLED, LOW); // Blink the LEDs to show the board is on and starting up greenredflash(); // Set the timezone and offsets // Logging in the given time zone Logger::setTimeZone(timeZone); // Offset is the same as the time zone because the RTC is in UTC Logger::setTZOffset(timeZone); // Set information pins dataLogger.setAlertPin(greenLED); dataLogger.setTestingModePin(buttonPin); // Begin the logger dataLogger.begin(); } // ========================================================================== // Main loop function // ========================================================================== void loop() { dataLogger.logData(); } |