/***************************************************************************** DWRI_CitSci.ino Written 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.16 This sketch is an example of logging data to an SD card and sending the data to both the EnviroDIY data portal as should be used by groups involved with The William Penn Foundation's Delaware River Watershed Initiative 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 64 #endif #ifndef TINY_GSM_YIELD_MS #define TINY_GSM_YIELD_MS 2 #endif // ========================================================================== // Include the base required libraries // ========================================================================== #include // The base Arduino library #include // for external and pin change interrupts #include // The modular sensors library // ========================================================================== // Data Logger Settings // ========================================================================== // The library version this example was written for const char *libraryVersion = "0.23.16"; // The name of this file const char *sketchName = "DWRI_CitSci.ino"; // Logger ID, also becomes the prefix for the name of the data file on SD card const char *LoggerID = "KB_Farm_Outlet"; // How frequently (in minutes) to log data const uint8_t loggingInterval = 5; // 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 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 HardwareSerial &modemSerial = Serial1; // Use hardware serial if possible // Modem Pins - Describe the physical pin connection of your modem to your board const int8_t modemVccPin = 23; // 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 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 // For the Sodaq 2GBee R6 and R7 based on the SIMCom SIM800 // NOTE: The Sodaq GPRSBee doesn't expose the SIM800's reset pin #include const long modemBaud = 9600; // SIM800 does auto-bauding by default Sodaq2GBeeR6 modem2GB(&modemSerial, modemVccPin, modemStatusPin, apn); // Create an extra reference to the modem by a generic name (not necessary) Sodaq2GBeeR6 modem = modem2GB; // ========================================================================== // Maxim DS3231 RTC (Real Time Clock) // ========================================================================== #include // Create a DS3231 sensor object MaximDS3231 ds3231(1); // ========================================================================== // Campbell OBS 3 / OBS 3+ Analog Turbidity Sensor // ========================================================================== #include const int8_t OBS3Power = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected) const uint8_t OBS3NumberReadings = 10; const uint8_t ADSi2c_addr = 0x48; // The I2C address of the ADS1115 ADC // Campbell OBS 3+ Low Range calibration in Volts const int8_t OBSLowADSChannel = 0; // The ADS channel for the low range output const float OBSLow_A = 8.9159E+00; // The "A" value (X^2) from the low range calibration const float OBSLow_B = 1.8343E+02; // The "B" value (X) from the low range calibration const float OBSLow_C = -5.1056E-01; // The "C" value from the low range calibration // Create a Campbell OBS3+ LOW RANGE sensor object CampbellOBS3 osb3low(OBS3Power, OBSLowADSChannel, OBSLow_A, OBSLow_B, OBSLow_C, ADSi2c_addr, OBS3NumberReadings); // Campbell OBS 3+ High Range calibration in Volts const int8_t OBSHighADSChannel = 1; // The ADS channel for the high range output const float OBSHigh_A = 1.1641E+02; // The "A" value (X^2) from the high range calibration const float OBSHigh_B = 7.5120E+02; // The "B" value (X) from the high range calibration const float OBSHigh_C = -2.0697E+00; // The "C" value from the high range calibration // Create a Campbell OBS3+ HIGH RANGE sensor object CampbellOBS3 osb3high(OBS3Power, OBSHighADSChannel, OBSHigh_A, OBSHigh_B, OBSHigh_C, ADSi2c_addr, OBS3NumberReadings); // ========================================================================== // Decagon CTD Conductivity, Temperature, and Depth Sensor // ========================================================================== #include const char *CTDSDI12address = "1"; // The SDI-12 Address of the CTD const uint8_t CTDNumberReadings = 6; // The number of readings to average const int8_t SDI12Power = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected) const int8_t SDI12Data = 7; // The SDI12 data pin // Create a Decagon CTD sensor object DecagonCTD ctd(*CTDSDI12address, SDI12Power, SDI12Data, CTDNumberReadings); // ========================================================================== // Creating the Variable Array[s] and Filling with Variable Objects // ========================================================================== Variable *variableList[] = { new CampbellOBS3_Turbidity(&osb3low, "", "TurbLow"), new CampbellOBS3_Turbidity(&osb3high, "", "TurbHigh"), new ProcessorStats_Battery(&mcuBoard), new MaximDS3231_Temp(&ds3231), new Modem_RSSI(&modem), new Modem_SignalPercent(&modem), }; // *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION *** // Check the order of your variables in the variable list!!! // Be VERY certain that they match the order of your UUID's! // Rearrange the variables in the variable list if necessary to match! // *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION *** const char *REGISTRATION_TOKEN = "e43ec73b-a5c0-40b4-a45c-9578885754b2"; // Device registration token const char *SAMPLING_FEATURE = "2bcbcfd7-eae1-4470-a847-04a48bf987e5"; // Sampling feature UUID const char *UUIDs[] = { "f31f8fa5-74d5-4bea-950d-771fe044f0fd", "26ec213d-8862-4216-a320-463d515b53ad", "cb314031-7cab-4ba9-950b-87c26d1c3aa2", "fd90b7fc-1766-4756-91da-3f81a3313509", "4671be32-1193-4a63-a20d-88b30654b64a", "06c83667-73d0-4e4d-bcea-4456c81256b2", }; // Count up the number of pointers in the array int variableCount = sizeof(variableList) / sizeof(variableList[0]); // Create the VariableArray object VariableArray varArray(variableCount, variableList, UUIDs); // ========================================================================== // The Logger Object[s] // ========================================================================== // Create a new logger instance Logger dataLogger(LoggerID, loggingInterval, &varArray); // ========================================================================== // A Publisher to WikiWatershed // ========================================================================== // Device registration and sampling feature information can be obtained after // registration at http://data.WikiWatershed.org const char *registrationToken = REGISTRATION_TOKEN; // Device registration token const char *samplingFeature = SAMPLING_FEATURE; // Sampling feature UUID // Create a data publisher for the EnviroDIY/WikiWatershed POST endpoint #include 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]; } // ========================================================================== // 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!!")); // 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(0); // 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.55 || !dataLogger.isRTCSane()) { modem.modemPowerUp(); modem.wake(); modem.setup(); // 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.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\n")); dataLogger.systemSleep(); }