Example B: Changing the Address of your SDI-12 Sensor.
Example B: Changing the Address of your SDI-12 Sensor
This is a simple demonstration of the SDI-12 library for arduino. It discovers the address of the attached sensor and allows you to change it.
2 * @example{lineno} b_address_change.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Kevin M.Smith <SDI12@ethosengineering.org>
8 * @brief Example B: Changing the Address of your SDI-12 Sensor
10 * This is a simple demonstration of the SDI-12 library for arduino.
11 * It discovers the address of the attached sensor and allows you to change it.
17#define SDI12_DATA_PIN 7
19#ifndef SDI12_POWER_PIN
20#define SDI12_POWER_PIN 22
23uint32_t serialBaud = 57600; /*!< 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) */
26uint32_t wake_delay = 0; /*!< Extra time needed for the sensor to wake (0-100ms) */
28/** Define the SDI-12 bus */
29SDI12 mySDI12(dataPin);
31String myCommand = ""; // empty to start
32char oldAddress = '!'; // invalid address as placeholder
35 * @brief gets identification information from a sensor, and prints it to the serial
38 * @param i a character between '0'-'9', 'a'-'z', or 'A'-'Z'.
40void printInfo(char i) {
44 mySDI12.sendCommand(command, wake_delay);
47 String sdiResponse = mySDI12.readStringUntil('\n');
49 // allccccccccmmmmmmvvvxxx...xx<CR><LF>
50 Serial.print(sdiResponse.substring(0, 1)); // address
52 Serial.print(sdiResponse.substring(1, 3).toFloat() / 10); // SDI-12 version number
54 Serial.print(sdiResponse.substring(3, 11)); // vendor id
56 Serial.print(sdiResponse.substring(11, 17)); // sensor model
58 Serial.print(sdiResponse.substring(17, 20)); // sensor version
60 Serial.print(sdiResponse.substring(20)); // sensor id
64// this checks for activity at a particular address
65// expects a char, '0'-'9', 'a'-'z', or 'A'-'Z'
66bool checkActive(byte i) { // this checks for activity at a particular address
67 Serial.print("Checking address ");
68 Serial.print((char)i);
71 myCommand += (char)i; // sends basic 'acknowledge' command [address][!]
74 for (int j = 0; j < 3; j++) { // goes through three rapid contact attempts
75 mySDI12.sendCommand(myCommand, wake_delay);
77 if (mySDI12.available()) { // If we hear anything, assume we have an active sensor
78 Serial.println("Occupied");
79 mySDI12.clearBuffer();
82 Serial.println("Vacant"); // otherwise it is vacant.
83 mySDI12.clearBuffer();
90 Serial.begin(serialBaud);
91 while (!Serial && millis() < 10000L);
93 Serial.println("Opening SDI-12 bus...");
95 delay(500); // allow things to settle
97 Serial.println("Timeout value: ");
98 Serial.println(mySDI12.TIMEOUT);
100 // Power the sensors;
102 Serial.println("Powering up sensors, wait 30s...");
103 pinMode(powerPin, OUTPUT);
104 digitalWrite(powerPin, HIGH);
110 boolean found = false; // have we identified the sensor yet?
112 for (byte i = '0'; i <= '9'; i++) { // scan address space 0-9
114 if (checkActive(i)) {
121 for (byte i = 'a'; i <= 'z'; i++) { // scan address space a-z
123 if (checkActive(i)) {
130 for (byte i = 'A'; i <= 'Z'; i++) { // scan address space A-Z
132 if (checkActive(i)) {
141 "No sensor detected. Check physical connections."); // couldn't find a sensor.
142 // check connections..
146 Serial.print("Sensor active at address "); // found a sensor!
147 Serial.print(oldAddress);
150 Serial.println("Enter new address."); // prompt for a new address
151 while (!Serial.available());
152 char newAdd = Serial.read();
154 // wait for valid response
155 while (((newAdd < '0') || (newAdd > '9')) && ((newAdd < 'a') || (newAdd > 'z')) &&
156 ((newAdd < 'A') || (newAdd > 'Z'))) {
157 if (!(newAdd == '\n') || (newAdd == '\r') || (newAdd == ' ')) {
159 "Not a valid address. Please enter '0'-'9', 'a'-'A', or 'z'-'Z'.");
161 while (!Serial.available());
162 newAdd = Serial.read();
165 /* the syntax of the change address command is:
166 [currentAddress]A[newAddress]! */
168 Serial.println("Readdressing sensor.");
170 myCommand += (char)oldAddress;
172 myCommand += (char)newAdd;
174 mySDI12.sendCommand(myCommand);
176 /* wait for the response then throw it away by
177 clearing the buffer with clearBuffer() */
179 mySDI12.clearBuffer();
181 Serial.println("Success. Rescanning for verification.");