In previous posts I wrote about the hardware and electronics I used to build my garden watering system. This post is about the software component of the system. As with the rest of the system, the software is still a prototype and not very user friendly, though eventually I would like to create a good user interface for it.
The watering system software is divided into two parts:
- A program on a Raspberry Pi that can be run on a timer, or manually, that sends commands over the radio link to the controller, and
- A program on the Arduino-based controller that receives commands and switches the solenoid valves on and off accordingly.
For both of these, I used the ‘ping pair’ example that comes with the TMRh20 RF24 library as a starting point. Once I got two-way communications working using the example, I added some commands. To keep it simple, the only data sent are integer numbers.
The Raspberry Pi program is fairly simple – it just converts command line arguments to integers and sends them over the radio. If there are no command line arguments, it prompts you for a number to send and displays the return code. The code is resent if no acknowledgement is received from the controller.
The controller program runs in a loop, continually checking for new data from the radio receiver. When it receives a command it performs whatever action is needed and sends back a return code. It also keeps track of how long the water has been on, and switches it off after a defined time. There is an overall maximum time of 10 minutes, though a run can be started for as short as 1 minute. A separate timer tracks how long since the last radio packet was received. If nothing has been received for 15 minutes, the program re-initializes. This system of timers ensures that the water is not left on too long, and that the Arduino stays responsive, to avoid having to manually reset it.
|0||Switch all watering zones off|
|1||Switch on watering zone 1|
|2||Switch on watering zone 2|
|3||Switch on watering zone 3|
|4||Switch on watering zone 4|
|5||Switch on master solenoid for one second for testing.|
|21||Returns the number of milliseconds the current watering zone has been switched on for (0 if nothing on)|
|22||Returns the number of the currently switched on watering zone (1-4 or 0 if nothing on)|
|25||Returns the maximum on-time in milliseconds. This is the time after which a watering zone switched on with codes 1-4 will automatically switch off.|
|31-45||Sets the run time that will be used the next time a watering zone is switched on. 31 sets 1 minute, 32 sets 2 minutes, and so on up to 15, which sets a 15 minute run-time|
|Note:||Commands 1 – 4 are only valid when no watering zone is currently on. These commands return a code of 1-4 if successfully switched on, and code 20 if another watering zone is already running.|
Manually activating a watering zone
To manually switch on watering, I log into the Raspberry Pi command line using Putty SSH and enter a command:
sudo ./water 35 2
water is the name of the program executable. It needs to run with sudo because it requires admin privileges. The first number, 35, sets the run time to 5 minutes. The second number, switches on watering zone 2.
I use Cron to schedule watering. This is a built in command on the Raspberry Pi that runs task according to the Cron table. To edit the table, use the command:
sudo crontab -e
Each line in the file represents a task
Minutes Hours Day Month Weekday
45 7 * * * /home/pi/water 35 1>>/home/pi/waterlog.txt 2>&1
0 8 * * * /home/pi/water 35 2 >>/home/pi/waterlog.txt 2>&1
In this example I have set zones 1 and 2 to water for 5 minutes each at 7:45am and 8:00am. The output of the water command is appended to a log file.
I have another cron task that emails me the log file:
5 19 * * * /home/pi/uploadWaterLog.sh >>/home/pi/uploaderlog.txt 2>&1
Control from a smartphone
I use JuiceSSH on my Android phone to log in to the command line. It gets a bit tiresome to type commands on the phone keyboard though, so it’s better to use an app that provides configurable buttons for repeated tasks. I use Raspberry SSH lite for this at the moment. Both these apps can connect to the Raspberry Pi provided its IP address username, and password or SSH certificate. If you want to be able to log in when not on the same network, you will have to set up port forwarding on your internet router to forward port 22 to the Raspberry Pi.
You can download the software for the Arduino-based controller and Raspberry Pi program below, keeping in mind it is an experimental prototype for now:Download Arduino sketch for controller Download Raspberry Pi program
Modifying the software
The Arduino sketch should compile and upload to using the standard Arduino environment, you just need the RF24 library. I’ve included a pre-build executable in the Raspberry Pi download, but if you want to make any changes you will need the RF4 library set up. Extract the zip file to a folder under RF24, ie RF24/water. Type make at the command-line to re-compile water.cpp into the executable file “water.”
- A much smaller controller
- Direct Wifi connection to the controller
- Wireless rain and soil moisture sensors
- A good phone-based user interface
- Solar powered wifi controller on each solenoid