Skip to content

Commit cbbe9db

Browse files
committed
Rearranged setup for better performance
For reasons unbeknownst to me, it was crashing if the NIST sync was before the "begin" function. I didn't figure out why, but rearranging fixed it. The most frustrating thing is that if I turned on the extra print statements for debugging... it magically worked every time, but as soon as they were off again.. it crashed.
1 parent 46ce098 commit cbbe9db

File tree

9 files changed

+111
-58
lines changed

9 files changed

+111
-58
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ After defining your modem, set it up using one of these two commands, depending
237237
- The DTRSleepType controls _how_ the modem is put to sleep between readings. Use "held" if the DTR pin is held HIGH to keep the modem awake, as with a Sodaq GPRSBee rev6. Use "pulsed" if the DTR pin is pulsed high and then low to wake the modem up, as with an Adafruit Fona or Sodaq GPRSBee rev4. Use "reverse" if the DTR pin is held LOW to keep the modem awake, as with all XBees. Use "always_on" if you do not want the library to control the modem power and sleep.
238238

239239
Once the modem has been set up, these functions are available:
240+
- **on()** - Turns the modem on. Returns true if connection is successful.
241+
- **off()** - Turns the modem off and empties the send and receive buffer. Returns true if connection is successful.
240242
- **connectNetwork()** - Connects to the internet via WiFi or cellular network. Returns true if connection is successful.
241243
- **disconnectNetwork()** - Disconnects from the network, if applicable.
242244
- **connect(const char host, uint16_t port)** - Makes a TCP connection to a host url and port. (If you don't know the port, use "80".) Returns 1 if successful.

examples/DRWI_CitSci/DRWI_CitSci.ino

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,10 @@ void setup()
197197

198198
EnviroDIYLogger.modem.setupModem(&ModemSerial, modemVCCPin, modemCTSPin, modemDTRPin, ModemSleepMode, APN);
199199

200-
// Turn on the modem
201-
EnviroDIYLogger.modem.modemOnOff->on();
202-
// Connect to the network
203-
if (EnviroDIYLogger.modem.connectNetwork())
204-
{
205-
// Synchronize the RTC
206-
EnviroDIYLogger.modem.syncDS3231();
207-
// Disconnect from the network
208-
EnviroDIYLogger.modem.disconnectNetwork();
209-
}
210-
// Turn off the modem
211-
EnviroDIYLogger.modem.modemOnOff->off();
212-
213200
#ifdef DreamHostPortalRX
214201
EnviroDIYLogger.setDreamHostPortalRX(DreamHostPortalRX);
215202
#endif
203+
216204
// Begin the logger;
217205
EnviroDIYLogger.begin();
218206
}

examples/double_logger/double_logger.ino

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ void setup()
144144
// Blink the LEDs to show the board is on and starting up
145145
greenred4flash();
146146

147+
// Start the Real Time Clock
148+
rtc.begin();
149+
delay(100);
150+
147151
// Print a start-up note to the first serial port
148152
Serial.print(F("Now running "));
149153
Serial.print(SKETCH_NAME);
@@ -164,27 +168,6 @@ void setup()
164168

165169
modem.setupModem(&ModemSerial, modemVCCPin, modemCTSPin, modemDTRPin, ModemSleepMode, SSID, PWD);
166170

167-
// Turn on the modem
168-
modem.modemOnOff->on();
169-
// Connect to the network
170-
if (modem.connectNetwork())
171-
{
172-
// Synchronize the RTC
173-
modem.syncDS3231();
174-
// Disconnect from the network
175-
modem.disconnectNetwork();
176-
}
177-
// Turn off the modem
178-
modem.modemOnOff->off();
179-
180-
// Start the Real Time Clock
181-
rtc.begin();
182-
delay(100);
183-
184-
// Print a start-up note to the first serial port
185-
Serial.print(F("Current RTC time is: "));
186-
Serial.println(Logger::formatDateTime_ISO8601(Logger::getNow()));
187-
188171
// Set up the sensors on both loggers
189172
logger1min.setupSensors();
190173
logger5min.setupSensors();
@@ -203,6 +186,23 @@ void setup()
203186
// Create a header for the second logger and write it to the SD card
204187
logger5min.logToSD(logger5min.generateFileHeader());
205188

