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