In a previous post we described the overview and hardware setup of an automated plant watering system. Here we take an in-depth look at the node-red flow, including the configuration for the user dashboard.
First, we describe how to grab sensor data from the PiShield, process it, and then use it to drive the output relay connected to the GPIO pin. Then, we take a look at the dashboard components to present a nice interface for the user. Finally, we add an optional FRED extension so that you can send your data to a cloud hosted node-red service, including the potential to publicly serve the dashboard (if you like).
The entire flow is shown below, and the code can be copy+pasted from this link. It might look a bit daunting, but once we break it up it is quite simple.
1. Sensor Acquisition and Processing
This part deals with getting sensor data from the PiShield, doing some basic processing and scaling, and then controlling the relay output to drive the watering valve.
From the left, we see the start and stop messages that control the analog to digital sensor. This is the same as the basic example of reading data from the PiShield. The start and stop are inject nodes that send the two strings to control the sensor. We also configure the “start” inject node by double clicking it and selecting “inject on start”, so that the sensor becomes active when the flow is deployed:
The output of the sensor is put through a smooth node that filters out noise, and then a range node which translates the moisture values into an easier to use range. Based on experimentation, we see that the sensor outputs around 512 when dry, and 400 when inserted into the wet soil. Following is the range node that spits out a scale of 0 – 100, in increasing levels of moisture.
After that, the “check threshold” function node compares the current moisture value with a globally stored threshold variable, and if the moisture is below that value, sends out a “1” to turn on the relay or “0” to turn it off. The rbe (report-by-exception) node allows output to only be sent when the value changes, so we’re not constantly sending values to the output unless necessary.
You may notice two inputs going into the output relay. The second signal is from the user interface (explained in the next section) that allows manual triggering from the dashboard.
2. Dashboard User Interface
The dashboard components present a user interface to display data and control the system. For a brief introduction on the dashboard check out our dashboard intro here that deals with how to add components and organize them using the dashboard configuration.
The live moisture level is first sent to a gauge node (“Moisture level”), and also speed limited (to 1 message per minute here) for a history chart plot.
The “water now” is a button node that activates a 5 second trigger that turns on the relay, and also outputs the status of the relay (“Plant 1 Watering” switch node). This status is also driven from the automated control explained in the previous section.
The “Set Threshold” slider node allows the watering threshold to be adjusted, which updates the global variable in a function node. The set threshold is also displayed in a text box.
Finally, the rate limited moisture level is sent downwards into the optional FRED node to be sent to the cloud server.
The dashboard, when deployed, looks like this:
3. Optional FRED Integration
At the very bottom, we have a FRED output node that allows us to receive data on our cloud-hosted instance of node-red.
This allows us to send moisture data to a remote, always-on instance that can be accessed externally, or even exposed publicly if you configure your dashboard accordingly. For more information, check out our short tutorial here on FRED integration, or go to fred.sensetecnic.com to get your own cloud-hosted node-red instance! Below shows a simple receiving endpoint on FRED:
And the displayed dashboard: