f_basic_data_request.ino example

Example F: Basic Data Request to a Single Sensor.

Example F: Basic Data Request to a Single Sensor

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.

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

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#ifndef SDI12_DATA_PIN
20#define SDI12_DATA_PIN 7
21#endif
22#ifndef SDI12_POWER_PIN
23#define SDI12_POWER_PIN 22
24#endif
25
26/* connection information */
27uint32_t serialBaud = 115200; /*!< The baud rate for the output serial port */
28int8_t dataPin = SDI12_DATA_PIN; /*!< The pin of the SDI-12 data bus */
29int8_t powerPin = SDI12_POWER_PIN; /*!< The sensor power pin (or -1) */
30char sensorAddress = '1'; /*!< The address of the SDI-12 sensor */
31
32/** Define the SDI-12 bus */
33SDI12 mySDI12(dataPin);
34
35String sdiResponse = "";
36String myCommand = "";
37
38void setup() {
39 Serial.begin(serialBaud);
40 while (!Serial && millis() < 10000L);
41
42 Serial.println("Opening SDI-12 bus...");
43 mySDI12.begin();
44 delay(500); // allow things to settle
45
46 // Power the sensors;
47 if (powerPin >= 0) {
48 Serial.println("Powering up sensors...");
49 pinMode(powerPin, OUTPUT);
50 digitalWrite(powerPin, HIGH);
51 delay(200);
52 }
53}
54
55void loop() {
56 do { // wait for a response from the serial terminal to do anything
57 delay(30);
58 } while (!Serial.available());
59 Serial.read(); // simply hit enter in the terminal window or press send and the
60 // characters get discarded but now the rest of the loop continues
61
62 // first command to take a measurement
63 myCommand = String(sensorAddress) + "M!";
64 Serial.println(myCommand); // echo command to terminal
65
66 mySDI12.sendCommand(myCommand);
67 delay(30); // wait a while for a response
68
69 while (mySDI12.available()) { // build response string
70 char c = mySDI12.read();
71 if ((c != '\n') && (c != '\r')) {
72 sdiResponse += c;
73 delay(10); // 1 character ~ 7.5ms
74 }
75 }
76 if (sdiResponse.length() > 1)
77 Serial.println(sdiResponse); // write the response to the screen
78 mySDI12.clearBuffer();
79
80
81 delay(1000); // delay between taking reading and requesting data
82 sdiResponse = ""; // clear the response string
83
84
85 // next command to request data from last measurement
86 myCommand = String(sensorAddress) + "D0!";
87 Serial.println(myCommand); // echo command to terminal
88
89 mySDI12.sendCommand(myCommand);
90 delay(30); // wait a while for a response
91
92 while (mySDI12.available()) { // build string from response
93 char c = mySDI12.read();
94 if ((c != '\n') && (c != '\r')) {
95 sdiResponse += c;
96 delay(10); // 1 character ~ 7.5ms
97 }
98 }
99 if (sdiResponse.length() > 1)
100 Serial.println(sdiResponse); // write the response to the screen
101 mySDI12.clearBuffer();
102
103 // now go back to top and wait until user hits enter on terminal window
104}