Example G: Using the Arduino as a Command Terminal for SDI-12 Sensors

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

It's purpose is to allow a user to interact with an SDI-12 sensor directly, issuing commands through a serial terminal window.

PlatformIO Configuration

1; PlatformIO Project Configuration File
2
3[platformio]
4description = SDI-12 Library Example G: Sending SDI-12 Commands from Serial Monitor
5src_dir = .piolibdeps/Arduino-SDI-12_ID1486/examples/g_terminal_window
6
7[env:mayfly]
8monitor_speed = 57600
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} g_terminal_window.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 * @author Ruben Kertesz <github@emnet.net> or \@rinnamon on twitter
8 * @date 2016
9 *
10 * @brief Example G: Using the Arduino as a Command Terminal for SDI-12 Sensors
11 *
12 * This is a simple demonstration of the SDI-12 library for Arduino. It's purpose is to
13 * allow a user to interact with an SDI-12 sensor directly, issuing commands through a
14 * serial terminal window.
15 *
16 * Edited by Ruben Kertesz for ISCO Nile 502 2/10/2016
17 */
18
19#include <SDI12.h>
20
21uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
22int8_t dataPin = 7; /*!< The pin of the SDI-12 data bus */
23int8_t powerPin = 22; /*!< The sensor power pin (or -1 if not switching power) */
24
25/** Define the SDI-12 bus */
26SDI12 mySDI12(dataPin);
27
28char inByte = 0;
29String sdiResponse = "";
30String myCommand = "";
31
32void setup() {
33 Serial.begin(serialBaud);
34 while (!Serial)
35 ;
36
37 Serial.println("Opening SDI-12 bus...");
38 mySDI12.begin();
39 delay(500); // allow things to settle
40
41 // Power the sensors;
42 if (powerPin >= 0) {
43 Serial.println("Powering up sensors...");
44 pinMode(powerPin, OUTPUT);
45 digitalWrite(powerPin, HIGH);
46 delay(200);
47 }
48}
49
50void loop() {
51 if (Serial.available()) {
52 inByte = Serial.read();
53 if ((inByte != '\n') &&
54 (inByte != '\r')) { // read all values entered in terminal window before enter
55 myCommand += inByte;
56 delay(10); // 1 character ~ 7.5ms
57 }
58 }
59
60 if (inByte == '\r') { // once we press enter, send string to SDI sensor/probe
61 inByte = 0;
62 Serial.println(myCommand);
63 mySDI12.sendCommand(myCommand);
64 delay(30); // wait a while for a response
65
66 while (mySDI12.available()) { // build a string of the response
67 char c = mySDI12.read();
68 if ((c != '\n') && (c != '\r')) {
69 sdiResponse += c;
70 delay(10); // 1 character ~ 7.5ms
71 }
72 }
73 if (sdiResponse.length() >= 1)
74 Serial.println(sdiResponse); // write the response to the screen
75
76 mySDI12.clearBuffer(); // clear the line
77 myCommand = "";
78 sdiResponse = "";
79 }
80}