Example A: Using the Wildcard - Getting Single Sensor Information

This is a simple demonstration of the SDI-12 library for Arduino. It uses the blocking SDI12Recorder API to query the address of a single attached sensor, request its identification information, and print the parsed fields to the serial port.

PlatformIO Configuration

1; PlatformIO Project Configuration File
2
3[platformio]
4description = SDI-12 Library Example A: Getting information for a single attached sensor
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/a_wild_card
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} a_wild_card.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 A: Using the Wildcard - Getting Single Sensor Information
9 *
10 * This is a simple demonstration of the SDI-12 library for Arduino.
11 *
12 * It uses the wildcard query to find exactly one attached sensor and prints its
13 * identification information.
14 */
15
16#include <SDI12Config.h>
17#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && !defined(SDI12_INTERNAL_PCINT)
18#include <EnableInterrupt.h>
19#endif
20
21#include <SDI12Recorder.h>
22
23#ifndef SDI12_DATA_PIN
24#define SDI12_DATA_PIN 7
25#endif
26#ifndef SDI12_POWER_PIN
27#define SDI12_POWER_PIN 22
28#endif
29#ifndef SDI12_TERMINAL_BAUD
30#define SDI12_TERMINAL_BAUD 115200
31#endif
32
33uint32_t serialBaud = SDI12_TERMINAL_BAUD; /*!< The baud rate for the serial port */
34int8_t dataPin = SDI12_DATA_PIN; /*!< The pin of the SDI-12 data bus */
35int8_t powerPin = SDI12_POWER_PIN; /*!< The sensor power pin (or -1) */
36
37/** Define the SDI-12 bus and recorder */
38SDI12 mySDI12(dataPin);
39SDI12Recorder mySDI12Recorder(mySDI12);
40
41void setup() {
42 Serial.begin(serialBaud);
43 while (!Serial && millis() < 10000L);
44
45 Serial.print(F("Opening SDI-12 bus on pin "));
46 Serial.print(dataPin);
47 Serial.println(F("..."));
48 mySDI12.begin();
49 delay(500); // allow things to settle
50
51 // Power the sensors;
52 if (powerPin >= 0) {
53 Serial.println(F("Powering up sensors..."));
54 pinMode(powerPin, OUTPUT);
55 digitalWrite(powerPin, HIGH);
56 delay(200);
57 }
58}
59
60void loop() {
61 // The wildcard query works only when exactly one sensor is attached to the bus.
62 const char address = mySDI12Recorder.queryAddress();
63 if (address == '\0') {
64 // A return of null for the address means nothing was found
65 mySDI12Recorder.printRecorderResult(Serial);
66 Serial.println();
67 } else {
68 SDI12Recorder::Identification sensorInfo;
69 if (mySDI12Recorder.identify(address, sensorInfo)) {
70 mySDI12Recorder.printSensorInfo(sensorInfo, Serial);
71 } else {
72 Serial.print(F("Identification failed for address "));
73 Serial.print(address);
74 Serial.print(F(": "));
75 mySDI12Recorder.printRecorderResult(Serial);
76 Serial.println();
77 }
78 }
79 delay(3000); // print again in three seconds
80}