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
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>
8 * @brief Copy of SDI-12 Example B: Changing the Address of your SDI-12 Sensor
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
13 * @m_examplenavigation{page_extra_helper_sketches,}
17#include <EnableInterrupt.h>
20// The baud rate for the output serial port
21#define SERIAL_BAUD 115200
22// The pin of the SDI-12 data bus
24// The // Sensor power pin (or -1 if not switching power)
27// Define the SDI-12 bus
28SDI12 mySDI12(DATA_PIN);
30String myCommand = ""; // empty to start
31char oldAddress = '!'; // invalid address as placeholder
34// this checks for activity at a particular address
35// expects a char, '0'-'9', 'a'-'z', or 'A'-'Z'
37checkActive(byte i) { // this checks for activity at a particular address
38 Serial.print("Checking address ");
39 Serial.print(static_cast<char>(i));
43 static_cast<char>(i); // sends basic 'acknowledge' command [address][!]
46 for (int j = 0; j < 3; j++) { // goes through three rapid contact attempts
47 mySDI12.sendCommand(myCommand);
49 if (mySDI12.available()) { // If we here anything, assume we have an
51 Serial.println("Occupied");
52 mySDI12.clearBuffer();
55 Serial.println("Vacant"); // otherwise it is vacant.
56 mySDI12.clearBuffer();
64 Serial.begin(SERIAL_BAUD);
69 // Enable interrupts for the receive pin
70 pinMode(DATA_PIN, INPUT_PULLUP);
71 enableInterrupt(DATA_PIN, SDI12::handleInterrupt, CHANGE);
73 Serial.println("Opening SDI-12 bus...");
75 delay(500); // allow things to settle
79 Serial.println("Powering up sensors...");
80 pinMode(POWER_PIN, OUTPUT);
81 digitalWrite(POWER_PIN, HIGH);
87 boolean found = false; // have we identified the sensor yet?
89 for (byte i = '0'; i <= '9'; i++) { // scan address space 0-9
97 for (byte i = 'a'; i <= 'z'; i++) { // scan address space a-z
105 for (byte i = 'A'; i <= 'Z'; i++) { // scan address space A-Z
107 if (checkActive(i)) {
115 "No sensor detected. Check physical connections."); // couldn't
121 Serial.print("Sensor active at address "); // found a sensor!
122 Serial.print(oldAddress);
125 Serial.println("Enter new address."); // prompt for a new address
126 while (!Serial.available()) {
129 char newAdd = Serial.read();
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'.");
139 while (!Serial.available()) {
142 newAdd = Serial.read();
145 // the syntax of the change address command
146 // is:[currentAddress]A[newAddress]!
147 Serial.println("Readdressing sensor.");
149 myCommand += static_cast<char>(oldAddress);
151 myCommand += static_cast<char>(newAdd);
153 mySDI12.sendCommand(myCommand);
155 // wait for the response then throw it away by clearing the buffer with
158 mySDI12.clearBuffer();
160 Serial.println("Success. Re-scanning for verification.");