sdi12_address_change.ino example

Copy of SDI-12 Example B: Changing the Address of your SDI-12 Sensor.

Copy of SDI-12 Example B: Changing the Address of your SDI-12 Sensor

The SDI-12 specification is available at: http://www.sdi-12.org/ The library is available at: https://github.com/EnviroDIY/Arduino-SDI-12

1/**
2 * @example{lineno} sdi12_address_change.ino
3 * @copyright Stroud Water Research Center
4 * This example is published under the BSD-3 license.
5 * @author Kevin M.Smith <SDI12@ethosengineering.org>
6 * @date August 2013
7 *
8 * @brief Copy of SDI-12 Example B: Changing the Address of your SDI-12 Sensor
9 *
10 * The SDI-12 specification is available at: http://www.sdi-12.org/
11 * The library is available at: https://github.com/EnviroDIY/Arduino-SDI-12
12 *
13 * @m_examplenavigation{page_extra_helper_sketches,}
14 */
15
16
17#include <EnableInterrupt.h>
18#include <SDI12.h>
19
20// The baud rate for the output serial port
21#define SERIAL_BAUD 115200
22// The pin of the SDI-12 data bus
23#define DATA_PIN 7
24// The // Sensor power pin (or -1 if not switching power)
25#define POWER_PIN 22
26
27// Define the SDI-12 bus
28SDI12 mySDI12(DATA_PIN);
29
30String myCommand = ""; // empty to start
31char oldAddress = '!'; // invalid address as placeholder
32
33
34// this checks for activity at a particular address
35// expects a char, '0'-'9', 'a'-'z', or 'A'-'Z'
36boolean
37checkActive(byte i) { // this checks for activity at a particular address
38 Serial.print("Checking address ");
39 Serial.print(static_cast<char>(i));
40 Serial.print("...");
41 myCommand = "";
42 myCommand +=
43 static_cast<char>(i); // sends basic 'acknowledge' command [address][!]
44 myCommand += "!";
45
46 for (int j = 0; j < 3; j++) { // goes through three rapid contact attempts
47 mySDI12.sendCommand(myCommand);
48 delay(30);
49 if (mySDI12.available()) { // If we here anything, assume we have an
50 // active sensor
51 Serial.println("Occupied");
52 mySDI12.clearBuffer();
53 return true;
54 } else {
55 Serial.println("Vacant"); // otherwise it is vacant.
56 mySDI12.clearBuffer();
57 }
58 }
59 return false;
60}
61
62
63void setup() {
64 Serial.begin(SERIAL_BAUD);
65 while (!Serial) {
66 // wait
67 }
68
69 // Enable interrupts for the receive pin
70 pinMode(DATA_PIN, INPUT_PULLUP);
71 enableInterrupt(DATA_PIN, SDI12::handleInterrupt, CHANGE);
72
73 Serial.println("Opening SDI-12 bus...");
74 mySDI12.begin();
75 delay(500); // allow things to settle
76
77 // Power the sensors;
78 if (POWER_PIN > 0) {
79 Serial.println("Powering up sensors...");
80 pinMode(POWER_PIN, OUTPUT);
81 digitalWrite(POWER_PIN, HIGH);
82 delay(200);
83 }
84}
85
86void loop() {
87 boolean found = false; // have we identified the sensor yet?
88
89 for (byte i = '0'; i <= '9'; i++) { // scan address space 0-9
90 if (found) break;
91 if (checkActive(i)) {
92 found = true;
93 oldAddress = i;
94 }
95 }
96
97 for (byte i = 'a'; i <= 'z'; i++) { // scan address space a-z
98 if (found) break;
99 if (checkActive(i)) {
100 found = true;
101 oldAddress = i;
102 }
103 }
104
105 for (byte i = 'A'; i <= 'Z'; i++) { // scan address space A-Z
106 if (found) break;
107 if (checkActive(i)) {
108 found = true;
109 oldAddress = i;
110 }
111 }
112
113 if (!found) {
114 Serial.println(
115 "No sensor detected. Check physical connections."); // couldn't
116 // find a
117 // sensor.
118 // check
119 // connections..
120 } else {
121 Serial.print("Sensor active at address "); // found a sensor!
122 Serial.print(oldAddress);
123 Serial.println(".");
124
125 Serial.println("Enter new address."); // prompt for a new address
126 while (!Serial.available()) {
127 // wait
128 }
129 char newAdd = Serial.read();
130
131 // wait for valid response
132 while (((newAdd < '0') || (newAdd > '9')) &&
133 ((newAdd < 'a') || (newAdd > 'z')) &&
134 ((newAdd < 'A') || (newAdd > 'Z'))) {
135 if (!(newAdd == '\n') || (newAdd == '\r') || (newAdd == ' ')) {
136 Serial.println("Not a valid address. Please enter '0'-'9', "
137 "'a'-'A', or 'z'-'Z'.");
138 }
139 while (!Serial.available()) {
140 // wait
141 }
142 newAdd = Serial.read();
143 }
144
145 // the syntax of the change address command
146 // is:[currentAddress]A[newAddress]!
147 Serial.println("Readdressing sensor.");
148 myCommand = "";
149 myCommand += static_cast<char>(oldAddress);
150 myCommand += "A";
151 myCommand += static_cast<char>(newAdd);
152 myCommand += "!";
153 mySDI12.sendCommand(myCommand);
154
155 // wait for the response then throw it away by clearing the buffer with
156 // clearBuffer()
157 delay(300);
158 mySDI12.clearBuffer();
159
160 Serial.println("Success. Re-scanning for verification.");
161 }
162}