2 * @example{lineno} TestWarmUp.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Sara Damiano <sdamiano@stroudcenter.org>
12#define SDI12_DATA_PIN 7
14#ifndef SDI12_POWER_PIN
15#define SDI12_POWER_PIN 22
18/* connection information */
19uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
20int8_t dataPin = SDI12_DATA_PIN; /*!< The pin of the SDI-12 data bus */
21char sensorAddress = '0'; /*!< The address of the SDI-12 sensor */
22int8_t powerPin = SDI12_POWER_PIN; /*!< The sensor power pin (or -1) */
24/** Define the SDI-12 bus */
25SDI12 mySDI12(dataPin);
27/** Define some testing specs */
29/** Error codes, if returned */
30int8_t error_result_number = 7;
31float no_error_value = 0;
33/** Testing turning off power */
34int32_t min_power_delay = 100L; /*!< The min time to test wake after power on. */
35int32_t max_power_delay = 10000L; /*!< The max time to test wake after power on. */
36int32_t increment_power = 100; /*!< The time to lengthen waits between reps. */
38/** Testing the length of the break */
39int32_t min_wake_delay = 0; /*!< The min time to test wake after a line break. */
40int32_t max_wake_delay = 100; /*!< The max time to test wake (should be <=100). */
41int32_t increment_wake = 5; /*!< The time to lengthen waits between reps. */
43/** set some initial values */
44int32_t power_delay = min_power_delay;
45int32_t wake_delay = min_wake_delay;
47// this checks for activity at a particular address
48// expects a char, '0'-'9', 'a'-'z', or 'A'-'Z'
49bool checkActive(char address, int8_t numPings = 3, bool printCommands = false) {
51 command += (char)address; // sends basic 'acknowledge' command [address][!]
54 for (int j = 0; j < numPings; j++) { // goes through three rapid contact attempts
57 Serial.println(command);
59 mySDI12.sendCommand(command, wake_delay);
61 // the sensor should just return its address
62 String sdiResponse = mySDI12.readStringUntil('\n');
66 Serial.println(sdiResponse);
68 mySDI12.clearBuffer();
70 // check the address, return false if it's incorrect
71 String returned_address = sdiResponse.substring(0, 1);
72 char ret_addr_array[2];
73 returned_address.toCharArray(ret_addr_array, sizeof(ret_addr_array));
74 if (returned_address == String(address)) { return true; }
76 mySDI12.clearBuffer();
81 * @brief gets identification information from a sensor, and prints it to the serial
84 * @param i a character between '0'-'9', 'a'-'z', or 'A'-'Z'.
86bool printInfo(char i, bool printCommands = true) {
90 mySDI12.sendCommand(command, wake_delay);
93 Serial.println(command);
97 String sdiResponse = mySDI12.readStringUntil('\n');
99 // allccccccccmmmmmmvvvxxx...xx<CR><LF>
102 Serial.println(sdiResponse);
105 Serial.print("Address: ");
106 Serial.print(sdiResponse.substring(0, 1)); // address
107 Serial.print(", SDI-12 Version: ");
108 Serial.print(sdiResponse.substring(1, 3).toFloat() / 10); // SDI-12 version number
109 Serial.print(", Vendor ID: ");
110 Serial.print(sdiResponse.substring(3, 11)); // vendor id
111 Serial.print(", Sensor Model: ");
112 Serial.print(sdiResponse.substring(11, 17)); // sensor model
113 Serial.print(", Sensor Version: ");
114 Serial.print(sdiResponse.substring(17, 20)); // sensor version
115 Serial.print(", Sensor ID: ");
116 Serial.print(sdiResponse.substring(20)); // sensor id
119 if (sdiResponse.length() < 3) { return false; };
124 Serial.begin(serialBaud);
125 while (!Serial && millis() < 10000L);
127 Serial.println("Opening SDI-12 bus...");
129 delay(500); // allow things to settle
131 Serial.println("Timeout value: ");
132 Serial.println(mySDI12.TIMEOUT);
136 while (wake_delay <= max_wake_delay) {
137 Serial.println("-------------------------------------------------------------------"
139 // Power the sensors;
141 Serial.println("Powering down sensors...");
142 pinMode(powerPin, OUTPUT);
143 digitalWrite(powerPin, LOW);
147 // Power the sensors;
149 Serial.println("Powering up sensors...");
150 pinMode(powerPin, OUTPUT);
151 digitalWrite(powerPin, HIGH);
153 mySDI12.clearBuffer();
156 if (checkActive(sensorAddress, 1, true)) {
157 Serial.print("Got some response after ");
158 Serial.print(power_delay);
159 Serial.print("ms after power with ");
160 Serial.print(wake_delay);
161 Serial.println("ms with wake delay");
162 if (printInfo(sensorAddress, true)) {
163 // if we got sensor info, stop
164 Serial.println("Looks good. Stopping.");
167 Serial.println("Sensor info not valid!");
170 Serial.print("No response after ");
171 Serial.print(power_delay);
172 Serial.print("ms after power with ");
173 Serial.print(wake_delay);
174 Serial.println("ms with wake delay");
177 Serial.print("Increasing the power delay by ");
178 Serial.print(increment_power);
179 Serial.println("ms");
180 power_delay += increment_power;
181 Serial.print("The next delay will be ");
182 Serial.print(power_delay);
183 Serial.println("ms");
184 if (power_delay > max_power_delay) {
185 Serial.println("Reached maximum power delay!");
186 Serial.println("FINISHED!!");