Example C: Check all Addresses for Active Sensors and Print Status

This is a simple demonstration of the SDI-12 library for Arduino.

It discovers the address of all sensors active on any pin on your board.

Each sensor should have a unique address already - if not, multiple sensors may respond simultaenously to the same request and the output will not be readable by the Arduino.

To address a sensor, please see Example B: b_address_change.ino

PlatformIO Configuration

1; PlatformIO Project Configuration File
2
3[platformio]
4description = SDI-12 Library Example C: Searching all Addresses for Sensors
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/c_check_all_addresses
6
7[env:mayfly]
8monitor_speed = 115200
9board = mayfly
10platform = atmelavr
11framework = arduino
12lib_ldf_mode = deep+
13lib_ignore = RTCZero
14lib_deps =
15 SDI-12

The Complete Example

1/**
2 * @example{lineno} c_check_all_addresses.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>
6 * @date August 2013
7 *
8 * @brief Example C: Check all Addresses for Active Sensors and Print Status
9 *
10 * This is a simple demonstration of the SDI-12 library for Arduino.
11 *
12 * It discovers the address of all sensors active and attached to the board.
13 * THIS CAN BE *REALLY* SLOW TO RUN!!!
14 *
15 * Each sensor should have a unique address already - if not, multiple sensors may
16 * respond simultaenously to the same request and the output will not be readable
17 * by the Arduino.
18 *
19 * To address a sensor, please see Example B: b_address_change.ino
20 */
21
22#include <SDI12.h>
23
24uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
25int8_t powerPin = 22; /*!< The sensor power pin (or -1 if not switching power) */
26#define FirstPin 5 /*! change to lowest pin number on your board */
27#define LastPin 24 /*! change to highest pin number on your board */
28
29/**
30 * @brief gets identification information from a sensor, and prints it to the serial
31 * port expects
32 *
33 * @param sdi the SDI-12 instance
34 * @param i a character between '0'-'9', 'a'-'z', or 'A'-'Z'
35 */
36void printInfo(SDI12 sdi, char i) {
37 String command = "";
38 command += (char)i;
39 command += "I!";
40 sdi.sendCommand(command);
41 sdi.clearBuffer();
42 delay(30);
43
44 Serial.print(" --");
45 Serial.print(i);
46 Serial.print("-- ");
47
48 while (sdi.available()) {
49 Serial.write(sdi.read());
50 delay(10); // 1 character ~ 7.5ms
51 }
52}
53
54// this checks for activity at a particular address
55// expects a char, '0'-'9', 'a'-'z', or 'A'-'Z'
56boolean checkActive(SDI12 sdi, char i) {
57 String myCommand = "";
58 myCommand = "";
59 myCommand += (char)i; // sends basic 'acknowledge' command [address][!]
60 myCommand += "!";
61
62 for (int j = 0; j < 3; j++) { // goes through three rapid contact attempts
63 sdi.sendCommand(myCommand);
64 sdi.clearBuffer();
65 delay(30);
66 if (sdi.available()) { // If we here anything, assume we have an active sensor
67 return true;
68 }
69 }
70 sdi.clearBuffer();
71 return false;
72}
73
74void scanAddressSpace(SDI12 sdi) {
75 // scan address space 0-9
76 for (char i = '0'; i <= '9'; i++)
77 if (checkActive(sdi, i)) { printInfo(sdi, i); }
78 // scan address space a-z
79 for (char i = 'a'; i <= 'z'; i++)
80 if (checkActive(sdi, i)) { printInfo(sdi, i); }
81 // scan address space A-Z
82 for (char i = 'A'; i <= 'Z'; i++)
83 if (checkActive(sdi, i)) { printInfo(sdi, i); };
84}
85
86void setup() {
87 Serial.begin(serialBaud);
88 Serial.println("//\n// Start Search for SDI-12 Devices \n// -----------------------");
89
90 // Power the sensors;
91 if (powerPin >= 0) {
92 Serial.println("Powering up sensors...");
93 pinMode(powerPin, OUTPUT);
94 digitalWrite(powerPin, HIGH);
95 delay(200);
96 }
97
98 for (uint8_t pin = FirstPin; pin <= LastPin; pin++) {
99 if (pin != powerPin) {
100 pinMode(pin, INPUT);
101 SDI12 mySDI12(pin);
102 mySDI12.begin();
103 Serial.print("Checking pin ");
104 Serial.print(pin);
105 Serial.println("...");
106 scanAddressSpace(mySDI12);
107 mySDI12.end();
108 }
109 }
110
111 Serial.println("\n//\n// End Search for SDI-12 Devices \n// ---------------------");
112
113 // Cut power
114 digitalWrite(powerPin, LOW);
115}
116
117void loop() {}