We are now going to configure our project so it automatically waters the plant if the humidity falls below a given threshold.
The first step is actually to define two thresholds:
floatlowThreshold = 20.00; floathighThreshold = 25.00;
We need two thresholds here because if we just defined one, the pump will constantly switch between the on and off states.
So we will have the pump turn on when the humidity goes below the low threshold, and turn off when we reach the high threshold again.
Next, we define which pin the relay is connected to:
#define relayPin 15
In the setup()
function of the sketch, we set the relay pin as an output:
pinMode(relayPin, OUTPUT);
In the loop()
function, we constantly check whether the humidity went below the low threshold, or above the high threshold:
if (humidity <lowThreshold) { // Activate pump digitalWrite(relayPin, HIGH); } if (humidity >highThreshold) { // Deactivate pump digitalWrite(relayPin, LOW); }
I only highlighted the most important parts of the code here, but you can of course grab the complete code from the GitHub repository for the book. Now, get the code and modify it with your own credentials. Then, upload the code to the board.
Once that's done, go back to the dashboard you created before. You can now add another element for the pump, with the following set of parameters:
You should immediately see the buttons appear, with the current status of the relay:
You can now try it; press the On button, and the pump should immediately turn on:
You should also notice that any time the humidity drops below the threshold, it should immediately activate the pump, until the humidity reaches the high threshold again.
3.136.22.179