Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better sleeping checking #179

Merged
merged 12 commits into from
Oct 19, 2018
78 changes: 42 additions & 36 deletions examples/DRWI_CitSci/DRWI_CitSci.ino
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ const int8_t sensorPowerPin = 22; // MCU pin controlling main sensor power (-1 i
// Create and return the processor "sensor"
const char *MFVersion = "v0.5b";
ProcessorStats mayfly(MFVersion);
// Create the battery voltage and free RAM variable objects for the processor and return variable-type pointers to them
// We're going to use the battery variable in the set-up and loop to decide if the battery level is high enough to
// send data over the modem or if the data should only be logged.
Variable *mayflyBatt = new ProcessorStats_Batt(&mayfly, "12345678-abcd-1234-efgh-1234567890ab");
// Variable *mayflyRAM = new ProcessorStats_FreeRam(&mayfly, "12345678-abcd-1234-efgh-1234567890ab");


// ==========================================================================
// Modem/Internet connection options
Expand All @@ -78,31 +72,29 @@ Variable *mayflyBatt = new ProcessorStats_Batt(&mayfly, "12345678-abcd-1234-efgh
// Set the serial port for the modem - software serial can also be used.
HardwareSerial &ModemSerial = Serial1;

// Create a variable for the modem baud rate - this will be used in the begin function for the port
const long ModemBaud = 9600;

// Create a new TinyGSM modem to run on that serial port and return a pointer to it
TinyGsm *tinyModem = new TinyGsm(ModemSerial);

// Create a new TCP client on that modem and return a pointer to it
TinyGsmClient *tinyClient = new TinyGsmClient(*tinyModem);

// 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 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)
const long ModemBaud = 9600; // Communication speed of the modem
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)
const bool modemStatusLevel = HIGH; // The level of the status pin when the module is active (HIGH or LOW)

// And create the wake and sleep methods for the modem
// These can be functions of any type and must return a boolean
bool wakeFxn(void)
bool sleepFxn(void)
{
digitalWrite(modemSleepRqPin, HIGH);
digitalWrite(modemSleepRqPin, LOW);
return true;
}
bool sleepFxn(void)
bool wakeFxn(void)
{
digitalWrite(modemSleepRqPin, LOW);
digitalWrite(modemSleepRqPin, HIGH);
return true;
}

Expand Down Expand Up @@ -170,7 +162,7 @@ Variable *variableList[] = {
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"),
mayflyBatt,
new ProcessorStats_Batt(&mayfly, "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"),
Expand Down Expand Up @@ -212,6 +204,26 @@ void greenredflash(int numFlash = 4, int rate = 75)
}


// Read's the battery voltage
float getBatteryVoltage(const char *version = MFVersion)
{
float batteryVoltage;
if (strcmp(version, "v0.3") == 0 or strcmp(version, "v0.4") == 0)
{
// Get the battery voltage
float rawBattery = analogRead(A6);
batteryVoltage = (3.3 / 1023.) * 1.47 * rawBattery;
}
if (strcmp(version, "v0.5") == 0 or strcmp(version, "v0.5b") == 0)
{
// Get the battery voltage
float rawBattery = analogRead(A6);
batteryVoltage = (3.3 / 1023.) * 4.7 * rawBattery;
}
return batteryVoltage;
}


// ==========================================================================
// Main setup function
// ==========================================================================
Expand All @@ -220,6 +232,12 @@ 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);

// Start the serial connection with the modem
ModemSerial.begin(ModemBaud);

Expand All @@ -231,22 +249,10 @@ void setup()
// 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
pinMode(modemVccPin, OUTPUT);
digitalWrite(modemVccPin, LOW);
pinMode(sensorPowerPin, OUTPUT);
digitalWrite(sensorPowerPin, LOW);

// Set up the sleep/wake pin for the modem and put it's inital value as "off"
pinMode(modemSleepRqPin, OUTPUT);
digitalWrite(modemSleepRqPin, LOW);

// 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);

// Set the timezone and offsets
// Logging in the given time zone
Logger::setTimeZone(timeZone);
Expand All @@ -266,11 +272,11 @@ void setup()
EnviroDIYLogger.setDreamHostPortalRX(DreamHostPortalRX);

// Begin the logger
mayfly.update();
Serial.print("Battery: ");
Serial.println(mayflyBatt->getValue());
if (mayflyBatt->getValue() > 3.7) EnviroDIYLogger.beginAndSync();
else EnviroDIYLogger.begin();
Serial.println(getBatteryVoltage());
if (getBatteryVoltage() < 3.4) EnviroDIYLogger.begin(true); // skip sensor set-up
else if (getBatteryVoltage() < 3.7) EnviroDIYLogger.begin(); // set up sensors
else EnviroDIYLogger.beginAndSync(); // set up sensors and synchronize clock with NIST
}


