Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
400 lines (321 sloc) 15.9 KB
/*****************************************************************************
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.21.2
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.
*****************************************************************************/
// ==========================================================================
// Include the base required libraries
// ==========================================================================
#include <Arduino.h> // The base Arduino library
#include <EnableInterrupt.h> // for external and pin change interrupts
// ==========================================================================
// Data Logger Settings
// ==========================================================================
// The library version this example was written for
const char *libraryVersion = "0.21.2";
// 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 = "XXXXX";
// 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 <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 Main Chip Selection
// ==========================================================================
#define TINY_GSM_MODEM_SIM800 // Select for a SIM800, SIM900, or variant thereof
// ==========================================================================
// Modem Pins
// ==========================================================================
const int8_t modemVccPin = -2; // MCU pin controlling modem power (-1 if not applicable)
const int8_t modemSleepRqPin = 23; // MCU pin used for modem sleep/wake request (-1 if not applicable)
const int8_t modemStatusPin = 19; // MCU pin used to read modem status (-1 if not applicable)
// ==========================================================================
// TinyGSM Client
// ==========================================================================
#define TINY_GSM_YIELD() { delay(2); } // Use to counter slow (9600) baud rate
// Include TinyGSM for the modem
// This include must be included below the define of the modem name!
#include <TinyGsmClient.h>
// Create a reference to the serial port for the modem
HardwareSerial &modemSerial = Serial1; // Use hardware serial if possible
// Create a new TinyGSM modem to run on that serial port and return a pointer to it
TinyGsm *tinyModem = new TinyGsm(modemSerial);
// Create a TCP client on that modem
TinyGsmClient *tinyClient = new TinyGsmClient(*tinyModem);
// ==========================================================================
// Specific Modem On-Off Methods
// ==========================================================================
// THIS ONLY APPLIES TO A SODAQ GPRSBEE R6!!!
// Describe the physical pin connection of your modem to your board
const long modemBaud = 9600; // Communication speed of the modem
const bool modemStatusLevel = HIGH; // The level of the status pin when the module is active (HIGH or LOW)
// Create the wake and sleep methods for the modem
// These can be functions of any type and must return a boolean
bool modemWakeFxn(void)
{
digitalWrite(modemSleepRqPin, HIGH);
digitalWrite(redLED, HIGH); // A light just for show
return true;
}
bool modemSleepFxn(void)
{
digitalWrite(modemSleepRqPin, LOW);
digitalWrite(redLED, LOW);
return true;
}
void extraModemSetup(void){}
// ==========================================================================
// Network Information and LoggerModem Object
// ==========================================================================
#include <LoggerModem.h>
// Network connection information
const char *apn = "hologram"; // The APN for the gprs connection, unnecessary for WiFi
// Create the loggerModem instance
// A "loggerModem" is a combination of a TinyGSM Modem, a Client, and functions for wake and sleep
loggerModem modem(modemVccPin, modemStatusPin, modemStatusLevel, modemWakeFxn, modemSleepFxn, tinyModem, tinyClient, apn);
// ^^ Use this for cellular
// ==========================================================================
// Maxim DS3231 RTC (Real Time Clock)
// ==========================================================================
#include <sensors/MaximDS3231.h>
// Create a DS3231 sensor object
MaximDS3231 ds3231(1);
// ==========================================================================
// CAMPBELL OBS 3 / OBS 3+ Analog Turbidity Sensor
// ==========================================================================
#include <sensors/CampbellOBS3.h>
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 = 0.000E+00; // The "A" value (X^2) from the low range calibration
const float OBSLow_B = 1.000E+00; // The "B" value (X) from the low range calibration
const float OBSLow_C = 0.000E+00; // 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 = 0.000E+00; // The "A" value (X^2) from the high range calibration
const float OBSHigh_B = 1.000E+00; // The "B" value (X) from the high range calibration
const float OBSHigh_C = 0.000E+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 <sensors/DecagonCTD.h>
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
// ==========================================================================
#include <VariableArray.h>
Variable *variableList[] = {
new DecagonCTD_Cond(&ctd, "12345678-abcd-1234-efgh-1234567890ab"),
new DecagonCTD_Temp(&ctd, "12345678-abcd-1234-efgh-1234567890ab"),
new DecagonCTD_Depth(&ctd, "12345678-abcd-1234-efgh-1234567890ab"),
new CampbellOBS3_Turbidity(&osb3low, "12345678-abcd-1234-efgh-1234567890ab", "TurbLow"),
new CampbellOBS3_Turbidity(&osb3high, "12345678-abcd-1234-efgh-1234567890ab", "TurbHigh"),
new ProcessorStats_Batt(&mcuBoard, "12345678-abcd-1234-efgh-1234567890ab"),
new MaximDS3231_Temp(&ds3231, "12345678-abcd-1234-efgh-1234567890ab"),
new Modem_RSSI(&modem, "12345678-abcd-1234-efgh-1234567890ab"),
new Modem_SignalPercent(&modem, "12345678-abcd-1234-efgh-1234567890ab"),
};
// 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, &varArray);
// ==========================================================================
// A Publisher to WikiWatershed
// ==========================================================================
// Device registration and sampling feature information can be obtained after
// registration at http://data.WikiWatershed.org
const char *registrationToken = "12345678-abcd-1234-efgh-1234567890ab"; // Device registration token
const char *samplingFeature = "12345678-abcd-1234-efgh-1234567890ab"; // Sampling feature UUID
// Create a data publisher for the EnviroDIY/WikiWatershed POST endpoint
#include <publishers/EnviroDIYPublisher.h>
EnviroDIYPublisher EnviroDIYPOST(dataLogger, 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 up some of the power pins so the board boots up with them off
// NOTE: This isn't necessary at all. The logger begin() function
// should leave all power pins off when it finishes.
if (modemVccPin >= 0)
{
pinMode(modemVccPin, OUTPUT);
digitalWrite(modemVccPin, LOW);
}
if (sensorPowerPin >= 0)
{
pinMode(sensorPowerPin, OUTPUT);
digitalWrite(sensorPowerPin, LOW);
}
if (modemSleepRqPin >= 0)
{
pinMode(modemSleepRqPin, OUTPUT);
digitalWrite(modemSleepRqPin, LOW);
}
// 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);
// Attach the modem and information pins to the logger
dataLogger.attachModem(modem);
dataLogger.setLoggerPins(wakePin, sdCardSSPin, sensorPowerPin, 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)
{
modem.modemPowerUp();
modem.wake();
// 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) /*Before 1/1/2025*/
{
// Synchronize the RTC with NIST
Serial.println(F("Attempting to synchronize RTC with NIST"));
if (modem.connectInternet(120000L))
{
dataLogger.setRTClock(modem.getNISTTime());
}
}
}
// 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
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.7)
{
dataLogger.logData();
}
// If the battery is good, send the data to the world
else
{
dataLogger.logDataAndSend();
}
}
You can’t perform that action at this time.