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

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

It uses the blocking SDI12Recorder API to discover and identify all sensors active on any pin on your board.

Each sensor should have a unique address already - if not, multiple sensors may respond simultaneously 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 searches the configured pin and address ranges and prints identification
13 * information for each active sensor.
14 * THIS CAN BE *REALLY* SLOW TO RUN!!!
15 *
16 * Each sensor should have a unique address already - if not, multiple sensors may
17 * respond simultaneously to the same request and the output will not be readable
18 * by the Arduino.
19 *
20 * To address a sensor, please see Example B: b_address_change.ino
21 */
22
23#include <SDI12Config.h>
24#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && !defined(SDI12_INTERNAL_PCINT)
25#include <EnableInterrupt.h>
26#endif
27
28#include <SDI12Recorder.h>
29
30#ifndef SDI12_DATA_PIN
31#define SDI12_DATA_PIN 7
32#endif
33#ifndef SDI12_POWER_PIN
34#define SDI12_POWER_PIN 22
35#endif
36#ifndef SDI12_TERMINAL_BAUD
37#define SDI12_TERMINAL_BAUD 115200
38#endif
39uint32_t serialBaud = SDI12_TERMINAL_BAUD; /*!< The baud rate for the serial port */
40int8_t powerPin = SDI12_POWER_PIN; /*!< The sensor power pin (or -1) */
41int8_t firstPin = 3; /*! change to lowest pin number to search on your board */
42int8_t lastPin = 7; /*! change to highest pin number to search on your board */
43int8_t firstAddress = 0; /* The first address in the address space to check (0='0') */
44int8_t lastAddress = 61; /* The last address in the address space to check (61='z') */
45
46/**
47 * @brief gets identification information from a sensor, and prints it to the serial
48 * port
49 *
50 * @param recorder the SDI-12 recorder instance
51 * @param address a character between '0'-'9', 'a'-'z', or 'A'-'Z'.
52 */
53void printInfo(SDI12Recorder& recorder, char address) {
54 SDI12Recorder::Identification sensorInfo;
55 if (recorder.identify(address, sensorInfo)) {
56 recorder.printSensorInfo(sensorInfo, Serial);
57 } else {
58 recorder.printRecorderResult(Serial);
59 Serial.println();
60 }
61}
62
63void scanAddressSpace(SDI12Recorder& recorder) {
64 // Quickly scan the address space
65 for (int8_t i = firstAddress; i <= lastAddress; i++) {
66 char addr = SDI12Address::fromIndex(i);
67 Serial.print(F("i="));
68 Serial.print(i);
69 Serial.print(F(" addr="));
70 Serial.println(addr);
71 if (recorder.requestAcknowledgement(addr)) { printInfo(recorder, addr); }
72 }
73}
74
75void setup() {
76 Serial.begin(serialBaud);
77 while (!Serial && millis() < 10000L);
78 char tbuf[3] = {'\0'};
79
80 Serial.println(
81 F("//\n// Start Search for SDI-12 Devices \n// -----------------------"));
82
83 // Power the sensors;
84 if (powerPin >= 0) {
85 Serial.print(F("Powering up sensors with pin "));
86 Serial.print(String(powerPin));
87 Serial.println(F(", wait 10s..."));
88 pinMode(powerPin, OUTPUT);
89 digitalWrite(powerPin, HIGH);
90 delay(10000L);
91 } else {
92 Serial.println(F("Wait 5s..."));
93 delay(5000L);
94 }
95
96 for (int8_t pin = firstPin; pin <= lastPin; pin++) {
97 if (pin != powerPin) {
98 Serial.print(F("Opening SDI-12 bus on pin "));
99 Serial.print(pin);
100 Serial.print(F(" ("));
101 Serial.print(itoa(pin, tbuf, 10));
102 Serial.print(')');
103 Serial.println(F("..."));
104 SDI12 mySDI12(pin);
105 SDI12Recorder mySDI12Recorder(mySDI12);
106 mySDI12.begin();
107 delay(500); // allow things to settle
108
109 scanAddressSpace(mySDI12Recorder);
110 mySDI12.end();
111 }
112 }
113
114 Serial.println(
115 F("\n//\n// End Search for SDI-12 Devices \n// ---------------------"));
116
117 // Cut power
118 digitalWrite(powerPin, LOW);
119}
120
121void loop() {}