g_terminal_window.ino example

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

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.

Edited by Ruben Kertesz for ISCO Nile 502 2/10/2016

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
21#ifndef SDI12_DATA_PIN
22#define SDI12_DATA_PIN 7
23#endif
24#ifndef SDI12_POWER_PIN
25#define SDI12_POWER_PIN 22
26#endif
27
28uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
29int8_t dataPin = SDI12_DATA_PIN; /*!< The pin of the SDI-12 data bus */
30int8_t powerPin = SDI12_POWER_PIN; /*!< The sensor power pin (or -1) */
31
32/** Define the SDI-12 bus */
33SDI12 mySDI12(dataPin);
34
35char inByte = 0;
36String sdiResponse = "";
37String myCommand = "";
38
39void setup() {
40 Serial.begin(serialBaud);
41 while (!Serial && millis() < 10000L);
42
43 Serial.println("Opening SDI-12 bus...");
44 mySDI12.begin();
45 delay(500); // allow things to settle
46
47 // Power the sensors;
48 if (powerPin >= 0) {
49 Serial.println("Powering up sensors...");
50 pinMode(powerPin, OUTPUT);
51 digitalWrite(powerPin, HIGH);
52 delay(200);
53 }
54}
55
56void loop() {
57 if (Serial.available()) {
58 inByte = Serial.read();
59 if ((inByte != '\n') &&
60 (inByte != '\r')) { // read all values entered in terminal window before enter
61 myCommand += inByte;
62 delay(10); // 1 character ~ 7.5ms
63 }
64 }
65
66 if (inByte == '\r') { // once we press enter, send string to SDI sensor/probe
67 inByte = 0;
68 Serial.println(myCommand);
69 mySDI12.sendCommand(myCommand);
70 delay(30); // wait a while for a response
71
72 while (mySDI12.available()) { // build a string of the response
73 char c = mySDI12.read();
74 if ((c != '\n') && (c != '\r')) {
75 sdiResponse += c;
76 delay(10); // 1 character ~ 7.5ms
77 }
78 }
79 if (sdiResponse.length() >= 1)
80 Serial.println(sdiResponse); // write the response to the screen
81
82 mySDI12.clearBuffer(); // clear the line
83 myCommand = "";
84 sdiResponse = "";
85 }
86}