Example L: Verifying CRC Values

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
2
3[platformio]
4description = SDI-12 Library Example L: Verifying CRC Values
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/l_verify_crc
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} 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
6 * @date 2/10/2016
7 *
8 * @brief Example L: Verify CRC
9 *
10 * This example initiates a measurement anc checks the CRC on the returns.
11 */
12
13#include <SDI12.h>
14
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 */
20
21/** Define the SDI-12 bus */
22SDI12 mySDI12(dataPin);
23
24String sdiResponse = "";
25String myCommand = "";
26
27void setup() {
28 Serial.begin(serialBaud);
29 while (!Serial)
30 ;
31
32 Serial.println("Opening SDI-12 bus...");
33 mySDI12.begin();
34 delay(500); // allow things to settle
35
36 // Power the sensors;
37 if (powerPin >= 0) {
38 Serial.println("Powering up sensors...");
39 pinMode(powerPin, OUTPUT);
40 digitalWrite(powerPin, HIGH);
41 delay(200);
42 }
43
44 // print out the sensor info
45 String command = "";
46 command += String(sensorAddress);
47 command += "I!";
48 mySDI12.sendCommand(command);
49 Serial.print(">>>");
50 Serial.println(command);
51 delay(100);
52
53 sdiResponse = mySDI12.readStringUntil('\n');
54 sdiResponse.trim();
55 // allccccccccmmmmmmvvvxxx...xx<CR><LF>
56 Serial.print("<<<");
57 Serial.println(sdiResponse);
58
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
71 Serial.println();
72}
73
74void loop() {
75 // first command to take a measurement
76 myCommand = String(sensorAddress) + "MC!";
77 Serial.print(">>>");
78 Serial.println(myCommand); // echo command to terminal
79
80 mySDI12.sendCommand(myCommand);
81 delay(5);
82
83 // wait for acknowlegement with format [address][ttt (3 char, seconds)][number of
84 // measurments available, 0-9]
85 String sdiResponse = mySDI12.readStringUntil('\n');
86 sdiResponse.trim();
87 Serial.print("<<<");
88 Serial.println(sdiResponse);
89 mySDI12.clearBuffer();
90
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);
95 Serial.print(" s, ");
96
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);
101
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
106 {
107 unsigned long measTime = millis() - timerStart;
108 Serial.print("<<<");
109 Serial.println(mySDI12.readStringUntil('\n'));
110 Serial.print("Completed after ");
111 Serial.print(measTime);
112 Serial.println(" ms");
113 break;
114 }
115 }
116
117
118 // next command to request data from last measurement
119 myCommand = String(sensorAddress) + "D0!";
120 Serial.print(">>>");
121 Serial.println(myCommand); // echo command to terminal
122
123 mySDI12.sendCommand(myCommand);
124 delay(30); // wait a while for a response
125
126
127 sdiResponse = mySDI12.readStringUntil('\n');
128 sdiResponse.trim();
129 Serial.print("<<<");
130 Serial.println(sdiResponse); // write the response to the screen
131 bool crcMatch = mySDI12.verifyCRC(sdiResponse);
132 if (crcMatch) {
133 Serial.println("CRC matches!");
134 } else {
135 Serial.println("CRC check failed!");
136 }
137 mySDI12.clearBuffer();
138}