This is a simple demonstration of the SDI-12 library for Arduino.
This is a very basic (stripped down) example where the user initiates a measurement with a CRC check and receives and verifies the CRC response
PlatformIO Configuration
1; PlatformIO Project Configuration File
4description = SDI-12 Library Example L: Verifying CRC Values
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/l_verify_crc
The Complete Example
2 * @example{lineno} l_verify_crc.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Ruben Kertesz <github@emnet.net> or \@rinnamon on twitter
8 * @brief Example L: Verify CRC
10 * This example initiates a measurement anc checks the CRC on the returns.
15/* connection information */
16uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
17int8_t dataPin = 7; /*!< The pin of the SDI-12 data bus */
18int8_t powerPin = 22; /*!< The sensor power pin (or -1 if not switching power) */
19char sensorAddress = '2'; /*!< The address of the SDI-12 sensor */
21/** Define the SDI-12 bus */
22SDI12 mySDI12(dataPin);
24String sdiResponse = "";
28 Serial.begin(serialBaud);
32 Serial.println("Opening SDI-12 bus...");
34 delay(500); // allow things to settle
38 Serial.println("Powering up sensors...");
39 pinMode(powerPin, OUTPUT);
40 digitalWrite(powerPin, HIGH);
44 // print out the sensor info
46 command += String(sensorAddress);
48 mySDI12.sendCommand(command);
50 Serial.println(command);
53 sdiResponse = mySDI12.readStringUntil('\n');
55 // allccccccccmmmmmmvvvxxx...xx<CR><LF>
57 Serial.println(sdiResponse);
59 Serial.print("Address: ");
60 Serial.print(sdiResponse.substring(0, 1)); // address
61 Serial.print(", SDI-12 Version: ");
62 Serial.print(sdiResponse.substring(1, 3).toFloat() / 10); // SDI-12 version number
63 Serial.print(", Vendor ID: ");
64 Serial.print(sdiResponse.substring(3, 11)); // vendor id
65 Serial.print(", Sensor Model: ");
66 Serial.print(sdiResponse.substring(11, 17)); // sensor model
67 Serial.print(", Sensor Version: ");
68 Serial.print(sdiResponse.substring(17, 20)); // sensor version
69 Serial.print(", Sensor ID: ");
70 Serial.print(sdiResponse.substring(20)); // sensor id
75 // first command to take a measurement
76 myCommand = String(sensorAddress) + "MC!";
78 Serial.println(myCommand); // echo command to terminal
80 mySDI12.sendCommand(myCommand);
83 // wait for acknowlegement with format [address][ttt (3 char, seconds)][number of
84 // measurments available, 0-9]
85 String sdiResponse = mySDI12.readStringUntil('\n');
88 Serial.println(sdiResponse);
89 mySDI12.clearBuffer();
91 // find out how long we have to wait (in seconds).
92 uint8_t meas_time_s = sdiResponse.substring(1, 4).toInt();
93 Serial.print("expected measurement time: ");
94 Serial.print(meas_time_s);
97 // Set up the number of results to expect
98 int numResults = sdiResponse.substring(4).toInt();
99 Serial.print("Number Results: ");
100 Serial.println(numResults);
102 // listen for measurement to finish
103 unsigned long timerStart = millis();
104 while ((millis() - timerStart) < (static_cast<uint32_t>(meas_time_s) + 1) * 1000) {
105 if (mySDI12.available()) // sensor can interrupt us to let us know it is done early
107 unsigned long measTime = millis() - timerStart;
109 Serial.println(mySDI12.readStringUntil('\n'));
110 Serial.print("Completed after ");
111 Serial.print(measTime);
112 Serial.println(" ms");
118 // next command to request data from last measurement
119 myCommand = String(sensorAddress) + "D0!";
121 Serial.println(myCommand); // echo command to terminal
123 mySDI12.sendCommand(myCommand);
124 delay(30); // wait a while for a response
127 sdiResponse = mySDI12.readStringUntil('\n');
130 Serial.println(sdiResponse); // write the response to the screen
131 bool crcMatch = mySDI12.verifyCRC(sdiResponse);
133 Serial.println("CRC matches!");
135 Serial.println("CRC check failed!");
137 mySDI12.clearBuffer();