189+
// Print out the current time
190+
Serial.print(F("Current RTC time is: "));
191+
Serial.println(Logger::formatDateTime_ISO8601(Logger::getNow()));
192+
193+
// Turn on the modem
194+
modem.on();
195+
// Connect to the network
196+
if (modem.connectNetwork())
197+
{
198+
// Synchronize the RTC
199+
modem.syncDS3231();
200+
// Disconnect from the network
201+
modem.disconnectNetwork();
202+
}
203+
// Turn off the modem
204+
modem.off();
205+
206206
// Set up the processor sleep mode
207207
// Because there's only one processor, we only need to do this once
208208
logger1min.setupSleep();
@@ -274,7 +274,7 @@ void loop()
274274
if (Logger::markedEpochTime % 86400 == 0)
275275
{
276276
// Turn on the modem
277-
modem.modemOnOff->on();
277+
modem.on();
278278
// Connect to the network
279279
if (modem.connectNetwork())
280280
{
@@ -284,7 +284,7 @@ void loop()
284284
modem.disconnectNetwork();
285285
}
286286
// Turn off the modem
287-
modem.modemOnOff->off();
287+
modem.off();
288288
}
289289

290290
// Call the processor sleep

examples/logging_to_EnviroDIY/logging_to_EnviroDIY.ino

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -335,22 +335,10 @@ void setup()
335335
EnviroDIYLogger.modem.setupModem(&ModemSerial, modemVCCPin, modemCTSPin, modemDTRPin, ModemSleepMode, APN);
336336
#endif
337337

338-
// Turn on the modem
339-
EnviroDIYLogger.modem.modemOnOff->on();
340-
// Connect to the network
341-
if (EnviroDIYLogger.modem.connectNetwork())
342-
{
343-
// Synchronize the RTC
344-
EnviroDIYLogger.modem.syncDS3231();
345-
// Disconnect from the network
346-
EnviroDIYLogger.modem.disconnectNetwork();
347-
}
348-
// Turn off the modem
349-
EnviroDIYLogger.modem.modemOnOff->off();
350-
351338
#ifdef DreamHostPortalRX
352339
EnviroDIYLogger.setDreamHostPortalRX(DreamHostPortalRX);
353340
#endif
341+
354342
// Begin the logger;
355343
EnviroDIYLogger.begin();
356344
}

src/LoggerBase.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,23 +502,26 @@ class Logger : public VariableArray
502502
// This calls all of the setup functions - must be run AFTER init
503503
virtual void begin(void)
504504
{
505+
// Print a start-up note to the first serial port
506+
PRINTOUT(F("Beginning logger "), _loggerID, F("\n"));
507+
505508
// Start the Real Time Clock
506509
rtc.begin();
507510
delay(100);
508511

509512
// Set up pins for the LED's
510513
pinMode(_ledPin, OUTPUT);
511514

512-
// Print a start-up note to the first serial port
513-
PRINTOUT(F("Current RTC time is: "));
514-
PRINTOUT(formatDateTime_ISO8601(getNow()), F("\n"));
515-
516515
// Set up the sensors
517516
setupSensors();
518517

519518
// Set up the log file
520519
setupLogFile();
521520

521+
// Print the current time
522+
PRINTOUT(F("Current RTC time is: "));
523+
PRINTOUT(formatDateTime_ISO8601(getNow()), F("\n"));
524+
522525
// Setup sleep mode
523526
if(_sleep){setupSleep();}
524527

src/LoggerDreamHost.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class LoggerDreamHost : public LoggerEnviroDIY
109109
digitalWrite(_ledPin, HIGH);
110110

111111
// Turn on the modem to let it start searching for the network
112-
LoggerEnviroDIY::modem.modemOnOff->on();
112+
LoggerEnviroDIY::modem.on();
113113

114114
// Wake up all of the sensors
115115
// I'm not doing as part of sleep b/c it may take up to a second or
@@ -143,7 +143,7 @@ class LoggerDreamHost : public LoggerEnviroDIY
143143
}
144144

145145
// Turn on the modem off
146-
modem.modemOnOff->off();
146+
modem.off();
147147

148148
// Turn off the LED
149149
digitalWrite(_ledPin, LOW);

src/LoggerEnviroDIY.h

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,48 @@ class LoggerEnviroDIY : public Logger
139139
}
140140