Expand All @@ -280,8 +286,8 @@ void setup()
void loop()
{
// Log the data
if (mayflyBatt->getValue() > 3.7)
// This will check against the battery level at the previous logging interval!
EnviroDIYLogger.logAndSend();
else EnviroDIYLogger.log();
if (getBatteryVoltage() < 3.4) EnviroDIYLogger.systemSleep(); // just keep sleeping
else if (getBatteryVoltage() < 3.7) EnviroDIYLogger.logData(); // log data
else EnviroDIYLogger.logDataAndSend(); // log data and send it out
}
2 changes: 1 addition & 1 deletion examples/DRWI_CitSci/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ framework = arduino
lib_ldf_mode = deep
lib_ignore = RTCZero
lib_deps =
EnviroDIY_ModularSensors@=0.15.3
EnviroDIY_ModularSensors@=0.16.0
65 changes: 48 additions & 17 deletions examples/DRWI_NoCellular/DRWI_NoCellular.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ THIS CODE IS PROVIDED "AS IS" - NO WARRANTY IS GIVEN.
// ==========================================================================
#include <Arduino.h> // The base Arduino library
#include <EnableInterrupt.h> // for external and pin change interrupts
#include <LoggerBase.h>


// ==========================================================================
Expand All @@ -32,28 +31,29 @@ 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;
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; // Pin for the green LED (-1 if unconnected)
const int8_t redLED = 9; // Pin for the red LED (-1 if unconnected)
const int8_t buttonPin = 21; // Pin for a button to use to enter debugging mode (-1 if unconnected)
const int8_t wakePin = A7; // Interrupt/Alarm pin to wake from sleep
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; // SD Card Chip Select/Slave Select Pin (must be defined!)
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 processor "sensor"
const char *MFVersion = "v0.5b";
ProcessorStats mayfly(MFVersion);


// ==========================================================================
// Maxim DS3231 RTC (Real Time Clock)
// ==========================================================================
Expand Down Expand Up @@ -100,6 +100,7 @@ DecagonCTD ctd(*CTDSDI12address, SDI12Power, SDI12Data, CTDnumberReadings);
// ==========================================================================
// The array that contains all variables to be logged
// ==========================================================================
#include <VariableArray.h>
// Create pointers for all of the variables from the sensors
// at the same time putting them into an array
Variable *variableList[] = {
Expand All @@ -115,7 +116,9 @@ Variable *variableList[] = {
int variableCount = sizeof(variableList) / sizeof(variableList[0]);
// Create the VariableArray object
VariableArray varArray(variableCount, variableList);

// Create a new logger instance
#include <LoggerBase.h>
Logger logger(LoggerID, loggingInterval, sdCardPin, wakePin, &varArray);

// ==========================================================================
Expand Down Expand Up @@ -145,6 +148,26 @@ void greenredflash(int numFlash = 4, int rate = 75)
}


// Read's the battery voltage
float getBatteryVoltage(const char *version = MFVersion)
{
float batteryVoltage;
if (strcmp(version, "v0.3") == 0 or strcmp(version, "v0.4") == 0)
{
// Get the battery voltage
float rawBattery = analogRead(A6);
batteryVoltage = (3.3 / 1023.) * 1.47 * rawBattery;
}
if (strcmp(version, "v0.5") == 0 or strcmp(version, "v0.5b") == 0)
{
// Get the battery voltage
float rawBattery = analogRead(A6);
batteryVoltage = (3.3 / 1023.) * 4.7 * rawBattery;
}
return batteryVoltage;
}


// ==========================================================================
// Main setup function
// ==========================================================================
Expand All @@ -153,18 +176,20 @@ void setup()
// Start the primary serial connection
Serial.begin(serialBaud);

// Set up pins for the LED's
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
// Blink the LEDs to show the board is on and starting up
greenredflash();

// 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);

// 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);
Expand All @@ -176,7 +201,10 @@ void setup()
logger.setTestingModePin(buttonPin);

// Begin the logger
logger.begin();
Serial.print("Battery: ");
Serial.println(getBatteryVoltage());
if (getBatteryVoltage() < 3.4) logger.begin(true); // skip sensor set-up
else logger.begin(); // set up sensors
}


Expand All @@ -186,5 +214,8 @@ void setup()
void loop()
{
// Log the data
logger.log();
Serial.print("Battery: ");
Serial.println(getBatteryVoltage());
if (getBatteryVoltage() < 3.4) logger.systemSleep(); // just go back to sleep
else logger.logData(); // log data
}
2 changes: 1 addition & 1 deletion examples/DRWI_NoCellular/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ framework = arduino
lib_ldf_mode = deep
lib_ignore = RTCZero
lib_deps =
EnviroDIY_ModularSensors@=0.15.3
EnviroDIY_ModularSensors@=0.16.0
2 changes: 1 addition & 1 deletion examples/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ This is uses just the sensors and equipment standard to the DWRI Citizen Science
This example demonstrates how to work with calculated variables.

### double_logger.ino
This is a more complicated example using two different logger instances to log data at two different intervals, in this case, an AM3215 logging every minute, while checking the battery voltage only every 5 minutes. This showcases both how to use two different logging instances and how to use some of the functions to set up your own logging loop rather than using the log() function.
This is a more complicated example using two different logger instances to log data at two different intervals, in this case, an AM3215 logging every minute, while checking the battery voltage only every 5 minutes. This showcases both how to use two different logging instances and how to use some of the functions to set up your own logging loop rather than using the logData() function.

### data_saving.ino
This is another double logger example, but in this case, both loggers are going at the same interval and the only difference between the loggers is the list of variables. There are two sets of variables, all coming from Yosemitech sensors. Because each sensor outputs temperature and we don't want to waste cellular data sending out multiple nearly identical temperature values, we have one logger that logs every possible variable result to the SD card and another logger that sends only unique results to the EnviroDIY data portal.
Loading