Raw Extended Commands

This advanced example sends one manufacturer-specific command with SDI12Recorder::startRawCommand(). Replace the example command body with the value documented by the sensor manufacturer. Do not include the sensor address or terminating exclamation point.

The raw path validates the response address and retry timing, but it does not interpret the remaining payload or validate a command-specific CRC. It also invalidates retained measurement context for the addressed sensor. Use a typed recorder method for every standard command that has one.

PlatformIO Configuration

1; PlatformIO Project Configuration File
2
3[platformio]
4description = SDI-12 Library Advanced Example: Raw Extended Commands
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/extras/raw_extended_commands
6
7[env:mayfly]
8monitor_speed = 115200
9board = mayfly
10platform = atmelavr
11framework = arduino
12lib_ldf_mode = deep+
13lib_ignore = RTCZero
14lib_deps =
15 SDI-12

The Complete Example

1/**
2 * @example{lineno} raw_extended_commands.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Codex, at instruction of Sara Geleskie Damiano <sdamiano@stroudcenter.org>
6 *
7 * @brief Send a manufacturer-specific extended command through SDI12Recorder.
8 */
9
10#include <SDI12Config.h>
11#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && !defined(SDI12_INTERNAL_PCINT)
12#include <EnableInterrupt.h>
13#endif
14
15#include <SDI12Recorder.h>
16
17#ifndef SDI12_DATA_PIN
18#define SDI12_DATA_PIN 7
19#endif
20#ifndef SDI12_POWER_PIN
21#define SDI12_POWER_PIN 22
22#endif
23#ifndef SDI12_TERMINAL_BAUD
24#define SDI12_TERMINAL_BAUD 115200
25#endif
26
27const int8_t dataPin = SDI12_DATA_PIN;
28const int8_t powerPin = SDI12_POWER_PIN;
29const uint32_t serialBaud = SDI12_TERMINAL_BAUD;
30const char sensorAddress = '0';
31
32// Replace X with the command body documented by the sensor manufacturer. Do not
33// include the sensor address or terminating exclamation point.
34const String extendedCommand = "X";
35
36SDI12 mySDI12(dataPin);
37SDI12Recorder mySDI12Recorder(mySDI12);
38
39bool commandStarted = false;
40bool resultPrinted = false;
41
42void setup() {
43 Serial.begin(serialBaud);
44 while (!Serial && millis() < 10000UL);
45
46 if (powerPin >= 0) {
47 pinMode(powerPin, OUTPUT);
48 digitalWrite(powerPin, HIGH);
49 delay(10000UL);
50 }
51
52 mySDI12.begin();
53}
54
55void loop() {
56 if (!commandStarted) {
57 mySDI12Recorder.startRawCommand(sensorAddress, extendedCommand);
58 commandStarted = true;
59 }
60
61 mySDI12Recorder.update();
62
63 if (!resultPrinted && mySDI12Recorder.isOperationComplete()) {
64 if (mySDI12Recorder.result() == SDI12Recorder::RESULT_SUCCESS) {
65 Serial.print(F("Opaque response payload: "));
66 Serial.println(mySDI12Recorder.response());
67 } else {
68 Serial.print(F("Extended command failed: "));
69 mySDI12Recorder.printRecorderResult(Serial);
70 Serial.println();
71 }
72 resultPrinted = true;
73 }
74}