TestWarmUp.ino example

1/**
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>
6 * @date March 2021
7 */
8
9#include <SDI12.h>
10
11#ifndef SDI12_DATA_PIN
12#define SDI12_DATA_PIN 7
13#endif
14#ifndef SDI12_POWER_PIN
15#define SDI12_POWER_PIN 22
16#endif
17
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) */
23
24/** Define the SDI-12 bus */
25SDI12 mySDI12(dataPin);
26
27/** Define some testing specs */
28
29/** Error codes, if returned */
30int8_t error_result_number = 7;
31float no_error_value = 0;
32
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. */
37
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. */
42
43/** set some initial values */
44int32_t power_delay = min_power_delay;
45int32_t wake_delay = min_wake_delay;
46
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) {
50 String command = "";
51 command += (char)address; // sends basic 'acknowledge' command [address][!]
52 command += "!";
53
54 for (int j = 0; j < numPings; j++) { // goes through three rapid contact attempts
55 if (printCommands) {
56 Serial.print(">>>");
57 Serial.println(command);
58 }
59 mySDI12.sendCommand(command, wake_delay);
60
61 // the sensor should just return its address
62 String sdiResponse = mySDI12.readStringUntil('\n');
63 sdiResponse.trim();
64 if (printCommands) {
65 Serial.print("<<<");
66 Serial.println(sdiResponse);
67 }
68 mySDI12.clearBuffer();
69
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; }
75 }
76 mySDI12.clearBuffer();
77 return false;
78}
79
80/**
81 * @brief gets identification information from a sensor, and prints it to the serial
82 * port
83 *
84 * @param i a character between '0'-'9', 'a'-'z', or 'A'-'Z'.
85 */
86bool printInfo(char i, bool printCommands = true) {
87 String command = "";
88 command += (char)i;
89 command += "I!";
90 mySDI12.sendCommand(command, wake_delay);
91 if (printCommands) {
92 Serial.print(">>>");
93 Serial.println(command);
94 }
95 delay(100);
96
97 String sdiResponse = mySDI12.readStringUntil('\n');
98 sdiResponse.trim();
99 // allccccccccmmmmmmvvvxxx...xx<CR><LF>
100 if (printCommands) {
101 Serial.print("<<<");
102 Serial.println(sdiResponse);
103 }
104
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
117 Serial.println();
118
119 if (sdiResponse.length() < 3) { return false; };
120 return true;
121}
122
123void setup() {
124 Serial.begin(serialBaud);
125 while (!Serial && millis() < 10000L);
126
127 Serial.println("Opening SDI-12 bus...");
128 mySDI12.begin();
129 delay(500); // allow things to settle
130
131 Serial.println("Timeout value: ");
132 Serial.println(mySDI12.TIMEOUT);
133}
134
135void loop() {
136 while (wake_delay <= max_wake_delay) {
137 Serial.println("-------------------------------------------------------------------"
138 "------------");
139 // Power the sensors;
140 if (powerPin >= 0) {
141 Serial.println("Powering down sensors...");
142 pinMode(powerPin, OUTPUT);
143 digitalWrite(powerPin, LOW);
144 delay(5000L);
145 }
146
147 // Power the sensors;
148 if (powerPin >= 0) {
149 Serial.println("Powering up sensors...");
150 pinMode(powerPin, OUTPUT);
151 digitalWrite(powerPin, HIGH);
152 delay(power_delay);
153 mySDI12.clearBuffer();
154 }
155
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.");
165 while (1);
166 } else {
167 Serial.println("Sensor info not valid!");
168 }
169 } else {
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");
175 }
176
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!!");
187 while (1);
188 }
189 }
190}