Example F: Basic Data Request to a Single Sensor

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

This is a very basic (stripped down) example where the user initiates a measurement and receives the results to a terminal window without typing numerous commands into the terminal.

PlatformIO Configuration

1; PlatformIO Project Configuration File
2
3[platformio]
4description = SDI-12 Library Example F: Taking Measurements and Requesting Results
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/f_basic_data_request
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} f_basic_data_request.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Ruben Kertesz <github@emnet.net> or \@rinnamon on twitter
6 * @date 2/10/2016
7 *
8 * @brief Example F: Basic Data Request to a Single Sensor
9 *
10 * This is a very basic (stripped down) example where the user initiates a measurement
11 * and receives the results to a terminal window without typing numerous commands into
12 * the terminal.
13 *
14 * Edited by Ruben Kertesz for ISCO Nile 502 2/10/2016
15 */
16
17#include <SDI12.h>
18
19/* connection information */
20uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
21int8_t dataPin = 7; /*!< The pin of the SDI-12 data bus */
22int8_t powerPin = 22; /*!< The sensor power pin (or -1 if not switching power) */
23char sensorAddress = '1'; /*!< The address of the SDI-12 sensor */
24
25/** Define the SDI-12 bus */
26SDI12 mySDI12(dataPin);
27
28String sdiResponse = "";
29String myCommand = "";
30
31void setup() {
32 Serial.begin(serialBaud);
33 while (!Serial)
34 ;
35
36 Serial.println("Opening SDI-12 bus...");
37 mySDI12.begin();
38 delay(500); // allow things to settle
39
40 // Power the sensors;
41 if (powerPin >= 0) {
42 Serial.println("Powering up sensors...");
43 pinMode(powerPin, OUTPUT);
44 digitalWrite(powerPin, HIGH);
45 delay(200);
46 }
47}
48
49void loop() {
50 do { // wait for a response from the serial terminal to do anything
51 delay(30);
52 } while (!Serial.available());
53 Serial.read(); // simply hit enter in the terminal window or press send and the
54 // characters get discarded but now the rest of the loop continues
55
56 // first command to take a measurement
57 myCommand = String(sensorAddress) + "M!";
58 Serial.println(myCommand); // echo command to terminal
59
60 mySDI12.sendCommand(myCommand);
61 delay(30); // wait a while for a response
62
63 while (mySDI12.available()) { // build response string
64 char c = mySDI12.read();
65 if ((c != '\n') && (c != '\r')) {
66 sdiResponse += c;
67 delay(10); // 1 character ~ 7.5ms
68 }
69 }
70 if (sdiResponse.length() > 1)
71 Serial.println(sdiResponse); // write the response to the screen
72 mySDI12.clearBuffer();
73
74
75 delay(1000); // delay between taking reading and requesting data
76 sdiResponse = ""; // clear the response string
77
78
79 // next command to request data from last measurement
80 myCommand = String(sensorAddress) + "D0!";
81 Serial.println(myCommand); // echo command to terminal
82
83 mySDI12.sendCommand(myCommand);
84 delay(30); // wait a while for a response
85
86 while (mySDI12.available()) { // build string from response
87 char c = mySDI12.read();
88 if ((c != '\n') && (c != '\r')) {
89 sdiResponse += c;
90 delay(10); // 1 character ~ 7.5ms
91 }
92 }
93 if (sdiResponse.length() > 1)
94 Serial.println(sdiResponse); // write the response to the screen
95 mySDI12.clearBuffer();
96
97 // now go back to top and wait until user hits enter on terminal window
98}