To explore the concept of leveraging cellular connected devices for monitoring water systems in developing communities, a system has been configured in which flow measurements are taken by a Particle Electron microcontroller, and routinely transferred via cellular to a ThingSpeak dashboard for visualization. This tutorial details the configuration of a prototype water system monitoring device.
The Sustainable Development Goals (SDGs), declared by the United
Nations, include a goal that all humans will have access to safely managed
water services by 2030. Safely managed water services are defined as
access to an improved water source (e.g. piped water, a covered well, or
protected spring) that is on-premises, available when needed, and free of
microbiological and priority chemical contaminants. As of 2016, an
estimated 2.1 billion people do not have access to safely managed water
services. Eliminating water scarcity is an incredibly ambitious goal; the
task of monitoring the current situation alone is labor intensive and
expensive. Utilizing the increasing connectedness of the world, and the
decreasing price of collecting and sending data over cellular networks
could prove beneficial in monitoring and improving the access to water
across the world.
By leveraging inexpensive microcontrollers with cellular connectivity,
municipalities in developing countries can be provided with a platform of
monitoring and documenting the current status of water systems in nearby
villages. This device, equipped with sensors and cellular connectivity
capabilities, could enhance the feedback loop between the water provider
and the supporting municipality. These data can be stored and viewed by
municipalities through an IoT platform, such as ThingSpeak. These data
could prove valuable over time in validating water system sustainability
practices. It will also save time and money by eliminating the need to
visit a community to determine the current functionality of a system.
The flow detection system will consist of two flow meters that will be placed at the inlet and outlet of a water storage tank. The inlet flow meter will monitor the water flowing through the pipeline from the water source to the tank. The outlet flow meter will monitor the water flow into the community. Flow sensors will be placed on the inside of the tank to prevent potential water loss due to leaking.
In addition to the following components, you may desire to purchase a box
to house the system, and connector jacks to connect the sensors to the
microcontroller.
Component |
Quantity |
Price per Unit (US $) |
Particle
Electron Mictrocontroller |
1 |
69.00 |
Flow
Sensor |
2 |
9.95 |
Solar
Panel |
1 |
60.00 |
10k ohm resistors | 2 | 0.01 |
TOTAL: |
148.90 |
The flow sensor has a wire harness with three connections; power (red),
ground (black), and a signal terminal (yellow). The red and black wires
correspond to the power-in and ground connections, respectively. The
yellow wire is an output that will produce a pulse when water if flowing
through the sensor. From the specifications listed on the Adafruit page,
it is known that the meter will produce 450 pulses per liter of water that
flows through the sensor. We can use this information to determine the
amount of water that flows through the sensor, and the flow rate. To
connect the flow sensor to the Electron, connect the red wire from the
sensor to the VBAT terminal on the Electron, and connect the black wire
(represented by a blue jumper wire in the figure below) to the ground
terminal.
Now, we will add in a 10K pull-up resistor into the circuit to prevent
the output from the sensor from floating. It is possible for the output
from the sensor to be somewhere between LOW and HIGH, which is problematic
when trying to count the total number of pulses. By introducing a pull-up
resistor, the output from the sensor will remain HIGH until a pulse from
the sensor arrives. Place the resistor between the VBAT terminal, and a
new strip on the breadboard. Now, connect the other end of the resistor to
the yellow wire terminal on the flow sensor wire harness, and the D1 input
on the Particle Electron.
To correctly register all pulses delivered by the flow sensor, an interrupt is attached to the "flowPin" variable. This allows the function "Flow" to be triggered every time a pulse is registered, resulting in the variable "count" to increase. Please note that the digital input D0 will not function correctly as an interrupt.
As specified by the manufacturer, the flow sensor will deliver 450 pulses for every 1 liter of water that flows through it. Dividing the number of pulses by 450 results in the number of liters recorded by the sensor, and again dividing by the sampling interval results in the flow rate.
Copy and paste the code below into the IDE portal on your Particle account, then "flash" the code unto the Particle Electron. Flashing can be performed through cellular, but will use a significant amount of data to do so. Another option is to flash the code onto the microcontroller using a USB cable. Refer to the Particle CLI documentation for instructions on how to flash a code through a USB from the command prompt. This code is a based on a code provided by BC Robotics.
//Configure
the variables
int flowPin = D1; //This is the input pin for the flow sensor
double flowRate; //This is the flow rate value to be
calculated
double numLiters; //This is the number of liters that has
passed the sensor, calculated from the flow rate
double interval; //This value allows us to set the data
collection period
volatile int count; //This integer needs to be set as volatile to ensure
it updates correctly during the interrupt process
//Put setup code:
void setup() {
pinMode(flowPin,
INPUT);
//Sets the pin as an input
attachInterrupt(D1, Flow, RISING); //Configures interrupt to
run the function "Flow" and attaches to flowPin
interval = 10000 ; //Declare the interval for data collection, in
milliseconds
}
//Put main code here:
void loop() {
count = 0;
//Reset the counter so we start counting from 0 again
interrupts(); //Enables
interrupts on the Arduino
delay (interval); //Wait for 10 seconds
noInterrupts(); //Disable the interrupts
on the Arduino
numLiters = count / 450; //Calculate number
of liters registered by Count. Note: The flow sensor signals 450 pulses
per 1 liter
flowRate = numLiters / (interval / 60000);
//Calculate the flow rate in LPM
Particle.publish("FlowRate",String(flowRate)); //Publish the
flowRate value, as a string type, through the event "FlowRate"
}
//Create function Flow
void Flow()
{
count++; //Every time this function is called, increment
"count" by 1
}
The code above is for one flow sensor. To connect two flow sensors, as described in the introduction, additional code must be added to the above script. Essentially, you will need to duplicate every line of the code and rename the variables for sensor 1 and sensor 2. To save data, the two flow rates can be concatenated together into a single string before being published.
The flow measurements obtained with the device
were compared to measurements taken by hand. The procedure for testing
the sensor involved allowing water to flow through the sensor at a
steady rate, and collecting the water into a container of known volume.
The time required to fill the container was utilized to calculate the
flow rate. This was repeated three times at various flow rates. The
results show the flow rate sensors to be calibrated within a tolerance
acceptable for this project.
In order to visualize the flow measurement data, the Particle server
will be connected to ThingSpeak through an internet linking protocal
called a webhook. ThingSpeak is an IoT platform that can recieve incoming
data from a server via a webhook, and create dynamically-updated graphs
from the incoming data. There are numerous IoT platforms available, but
ThingSpeak was chosen for its capablity to routinely execute MATLAB
scripts that can be used to analyze the data. This feature, however, will
not be required in this project, as the incoming data does not need to be
altered.
First, create a ThingSpeak account, and create a new channel. You do not
need to worry about altering the channel details at this point, but feel
free to add a channel name, description, and field 1 title. Now that the
channel has been created, a webhook will be created to automatically send
the flow measurements from the Particle server to the ThingSpeak channel.
To configure the webhook, navigate to the "Integrations" tab on the
Particle Console, and select "New Integration", then "Webhook". The event
name corresponds to the Particle publish function's first input, which is
"FlowRate" in this case (note: the event name is case sensitive). The URL
for the webhook will be https://api.thingspeak.com/update. Leave
the "Request Type" as POST, the "Request Format" as Web Form. Choose
whether you'd like any microcontroller listed on the Particle account to
be able to trigger the webhook, or a specifc device using the "Device"
input. Now, click on the Advanced Settings tab, click Custom, and mimic
the two form field inputs as shown below. Fill in API key location with
the API Write Key located on the API Keys tab of the ThingSpeak channel.
Turn on the Electron, the data that is being published to the Particle
server should now automatically be added to the field 1 graph on your
ThingSpeak channel!
An example of how the value will be displayed on a ThingSpeak plot is shown in the following figure. The settings can be adjusted to alter the number of points displayed, the amount of previous days displayed, and other details.
It is possible to concatenate additional variables as a string, such as flow measurements from multiple sensors, and publish the entire string to ThingSpeak. In this example, an additional channel will need to be configured. One channel serves to store the entire data string, where a MATLAB script can be routinely executed to import the string, split the string into individual variables, and then reupload the variables to individual graphs on a new channel. A diagram demonstrating this flow of data is shown in the following figure. The TimeControl application on thingspeak is used to execute the MATLAB scripts at set intervals. MATLAB scripts are stored under the MATLAB Analysis application on ThingSpeak.
A
solar panel allows the device to remain operational off-grid. The solar
panel can be connected directly to the VIN and GROUND terminals on the
Electron. The power management integrated circuit (PMIC) will prevent
excessive voltage/current from damaging the microcontroller. To optimize
the system, the following code can be added to the SETUP section of the
code for the electron.
PMIC pmic; //Initalize PMIC class pmic.setChargeCurrent(0,0,1,0,0,0); //Set charging current to 1024mA (512 + 512 offset) pmic.setInputVoltageLimit(4840); //Set the lowest input voltage to 4.84 volts
The tutorial "Build Solar Powered and Connected Devices" by Suyash Kumar further explains how to incorporate solar into the project. Additionally, the particle electron can be placed into a low-power sleep mode to conserve power. The tutorial "Choosing the Right Sleep Mode for Your Electron" by Rick Kaseguma is a great resource for understanding the different sleep modes available for the Electron.
To create a clean package for the system, the Electron was placed in a waterproof housing. Headers were soldered onto a circuit board so that the microcontroller can be removed from the housing. Connectors were mounted on the outside of the housing so that the flow sensors and the solar panel can be disconnected. The battery for the electron was ziptied to the bottom of the circuit board.