l_verify_crc.ino example

Example L: Verify CRC.

Example L: Verify CRC

This example initiates a measurement anc checks the CRC on the returns.

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#ifndef SDI12_DATA_PIN
16#define SDI12_DATA_PIN 7
17#endif
18#ifndef SDI12_POWER_PIN
19#define SDI12_POWER_PIN 22
20#endif
21
22/* connection information */
23uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
24int8_t dataPin = SDI12_DATA_PIN; /*!< The pin of the SDI-12 data bus */
25int8_t powerPin = SDI12_POWER_PIN; /*!< The sensor power pin (or -1) */
26char sensorAddress = '2'; /*!< The address of the SDI-12 sensor */
27
28/** Define the SDI-12 bus */
29SDI12 mySDI12(dataPin);
30
31String sdiResponse = "";
32String myCommand = "";
33
34void setup() {
35 Serial.begin(serialBaud);
36 while (!Serial && millis() < 10000L);
37
38 Serial.println("Opening SDI-12 bus...");
39 mySDI12.begin();
40 delay(500); // allow things to settle
41
42 // Power the sensors;
43 if (powerPin >= 0) {
44 Serial.println("Powering up sensors...");
45 pinMode(powerPin, OUTPUT);
46 digitalWrite(powerPin, HIGH);
47 delay(200);
48 }
49
50 // print out the sensor info
51 String command = "";
52 command += String(sensorAddress);
53 command += "I!";
54 mySDI12.sendCommand(command);
55 Serial.print(">>>");
56 Serial.println(command);
57 delay(30);
58
59 sdiResponse = mySDI12.readStringUntil('\n');
60 sdiResponse.trim();
61 // allccccccccmmmmmmvvvxxx...xx<CR><LF>
62 Serial.print("<<<");
63 Serial.println(sdiResponse);
64
65 Serial.print("Address: ");
66 Serial.print(sdiResponse.substring(0, 1)); // address
67 Serial.print(", SDI-12 Version: ");
68 Serial.print(sdiResponse.substring(1, 3).toFloat() / 10); // SDI-12 version number
69 Serial.print(", Vendor ID: ");
70 Serial.print(sdiResponse.substring(3, 11)); // vendor id
71 Serial.print(", Sensor Model: ");
72 Serial.print(sdiResponse.substring(11, 17)); // sensor model
73 Serial.print(", Sensor Version: ");
74 Serial.print(sdiResponse.substring(17, 20)); // sensor version
75 Serial.print(", Sensor ID: ");
76 Serial.print(sdiResponse.substring(20)); // sensor id
77 Serial.println();
78}
79
80void loop() {
81 // first command to take a measurement
82 myCommand = String(sensorAddress) + "MC!";
83 Serial.print(">>>");
84 Serial.println(myCommand); // echo command to terminal
85
86 mySDI12.sendCommand(myCommand);
87 delay(5);
88
89 // wait for acknowledgement with format [address][ttt (3 char, seconds)][number of
90 // measurements available, 0-9]
91 String sdiResponse = mySDI12.readStringUntil('\n');
92 sdiResponse.trim();
93 Serial.print("<<<");
94 Serial.println(sdiResponse);
95 mySDI12.clearBuffer();
96
97 // find out how long we have to wait (in seconds).
98 uint8_t meas_time_s = sdiResponse.substring(1, 4).toInt();
99 Serial.print("expected measurement time: ");
100 Serial.print(meas_time_s);
101 Serial.print(" s, ");
102
103 // Set up the number of results to expect
104 int numResults = sdiResponse.substring(4).toInt();
105 Serial.print("Number Results: ");
106 Serial.println(numResults);
107
108 // listen for measurement to finish
109 unsigned long timerStart = millis();
110 while ((millis() - timerStart) < (static_cast<uint32_t>(meas_time_s) + 1) * 1000) {
111 if (mySDI12.available()) // sensor can interrupt us to let us know it is done early
112 {
113 unsigned long measTime = millis() - timerStart;
114 Serial.print("<<<");
115 Serial.println(mySDI12.readStringUntil('\n'));
116 Serial.print("Completed after ");
117 Serial.print(measTime);
118 Serial.println(" ms");
119 break;
120 }
121 }
122
123
124 // next command to request data from last measurement
125 myCommand = String(sensorAddress) + "D0!";
126 Serial.print(">>>");
127 Serial.println(myCommand); // echo command to terminal
128
129 mySDI12.sendCommand(myCommand);
130 delay(30); // wait a while for a response
131
132
133 sdiResponse = mySDI12.readStringUntil('\n');
134 sdiResponse.trim();
135 Serial.print("<<<");
136 Serial.println(sdiResponse); // write the response to the screen
137 bool crcMatch = mySDI12.verifyCRC(sdiResponse);
138 if (crcMatch) {
139 Serial.println("CRC matches!");
140 } else {
141 Serial.println("CRC check failed!");
142 }
143 mySDI12.clearBuffer();
144}