141141
// Convience functions to do it all
142+
// This calls all of the setup functions - must be run AFTER init
143+
virtual void begin(void) override
144+
{
145+
// Print a start-up note to the first serial port
146+
PRINTOUT(F("Beginning logger "), _loggerID, F("\n"));
147+
148+
// Start the Real Time Clock
149+
rtc.begin();
150+
delay(100);
151+
152+
// Set up pins for the LED's
153+
pinMode(_ledPin, OUTPUT);
154+
155+
// Set up the sensors
156+
setupSensors();
157+
158+
// Set up the log file
159+
setupLogFile();
160+
161+
// Sync the clock with NIST
162+
PRINTOUT(F("Current RTC time is: "));
163+
PRINTOUT(formatDateTime_ISO8601(getNow()), F("\n"));
164+
// Turn on the modem
165+
modem.on();
166+
// Connect to the network
167+
if (modem.connectNetwork())
168+
{
169+
// Synchronize the RTC
170+
modem.syncDS3231();
171+
// Disconnect from the network
172+
modem.disconnectNetwork();
173+
}
174+
// Turn off the modem
175+
modem.off();
176+
177+
// Setup sleep mode
178+
if(_sleep){setupSleep();}
179+
180+
PRINTOUT(F("Logger setup finished!\n"));
181+
PRINTOUT(F("------------------------------------------\n\n"));
182+
}
183+
142184
virtual void log(void) override
143185
{
144186
// Check of the current time is an even interval of the logging interval
@@ -150,7 +192,7 @@ class LoggerEnviroDIY : public Logger
150192
digitalWrite(_ledPin, HIGH);
151193

152194
// Turn on the modem to let it start searching for the network
153-
LoggerEnviroDIY::modem.modemOnOff->on();
195+
LoggerEnviroDIY::modem.on();
154196

155197
// Wake up all of the sensors
156198
// I'm not doing as part of sleep b/c it may take up to a second or
@@ -181,7 +223,7 @@ class LoggerEnviroDIY : public Logger
181223
}
182224

183225
// Turn on the modem off
184-
modem.modemOnOff->off();
226+
modem.off();
185227

186228
// Turn off the LED
187229
digitalWrite(_ledPin, LOW);

src/ModemSupport.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ class pulsedOnOff : public ModemOnOff
153153

154154
bool off(void) override
155155
{
156-
if (isOn()) {pulse();}
156+
if (isOn())
157+
{
158+
DBG(F("Pulsing modem off with pin "));
159+
DBG(_onoff_DTR_pin, F("\n"));
160+
pulse();
161+
}
157162
else DBG(F("Modem was not ever on.\n"));
158163
// Wait until is off
159164
for (unsigned long start = millis(); millis() - start < 5000; )
@@ -349,6 +354,26 @@ class loggerModem
349354
init(modemStream, vcc33Pin, status_CTS_pin, onoff_DTR_pin, sleepType);
350355
}
351356

357+
bool on(void)
358+
{
359+
// Check if the modem is on; turn it on if not
360+
if(!modemOnOff->isOn()) return modemOnOff->on();
361+
else return true;
362+
}
363+
364+
bool off(void)
365+
{
366+
bool retVal = true;
367+
// Wait for any sending to complete
368+
stream->flush();
369+
// Check if the modem is on; turn it off if so
370+
if(modemOnOff->isOn()) retVal = modemOnOff->off();
371+
else retVal = true;
372+
// Empty anything out of the receive buffer
373+
dumpBuffer(stream);
374+
return retVal;
375+
}
376+
352377
bool connectNetwork(void)
353378
{
354379
bool retVal = false;
@@ -358,6 +383,8 @@ class loggerModem
358383
// Check again if the modem is on. If it still isn't on, give up
359384
if(!modemOnOff->isOn()) return false;
360385

386+
// WiFi modules don't naturally "disconnect" from the network, so first
387+
// check the connection before resending credentials
361388
#if defined(TINY_GSM_MODEM_XBEE) || defined(TINY_GSM_MODEM_ESP8266)
362389
if (_ssid)
363390
{

src/VariableArray.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class VariableArray
9595
{
9696
// Wake everyone up for set up
9797
success &= _variableList[i]->parentSensor->wake();
98+
delay(10);
9899

99100
// Make 5 attempts to contact the sensor before giving up
100101
while(setupTries < 5)
@@ -106,6 +107,7 @@ class VariableArray
106107
// the variable list until after the sensors have all been
107108
// setup and then all of the variables attached.
108109
sensorSuccess = _variableList[i]->parentSensor->setup();
110+
delay(10);
109111

110112
if(sensorSuccess) break;
111113
else
@@ -120,6 +122,7 @@ class VariableArray
120122

121123
// Put everyone back to sleep
122124
success &= _variableList[i]->parentSensor->sleep();
125+
delay(10);
123126
}
124127

125128
// Now attach all of the variables to their parents

0 commit comments

Comments
 (0)