Home › Forums › Mayfly Data Logger › Calculated Variable Celsius to Fahrenheit DS18 › Reply To: Calculated Variable Celsius to Fahrenheit DS18
2021-02-15 at 11:08 AM
#15147
Here’s the version without the extra un-needed BME code. I left in all the code for the display.
Arduino
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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
/***************************************************************************** Based on logging_to MMW.ino Adapted by Anthony and Brian from source by: Sara Damiano (sdamiano@stroudcenter.org) Development Environment: PlatformIO Hardware Platform: EnviroDIY Mayfly Arduino Datalogger Software License: BSD-3. Copyright (c) 2017, Stroud Water Research Center (SWRC) and the EnviroDIY Development Team This example sketch is written for ModularSensors library version 0.23.4 This shows most of the standard functions of the library at once. DISCLAIMER: THIS CODE IS PROVIDED "AS IS" - NO WARRANTY IS GIVEN. *****************************************************************************/ // ========================================================================== // Defines for the Arduino IDE // In PlatformIO, set these build flags in your platformio.ini // ========================================================================== #ifndef TINY_GSM_RX_BUFFER #define TINY_GSM_RX_BUFFER 512 #endif #ifndef TINY_GSM_YIELD_MS #define TINY_GSM_YIELD_MS 2 #endif #ifndef MQTT_MAX_PACKET_SIZE #define MQTT_MAX_PACKET_SIZE 240 #endif // ========================================================================== // Include the base required libraries // ========================================================================== #include <Arduino.h> // The base Arduino library #include <EnableInterrupt.h> // for external and pin change interrupts #include <LoggerBase.h> // The modular sensors library // ========================================================================== // Include libraries for OLED // ========================================================================== #include <SDL_Arduino_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility // Create an instance of the OLED display SDL_Arduino_SSD1306 display(4); // FOR I2C // ========================================================================== // Data Logger Settings // ========================================================================== // The library version this example was written for|previously 0.23.11 const char *libraryVersion = "0.25.1"; // The name of this file const char *sketchName = "WXSTN_Mini_Mobile.ino"; // Logger ID, also becomes the prefix for the name of the data file on SD card const char *LoggerID = "WX001"; // How frequently (in minutes) to log data const uint8_t loggingInterval = 15; // Your logger's timezone. const int8_t timeZone = -6; // Central 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 sdCardPwrPin = -1; // MCU SD card power pin (-1 if not applicable) const int8_t sdCardSSPin = 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 the main processor chip "sensor" - for general metadata const char *mcuBoardVersion = "v0.5b"; ProcessorStats mcuBoard(mcuBoardVersion); // ========================================================================== // Wifi/Cellular Modem Settings // ========================================================================== // Create a reference to the serial port for the modem // Extra hardware and software serial ports are created in the "Settings for Additional Serial Ports" section HardwareSerial &modemSerial = Serial1; // Use hardware serial if possible // AltSoftSerial &modemSerial = altSoftSerial; // For software serial if needed // NeoSWSerial &modemSerial = neoSSerial1; // For software serial if needed // Modem Pins - Describe the physical pin connection of your modem to your board const int8_t modemVccPin = -2; // MCU pin controlling modem power (-1 if not applicable) const int8_t modemStatusPin = 19; // MCU pin used to read modem status (-1 if not applicable) const int8_t modemResetPin = 20; // MCU pin connected to modem reset pin (-1 if unconnected) const int8_t modemSleepRqPin = 23; // MCU pin used for modem sleep/wake request (-1 if not applicable) const int8_t modemLEDPin = redLED; // MCU pin connected an LED to show modem status (-1 if unconnected) // Network connection information const char *apn = "hologram"; // The APN for the gprs connection #if not defined MS_BUILD_TESTING || defined MS_BUILD_TEST_XBEE_CELLULAR // For any Digi Cellular XBee's // NOTE: The u-blox based Digi XBee's (3G global and LTE-M global) // are more stable used in bypass mode (below) // The Telit based Digi XBees (LTE Cat1) can only use this mode. #include <modems/DigiXBeeCellularTransparent.h> const long modemBaud = 9600; // All XBee's use 9600 by default const bool useCTSforStatus = false; // Flag to use the modem CTS pin for status // NOTE: If possible, use the STATUS/SLEEP_not (XBee pin 13) for status, but // the CTS pin can also be used if necessary DigiXBeeCellularTransparent modemXBCT(&modemSerial, modemVccPin, modemStatusPin, useCTSforStatus, modemResetPin, modemSleepRqPin, apn); // Create an extra reference to the modem by a generic name (not necessary) DigiXBeeCellularTransparent modem = modemXBCT; #endif // ========================================================================== // Maxim DS3231 RTC (Real Time Clock) // ========================================================================== #include <sensors/MaximDS3231.h> // Create a DS3231 sensor object MaximDS3231 ds3231(1); // ========================================================================== // Calculated Variables // ========================================================================== // CtoF - example from the internet float CtoF(float cel) { float fahrenheit = (cel * 1.8) + 32; return fahrenheit; } // End CtoF - example from internet // Properties of the calculated temperature variable const char *TempFVarName = "temperature"; const char *TempFVarUnit = "degreeFahrenheit"; int TempFVarResolution = 2; // ========================================================================== // 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 // Create a Bosch BME280 sensor object BoschBME280 bme280(I2CPower, BMEi2c_addr); // Create four variable pointers for the BME280 Variable *bme280Humid = new BoschBME280_Humidity(&bme280, "12345678-abcd-1234-ef00-1234567890ab"); Variable *bme280Temp = new BoschBME280_Temp(&bme280, "12345678-abcd-1234-ef00-1234567890ab"); Variable *bme280Press = new BoschBME280_Pressure(&bme280, "12345678-abcd-1234-ef00-1234567890ab"); Variable *bme280Alt = new BoschBME280_Altitude(&bme280, "12345678-abcd-1234-ef00-1234567890ab"); // Create the function to convert the BME280 celcius to fahrenheit float calculatebme280TempF(void) { float TempCFromBME280 = bme280Temp->getValue(); float TempF = CtoF(TempCFromBME280); if (TempCFromBME280 == -9999) { TempF = -9999; } return TempF; } const char *bme280TempFUUID = "12345678-abcd-1234-ef00-1234567890ab"; const char *bme280TempFVarCode = "bme280TempF"; // Create the calculated fahrenheit variable object and return a variable pointer to it Variable *bme280TempF = new Variable(calculatebme280TempF, TempFVarResolution, TempFVarName, TempFVarUnit, bme280TempFVarCode, bme280TempFUUID); // ========================================================================== // Maxim DS18 One Wire Temperature Sensor // ========================================================================== #include <sensors/MaximDS18.h> // OneWire Address [array of 8 hex characters] // If only using a single sensor on the OneWire bus, you may omit the address // DeviceAddress OneWireAddress1 = {0x28, 0xFF, 0xBD, 0xBA, 0x81, 0x16, 0x03, 0x0C}; const int8_t OneWirePower = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected) const int8_t OneWireBus = 7; // Pin attached to the OneWire Bus (-1 if unconnected) (D24 = A0) // Create a Maxim DS18 sensor object (use this form for a single sensor on bus with an unknown address) MaximDS18 ds18(OneWirePower, OneWireBus); // Create a temperature variable pointer for the DS18 Variable *ds18Temp = new MaximDS18_Temp(&ds18, "12345678-abcd-1234-ef00-1234567890ab"); // Create the function to convert the DS18 celcius to fahrenheit float calculateds18TempF(void) { float TempCFromMaximDS18 = ds18Temp->getValue(); float TempF = CtoF(TempCFromMaximDS18); if (TempCFromMaximDS18 == -9999) { TempF = -9999; } return TempF; } // Properties of the calculated temperature variable const char *ds18TempFUUID = "12345678-abcd-1234-ef00-1234567890ab"; const char *ds18TempFVarCode = "ds18TempF"; // Create the calculated fahrenheit variable object and return a variable pointer to it Variable *ds18TempF = new Variable(calculateds18TempF, TempFVarResolution, TempFVarName, TempFVarUnit, ds18TempFVarCode, ds18TempFUUID); // ========================================================================== // Creating the Variable Array[s] and Filling with Variable Objects // ========================================================================== Variable *variableList[] = { // new ProcessorStats_SampleNumber(&mcuBoard, "12345678-abcd-1234-ef00-1234567890ab"), new ProcessorStats_Battery(&mcuBoard, "b17cb0f2-5538-4790-8641-39f416d185a3"), new Modem_RSSI(&modem, "e1788d85-f8ca-451f-af49-5d4068650a04"), new Modem_SignalPercent(&modem, "94c67feb-ade1-42eb-aef2-dcb021b85ef4"), new MaximDS3231_Temp(&ds3231, "3a304193-2e51-49e8-96a5-41015b445484"), ds18Temp, ds18TempF, bme280Humid, bme280Temp, bme280TempF, bme280Press, bme280Alt, }; // 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] // ========================================================================== // Create a new logger instance Logger dataLogger(LoggerID, loggingInterval, &varArray); // ========================================================================== // A Publisher to Monitor My Watershed / EnviroDIY Data Sharing Portal // ========================================================================== // Device registration and sampling feature information can be obtained after // registration at https://monitormywatershed.org or https://data.envirodiy.org const char *registrationToken = "ffb78d86-af9e-426d-ad44-b807f9e0cd4a"; // Device registration token const char *samplingFeature = "f5290c01-05a9-4047-b096-ca21f21bfbdd"; // Sampling feature UUID // Create a data publisher for the EnviroDIY/WikiWatershed POST endpoint #include <publishers/EnviroDIYPublisher.h> EnviroDIYPublisher EnviroDIYPOST(dataLogger, &modem.gsmClient, registrationToken, samplingFeature); // ========================================================================== // 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); } // Read's the battery voltage // NOTE: This will actually return the battery level from the previous update! float getBatteryVoltage() { if (mcuBoard.sensorValues[0] == -9999) mcuBoard.update(); return mcuBoard.sensorValues[0]; } unsigned long delayTime; // ========================================================================== // Main setup function // ========================================================================== void setup() { // Wait for USB connection to be established by PC // NOTE: Only use this when debugging - if not connected to a PC, this // could prevent the script from starting // #if defined SERIAL_PORT_USBVIRTUAL // while (!SERIAL_PORT_USBVIRTUAL && (millis() < 10000)){} // #endif // Start the primary serial connection Serial.begin(serialBaud); // Turn on switched power (for the display) pinMode(I2CPower, OUTPUT); digitalWrite(I2CPower, HIGH); display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // initialize with the I2C addr 0x3C (for the 128x64) display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0, 0); display.println(F("Mayfly\nBME280\nDEMO...")); display.display(); // 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); // Start the serial connection with the modem modemSerial.begin(modemBaud); // 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 timezones for the logger/data and the RTC // Logging in the given time zone Logger::setLoggerTimeZone(timeZone); // It is STRONGLY RECOMMENDED that you set the RTC to be in UTC (UTC+0) Logger::setRTCTimeZone(-6); // Attach the modem and information pins to the logger dataLogger.attachModem(modem); modem.setModemLED(modemLEDPin); dataLogger.setLoggerPins(wakePin, sdCardSSPin, sdCardPwrPin, buttonPin, greenLED); // Begin the logger dataLogger.begin(); // Note: Please change these battery voltages to match your battery // Check that the battery is OK before powering the modem if (getBatteryVoltage() > 3.7) { Serial.println(F("Beginning modem setup")); modem.modemPowerUp(); modem.wake(); modem.setup(); // At very good battery voltage, or with suspicious time stamp, sync the clock // Note: Please change these battery voltages to match your battery if (getBatteryVoltage() > 3.8 || dataLogger.getNowEpoch() < 1546300800 || /*Before 01/01/2019*/ dataLogger.getNowEpoch() > 1735689600) /*After 1/1/2025*/ { // Synchronize the RTC with NIST Serial.println(F("Attempting to connect to the internet and synchronize RTC with NIST")); if (modem.connectInternet(120000L)) { dataLogger.setRTClock(modem.getNISTTime()); } else { Serial.println(F("Could not connect to internet for clock sync.")); } } } // Set up the sensors, except at lowest battery level if (getBatteryVoltage() > 3.4) { Serial.println(F("Setting up sensors...")); varArray.setupSensors(); } // Power down the modem modem.disconnectInternet(); modem.modemSleepPowerDown(); // Create the log file, adding the default header to it // Do this last so we have the best chance of getting the time correct and // all sensor names correct // Writing to the SD card can be power intensive, so if we're skipping // the sensor setup we'll skip this too. if (getBatteryVoltage() > 3.4) { dataLogger.turnOnSDcard(true); // true = wait for card to settle after power up dataLogger.createLogFile(true); // true = write a new header dataLogger.turnOffSDcard(true); // true = wait for internal housekeeping after write } // Call the processor sleep Serial.println(F("Putting processor to sleep")); dataLogger.systemSleep(); } // ========================================================================== // Main loop function // ========================================================================== // Use this short loop for simple data logging and sending void loop() { // Note: Please change these battery voltages to match your battery // At very low battery, just go back to sleep if (getBatteryVoltage() < 3.4) { dataLogger.systemSleep(); } // At moderate voltage, log data but don't send it over the modem else if (getBatteryVoltage() < 3.6) { dataLogger.logData(); } // If the battery is good, send the data to the world else { dataLogger.logDataAndPublish(); // Turn on switched power (for the display) pinMode(I2CPower, OUTPUT); digitalWrite(I2CPower, HIGH); display.clearDisplay(); display.setTextSize(1.5); display.setTextColor(WHITE); display.setCursor(0, 0); display.print(F("DS18 T: ")); display.print(ds18Temp->getValueString()); display.println(F(" C")); display.print(F("DS18 T: ")); display.print(ds18TempF->getValueString()); display.println(F(" F")); display.println(); display.print(F("BME T: ")); display.print(bme280Temp->getValueString()); display.println(F(" C")); display.print(F("BME T: ")); display.print(bme280TempF->getValueString()); display.println(F(" F")); display.print(F("BME H: ")); display.print(bme280Humid->getValueString()); display.println(F(" %")); display.print(F("BME E:")); display.print(bme280Alt->getValueString()); display.println(F(" M")); display.print(F("BME P: ")); display.print(bme280Press->getValueString()); display.println(F(" Pa")); display.display(); PRINTOUT(F("DS18 T:"), ds18Temp->getValueString(), F("°C")); PRINTOUT(F("DS18 T:"), ds18TempF->getValueString(), F("°F\n")); PRINTOUT(F("BME T:"), bme280Temp->getValueString(), F("°C")); PRINTOUT(F("BME T:"), bme280TempF->getValueString(), F("°F")); PRINTOUT(F("BME H:"), bme280Humid->getValueString(), F("%")); PRINTOUT(F("BME E:"), bme280Alt->getValueString(), F("m")); PRINTOUT(F("BME P:"), bme280Press->getValueString(), F("Pa")); } } |