DRWI sites with no Cellular Service

This is the code example that should be used for all groups working with the Stroud Water Research Center within the Delaware River Watershed Initiative. This example should be used in cases where no cellular service of any kind is available and the data will only be logged on the SD card.

The exact hardware configuration used in this example:

  • Mayfly v1.x board
  • Hydros21 CTD sensor
  • Campbell OBS3+ turbidity sensor

Before using this example, you must register a site and sensors at the data portal (http://data.envirodiy.org/). After you have registered the site and sensors, the portal will generate a registration token and universally unique identifier (UUID) for each site and further UUID's for each variable. You will need to copy all of those UUID values into your sketch to replace the 12345678-abcd-1234-ef00-1234567890ab place holders in this example. You should register even if your logger will not be sending live data. This ensures that the data file your logger writes will be ready to immediately upload to the portal.



Unique Features of the DRWI LTE Example

  • Specifically for sites within the Delaware River Watershed Initiative.
  • Does not include any live data uploads.

To Use this Example

Prepare and set up PlatformIO

  • Register a site and sensors at the Monitor My Watershed/EnviroDIY data portal (http://monitormywatershed.org/)
  • Create a new PlatformIO project
  • Replace the contents of the platformio.ini for your new project with the platformio.ini file in the examples/DRWI_NoCellular folder on GitHub.
    • It is important that your PlatformIO configuration has the lib_ldf_mode and build flags set as they are in the example.
    • Without this, the program won't compile.
  • Open DRWI_NoCellular.ino and save it to your computer.
    • After opening the link, you should be able to right click anywhere on the page and select "Save Page As".
    • Move it into the src directory of your project.
    • Delete main.cpp in that folder.

Set the logger ID

  • Change the "XXXX" in this section of code to the loggerID assigned by Stroud:
1// Logger ID, also becomes the prefix for the name of the data file on SD card
2const char *LoggerID = "XXXX";

Set the calibration coefficients for the Campbell OBS3+

  • The OBS3+ ships with a calibration certificate; you need this sheet!
  • Change *__all__* of the the 0.000E+00 and 1.000E+00 values in this section of code to the values on that calibration sheet. Use numbers from the side of the calibration sheet that shows the calibration in *__volts__*.
    • The sketch will not compile if these values are not entered properly.
    • Do not change any values except those that are 0.000E+00 and 1.000E+00!
1// ==========================================================================
2// Campbell OBS 3 / OBS 3+ Analog Turbidity Sensor
3// ==========================================================================
4#include <sensors/CampbellOBS3.h>
5const int8_t OBS3Power = sensorPowerPin; // Power pin (-1 if unconnected)
6const uint8_t OBS3numberReadings = 10;
7const uint8_t ADSi2c_addr = 0x48; // The I2C address of the ADS1115 ADC
8// Campbell OBS 3+ *Low* Range Calibration in Volts
9const int8_t OBSLowADSChannel = 0; // ADS channel for *low* range output
10const float OBSLow_A = 0.000E+00; // "A" value (X^2) [*low* range]
11const float OBSLow_B = 1.000E+00; // "B" value (X) [*low* range]
12const float OBSLow_C = 0.000E+00; // "C" value [*low* range]
13CampbellOBS3 osb3low(OBS3Power, OBSLowADSChannel, OBSLow_A, OBSLow_B, OBSLow_C, ADSi2c_addr, OBS3numberReadings);
14// Campbell OBS 3+ *High* Range Calibration in Volts
15const int8_t OBSHighADSChannel = 1; // ADS channel for *high* range output
16const float OBSHigh_A = 0.000E+00; // "A" value (X^2) [*high* range]
17const float OBSHigh_B = 1.000E+00; // "B" value (X) [*high* range]
18const float OBSHigh_C = 0.000E+00; // "C" value [*high* range]
19CampbellOBS3 osb3high(OBS3Power, OBSHighADSChannel, OBSHigh_A, OBSHigh_B, OBSHigh_C, ADSi2c_addr, OBS3numberReadings);

Set the universally universal identifiers (UUID) for each variable

  • Go back to the web page for your site at the Monitor My Watershed/EnviroDIY data portal (http://monitormywatershed.org/)
  • Find and click the white "View Token UUID List" button above the small map on your site page
  • VERY CAREFULLY check that the variables are in exactly the same order as in the variable array:
1Variable* variableList[] = {
2 ...
3}
  • If any of the variables are in a different order on the web page than in your code reorder the variables in your code to match the website.
  • After you are completely certain that you have the order right in the variable section of your code use the teal "Copy" button on the website to copy the section of code containing all of the UUID's.
  • Paste the code from the website into your program in this section below the variable array
1// *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION ***
2// Check the order of your variables in the variable list!!!
3// Be VERY certain that they match the order of your UUID's!
4// Rearrange the variables in the variable list if necessary to match!
5// *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION ***
6/* clang-format off */
7const char* UUIDs[] = {
8 "12345678-abcd-1234-ef00-1234567890ab", // Electrical conductivity (Decagon_CTD-10_Cond)
9 "12345678-abcd-1234-ef00-1234567890ab", // Temperature (Decagon_CTD-10_Temp)
10 "12345678-abcd-1234-ef00-1234567890ab", // Water depth (Decagon_CTD-10_Depth)
11 "12345678-abcd-1234-ef00-1234567890ab", // Turbidity (Campbell_OBS3_Turb)
12 "12345678-abcd-1234-ef00-1234567890ab", // Turbidity (Campbell_OBS3_Turb)
13 "12345678-abcd-1234-ef00-1234567890ab", // Battery voltage (EnviroDIY_Mayfly_Batt)
14 "12345678-abcd-1234-ef00-1234567890ab", // Temperature (EnviroDIY_Mayfly_Temp)
15};
16const char* registrationToken = "12345678-abcd-1234-ef00-1234567890ab"; // Device registration token
17const char* samplingFeature = "12345678-abcd-1234-ef00-1234567890ab"; // Sampling feature UUID
18/* clang-format on */

Upload!

  • Test everything at home before deploying out in the wild!

PlatformIO Configuration

1; PlatformIO Project Configuration File
2;
3; Build options: build flags, source filter
4; Upload options: custom upload port, speed and extra flags
5; Library options: dependencies, extra library storages
6; Advanced options: extra scripting
7;
8; Please visit documentation for the other options and examples
9; http://docs.platformio.org/page/projectconf.html
10
11[platformio]
12description = ModularSensors example intended for DRWI users with CTD and turbidity but no cellular service
13
14[env:mayfly]
15monitor_speed = 115200
16board = mayfly
17platform = atmelavr
18framework = arduino
19lib_ldf_mode = deep+
20lib_ignore =
21 RTCZero
22 Adafruit NeoPixel
23 Adafruit GFX Library
24 Adafruit SSD1306
25 Adafruit ADXL343
26 Adafruit STMPE610
27 Adafruit TouchScreen
28 Adafruit ILI9341
29build_flags =
30 -DSDI12_EXTERNAL_PCINT
31 -DNEOSWSERIAL_EXTERNAL_PCINT
32 -DMQTT_MAX_PACKET_SIZE=240
33 -DTINY_GSM_RX_BUFFER=64
34 -DTINY_GSM_YIELD_MS=2
35lib_deps =
36 envirodiy/EnviroDIY_ModularSensors
37; ^^ Use this when working from an official release of the library
38; https://github.com/EnviroDIY/ModularSensors.git#develop
39; ^^ Use this when if you want to pull from the develop branch

The Complete Code

1/** =========================================================================
2 * @example{lineno} DRWI_NoCellular.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Sara Geleskie Damiano <sdamiano@stroudcenter.org>
6 *
7 * @brief Example for DRWI CitSci without cellular service.
8 *
9 * See [the walkthrough page](@ref example_drwi_no_cell) for detailed
10 * instructions.
11 *
12 * @m_examplenavigation{example_drwi_no_cell,}
13 * ======================================================================= */
14
15// ==========================================================================
16// Include the libraries required for any data logger
17// ==========================================================================
18/** Start [includes] */
19// The Arduino library is needed for every Arduino program.
20#include <Arduino.h>
21
22// Include the main header for ModularSensors
23#include <ModularSensors.h>
24/** End [includes] */
25
26
27// ==========================================================================
28// Data Logging Options
29// ==========================================================================
30/** Start [logging_options] */
31// The name of this program file
32const char* sketchName = "DRWI_NoCellular.ino";
33// Logger ID, also becomes the prefix for the name of the data file on SD card
34const char* LoggerID = "XXXXX";
35// How frequently (in minutes) to log data
36const uint8_t loggingInterval = 15;
37// Your logger's timezone.
38const int8_t timeZone = -5; // Eastern Standard Time
39// NOTE: Daylight savings time will not be applied! Please use standard time!
40
41// Set the input and output pins for the logger
42// NOTE: Use -1 for pins that do not apply
43const int32_t serialBaud = 115200; // Baud rate for debugging
44const int8_t greenLED = 8; // Pin for the green LED
45const int8_t redLED = 9; // Pin for the red LED
46const int8_t buttonPin = 21; // Pin for debugging mode (ie, button pin)
47const int8_t wakePin = 31; // MCU interrupt/alarm pin to wake from sleep
48// Mayfly 0.x D31 = A7
49const int8_t sdCardPwrPin = -1; // MCU SD card power pin
50const int8_t sdCardSSPin = 12; // SD card chip select/slave select pin
51const int8_t sensorPowerPin = 22; // MCU pin controlling main sensor power
52/** End [logging_options] */
53
54
55// ==========================================================================
56// Using the Processor as a Sensor
57// ==========================================================================
58/** Start [processor_sensor] */
59#include <sensors/ProcessorStats.h>
60
61// Create the main processor chip "sensor" - for general metadata
62const char* mcuBoardVersion = "v1.1";
63ProcessorStats mcuBoard(mcuBoardVersion);
64/** End [processor_sensor] */
65
66
67// ==========================================================================
68// Maxim DS3231 RTC (Real Time Clock)
69// ==========================================================================
70/** Start [ds3231] */
71#include <sensors/MaximDS3231.h>
72
73// Create a DS3231 sensor object
74MaximDS3231 ds3231(1);
75/** End [ds3231] */
76
77
78// ==========================================================================
79// Campbell OBS 3 / OBS 3+ Analog Turbidity Sensor
80// ==========================================================================
81/** Start [obs3] */
82#include <sensors/CampbellOBS3.h>
83
84const int8_t OBS3Power = sensorPowerPin; // Power pin (-1 if unconnected)
85const uint8_t OBS3NumberReadings = 10;
86const uint8_t ADSi2c_addr = 0x48; // The I2C address of the ADS1115 ADC
87// Campbell OBS 3+ *Low* Range Calibration in Volts
88const int8_t OBSLowADSChannel = 0; // ADS channel for *low* range output
89const float OBSLow_A = 0.000E+00; // "A" value (X^2) [*low* range]
90const float OBSLow_B = 1.000E+00; // "B" value (X) [*low* range]
91const float OBSLow_C = 0.000E+00; // "C" value [*low* range]
92
93// Create a Campbell OBS3+ *low* range sensor object
94CampbellOBS3 osb3low(OBS3Power, OBSLowADSChannel, OBSLow_A, OBSLow_B, OBSLow_C,
95 ADSi2c_addr, OBS3NumberReadings);
96
97
98// Campbell OBS 3+ *High* Range Calibration in Volts
99const int8_t OBSHighADSChannel = 1; // ADS channel for *high* range output
100const float OBSHigh_A = 0.000E+00; // "A" value (X^2) [*high* range]
101const float OBSHigh_B = 1.000E+00; // "B" value (X) [*high* range]
102const float OBSHigh_C = 0.000E+00; // "C" value [*high* range]
103
104// Create a Campbell OBS3+ *high* range sensor object
105CampbellOBS3 osb3high(OBS3Power, OBSHighADSChannel, OBSHigh_A, OBSHigh_B,
106 OBSHigh_C, ADSi2c_addr, OBS3NumberReadings);
107/** End [obs3] */
108
109
110// ==========================================================================
111// Meter Hydros 21 Conductivity, Temperature, and Depth Sensor
112// ==========================================================================
113/** Start [hydros21] */
114#include <sensors/MeterHydros21.h>
115
116const char* hydrosSDI12address = "1"; // The SDI-12 Address of the Hydros 21
117const uint8_t hydrosNumberReadings = 6; // The number of readings to average
118const int8_t SDI12Power = sensorPowerPin; // Power pin (-1 if unconnected)
119const int8_t SDI12Data = 7; // The SDI12 data pin
120
121// Create a Meter Hydros 21 sensor object
122MeterHydros21 hydros(*hydrosSDI12address, SDI12Power, SDI12Data,
123 hydrosNumberReadings);
124/** End [hydros21] */
125
126
127// ==========================================================================
128// Creating the Variable Array[s] and Filling with Variable Objects
129// ==========================================================================
130/** Start [variable_arrays] */
131Variable* variableList[] = {
132 new MeterHydros21_Cond(&hydros),
133 new MeterHydros21_Temp(&hydros),
134 new MeterHydros21_Depth(&hydros),
135 new CampbellOBS3_Turbidity(&osb3low, "", "TurbLow"),
136 new CampbellOBS3_Turbidity(&osb3high, "", "TurbHigh"),
137 new ProcessorStats_Battery(&mcuBoard),
138 new MaximDS3231_Temp(&ds3231),
139};
140
141// All UUID's, device registration, and sampling feature information can be
142// pasted directly from Monitor My Watershed. To get the list, click the "View
143// token UUID list" button on the upper right of the site page.
144// Even if not publishing live data, this is needed so the logger file will be
145// "drag-and-drop" ready for manual upload to the portal.
146
147// *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION ***
148// Check the order of your variables in the variable list!!!
149// Be VERY certain that they match the order of your UUID's!
150// Rearrange the variables in the variable list if necessary to match!
151// *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION ***
152/* clang-format off */
153const char* UUIDs[] = {
154 "12345678-abcd-1234-ef00-1234567890ab", // Electrical conductivity (Decagon_CTD-10_Cond)
155 "12345678-abcd-1234-ef00-1234567890ab", // Temperature (Decagon_CTD-10_Temp)
156 "12345678-abcd-1234-ef00-1234567890ab", // Water depth (Decagon_CTD-10_Depth)
157 "12345678-abcd-1234-ef00-1234567890ab", // Turbidity (Campbell_OBS3_Turb)
158 "12345678-abcd-1234-ef00-1234567890ab", // Turbidity (Campbell_OBS3_Turb)
159 "12345678-abcd-1234-ef00-1234567890ab", // Battery voltage (EnviroDIY_Mayfly_Batt)
160 "12345678-abcd-1234-ef00-1234567890ab" // Temperature (EnviroDIY_Mayfly_Temp)
161};
162const char* registrationToken = "12345678-abcd-1234-ef00-1234567890ab"; // Device registration token
163const char* samplingFeature = "12345678-abcd-1234-ef00-1234567890ab"; // Sampling feature UUID
164/* clang-format on */
165
166// Count up the number of pointers in the array
167int variableCount = sizeof(variableList) / sizeof(variableList[0]);
168
169// Create the VariableArray object
170VariableArray varArray(variableCount, variableList, UUIDs);
171/** End [variable_arrays] */
172
173
174// ==========================================================================
175// The Logger Object[s]
176// ==========================================================================
177/** Start [loggers] */
178// Create a new logger instance
179Logger dataLogger(LoggerID, loggingInterval, &varArray);
180/** End [loggers] */
181
182
183// ==========================================================================
184// Working Functions
185// ==========================================================================
186/** Start [working_functions] */
187// Flashes the LED's on the primary board
188void greenredflash(uint8_t numFlash = 4, uint8_t rate = 75) {
189 for (uint8_t i = 0; i < numFlash; i++) {
190 digitalWrite(greenLED, HIGH);
191 digitalWrite(redLED, LOW);
192 delay(rate);
193 digitalWrite(greenLED, LOW);
194 digitalWrite(redLED, HIGH);
195 delay(rate);
196 }
197 digitalWrite(redLED, LOW);
198}
199
200// Reads the battery voltage
201// NOTE: This will actually return the battery level from the previous update!
202float getBatteryVoltage() {
203 if (mcuBoard.sensorValues[0] == -9999) mcuBoard.update();
204 return mcuBoard.sensorValues[0];
205}
206/** End [working_functions] */
207
208
209// ==========================================================================
210// Arduino Setup Function
211// ==========================================================================
212/** Start [setup] */
213void setup() {
214 // Start the primary serial connection
215 Serial.begin(serialBaud);
216
217 // Print a start-up note to the first serial port
218 Serial.print(F("Now running "));
219 Serial.print(sketchName);
220 Serial.print(F(" on Logger "));
221 Serial.println(LoggerID);
222 Serial.println();
223
224 Serial.print(F("Using ModularSensors Library version "));
225 Serial.println(MODULAR_SENSORS_VERSION);
226
227 // Set up pins for the LED's
228 pinMode(greenLED, OUTPUT);
229 digitalWrite(greenLED, LOW);
230 pinMode(redLED, OUTPUT);
231 digitalWrite(redLED, LOW);
232 // Blink the LEDs to show the board is on and starting up
233 greenredflash();
234
235 // Set the timezones for the logger/data and the RTC
236 // Logging in the given time zone
237 Logger::setLoggerTimeZone(timeZone);
238 // It is STRONGLY RECOMMENDED that you set the RTC to be in UTC (UTC+0)
239 Logger::setRTCTimeZone(0);
240
241 // Attach information pins to the logger
242 dataLogger.setLoggerPins(wakePin, sdCardSSPin, sdCardPwrPin, buttonPin,
243 greenLED);
244 dataLogger.setSamplingFeatureUUID(samplingFeature);
245
246 // Begin the logger
247 dataLogger.begin();
248
249 // Note: Please change these battery voltages to match your battery
250 // Set up the sensors, except at lowest battery level
251 if (getBatteryVoltage() > 3.4) {
252 Serial.println(F("Setting up sensors..."));
253 varArray.setupSensors();
254 }
255
256 // Create the log file, adding the default header to it
257 // Do this last so we have the best chance of getting the time correct and
258 // all sensor names correct
259 // Writing to the SD card can be power intensive, so if we're skipping
260 // the sensor setup we'll skip this too.
261 if (getBatteryVoltage() > 3.4) {
262 Serial.println(F("Setting up file on SD card"));
263 dataLogger.turnOnSDcard(
264 true); // true = wait for card to settle after power up
265 dataLogger.createLogFile(true); // true = write a new header
266 dataLogger.turnOffSDcard(
267 true); // true = wait for internal housekeeping after write
268 }
269
270 // Call the processor sleep
271 Serial.println(F("Putting processor to sleep\n"));
272 dataLogger.systemSleep();
273}
274/** End [setup] */
275
276
277// ==========================================================================
278// Arduino Loop Function
279// ==========================================================================
280/** Start [loop] */
281// Use this short loop for simple data logging and sending
282void loop() {
283 // Note: Please change these battery voltages to match your battery
284 // At very low battery, just go back to sleep
285 if (getBatteryVoltage() < 3.4) {
286 dataLogger.systemSleep();
287 }
288 // If the battery is OK, log data
289 else {
290 dataLogger.logData();
291 }
292}
293/** End [loop] */