Using a Single Sensor

This somewhat trivial example show making use of the unified set of commands to print data from a MaxBotix ultrasonic range finder to the serial port. It also shows creating a calculated variable which is the water depth.



Unique Features of the Single Sensor Example

  • Only communicates with and collects data from a single sensor.
  • Does not make use of any VariableArray or logging features.

To Use this Example

Prepare and set up PlatformIO

  • Create a new PlatformIO project
  • Replace the contents of the platformio.ini for your new project with the platformio.ini file in the examples/single_sensor folder on GitHub.
    • It is important that your PlatformIO configuration has the lib_ldf_mode and build flags set as they are in the example.
    • Without this, the program won't compile.
  • Open single_sensor.ino and save it to your computer. Put it into the src directory of your project.
    • Delete main.cpp in that folder.

Upload!

  • Upload and see what happens

PlatformIO Configuration

1; PlatformIO Project Configuration File
2;
3; Build options: build flags, source filter
4; Upload options: custom upload port, speed and extra flags
5; Library options: dependencies, extra library storages
6; Advanced options: extra scripting
7;
8; Please visit documentation for the other options and examples
9; http://docs.platformio.org/page/projectconf.html
10
11[platformio]
12description = ModularSensors example requesting values from a single sensor
13
14[env:mayfly]
15monitor_speed = 115200
16board = mayfly
17platform = atmelavr
18framework = arduino
19lib_ldf_mode = deep+
20lib_ignore =
21 RTCZero
22 Adafruit NeoPixel
23 Adafruit GFX Library
24 Adafruit SSD1306
25 Adafruit ADXL343
26 Adafruit STMPE610
27 Adafruit TouchScreen
28 Adafruit ILI9341
29build_flags =
30 -DSDI12_EXTERNAL_PCINT
31 -DNEOSWSERIAL_EXTERNAL_PCINT
32 -DMQTT_MAX_PACKET_SIZE=240
33 -DTINY_GSM_RX_BUFFER=64
34 -DTINY_GSM_YIELD_MS=2
35lib_deps =
36 envirodiy/EnviroDIY_ModularSensors
37; ^^ Use this when working from an official release of the library
38; https://github.com/EnviroDIY/ModularSensors.git#develop
39; ^^ Use this when if you want to pull from the develop branch

The Complete Code

1/** =========================================================================
2 * @example{lineno} single_sensor.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Sara Geleskie Damiano <sdamiano@stroudcenter.org>
6 *
7 * @brief An example using only sensor functions and no logging.
8 *
9 * See [the walkthrough page](@ref example_single_sensor) for detailed
10 * instructions.
11 *
12 * @m_examplenavigation{example_single_sensor,}
13 * ======================================================================= */
14
15// ==========================================================================
16// Include the base required libraries
17// ==========================================================================
18/** Start [includes] */
19// The Arduino library is needed for every Arduino program.
20#include <Arduino.h>
21
22// Include the main header for ModularSensors
23#include <ModularSensors.h>
24/** End [includes] */
25
26// ==========================================================================
27// Board setup info
28// ==========================================================================
29/** Start [sketch_info] */
30// The name of this program file
31const char* sketchName = "single_sensor.ino";
32
33const int32_t serialBaud = 115200; // Baud rate for debugging
34const int8_t greenLED = 8; // Pin for the green LED
35const int8_t redLED = 9; // Pin for the red LED
36/** End [sketch_info] */
37
38
39// ==========================================================================
40// Set up the sensor object
41// ==========================================================================
42/** Start [sensor] */
43#include <sensors/MaxBotixSonar.h>
44
45// Create a reference to the serial port for the sonar
46HardwareSerial& sonarSerial = Serial1; // Use hardware serial if possible
47
48const int8_t SonarPower = 22; // excite (power) pin
49const int SonarTrigger = -1; // Trigger pin
50
51// Create a new instance of the sonar sensor;
52MaxBotixSonar sonar(sonarSerial, SonarPower, SonarTrigger);
53
54// Create a new instance of the range variable;
55MaxBotixSonar_Range sonar_range(&sonar);
56/** End [sensor] */
57
58/* Start [calculated variables] */
59// Create a function to calculate the water depth from the sonar range
60// For this example, we'll assume that the sonar is mounted 5m above the stream
61// bottom
62float calcDepth(void) {
63 float mountHeight = 5000;
64 float sonarRange = sonar_range.getValue();
65 return mountHeight - sonarRange;
66}
67// Create a calculated variable for the water depth
68// Variable calcVar(functionName, VariableName, VariableUnit, Resolution, UUID,
69// Code); VariableName must be a value from
70// http://vocabulary.odm2.org/variablename/ VariableUnit must be a value from
71// http://vocabulary.odm2.org/units/
72Variable waterDepth(calcDepth, 0, "waterDepth", "millimeter", "sonarDepth",
73 "12345678-abcd-1234-ef00-1234567890ab");
74/** End [calculated_variables] */
75
76
77// ==========================================================================
78// Working Functions
79// ==========================================================================
80/** Start [working_functions] */
81// Flashes to Mayfly's LED's
82void greenRedFlash(int numFlash = 4) {
83 for (int i = 0; i < numFlash; i++) {
84 digitalWrite(greenLED, HIGH);
85 digitalWrite(redLED, LOW);
86 delay(75);
87 digitalWrite(greenLED, LOW);
88 digitalWrite(redLED, HIGH);
89 delay(75);
90 }
91 digitalWrite(redLED, LOW);
92}
93/** End [working_functions] */
94
95
96// ==========================================================================
97// Arduino Setup Function
98// ==========================================================================
99/** Start [setup] */
100void setup() {
101 // Start the primary serial connection
102 Serial.begin(serialBaud);
103
104 // Print a start-up note to the first serial port
105 Serial.print(F("Now running "));
106 Serial.println(sketchName);
107
108 Serial.print(F("Using ModularSensors Library version "));
109 Serial.println(MODULAR_SENSORS_VERSION);
110
111 // Start the stream for the sonar
112 sonarSerial.begin(9600);
113
114 // Set up pins for the LED's
115 pinMode(greenLED, OUTPUT);
116 pinMode(redLED, OUTPUT);
117 // Blink the LEDs to show the board is on and starting up
118 greenRedFlash();
119
120 // Print a start-up note to the first serial port
121 Serial.println(F("Single Sensor Example - Sonar Ranging"));
122
123 // Set up the sensor
124 sonar.setup();
125}
126/** End [setup] */
127
128
129// ==========================================================================
130// Arduino Loop Function
131// ==========================================================================
132/** Start [loop] */
133void loop() {
134 // Turn on the LED to show we're taking a reading
135 digitalWrite(greenLED, HIGH);
136
137 // Send power to the sensor
138 sonar.powerUp();
139
140 // Wake up the sensor
141 sonar.wake();
142
143 // Update the sensor value
144 sonar.update();
145
146 // Print the sonar result
147 Serial.print("Current sonar range: ");
148 Serial.println(sonar_range.getValueString());
149 Serial.print("Calculated water depth: ");
150 Serial.println(waterDepth.getValueString());
151
152 // Put the sensor back to sleep
153 sonar.sleep();
154
155 // Cut the sensor power
156 sonar.powerDown();
157
158 // Turn off the LED to show we're done with the reading
159 digitalWrite(greenLED, LOW);
160
161 // Wait for the next reading
162 delay(5000);
163}
164/** End [loop] */