@@ -211,11 +211,32 @@ float SDI12::parseFloat(LookaheadMode lookahead, char ignore) {
211
211
/* ================ Constructor, Destructor, begin(), end(), and timeout ============*/
212
212
// Constructor
213
213
SDI12::SDI12 () {
214
+ _dataPin = NULL ;
214
215
_bufferOverflow = false ;
216
+ // SDI-12 protocol says sensors must respond within 15 milliseconds
217
+ // We'll bump that up to 150, just for good measure, but we don't want to
218
+ // wait the whole stream default of 1s for a response.
219
+ setTimeout (150 );
220
+ // Because SDI-12 is mostly used for environmental sensors, we want to be able
221
+ // to distinguish between the '0' that parseInt and parseFloat usually return
222
+ // on timeouts and a real measured 0 value. So we force the timeout response
223
+ // to be -9999, which is not a common value for most variables measured by
224
+ // in-site environmental sensors.
225
+ setTimeoutValue (-9999 );
215
226
}
216
227
SDI12::SDI12 (int8_t dataPin) {
217
- _bufferOverflow = false ;
218
228
_dataPin = dataPin;
229
+ _bufferOverflow = false ;
230
+ // SDI-12 protocol says sensors must respond within 15 milliseconds
231
+ // We'll bump that up to 150, just for good measure, but we don't want to
232
+ // wait the whole stream default of 1s for a response.
233
+ setTimeout (150 );
234
+ // Because SDI-12 is mostly used for environmental sensors, we want to be able
235
+ // to distinguish between the '0' that parseInt and parseFloat usually return
236
+ // on timeouts and a real measured 0 value. So we force the timeout response
237
+ // to be -9999, which is not a common value for most variables measured by
238
+ // in-site environmental sensors.
239
+ setTimeoutValue (-9999 );
219
240
}
220
241
221
242
// Destructor
@@ -231,16 +252,6 @@ SDI12::~SDI12() {
231
252
void SDI12::begin () {
232
253
// setState(SDI12_HOLDING);
233
254
setActive ();
234
- // SDI-12 protocol says sensors must respond within 15 milliseconds
235
- // We'll bump that up to 150, just for good measure, but we don't want to
236
- // wait the whole stream default of 1s for a response.
237
- setTimeout (150 );
238
- // Because SDI-12 is mostly used for environmental sensors, we want to be able
239
- // to distinguish between the '0' that parseInt and parseFloat usually return
240
- // on timeouts and a real measured 0 value. So we force the timeout response
241
- // to be -9999, which is not a common value for most variables measured by
242
- // in-site environmental sensors.
243
- setTimeoutValue (-9999 );
244
255
// Set up the prescaler as needed for timers
245
256
// This function is defined in SDI12_boards.h
246
257
sdi12timer.configSDI12TimerPrescale ();
@@ -352,7 +363,7 @@ void SDI12::setState(SDI12_STATES state) {
352
363
case SDI12_HOLDING: {
353
364
pinMode (_dataPin, INPUT); // Turn off the pull-up resistor
354
365
pinMode (_dataPin, OUTPUT); // Pin mode = output
355
- digitalWrite (_dataPin, LOW); // Pin state = low - hold the line low
366
+ digitalWrite (_dataPin, LOW); // Pin state = low - marking
356
367
setPinInterrupts (false ); // Interrupts disabled on data pin
357
368
break ;
358
369
}
@@ -363,7 +374,7 @@ void SDI12::setState(SDI12_STATES state) {
363
374
break ;
364
375
}
365
376
case SDI12_LISTENING: {
366
- digitalWrite (_dataPin, LOW); // Pin state = low
377
+ digitalWrite (_dataPin, LOW); // Pin state = low (turns off pull-up)
367
378
pinMode (_dataPin, INPUT); // Pin mode = input, pull-up resistor off
368
379
interrupts (); // Enable general interrupts
369
380
setPinInterrupts (true ); // Enable Rx interrupts on data pin
@@ -372,7 +383,7 @@ void SDI12::setState(SDI12_STATES state) {
372
383
}
373
384
default : // SDI12_DISABLED or SDI12_ENABLED
374
385
{
375
- digitalWrite (_dataPin, LOW); // Pin state = low
386
+ digitalWrite (_dataPin, LOW); // Pin state = low (turns off pull-up)
376
387
pinMode (_dataPin, INPUT); // Pin mode = input, pull-up resistor off
377
388
setPinInterrupts (false ); // Interrupts disabled on data pin
378
389
break ;
@@ -393,15 +404,16 @@ void SDI12::forceListen() {
393
404
394
405
/* ================ Waking Up and Talking To Sensors ================================*/
395
406
// this function wakes up the entire sensor bus
396
- void SDI12::wakeSensors () {
407
+ void SDI12::wakeSensors (int8_t extraWakeTime ) {
397
408
setState (SDI12_TRANSMITTING);
398
409
// Universal interrupts can be on while the break and marking happen because
399
410
// timings for break and from the recorder are not critical.
400
411
// Interrupts on the pin are disabled for the entire transmitting state
401
- digitalWrite (_dataPin, HIGH);
402
- delayMicroseconds (lineBreak_micros); // Required break of 12 milliseconds
403
- digitalWrite (_dataPin, LOW);
404
- delayMicroseconds (marking_micros); // Required marking of 8.33 milliseconds
412
+ digitalWrite (_dataPin, HIGH); // break is HIGH
413
+ delayMicroseconds (lineBreak_micros); // Required break of 12 milliseconds (12,000 µs)
414
+ delay (extraWakeTime); // allow the sensors to wake
415
+ digitalWrite (_dataPin, LOW); // marking is LOW
416
+ delayMicroseconds (marking_micros); // Required marking of 8.33 milliseconds(8,333 µs)
405
417
}
406
418
407
419
// this function writes a character out on the data line
@@ -475,24 +487,24 @@ size_t SDI12::write(uint8_t byte) {
475
487
}
476
488
477
489
// this function sends out the characters of the String cmd, one by one
478
- void SDI12::sendCommand (String& cmd) {
479
- wakeSensors (); // set state to transmitting and send break/marking
490
+ void SDI12::sendCommand (String& cmd, int8_t extraWakeTime ) {
491
+ wakeSensors (extraWakeTime ); // wake up sensors
480
492
for (int unsigned i = 0 ; i < cmd.length (); i++) {
481
493
writeChar (cmd[i]); // write each character
482
494
}
483
495
setState (SDI12_LISTENING); // listen for reply
484
496
}
485
497
486
- void SDI12::sendCommand (const char * cmd) {
487
- wakeSensors (); // wake up sensors
498
+ void SDI12::sendCommand (const char * cmd, int8_t extraWakeTime ) {
499
+ wakeSensors (extraWakeTime ); // wake up sensors
488
500
for (int unsigned i = 0 ; i < strlen (cmd); i++) {
489
501
writeChar (cmd[i]); // write each character
490
502
}
491
503
setState (SDI12_LISTENING); // listen for reply
492
504
}
493
505
494
- void SDI12::sendCommand (FlashString cmd) {
495
- wakeSensors (); // wake up sensors
506
+ void SDI12::sendCommand (FlashString cmd, int8_t extraWakeTime ) {
507
+ wakeSensors (extraWakeTime ); // wake up sensors
496
508
for (int unsigned i = 0 ; i < strlen_P ((PGM_P)cmd); i++) {
497
509
// write each character
498
510
writeChar (static_cast <char >(pgm_read_byte ((const char *)cmd + i)));
@@ -505,8 +517,8 @@ void SDI12::sendCommand(FlashString cmd) {
505
517
// that is, when the Arduino itself is acting as an SDI-12 device rather than a
506
518
// recorder).
507
519
void SDI12::sendResponse (String& resp) {
508
- setState (SDI12_TRANSMITTING); // Get ready to send data to the recorder
509
- digitalWrite (_dataPin, LOW);
520
+ setState (SDI12_TRANSMITTING); // Get ready to send data to the recorder
521
+ digitalWrite (_dataPin, LOW); // marking is LOW
510
522
delayMicroseconds (marking_micros); // 8.33 ms marking before response
511
523
for (int unsigned i = 0 ; i < resp.length (); i++) {
512
524
writeChar (resp[i]); // write each character
@@ -515,8 +527,8 @@ void SDI12::sendResponse(String& resp) {
515
527
}
516
528
517
529
void SDI12::sendResponse (const char * resp) {
518
- setState (SDI12_TRANSMITTING); // Get ready to send data to the recorder
519
- digitalWrite (_dataPin, LOW);
530
+ setState (SDI12_TRANSMITTING); // Get ready to send data to the recorder
531
+ digitalWrite (_dataPin, LOW); // marking is LOW
520
532
delayMicroseconds (marking_micros); // 8.33 ms marking before response
521
533
for (int unsigned i = 0 ; i < strlen (resp); i++) {
522
534
writeChar (resp[i]); // write each character
@@ -525,8 +537,8 @@ void SDI12::sendResponse(const char* resp) {
525
537
}
526
538
527
539
void SDI12::sendResponse (FlashString resp) {
528
- setState (SDI12_TRANSMITTING); // Get ready to send data to the recorder
529
- digitalWrite (_dataPin, LOW);
540
+ setState (SDI12_TRANSMITTING); // Get ready to send data to the recorder
541
+ digitalWrite (_dataPin, LOW); // marking is LOW
530
542
delayMicroseconds (marking_micros); // 8.33 ms marking before response
531
543
for (int unsigned i = 0 ; i < strlen_P ((PGM_P)resp); i++) {
532
544
// write each character
0 commit comments