In this tutorial, we'll use a NodeMCU Amica (ESP8266 based development board) and a Funduino moisture sensor to measure soil moisture, and build a Wia Flow that notifies you when your plants need watering.
If you haven't already, check out our tutorial on getting started with the ESP8266. It details setting up the correct environment and libraries to complete this tutorial.
Connecting the hardwareUse jumper wires to connect the moisture sensor to the board as follows:
In the Arduino IDE, Go to Sketch > Include Libraries > Manage Libraries
. Install each of the following libraries by searching for their name in the search bar within the modal. A button will appear in the bottom right of the box that will allow you to install the library.
- ArduinoJson
- ESP8266WiFi
- ArduinoHttpClient
In the Arduino IDE, copy and paste the code labeled Publish Event to Wia
.
Replace the following values of the following variables (Place the correct value between the quotation marks right of the variable names in the code):
your-ssid
- with your WiFi network name
your-password
- with your WiFi network password
your-device-secret-key
with your device secret key from the Wia Dashboard (the one that begins with d_sk)
Make sure the correct board is selected - go to tools > board
and select NodeMCU 1.0 (ESP-12E Module)
.Make sure the correct port is selected - tools > port
.Click Sketch > Upload
in the menu.
Go to your Wia dashboard and view the Events as they come in via the Device debugger.
Notifying you when your plant needs wateringNext, we'll set up a Flow that will send a notification to your phone when the moisture level drops below a certain threshold. The Wia app is required to send a push notification to your phone, it can be installed here for Android and for iOS here.
Go to your Wia Dashboard and click Flows
in the left hand side menu. Create a new Flow with any name you like.
In Flow Studio, drag an Event
node from the triggers section and:
- Enter moisture as the the
Event name
- Add the Device that matches the one you added to the code earlier
Drag a function
node from the logic section and copy and paste the following code:
if
if (input.body.data > 500) {
output.process = 0;
}
}
else
output.process = 0;
}
The Output from the soil moisture sensor is a number between 0 and 1023 with 0 being completely dry and 1023 being completely wet. The code above sets to not process the output when there is no input or if the input is greater than 500 (i.e The doesn't plant need watering).
Finally, add a notification
node and enter The plant needs watering
in the text field so that Wia can send a notification to you when your plant needs watering.
Here's just one way you could take this project to the next level: we're going to feed our moisture data into two Widgets on the Wia Dashboard, in addition to sending the notification.
First, add another function
node from the logic section, and connect it to the same event
. In this node, paste the following code:
// Check moisture levels
if
// Plant is thirsty
output.body.data = "Thirsty";
}
// Plant is ok
output.body.data = "Ok";
}
This code outputs 'Thirsty' if the plant needs watering, and 'Ok' otherwise. We're now going to connect the output of this function
node to a new event
action node. Drag one over from the 'actions' section (not 'triggers'). This will create a new event. Give the event a name (such as status
), and connect the function
node to it.
Next, head over to your Device's 'Overview' tab. Click 'Add a Widget'. Give the Widget a name ('Status'), choose Widget type 'text', and enter the name of the Event that you created in your Flow (status
).
Create another Widget of type 'text', give it a name ('Moisture') and enter the name of your original Event trigger (moisture
).
Now, when your events are published, the Widgets on your Device overview page will update in real time!
Comments